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

newsRiverSuite.podcatcher.downloadEnclosure

on downloadEnclosure (adrq) {
	<<Changes
		<<1/21/06; 11:45:30 AM by DW
			<<Callbacks, after download.
		<<1/19/06; 5:29:55 AM by DW
			<<Respect flOutlineEnclosures pref.
		<<1/18/06; 4:56:45 PM by DW
			<<Adapted for newsRiver.root.
		<<9/19/05; 10:50:03 AM by DW
			<<If the feed doesn't have a title, store its podcasts in a folder named "Untitled feed."
			<<http://archive.scripting.com/2005/09/19#When:1:41:22PM
	local (adrdata = newsRiverSuite.init (), feedtitle, foldername);
	try {
		feedtitle = adrq^.adrservice^.compilation.channeltitle;
		foldername = newsRiverSuite.podcatcher.cleanFilename (feedtitle)}
	else {
		feedtitle = "Untitled feed";
		foldername = "Untitled feeds"};
	adrq^.feedtitle = feedtitle; //for the log outline
	
	local (folder = adrdata^.newsRiver.podcatcher.prefs.folder + foldername + file.getpathchar ());
	bundle { //see if we can access the folder
		try {
			file.surefilepath (folder + "xxx")}
		else { //nope
			if system.environment.isMac {
				if sizeof (foldername) >= 32 { //too long!
					foldername = string.mid (foldername, 1, 31);
					folder = adrdata^.newsRiver.podcatcher.prefs.folder + foldername + file.getpathchar ()}}}};
	
	local (url = adrq^.url);
	local (fname = string.nthfield (url, "/", string.countfields (url, "/")));
	fname = string.nthfield (fname, "?", 1);
	fname = string.urlDecode (fname);
	fname = newsRiverSuite.podcatcher.cleanFilename (fname);
	local (f = folder + fname);
	bundle { //do some checking on the filename
		local (extension = string.nthfield (fname, ".", string.countfields (fname, ".")));
		local (fnamedefault = "podcast" + nameof (adrq^) + "." + extension);
		if system.environment.isMac {
			if sizeof (fname) > 32 { //too long!
				fname = fnamedefault;
				f = folder + fname}};
		if file.exists (f) { //don't overwrite existing file
			fname = fnamedefault;
			f = folder + fname}};
	adrq^.f = f;
	file.surefilepath (f);
	local (s);
	bundle { //read the url
		local (urllist = string.urlsplit (url));
		s = string.httpResultSplit (tcp.httpClient (server:urllist [2], path:urllist [3], ctFollowRedirects:5))};
	file.writewholefile (f, s);
	bundle { //if we have a pubdate, set the file dates to that
		try {
			if adrq^.pubdate != date (0) {
				file.setcreated (f, adrq^.pubdate);
				file.setmodified (f, adrq^.pubdate)}}};
	adrq^.whenDownload = clock.now ();
	if adrdata^.newsRiver.podcatcher.prefs.flOutlineEnclosures {
		newsRiverSuite.podcatcher.logDownload (adrq)};
	bundle { //callbacks, 1/21/06 by DW
		local (adrscript);
		for adrscript in @adrdata^.newsRiver.callbacks.afterEnclosureDownload {
			try {
				adrscript^ (adrq)}}}}
<<bundle //test code
	<<downloadEnclosure (@aggregatorData.newsRiver.podcatcher.queue.["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.