| 1 | /** |
|---|
| 2 | * Parses ATOM feeds and returns an indexed array with all elements |
|---|
| 3 | * |
|---|
| 4 | * @author Jeroen Wijering |
|---|
| 5 | * @version 1.5 |
|---|
| 6 | **/ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.jeroenwijering.feeds.AbstractParser; |
|---|
| 10 | import com.jeroenwijering.utils.StringMagic; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | class com.jeroenwijering.feeds.RSSParser extends AbstractParser { |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** Contructor **/ |
|---|
| 17 | function RSSParser() { super(); }; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | /** build an array with all regular elements **/ |
|---|
| 21 | private function setElements() { |
|---|
| 22 | elements = new Object(); |
|---|
| 23 | elements["title"] = "title"; |
|---|
| 24 | elements["guid"] = "id"; |
|---|
| 25 | elements["category"] = "category"; |
|---|
| 26 | elements["link"] = "link"; |
|---|
| 27 | elements["geo:lat"] = "latitude"; |
|---|
| 28 | elements["geo:long"] = "longitude"; |
|---|
| 29 | elements["geo:city"] = "city"; |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | /** Convert RSS structure to array **/ |
|---|
| 34 | private function parse(xml:XML):Array { |
|---|
| 35 | var arr = new Array(); |
|---|
| 36 | var tpl = xml.firstChild.firstChild.firstChild; |
|---|
| 37 | var ttl; |
|---|
| 38 | while(tpl != null) { |
|---|
| 39 | if (tpl.nodeName.toLowerCase() == "item") { |
|---|
| 40 | var obj = new Object(); |
|---|
| 41 | for(var j=0; j<tpl.childNodes.length; j++) { |
|---|
| 42 | var nod:XMLNode = tpl.childNodes[j]; |
|---|
| 43 | var nnm = nod.nodeName.toLowerCase(); |
|---|
| 44 | if(elements[nnm] != undefined) { |
|---|
| 45 | obj[elements[nnm]] = nod.firstChild.nodeValue; |
|---|
| 46 | } else if(nnm == "description") { |
|---|
| 47 | obj["description"] = StringMagic.stripTagsBreaks( |
|---|
| 48 | nod.firstChild.nodeValue); |
|---|
| 49 | } else if(nnm == "pubdate") { |
|---|
| 50 | obj["date"] = rfc2Date(nod.firstChild.nodeValue); |
|---|
| 51 | } else if(nnm == "dc:date") { |
|---|
| 52 | obj["date"] = iso2Date(nod.firstChild.nodeValue); |
|---|
| 53 | } else if(nnm == "media:credit") { |
|---|
| 54 | obj["author"] = nod.firstChild.nodeValue; |
|---|
| 55 | } else if(nnm == "media:thumbnail") { |
|---|
| 56 | obj["image"] = nod.attributes.url; |
|---|
| 57 | } else if(nnm == "itunes:image") { |
|---|
| 58 | obj["image"] = nod.attributes.href; |
|---|
| 59 | } else if(nnm == "georss:point") { |
|---|
| 60 | var gpt = nod.firstChild.nodeValue.split(" "); |
|---|
| 61 | obj["latitude"] = Number(gpt[0]); |
|---|
| 62 | obj["longitude"] = Number(gpt[1]); |
|---|
| 63 | } else if(nnm == "enclosure" || nnm == "media:content") { |
|---|
| 64 | var typ = nod.attributes.type.toLowerCase(); |
|---|
| 65 | if(mimetypes[typ]!=undefined && obj["type"] == undefined) { |
|---|
| 66 | obj["type"] = mimetypes[typ]; |
|---|
| 67 | obj['file'] = nod.attributes.url; |
|---|
| 68 | obj['duration'] = |
|---|
| 69 | StringMagic.toSeconds(nod.attributes.duration); |
|---|
| 70 | if(obj["file"].substr(0,4) == "rtmp") { |
|---|
| 71 | obj["type"] = "rtmp"; |
|---|
| 72 | } else if(obj['file'].indexOf('youtube.com') > -1) { |
|---|
| 73 | obj["type"] = "youtube"; |
|---|
| 74 | } |
|---|
| 75 | if(nod.childNodes[0].nodeName=="media:thumbnail"){ |
|---|
| 76 | obj["image"]=nod.childNodes[0].attributes.url; |
|---|
| 77 | } |
|---|
| 78 | } else if(obj["type"] != undefined && typ == "video/x-flv") { |
|---|
| 79 | obj['fallback'] = nod.attributes.url; |
|---|
| 80 | } else if(typ == "captions") { |
|---|
| 81 | obj["captions"] = nod.attributes.url; |
|---|
| 82 | } else if(typ == "audio") { |
|---|
| 83 | obj["audio"] = nod.attributes.url; |
|---|
| 84 | } |
|---|
| 85 | } else if(nnm == "media:group") { |
|---|
| 86 | for(var k=0; k< nod.childNodes.length; k++) { |
|---|
| 87 | var ncn=nod.childNodes[k].nodeName.toLowerCase(); |
|---|
| 88 | if(ncn == "media:content") { |
|---|
| 89 | var ftp = nod.childNodes[k].attributes.type.toLowerCase(); |
|---|
| 90 | if(mimetypes[ftp] != undefined && obj["type"] == undefined) { |
|---|
| 91 | obj["file"] = nod.childNodes[k].attributes.url; |
|---|
| 92 | obj['duration'] = StringMagic.toSeconds( |
|---|
| 93 | nod.attributes.duration); |
|---|
| 94 | obj["type"]=mimetypes[ftp]; |
|---|
| 95 | if(obj["file"].substr(0,4) == "rtmp") { |
|---|
| 96 | obj["type"] = "rtmp"; |
|---|
| 97 | } else if(obj['file'].indexOf('youtube.com') > -1) { |
|---|
| 98 | obj["type"] = "youtube"; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | if(obj["type"] != undefined && ftp == "video/x-flv") { |
|---|
| 102 | obj['fallback'] = nod.childNodes[k].attributes.url; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | if(ncn == "media:thumbnail") { |
|---|
| 106 | obj["image"]=nod.childNodes[k].attributes.url; |
|---|
| 107 | } |
|---|
| 108 | if(ncn == "media:credit") { |
|---|
| 109 | obj["author"]=nod.childNodes[k].firstChild.nodeValue; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | if(obj["image"] == undefined) { |
|---|
| 115 | if(obj["file"].indexOf(".jpg") > 0 || |
|---|
| 116 | obj["file"].indexOf(".png") > 0 || |
|---|
| 117 | obj["file"].indexOf(".gif") > 0) { |
|---|
| 118 | obj["image"] = obj["file"]; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | if(obj["author"] == undefined) { obj["author"] = ttl; } |
|---|
| 122 | arr.push(obj); |
|---|
| 123 | } else if (tpl.nodeName == "title") { |
|---|
| 124 | ttl = tpl.firstChild.nodeValue; |
|---|
| 125 | } |
|---|
| 126 | tpl = tpl.nextSibling; |
|---|
| 127 | } |
|---|
| 128 | return arr; |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | } |
|---|