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

river2Suite.podcasts.downloadOne

on downloadOne (adrinriver) {
	<<Changes
		<<11/18/09; 5:49:45 PM by DW
			<<Somehow we're being called for river items that don't have enclosures. So we check to see if it has one and if not, we don't do anything.
		<<9/17/09; 5:16:01 AM by DW
			<<If there's an error limiting the folder size don't die, and log the error.
		<<9/8/09; 1:42:20 PM by DW
			<<Change strings to say "enclosure" from "podcast" so it works equally well for pictures.
		<<9/2/09; 10:21:26 AM by DW
			<<maxFolderMegs is a podcast pref, not a top-level pref.
		<<9/1/09; 7:57:31 PM by DW
			<<Constrain the folder size by adrdata^.prefs.maxFolderMegs.
		<<8/24/09; 9:14:33 PM by DW
			<<Delete system.temp.river2.idDownloadThread when completed, allowing another download to begin.
		<<8/22/09; 6:44:13 PM by DW
			<<Created. Core podcatcher routine.
	if defined (adrinriver^.enclosure) { //11/18/09 by DW
		local (adrdata = river2Suite.init (), startticks = clock.ticks ());
		local (adrfeed = river2Suite.initFeed (adrinriver^.feedurl));
		river2Suite.initRiverItem (adrinriver);
		local (feedname = adrfeed^.feedinfo.title);
		local (folder = adrdata^.prefs.podcasts.downloadFolder, pc = file.getpathchar (), url = adrinriver^.enclosure.url);
		local (fname = river2Suite.podcasts.cleanFilename (string.nthfield (url, "/", string.countfields (url, "/"))));
		if fname contains "?" { //9/30/07 by DW
			fname = string.nthfield (fname, "?", 1)};
		local (f = folder + river2Suite.podcasts.cleanFilename (feedname) + pc + fname);
		file.surefilepath (f);
		<<dialog.alert (f)
		try { //download the podcast
			adrinriver^.podcast.whenDownloadBegan = clock.now ();
			adrinriver^.podcast.f = f;
			bundle { //msg
				local (s = "Downloading enclosure \"" + feedname + "\"");
				if adrinriver^.enclosure.length != 0 {
					s = s + ", " + string.gigabytestring (adrinriver^.enclosure.length)};
				s = s + ".";
				msg (s)};
			local (urllist = string.urlsplit (url));
			local (s = string.httpResultSplit (tcp.httpClient (server:urllist [2], path:urllist [3], ctFollowRedirects:5, flmessages:false)));
			file.writewholefile (f, s);
			msg ("");
			bundle { //if we have a pubdate, set the file dates to that
				try {
					if adrinriver^.pubdate != date (0) {
						file.setcreated (f, adrinriver^.pubdate);
						file.setmodified (f, adrinriver^.pubdate)}}};
			bundle { //log the event
				local (len = string.gigabytestring (adrinriver^.enclosure.length));
				local (s = "Downloaded an <a href=\"" + url + "\">enclosure</a> from <a href=\"" + river2Suite.data.urls.feedPage + string.urlencode (adrinriver^.feedurl) + "\">" + feedname + "</a> to a local <a href=\"" + html.getfileurl (f) + "\">file</a> (" + len  + ").");
				log2.add (river2Info.name, "Enclosure", s, startticks)};
			adrinriver^.podcast.whenDownloadFinished = clock.now ();
			adrfeed^.stats.ctPodcastDownloads++;
			adrfeed^.stats.whenLastPodcastDownload = clock.now ()}
			<<podcatcherSuite.logDownload (adrq)
			<<bundle //callbacks, 1/21/06 by DW
				<<local (adrscript)
				<<for adrscript in @adrdata^.callbacks.afterEnclosureDownload
					<<while typeof (adrscript^) == addresstype
						<<adrscript = adrscript^
					<<try
						<<adrscript^ (adrq)
		else {
			adrinriver^.podcast.lastError = tryerror};
		bundle { //limit the folder size per the pref, log it if there's an error, 9/17/09 by DW
			local (startticks = clock.ticks ());
			try {
				file.maxFolderSize (folder, adrdata^.prefs.podcasts.maxFolderMegs)} //9/2/09 by DW
			else {
				log2.add (river2Info.name, "Enclosure", "Error limiting the folder size after download: " + tryerror, startticks)}};
		try {delete (@system.temp.river2.idDownloadThread)}}} //8/24/09 by DW
<<bundle //test code
	<<downloadOne (@config.river2.river.["2009"].["08"].["20"].["0034910"])



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.