forked from robmiracle/Corona-SDK-RSS-Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
90 lines (80 loc) · 3.12 KB
/
main.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
local rss = require("rss")
local atom = require("atom")
local print_r = require("utility").print_r
--local feedURL = "http://blog.anscamobile.com/feed/"
local feedURL = "http://daringfireball.net/index.xml"
--local feedName = "index.rss"
local feedName = "index.xml"
local baseDir = system.CachesDirectory
function displayFeed(feedName, feedURL)
print("entering displayFeed", feedName, feedURL)
local function processRSSFeed(file, path)
print("Parsing the feed")
local story = {}
local feed = rss.feed(file, path)
print_r(feed)
local stories = feed.items
native.setActivityIndicator( false )
print("Num stories: " .. #stories)
print("Got ", #stories, " stories")
--print_r(stories)
end
local function processAtomFeed(file, path)
print("Parsing the feed")
local story = {}
local feed = atom.feed(file, path)
print_r(feed)
local stories = feed.entries
native.setActivityIndicator( false )
print("Num stories: " .. #stories)
print("Got ", #stories, " stories")
--print_r(stories)
end
local function onAlertComplete( event )
return true
end
local networkListener = function( event )
utility.print_r(event)
if ( event.isError ) then
local alert = native.showAlert( "RSS", "Feed temporarily unavaialble.",
{ "OK" }, onAlertComplete )
else
print("calling processRSSFeed because the feed is avaialble")
processAtomFeed(feedName, baseDir)
end
return true
end
function MyNetworkReachabilityListener(event)
--native.showAlert("network", "is reachable", {"OK"})
print( "isReachable", event.isReachable )
network.setStatusListener( "blog.anscamobile.com", nil )
if event.isReachable then
-- download the latest file
native.setActivityIndicator( true )
--native.showAlert("network", "downloading", {"OK"})
network.download(feedURL, "GET", networkListener, feedName, baseDir)
else
print("not reachable")
--native.showAlert("network", "using cached copy", {"OK"})
-- look for an existing copy
local path = system.pathForFile(feedName, baseDir)
local fh, errStr = io.open( path, "r" )
if fh then
io.close(fh)
print("calling processRSSfeed because the network isn't reachable")
processAtomFeed(feedName, baseDir)
else
local alert = native.showAlert( "RSS", "Feed temporarily unavaialble.",
{ "OK" }, onAlertComplete )
end
end
return true
end
if network.canDetectNetworkStatusChanges then
network.setStatusListener( "blog.anscamobile.com", MyNetworkReachabilityListener )
else
native.showAlert("network", "not supported", {"OK"})
print("network reachability not supported on this platform")
end
end
displayFeed(feedName, feedURL)