Saturday, April 02, 2011 at 9:21 PM.

rssToBlogSuite.saveIfNewOrChanged

on saveIfNewOrChanged (adrblog, adrpost) {
	<<Changes
		<<12/24/10; 3:12:42 PM by DW
			<<Created. 
	local (title="", link="", description="", struct, now = clock.now (), startticks = clock.ticks ());
	rssToBlogSuite.initPost (adrpost);
	bundle { //set up title, etc
		on decode (s) {
			return (xml.entitydecode (s, flAlphaEntities:true))};
		on getval (name, adrval) {
			try {
				adrval^ = decode (xml.getvalue (@adrpost^.itemdata, name))}};
		getval ("title", @title);
		getval ("link", @link);
		getval ("description", @description);
		<<
		<<bundle //add a prefix to the description
			<<local (url = link)
			<<getval ("linkFull", @url)
			<<local (ul = string.urlsplit (url))
			<<local (domain = string.lower (ul [2]))
			<<if domain beginswith "www."
				<<domain = string.delete (domain, 1, 4)
			<<description = "<a href=\"" + link + "\">From " + domain + "</a>. " + description
		};
	on savePreviousValues () {
		new (tabletype, @adrpost^.stats.previousValues);
		adrpost^.stats.previousValues.title = title;
		adrpost^.stats.previousValues.link = link;
		adrpost^.stats.previousValues.description = description;
		adrpost^.stats.ctSaves++;
		adrpost^.stats.whenLastSave = now;
		adrblog^.stats.ctSaves++;
		adrblog^.stats.whenLastSave = now};
	on setupRpcData (adrdata) {
		new (tabletype, @rpcdata);
		local (ul = string.urlsplit (adrblog^.prefs.params.url));
		rpcdata.server = ul [2];
		rpcdata.port = 80;
		rpcdata.rpcPath = "/" + ul [3]};
	on logit (operation) {
		local (titleforlog, storylink = "story", s);
		if title == "" {
			titleforlog = description}
		else {
			titleforlog = title};
		titleforlog = string.trimwhitespace (titleforlog);
		if titleforlog [sizeof (titleforlog)] == "." {
			titleforlog = string.delete (titleforlog, sizeof (titleforlog), 1)};
		if link != "" {
			storylink = "<a href=\"" + link + "\">" + storylink + "</a>"};
		<<Saved story, "Apache plus Dropbox = Hello World," to a WordPress blog.
		s = "Saved " + storylink + ", \"" + titleforlog + "\" to a <a href=\"" + adrblog^.prefs.urlBlogHome + "\">" + adrblog^.prefs.params.blogType + " blog</a>.";
		log2.add (rssToBlogInfo.name, operation, s, startticks)};
	try {
		local (bodytext = rssToBlogSuite.postToText (adrblog, adrpost));
		new (tabletype, @struct);
		case string.lower (adrblog^.prefs.params.blogType) {
			"wordpress" {
				if adrpost^.stats.postID == nil {
					local (rpcdata);
					setupRpcData (@rpcdata);
					adrpost^.stats.postID = metaweblog.newPost (title, link, bodytext, @struct, adrblog^.prefs.params.username, adrblog^.prefs.params.password, adrblog^.prefs.params.blogID, adrdata:@rpcdata);
					adrblog^.stats.whenLastPost = now; //2/27/11 by DW
					savePreviousValues ();
					logit ("New")}
				else {
					local (flchanged = true);
					if title == adrpost^.stats.previousValues.title {
						if link == adrpost^.stats.previousValues.link {
							if description == adrpost^.stats.previousValues.description {
								flchanged = false}}};
					if flchanged {
						local (rpcdata);
						setupRpcData (@rpcdata);
						metaweblog.editpost (title, link, bodytext, @struct, adrpost^.stats.postID, adrblog^.prefs.params.username, adrblog^.prefs.params.password, adrblog^.prefs.params.blogID, true, @rpcdata);
						savePreviousValues ();
						logit ("Update")}}};
			"tumblr" {
				local (idpost = nil, operation = "New Post");
				if defined (adrpost^.stats.postID) {
					idpost = adrpost^.stats.postID;
					operation = "Edit Post"};
				adrpost^.stats.postID = tumblr.newpost (title, description, adrblog^.prefs.params.username, adrblog^.prefs.params.password, idPost:idpost, pubdate:timecreated (adrpost), group:adrblog^.prefs.params.domain);
				logit (operation)}}}
	else {
		local (now = clock.now ());
		adrpost^.stats.ctErrors++;
		adrpost^.stats.whenLastError = now;
		adrpost^.stats.lastError = tryerror;
		adrblog^.stats.ctErrors++;
		adrblog^.stats.whenLastError = now;
		log2.add (rssToBlogInfo.name, "Error", adrpost^.stats.lastError, startticks)}};
bundle { //test code
	<<saveIfNewOrChanged (@config.rssToBlog.blogs.scripting, @config.rssToBlog.blogs.scripting.calendar.["2011"].["03"].["10"].["00025"])
	saveIfNewOrChanged (@config.rssToBlog.blogs.["linkblog.xml"], @config.rssToBlog.blogs.["linkblog.xml"].calendar.["2011"].["03"].["10"].["00066"])}



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.