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

system.verbs.builtins.xml.rss.formatDrivers.NewYorkTimes.compile

on compile (adrservice, flSaveData, adrStoryArrivedCallback) {
	<<Changes
		<<3/20/02; 8:56:12 AM by DW
			<<Sam Devore and others got hit by a limit in xml.getAddressList re the length of addresses. If they get too long the kernel starts doing weird things. Work around it by working with a copy of the compiled XML structure.
		<<3/19/02; 5:42:21 PM by DW
			<<Handle case where byline is empty.
			<<Decode the timestamp into the internal date format.
		<<3/19/02; 1:31:15 PM by DW
			<<Assume one section only per feed, include the section title in the channel title.
		<<3/15/02; 2:58:27 PM by DW
			<<Created.
	on decode (s) {
		return (xml.rss.decodeString (s))};
	on easy2readname (name) {
		<<Convert BULL MANCUSO to Bull Mancuso.
		local (s = "", flcap = true, ch);
		while sizeof (name) > 0 {
			ch = name [1];
			name = string.delete (name, 1, 1);
			if not flcap {
				ch = string.lower (ch)};
			s = s + ch;
			flcap = ch == " "};
		return (s)};
	on converttimestamp (nytimesformatdate) {
		<<Convert something like 20020319140340 to a date type.
		local (day, month, year, hour, minute, second);
		date.get (clock.now (), @day, @month, @year, @hour, @minute, @second);
		try {year = number (string.mid (nytimesformatdate, 1, 4))};
		try {month = number (string.mid (nytimesformatdate, 5, 2))};
		try {day = number (string.mid (nytimesformatdate, 7, 2))};
		try {hour = number (string.mid (nytimesformatdate, 9, 2))};
		try {minute = number (string.mid (nytimesformatdate, 11, 2))};
		try {second = number (string.mid (nytimesformatdate, 13, 2))};
		return (date.set (day, month, year, hour, minute, second))};
	on addToHistory (adritem, categorylist={}) {
		local (title = adritem^.title);
		if sizeOf (title) > 0 {
			local (adrinhistory = @adrcompilation^.itemHistory.[title]);
			if not defined (adrinhistory^) { //it's a new story
				if adrStoryArrivedCallback != nil {
					adrStoryArrivedCallback^ (adrservice, adritem)}};
			adrinhistory^ = true}}; //it's current
	
	local (xstructcopy = adrservice^.xmlstruct, adrxstruct = @xstructcopy);
	local (adrcompilation = @adrservice^.compilation);
	local (adrnewyorktimes = xml.getAddress (adrxstruct, "NewYorkTimes"));
	new (tabletype, @adrcompilation^.items);
	local (sectionlist = xml.getAddressList (adrnewyorktimes, "Section"), ct = 1, i);
	local (section = sectionlist [1]);
	local (sectionname = xml.getAttributeValue (section, "name"));
	bundle { //set channeltitle, channellink, channeldescription
		adrcompilation^.channeltitle = "New York Times: " + sectionname;
		adrcompilation^.channellink = "http://radio.userland.com/newYorkTimes";
		adrcompilation^.channeldescription = "Headlines, abstracts and links for the latest New York Times articles, for Radio UserLand."};
	local (articlelist = xml.getAddressList (section, "article"), article);
	for j = sizeof (articlelist) downto 1 {
		local (article = articlelist [j]);
		adritem = @adrcompilation^.items.[string.padwithzeros (ct++, 5)];
		new (tabletype, adritem);
		local (link = decode (xml.getValue (article, "url")));
		local (title = decode (xml.getValue (article, "headline")));
		local (byline = easy2readname (decode (xml.getValue (article, "byline"))));
		local (description = decode (xml.getValue (article, "abstract")));
		adritem^.title = "<a href=\"" + link + "\">" + title + "</a>. " + description;
		if sizeof (byline) > 0 {
			adritem^.title = adritem^.title + " " + byline + "."};
		if flSaveData {
			local (adrdata = @adritem^.data);
			new (tabletype, adrdata);
			adrdata^.link = link;
			adrdata^.title = title;
			adrdata^.description = description;
			adrdata^.byline = byline;
			adrdata^.timestamp = converttimestamp (decode (xml.getValue (article, "timestamp")))};
		addToHistory (adritem)};
	return (true)}
<<bundle //test code
	<<local (url = "http://radiouser:Csm!]-tvMm@partners.userland.com/nyt/business.xml")
	<<local (adrservice = xml.rss.initService (url, @aggregatorData.services))
	<<new (tabletype, @adrservice^.compilation)
	<<new (tabletype, @adrservice^.compilation.itemHistory)
	<<adrservice^.xmltext = tcp.httpReadUrl (url)
	<<xml.compile (adrservice^.xmltext, @adrservice^.xmlstruct)
	<<compile (adrservice, true, @xml.aggregator.storyArrivedCallback)



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.