Monday, November 08, 2010 at 12:01 AM.

system.verbs.apps.posterous.folderWatcher.scan

on scan () {
	<<Changes
		<<5/10/09; 11:10:39 AM by DW
			<<Cribbed from Flickr. 
			<<http://www.posterous.com/api/twitter
			<<Posterous doesn't support tags through their API, but we left the code here and the prefs are set so that in case someday they do support tags we'll be almost ready to go.
		<<1/8/08; 3:37:20 PM by DW
			<<Support for callbacks.
		<<1/6/08; 6:56:35 PM by DW
			<<Support default tags.
		<<12/4/07; 4:31:01 PM by DW
			<<Nested folders correspond to tags.
			<<If a picture is dropped in the top level of the folder it has no tags. If it's dropped in a sub-folder named Iowa it is uploaded with one tag -- Iowa. If it's dropped in a sub-sub-folder named Ames, it is uploaded with two tags, Iowa and Ames. You can nest to any level supported by the OS. Scanning is still very fast. 
		<<12/3/07; 11:12:27 AM by DW
			<<Respect user.flickr.folderWatcher.flBeepAfterUpload.
		<<6/15/07; 5:21:11 PM by DW
			<<Archive to S3.
		<<6/15/07; 4:58:54 PM by DW
			<<Add a flag to control local archiving, user.flickr.folderWatcher.flLocalArchive.
		<<6/15/07; 3:40:15 PM by DW
			<<Support for archive folder on local hard disk.
			<<Track the number of errors (but it isn't reported anywhere).
		<<6/13/07; 11:55:50 AM by DW
			<<Remove whitespace at end of HTML text.
		<<6/12/07; 2:06:52 PM by DW
			<<Respect the new user.flickr.folderWatcher.enabled and user.flickr.prefs.enableds boolean.
		<<6/11/07; 12:10:33 PM by DW
			<<Created.
	local (adrdata = posterous.init (), folder = adrdata^.prefs.folderWatcher.folder);
	file.surefilepath (folder + "xxx");
	local (ctcopied = 0, s = "");
	loop { //loop until the folder is empty -- the user could be copying while we're copying
		local (f, ctcopiedthisloop = 0);
		fileloop (f in folder, infinity) {
			local (fname = string.lower (file.filefrompath (f)));
			if (fname endswith ".gif") or (fname endswith ".jpg") or (fname endswith ".jpeg") {
				local (tags);
				bundle { //set tags
					tags = string.delete (file.folderfrompath (f), 1, sizeof (adrdata^.prefs.folderWatcher.folder));
					if sizeof (tags) == 0 {
						tags = adrdata^.prefs.folderWatcher.defaults.tags} //1/6/08 by DW
					else {
						<<Louisiana:New Orleans:Uptown:
						tags = string.delete (tags, sizeof (tags), 1);
						<<Louisiana:New Orleans:Uptown
						tags = "\"" + string.replaceall (tags, file.getpathchar (), "\" \"") + "\"";
						<<"Louisiana" "New Orleans" "Uptown"
						tags = adrdata^.prefs.folderWatcher.defaults.tags + " " + tags}}; //1/6/08 by DW
				local (hw = html.getImageHeightWidth (f), response, imghtml);
				posterous.uploadPhoto (f, @response);
				if response.flerror {
					scripterror ("Can't upload a picture to Posterous because: \"" + response.msg + "\"")};
				bundle { //set imghtml
					local (t);
					new (tabletype, @t);
					t.url = response.url;
					t.height = hw [1];
					t.width = hw [2];
					t.filename = file.filefrompath (f);
					imghtml = string.multiplereplaceall (string (adrdata^.prefs.folderWatcher.template), @t, false, "<%", "%>")};
				s = s + imghtml;
				if adrdata^.prefs.folderWatcher.flBeepAfterUpload {
					speaker.beep ()};
				ctcopiedthisloop++;
				ctcopied++;
				bundle { //archive the image locally
					if adrdata^.prefs.folderWatcher.flLocalArchive {
						local (farchive = adrdata^.prefs.folderWatcher.archiveFolder + file.getDatePath () + file.filefrompath (f));
						file.surefilepath (farchive);
						file.copy (f, farchive)}}};
				<<bundle //archive the image to S3
					<<if adrdata^.prefs.folderWatcher.flS3Archive
						<<local (path = adrdata^.prefs.folderWatcher.s3Path + file.getDatePath ("/") + file.filefrompath (f))
						<<s3.newObject (path, file.readwholefile (f))
			file.delete (f)};
		if ctcopiedthisloop == 0 {
			break}};
	if sizeof (s) > 0 {
		wp.newtextobject (string.trimwhitespace (s), @adrdata^.imageHtml);
		edit (@adrdata^.imageHtml, "Copy/Paste into your web page");
		bundle { //callbacks, 1/8/08 by DW
			local (adrscript);
			for adrscript in @adrdata^.prefs.folderWatcher.callbacks.afterUpload {
				while typeof (adrscript^) == addresstype {
					adrscript = adrscript^};
				try {
					adrscript^ ()}}}}};
bundle { //test code
	scan ()}



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.