forked from robmiracle/Corona-SDK-RSS-Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.lua
130 lines (125 loc) · 5.18 KB
/
rss.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--
-- rss_feed.lua
-- Copyright © 2011 Omnigeek Media. All Rights Reserved.
--
-- takes a rss feed and returns you a table of stories.
--
module(..., package.seeall)
local xml = require( "xml" ).newParser()
local print_r = require("utility").print_r
function feed(filename, base)
rssFile = "index.rss"
if filename then
rssFile = filename
end
baseDir = system.CachesDirectory
if base then
baseDir = base
end
local feed = {}
local stories = {}
--print("Parsing the feed")
local myFeed = xml:loadFile(rssFile, baseDir)
if myFeed == nil then return nil end
--utility.print_r(myFeed)
local items = myFeed.child[1].child
-- print_r(items)
local i
print("Number of items: " .. #items)
local l = 1
for i = 1, #items do
local item = items[i]
local enclosuers = {}
local e = 1
local story = {}
local thumbnail = {}
if item.name == "title" then feed.title = item.value end
if item.name == "link" then feed.link = item.value end
if item.name == "description" then feed.description = item.value end
if item.name == "lastBuildDate" then feed.lastBuildDate = item.value end
if item.name == "language" then feed.language = item.value end
if item.name == "generator" then feed.generator = item.value end
if item.name == "pubDate" then feed.pubDate = item.value end
if item.name == "ttl" then feed.ttl = item.value end
if item.name == "managingEditor" then feed.managingEditor = item.value end
if item.name == "webMaster" then feed.webMaster = item.value end
if item.name == "image" then feed.image = item.value end
if item.name == "rating" then feed.rating = item.value end
feed.sy = {}
if item.name == "sy:updateFrequency" then feed.sy.updateFrequency = item.value end
if item.name == "sy:updatePeriod" then feed.sy.updatePeriod = item.value end
if item.name == "item" then -- we have a story batman!
--utility.print_r(item.child)
local j
for j = 1, #item.child do
if item.child[j].name == "title" then
story.title = item.child[j].value
end
if item.child[j].name == "link" then
story.link = item.child[j].value
end
if item.child[j].name == "pubDate" then
story.pubDate = item.child[j].value
end
if item.child[j].name == "description" then
story.description = item.child[j].value
end
if item.child[j].name == "author" then story.author = item.child[j].value end
if item.child[j].name == "comments" then story.comments = item.child[j].value end
if item.child[j].name == "source" then story.source = item.child[j].value end
if item.child[j].name == "dc:creator" then
story.dc_creator = item.child[j].value
end
if item.child[j].name == "guid" then
story.guid = item.child[j].value
end
if item.child[j].name == "media:thumbnail" then
print("********** has thumbnail ************")
--utility.print_r(item.child[j].properties)
thumbnail = item.child[j].properties
end
-- Podcast's we have to handle differently
if item.child[j].name == "content:encoded" then
-- get the story body
--[[
print(item.child[j].value)
bodytag = {}
bodytag = item.child[j].child
local p;
story.content_encoded = ""
for p = 1, #bodytag do
if (bodytag[p].value) then
story.content_encoded = story.content_encoded .. bodytag[p].value .. "\n\n"
end
end
]]--
story.content_encoded = item.child[j].value
end
if item.child[j].name == "enclosure" then
local properties = {}
properties = item.child[j].properties
enclosuers[e] = properties
--utility.print_r(properties)
e = e + 1
end
end
--utility.print_r(story)
stories[l] = {}
stories[l].link = story.link
stories[l].title = story.title
stories[l].pubDate = story.pubDate
stories[l].description = story.description
stories[l].dc_creator = story.dc_creator
stories[l].guid = story.guid
stories[l].author = story.author
stories[l].comments = story.comments
stories[l].source = story.source
stories[l].content_encoded = story.content_encoded
stories[l].enclosures = enclosuers
stories[l].thumbnail = thumbnail
l = l + 1
end
end
feed.items = stories
return feed
end