Tuesday, August 18, 2009 at 7:35 PM.

photoFanSuite.flickrToTwitter.checkFeed

on checkFeed () {
	<<Changes
		<<12/29/08; 7:43:34 PM by DW
			<<If user.photoFan.prefs.twitter.password is empty, use the default twitter password. 
		<<7/11/08; 7:35:31 AM by DW
			<<If user.photoFan.prefs.flickrToTwitter.flUseBitly is true, use bit.ly to shorten the URL.
		<<3/24/08; 2:11:47 PM by DW
			<<Maintain user.photoFan.prefs.flickrToTwitter.ctTicksLastCheck.
		<<3/23/08; 11:43:54 AM by DW
			<<Created.
	local (now = clock.now (), startticks = clock.ticks (), xmltext, fldebugging = false);
	on decode (s) {
		return (xml.entitydecode (s, flAlphaEntities:true))};
	on setticks () {
		user.photoFan.prefs.flickrToTwitter.ctTicksLastCheck = clock.ticks () - startticks};
	on shorturl (url) {
		if user.photoFan.prefs.flickrToTwitter.flUseBitly {
			return (bitly.create (url))}
		else {
			return (tinyurl.create (url))}};
	photofansuite.init ();
	user.photoFan.prefs.flickrToTwitter.ctchecks++;
	user.photoFan.prefs.flickrToTwitter.whenLastCheck = now;
	bundle { //read xmltext, exit if no change
		local (flchanged);
		if user.photoFan.prefs.flickr.feed == "" {
			setticks ();
			return};
		xmltext = flickr.readfeed (user.photoFan.prefs.flickr.feed, @flchanged);
		if (not flchanged) and (not fldebugging) {
			setticks ();
			return}};
	xml.compile (xmltext, @xstruct);
	<<scratchpad.xstruct = xstruct
	local (adrrss = xml.getaddress (@xstruct, "rss"), adr);
	local (adrchannel = xml.getaddress (adrrss, "channel"));
	for adr in adrchannel {
		if nameof (adr^) endswith "item" {
			try {
				local (link = xml.getvalue (adr, "link"), adritem = adr);
				local (title = xml.getvalue (adr, "title"));
				local (guid = xml.getvalue (adr, "guid"));
				local (adrhistory = @user.photoFan.prefs.flickrToTwitter.history.[guid]);
				if not defined (adrhistory^) {
					new (tabletype, adrhistory);
					adrhistory^.when = now;
					adrhistory^.title = title;
					
					local (flcategorycheck = true); //passed the category check
					bundle { //check category, 8/9/07 by DW
						local (tag = string.lower (user.photoFan.prefs.flickrToTwitter.specialTag));
						tag = string.replaceall (tag, " ", ""); //remove blanks, 10/16/07 by DW
						if sizeof (tag) > 0 { //if tag is empty, don't check
							local (adrcat, flhavetag = false);
							for adrcat in adr {
								if nameof (adrcat^) endswith "category" {
									try {
										if adrcat^.["/atts"].namespace == "media:" {
											if adrcat^.["/atts"].scheme == "urn:flickr:tags" {
												local (s = string.lower (adrcat^.["/pcdata"]), i);
												for i = 1 to string.countfields (s, " ") {
													if string.nthfield (s, " ", i) == tag {
														flhavetag = true;
														break}}}}}}};
												<<if string.lower (adrcat^.["/pcdata"]) == tag
													<<flhavetag = true
													<<break
							if not flhavetag { //the picture isn't going to Twitter!
								flcategorycheck = false}}};
					if flcategorycheck {
						if not user.photoFan.prefs.flickrToTwitter.flFirstCheck { //don't post on first check
							if photoFanSuite.flickrToTwitter.howManyPicsSince (clock.now () - (60*60)) <= 5 { //10/8/07 by DW
								try { //post message to twitter
									local (s = string (title) + ": " + shorturl (link));
									adrhistory^.twitterPost = s;
									user.photoFan.prefs.flickrToTwitter.lastPost = s;
									user.photoFan.prefs.flickrToTwitter.whenLastPost = now;
									user.photoFan.prefs.flickrToTwitter.ctposts++;
									if fldebugging {
										dialog.alert (s)}
									else { //send to twitter
										local (password = user.photoFan.prefs.twitter.password);
										if password == "" { //12/29/08 by DW
											password = user.twitter.prefs.password};
										twitter.newpost (s, user.photoFan.prefs.twitter.username, password, source:"twittergram")}}
										<<twitterGramSuite.addToLog ("flickrToTwitter.checkUser", s, startticks)
										<<bundle //send to Pownce if enabled, 3/4/08 by DW
											<<if defined (adrsub^.pownce)
												<<try
													<<local (username = adrsub^.pownce.username)
													<<local (password = adrsub^.pownce.password)
													<<if username != ""
														<<pownce.newpost (title, username, password, link)
												<<else
													<<adrsub^.pownce.lastError = tryerror
													<<adrsub^.pownce.whenLastError = clock.now ()
									<<if twitterGramData.prefs.flickrToTwitter.globalAccount.enabled
										<<try
											<<local (username = twitterGramData.prefs.flickrToTwitter.globalAccount.username)
											<<local (password = twitterGramData.prefs.flickrToTwitter.globalAccount.password)
											<<twitter.newpost (s, username, password, source:"twittergram")
											<<twitterGramData.prefs.flickrToTwitter.globalAccount.lastPost = s
											<<twitterGramData.prefs.flickrToTwitter.globalAccount.whenLastPost = now
										<<else
											<<twitterGramData.prefs.flickrToTwitter.globalAccount.error = tryerror
											<<twitterGramData.prefs.flickrToTwitter.globalAccount.whenLastError = now
									<<bundle //post to the flickrFeed, 10/8/07; 8:25:39 PM by DW
										<<local (serialnum = twitterGramData.prefs.flickrToTwitter.feedSerialNum++)
										<<local (adrtable = @twittergramData.flickrFeed.[string.padwithzeros (serialnum, 7)])
										<<new (tabletype, adrtable)
										<<try
											<<adrtable^.twitterusername = nameof (adruser^)
											<<adrtable^.title = title
											<<adrtable^.link = link
											<<adrtable^.description = decode (xml.getvalue (adritem, "description"))
											<<local (adrimg = xml.getaddress (adritem, "content"))
											<<local (adratts = @adrimg^.["/atts"])
											<<adrtable^.imgHeight = adratts^.height
											<<adrtable^.imgWidth = adratts^.width
											<<adrtable^.imgUrl = adratts^.url
											<<adrtable^.pubDate = xml.getvalue (adritem, "pubDate")
										<<else
											<<adrtable^.error = tryerror
								else {
									adrhistory^.error = tryerror}}}}}}
				<<msg (link)
			else {
				user.photoFan.prefs.flickrToTwitter.lastError = tryerror}}};
	user.photoFan.prefs.flickrToTwitter.flFirstCheck = false;
	<<twitterGramSuite.addToLog ("flickrToTwitter.checkUser", nameof (adruser^), startticks)
	setticks ()};
bundle { //test code
	checkFeed ()}



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.