RSS Parser Take Two

Posted by robmiracle, Posted on January 6, 2012

1 vote
GitHub URL: 
https://github.com/robmiracle/Corona-SDK-RSS-Reader

I've replaced the old RSS parser with this one.....

Please use this one instead.

This project includes a very basic RSS parser which requires the use of Jonathan Beebee's XML parser (included).

It should process any well formed, valid RSS 2.0 feed.

I still need to update the atom.lua parser for ATOM 1.0 feeds and it will be added here when it's ready.

Please note, you will need daily build 721 if you want to use network.download() to take advantage of system.CachesDirectory.


Replies

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

Atom support has been added.

jpenne's picture
jpenne
User offline. Last seen 8 hours 5 min ago. Offline
Joined: 18 Jan 2011

Thx robmiracle

TandG's picture
TandG
User offline. Last seen 1 day 7 hours ago. Offline
Joined: 16 Jun 2011

Looks good, thanks!

Would this work with facebook RSS feeds too?

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

Facebook has RSS feeds? jk!

I've not tried it, but as long as they do RSS 2.0 and you don't need any whacky name-spaced nodes it should.

TandG's picture
TandG
User offline. Last seen 1 day 7 hours ago. Offline
Joined: 16 Jun 2011

That's what i was hoping.. but alas, i couldn't get it to work :(

I posted a thread to see if anyone else has tried to access facebooks RSS feeds but no replies as of yet.

http://developer.anscamobile.com/forum/2012/01/11/facebooktwitter-feeds-rss-or-alternative

akviby's picture
akviby
User offline. Last seen 1 week 6 days ago. Offline
Joined: 20 Jan 2011

This app does not work at all when I try to run it?

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

Are you getting an error?

How is it not running?

Look at your console window, there should be the feed dumped out.

This is just parser code, there is no display piece to it. You're handed a table of stories to do with as you please.

soclean
User offline. Last seen 16 weeks 57 sec ago. Offline
Joined: 14 Dec 2009

so... any way you could provide just a *bit* more explanation on how to get the information displayed inside of Corona terminal?

I tried to use something like this:

local FeedTexttwo = myfeed.title
text:setTextColor(255)
local text = display.newText(FeedTexttwo,30,35,"Helvetica", 12)

but that just got me a runtime error...

I really want to pull off the concept here, but could use a little more guidance?

I also looked at this (http://developer.anscamobile.com/content/tables-arrays), which was pretty complex, over my head I have to admit...

Thoughts; help? Designer used to basic actionscript over here.

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

Well first of all, you can't set the text color of the text until you've create it.

So try this.

Edit the function: processRSSFeed() in the main.lua

at the bottom below the: "print ("Got ", #stories, " stories")" line, add your code, but like this:

1
2
3
local FeedTexttwo = feed.title
local text = display.newText(FeedTexttwo,30,35,"Helvetica", 12)
text:setTextColor(255)

Realistically the simplest use of this, assuming you have used network.download() to fetch the rss feed and you've stored it in system.DocumentsDirectory with the name "index.rss" would be:

1
2
3
local rss = require("rss")
 
local myFeed = rss("index.rss",system.DocumentsDirectory)

At this point, myFeed is now a lua table with an entry for each of the top level RSS channel like link, title and description as well as a table of items called "items".

So you have:
myFeed.title
myFeed.link
myFeed.description
myFeed.items

It is then your responsibility, to take that table of times and iterate over it, pulling out whatever relevant information you need out of it.

soclean
User offline. Last seen 16 weeks 57 sec ago. Offline
Joined: 14 Dec 2009

ah... ok... replaced some of the info I was using with what you're using... starting to get some information pulling in to my app.

thanks.

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

If you look in the community code for a function called "print_r", its based on the PHP function of the same name, which is a table dumper.

Get that code and put it at the top of your program and then call

print_r(feed)

on the table returned from the rss parser and in your console window you will see all the values returned from the call. Then you can access them accordingly.

soclean
User offline. Last seen 16 weeks 57 sec ago. Offline
Joined: 14 Dec 2009

thank you! One other question. I notice when I view source on the web, that the image associated with each story/blog entry on the Ansca blog is tied to the title. But in your feed, there is only text associated with the title.

Is there any way to grab images through this parser, or is that outside the scope of what this parser does?

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

what is the URL?
what is missing?

RSS 2.0 have very well defined XML tags that are supported. There are a lot of other tags that are added in using what are called "Namespaces".

I only support a very limited set of named spaced fields. If it's in a tag that looks like:

or I'm currently not supporting much of anything that has a ":" in it, other than:

sy:updateFrequency
sy:updatePeriod
media:thumbnail
dc:creator
content:enccoded

I'm hyper busy right now. I do plan to eventually add in support for all of the media and itunes namespace.

hlh132132
User offline. Last seen 14 weeks 2 hours ago. Offline
Joined: 29 Oct 2011

why ?

The file sandbox for this project is located at the following folder:

entering displayFeed index.rss http://www.ijawad-dev.net/blog/?feed=rss
isReachable true
table: 0x1838730 {
[isError] => false
[name] => "networkRequest"
[response] => "/Users/ xx/Library/Application Support/Corona Simulator/robmiracle-Corona-SDK-RSS-Reader-9b6d429-62D835A841675FB82EB1630DA6A5F57D/tmp/index.rss"
}

calling processRSSFeed because the feed is avaialble
Parsing the feed
Runtime error
...ds/robmiracle-Corona-SDK-RSS-Reader-9b6d429/main.lua:27: attempt to call field 'feed' (a nil value)
stack traceback:
[C]: in function 'feed'
...ds/robmiracle-Corona-SDK-RSS-Reader-9b6d429/main.lua:27: in function 'processAtomFeed'
...ds/robmiracle-Corona-SDK-RSS-Reader-9b6d429/main.lua:47: in function <...ds/robmiracle-Corona-SDK-RSS-Reader-9b6d429/main.lua:40>

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

What are the contents of this file:

/Users/ xx/Library/Application Support/Corona Simulator/robmiracle-Corona-SDK-RSS-Reader-9b6d429-62D835A841675FB82EB1630DA6A5F57D/tmp/index.rss

hlh132132
User offline. Last seen 14 weeks 2 hours ago. Offline
Joined: 29 Oct 2011

nothing

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

Well the rss function can't parse an empty or an incomplete file.

Look at your downloading to make sure you're actually getting your file downloaded.

hlh132132
User offline. Last seen 14 weeks 2 hours ago. Offline
Joined: 29 Oct 2011
robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

what is the value of "feedURL" and "feedName"

Also in your network.setStatusListener call must be the domain name only, in other words: www.ijawad-dev.net

without the bits after the .net.

hlh132132
User offline. Last seen 14 weeks 2 hours ago. Offline
Joined: 29 Oct 2011

local feedURL = "www.ijawad-dev.net/blog/?feed=rss"
local feedName = "index.rss"

index.ss is :

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
<rss version="0.92">
<channel>
<title>iJawad</title>
<link>http://www.ijawad-dev.net/blog</link>
<description>مدونة ووردبريس عربية أخرى</description>
<lastBuildDate>Wed, 30 Nov 2011 23:14:16 +0000</lastBuildDate>
<docs>http://backend.userland.com/rss092</docs>
<language>en</language>
<!-- generator="WordPress/3.2.1" -->
<item>
<title>klug</title>
<description>
<![CDATA[ lkhhgf ]]>
</description>
<link>http://www.ijawad-dev.net/blog/?p=9</link>
</item>
<item>
<title>test</title>
<description>
<![CDATA[ this is test ]]>
</description>
<link>http://www.ijawad-dev.net/blog/?p=4</link>
</item>
<item>
<title>أهلاً بالعالم !</title>
<description>
<![CDATA[
مرحباً بك في ووردبريس. هذه تدوينتك الأولى. حررها أو إحذفها , ثم ابدأ التدوين !
]]>
</description>
<link>http://www.ijawad-dev.net/blog/?p=1</link>
</item>
</channel>
</rss>

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

you need to have an http:// in the feedURL.

hlh132132
User offline. Last seen 14 weeks 2 hours ago. Offline
Joined: 29 Oct 2011

thank you so much

but noq i have new problem

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
Copyright (C) 2009-2011  A n s c a ,  I n c .
        Version: 2.0.0
        Build: 2011.704
The file sandbox for this project is located at the following folder:
        (/Users/asmaltwyjry/Library/Application Support/Corona Simulator/robmiracle-Corona-SDK-RSS-Reader-9b6d429 2-AFE89693844069C736873A12D48A8D4D)
entering displayFeed    index.rss       http://ijawad-dev.net/blog/?feed=rss
isReachable     true
table: 0x1acb130 {
  [isError] => false
  [name] => "networkRequest"
  [response] => "/Users/asmaltwyjry/Library/Application Support/Corona Simulator/robmiracle-Corona-SDK-RSS-Reader-9b6d429 2-AFE89693844069C736873A12D48A8D4D/Documents/index.rss"
}
 
calling processRSSFeed because the feed is avaialble
Parsing the feed
/Users/asmaltwyjry/Library/Application Support/Corona Simulator/robmiracle-Corona-SDK-RSS-Reader-9b6d429 2-AFE89693844069C736873A12D48A8D4D/Caches/index.rss: No such file or directory
Runtime error
        .../robmiracle-Corona-SDK-RSS-Reader-9b6d429 2/atom.lua:27: attempt to index local 'myFeed' (a nil value)
stack traceback:
        [C]: ?
        .../robmiracle-Corona-SDK-RSS-Reader-9b6d429 2/atom.lua:27: in function 'feed'
        .../robmiracle-Corona-SDK-RSS-Reader-9b6d429 2/main.lua:30: in function 'processAtomFeed'
        .../robmiracle-Corona-SDK-RSS-Reader-9b6d429 2/main.lua:50: in function <.../robmiracle-Corona-SDK-RSS-Reader-9b6d429 2/main.lua:43>
 
 
 
 

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

I see two possible problems. The first:

/Users/asmaltwyjry/Library/Application Support/Corona Simulator/robmiracle-Corona-SDK-RSS-Reader-9b6d429 2-AFE89693844069C736873A12D48A8D4D/Caches/index.rss: No such file or directory
Runtime error

It's not finding the index.rss file. Since you are running the last stable build, you may need to change all references to system.CachesDirectory to system.TemporaryDirectory.

There was a bug in network.download that was recently fixed that prevented it from using system.CachesDirectory.

Secondly, your feed is an RSS 2.0 feed and to an ATOM feed. There are two parsers in the package, one for each feed type. You cannot run an RSS feed through the ATOM processor or the other way around.

hlh132132
User offline. Last seen 14 weeks 2 hours ago. Offline
Joined: 29 Oct 2011

i want example from you..
a live example ^^ (like your website)

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

Well I can't and I wont. This may seem like a snotty answer or an uncooperative answer, but here are the reasons why.

1. You are running the last public release of Corona. Since then, the Widget Library, which my app depends on has changed significantly. In fact it has changed two times and I don't have my app up to speed with the recent changes and it certainly won't work with the last public build. I'm not planning a new release of this app until mid spring at the earliest.

2. You are running the last public release of Corona. A bug in the network.download API has been fixed since that release that allows me to use the system "Caches" directory to store the XML/RSS files that I download per Apple's new requirements. Prior to that fix, you either have to download the files to system.DocumentsDirectory or system.TemporaryDirectory. If you not familiar with those items, I suggest you spend time reading the API and learning how Corona File IO and Network downloads work (as well as brush up on URL's; how they are specified and the difference between a domain name and a URL and where in the Corona API you need both.) I don't have the time to back-fit this project for 703 the last public build (though I think 704 which came out the next day may have replaced 703)

So until you purchase a license, you are not going to be able to use either my rss parser or my sample code unless....

3. I'm hyper busy with projects that other people are paying me to get done in a timely fashion. While I contributed the code (which has worked in several published apps and at least one other contributed project), my time to support it is limited. I assist where I can, but I'm not going to write your app for you. Paying customers come first. My Paypal address is rob at robmiracle dot com.

4. My app is also dependent on a tabView controller, Director 1.4, having access to my graphics. I'm not going to provide my entire working app. If you want me to buy my full app, my Paypal address is above.

5. I wrote the RSS parser to give back to the community for helping me and I'm willing to help people implement it, if they are willing to spend some time learning how to use it. Or more importantly learning the other parts my code doesn't do like put things on the screen, download files, learning how file paths work, etc.

6. I'm going to include the full lua file that I use on one of my tabs. I'm not going to gut it to take out the tabbar references in main, adjust it because it has room for banner ads, retrofit it to Corona.704 , make it storyboard compatible or anything. You are going to have to figure some things out on your own or hopefully have some other people chime in.

7. Finally the RSS code I submitted has the sole purpose of parsing a valid RSS2.0 feed. (Well the atom.lua file has the sole purpose of parsing a valid Atom feed.). It in no means expresses or implies what you should do with that data or how you get the data downloaded in the first place. The provided main.lua was a starting point, showing how I download files from the net. It should be pointed out, that we had trouble with downloading RSS files on Android running into some problem writing to the system.DocumentsDirectory. If someone would like to upgrade me to Pro and provide a working android device for me to test on I would be happy to figure it out and get it working on Android. Paypal account listed above!

So here it goes, no warranties expressed or implied!

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
module(..., package.seeall)
 
local widget = require("widget")
widget.setTheme( "theme_ios" )
local rss = require( "rss" )
--local webpage = require("webpage") -- display story as a web popup
local ui = require("ui")
local utility = require("utility")
local print_r = utility.print_r
local feedName = "podcasts.rss"
local feedURL = "http://theanimalscankillyou.com/category/podcasts/feed/"
local displayMode = "podcast"
local pageTitle = "tACKY Podcasts"
local myList = nil
local stories = {}
local listGroup = display.newGroup()
local imageList = {}
local itemIcon = {
        image = "images/tacky_bullet.png",
        width = 46,
        height = 56,
        base = system.ResourcesDirectory,
        paddingTop = 20,
        paddingLeft = 10,
        paddingRight = 12
    }
 
local function showTableView()
    print("Calling showTableView()")
    --native.setActivityIndicator( true )
    myList = widget.newTableView{ top = 64+50, width = 320, height = 320, maskFile = "images/mask-320x320.png" }
    listGroup:insert(myList)
 
    local function popUpClosed(event)
        print("popUpClosed")
        return true
    end
    
    local onItemRelease = function( event )
        if event.phase == "release" then
            --print_r(event.target)
            native.setActivityIndicator( true )
            id = event.index
            print(id)
            local story = {}
            story = stories[id]
            print_r(story)
            _G.story = story
            director:openPopUp("podcast", popUpClosed)
            --local myPage = webpage.new()
    --        myPage = webpage.new(story, pageTitle)
        end
        return true
    end
 
        
    local function onRowRender(event)
        print("row render")
        local row = event.target
        local rowGroup = event.view
        local id = event.index
        if event.index > #stories then return true end
        print(id)
        print(stories[id].title)
 
        row.bg = display.newRect(0, 0, display.contentWidth, 60)
        row.bg:setFillColor(217, 238, 221, 224)
        if id % 2 == 1 then 
            row.bg:setFillColor(213, 230, 228, 224)
        end
        rowGroup:insert(row.bg)
        row.icon = display.newImageRect(itemIcon.image, itemIcon.width , itemIcon.height )
        row.icon:setReferencePoint(display.TopLeftReferencePoint)
        row.icon.x = itemIcon.paddingLeft
        row.icon.y = 2
 
        local myTitle = string.sub(stories[id].title, 1, 22)
        row.title = display.newRetinaText( myTitle, 12, 0, "Helvetica-Bold", 18 )
        row.title:setReferencePoint( display.CenterLeftReferencePoint )
        row.title.y = itemIcon.paddingTop
        row.title.x = itemIcon.paddingLeft + itemIcon.paddingRight + itemIcon.width
        row.title:setTextColor( 0 )
 
        local timeStamp = string.match(stories[id].pubDate,"%w+, %d+ %w+ %w+ %w+:%w+")
        row.subtitle = display.newRetinaText( timeStamp, 12, 0, "Helvetica", 14)
        row.subtitle:setReferencePoint(display.CenterLeftReferencePoint )
        row.subtitle.y = itemIcon.paddingTop + itemIcon.paddingTop
        row.subtitle.x = itemIcon.paddingLeft + itemIcon.paddingRight + itemIcon.width
        row.subtitle:setTextColor(96, 96, 96, 255)
 
        row.rightArrow = display.newImageRect("images/rightarrow.png", 10, 14)
        row.rightArrow:setReferencePoint(display.TopLeftReferencePoint)
        row.rightArrow.x = display.contentWidth - itemIcon.paddingRight - 10
        row.rightArrow.y = itemIcon.paddingTop
        -- must insert everything into event.view:
        rowGroup:insert(row.icon)
        rowGroup:insert( row.title )
        rowGroup:insert(row.subtitle)
        rowGroup:insert(row.rightArrow)
        return true
    end
 
    for i = 1, #stories do
 
        print("insert row:  " .. i .. " [" .. stories[i].title .. "]")
        myList:insertRow{
            onEvent = onItemRelease,
            onRender = onRowRender,
            id = i,
            isCategory = false,
            rowColor = {255, 255, 255, 255},
            lineColor = { 70, 70, 130, 255 },
        }
    end
    native.setActivityIndicator( false )
end
 
function displayFeed(feedName, feedURL)
 
print("entering displayFeed", feedName, feedURL)
 
    local function processRSSFeed(file, path)
        print("Parsing the feed")
        --native.showAlert("processRSSFeed", "before rss.feed", {"OK"})
        local story = {}
        local feed = rss.feed(file, path)
        stories = feed.items
        --native.showAlert("processRSSFeed", "after rss.feed", {"OK"})
        --print_r(stories)
        print("Num stories: " .. #stories)
        print("Got ", #stories, " stories, now show the tableView")
        showTableView()
    end
    
    local function onAlertComplete( event )
        return true
    end
    
    local networkListener = function( event )
        utility.print_r(event)
        if ( event.isError ) then
            local alert = native.showAlert( "tACKY", "Feed temporarily unavaialble.", 
                                        { "OK" }, onAlertComplete )
        else
            print("calling processRSSFeed because the feed is avaialble")
            processRSSFeed(feedName, system.DocumentsDirectory)
        end
        return true
    end
    
    function MyNetworkReachabilityListener(event)
        --native.showAlert("network", "is reachable", {"OK"})
        print( "isReachable", event.isReachable )
        network.setStatusListener( "www.theanimalscankillyou.com", nil )
        if event.isReachable then
            -- download the latest file
            native.setActivityIndicator( true )
            --native.showAlert("network", "downloading", {"OK"})
            network.download(feedURL, "GET", networkListener, feedName, system.DocumentsDirectory)
        else
            print("not reachable")
            --native.showAlert("network", "using cached copy", {"OK"})
            -- look for an existing copy
            local path = system.pathForFile(feedName, system.DocumentsDirectory)
            local fh, errStr = io.open( path, "r" )
            if fh then
                io.close(fh)
                print("calling processRSSfeed because the network isn't reachable")
                processRSSFeed(feedName, system.DocumentsDirectory)
            else
                local alert = native.showAlert( "tACKY", "Feed temporarily unavaialble.", 
                                            { "OK" }, onAlertComplete )
            end
        end
        return true
    end
   
    if network.canDetectNetworkStatusChanges then
        network.setStatusListener( "www.theanimalscankillyou.com", MyNetworkReachabilityListener )
    else
        native.showAlert("network", "not supported", {"OK"})
        print("network reachability not supported on this platform")
    end
end
 
 
 
-- Called immediately after scene has moved onscreen:
new = function( params )
    local localGroup = display.newGroup()
    -----------------------------------------------------------------------------
            
    --      INSERT code here (e.g. start timers, load audio, start listeners, etc.)
    
    -----------------------------------------------------------------------------   
    print("Entering feed")
 
    print("calling displayFeed")
    displayFeed(feedName, feedURL)
 
 
    function localGroup:clean()
        print("cleaning")
        if myList then
            print("removing list")
            --myList:removeSelf()
        end
        --myList = nil
    end
    titleText:setText(pageTitle)
 
    return localGroup
end

I am struggling right now cleaning this up when its done. I suspect its not fully widgetfied for 726 that I'm running.

Sorry to be a mercenary jerk, but I have to take care of the income first.

eroth's picture
eroth
User offline. Last seen 3 days 3 hours ago. Offline
Joined: 31 Aug 2011

Looking for a little assistance or advice using this function...

When I use it as is, it works great. I can even change and load up some other sites. But, I am having issues when trying to use it with the site I need to.

Here is what show in the terminal...

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
Copyright (C) 2009-2012  A n s c a ,  I n c .
        Version: 2.0.0
        Build: 2012.781
The file sandbox for this project is located at the following folder:
        (/Users/EvilE/Library/Application Support/Corona Simulator/16-0CD4FF3926D00E17A4F592005238A642)
entering displayFeed    index.rss       http://thebannockalternative.com/wordpress/wp-rss.php
isReachable     true
 
table: 0x5610e0 {
  [name] => "networkRequest"
  [status] => 200
  [url] => "/Users/EvilE/Library/Application Support/Corona Simulator/16-0CD4FF3926D00E17A4F592005238A642/Caches/index.rss"
  [isError] => false
  [response] => "/Users/EvilE/Library/Application Support/Corona Simulator/16-0CD4FF3926D00E17A4F592005238A642/Caches/index.rss"
}
 
calling processRSSFeed because the feed is avaialble
Parsing the feed
table: 0x561420 {
  [entries] => table: 0x561420 {
               }
}
 
Num stories: 0
Got     0        stories

So (and I have verified this with the directories ) the data file IS downloading with no issue. I can even open it up, but it is not being parsed and I can not figure out why. the data looks like this ...

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
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        >
 
<channel>
        <title>TBA</title>
        <atom:link href="http://thebannockalternative.com/wordpress/feed/" rel="self" type="application/rss+xml" />
        <link>http://thebannockalternative.com/wordpress</link>
        <description>Southeast Idaho&#039;s Only Alternative</description>
        <lastBuildDate>Fri, 13 Apr 2012 18:40:23 +0000</lastBuildDate>
        <language>en</language>
        <sy:updatePeriod>hourly</sy:updatePeriod>
        <sy:updateFrequency>1</sy:updateFrequency>
        <generator>http://wordpress.org/?v=3.3.1</generator>
                <item>
                <title>Idaho Falls police looking for bank robber</title>
                <link>http://thebannockalternative.com/wordpress/idaho-falls-police-looking-for-bank-robber/</link>
                <comments>http://thebannockalternative.com/wordpress/idaho-falls-police-looking-for-bank-robber/#comments</comments>
                <pubDate>Fri, 13 Apr 2012 17:37:08 +0000</pubDate>
                <dc:creator>thebannockalternative</dc:creator>
                                <category><![CDATA[Blog]]></category>
                <category><![CDATA[Local News]]></category>
 
                <guid isPermaLink="false">http://thebannockalternative.com/wordpress/?p=2619</guid>
                <description><![CDATA[Idaho Falls police are looking for the man who robbed a Wells Fargo Bank on Friday morning. According to initial reports, a white man entered the bank at 305 First St. around 10:46 a.m. demanding money. Police do not know whether or not he had a weapon, but said it looks like he passed a note demanding the money. Bank [...]]]></description>
                        <content:encoded><![CDATA[<div id="attachment_2622" class="wp-caption alignright" style="width: 471px"><a href="http://thebannockalternative.com/wordpress/wp-content/uploads/2012/04/bank-robbery-suspect.jpg"><img class="size-full wp-image-2622 " title="Idaho Falls Bank Robbery Suspect" src="http://thebannockalternative.com/wordpress/wp-content/uploads/2012/04/bank-robbery-suspect.jpg" alt="" width="461" height="307" /></a><p class="wp-caption-text">This photo from the Idaho Falls Police Department shows the suspect in today's bank robbery</p></div>
<p style="text-align: center;">Idaho Falls police are looking for the man who robbed a Wells Fargo Bank on Friday morning.</p>
<p>According to initial reports, a white man entered the bank at 305 First St. around 10:46 a.m. demanding money. Police do not know whether or not he had a weapon, but said it looks like he passed a note demanding the money. Bank employees complied, giving him an undisclosed amount of cash.</p>
<p>The man then took off on foot, headed west. Witnesses described him as about six feet tall, medium build and in his early 40s. He is wearing a black jacket, khaki pants a black baseball cap and a neck brace.</p>
<p>Police are asking anyone with any information to call the IFPD at 208.529.1200 or Crimestoppers at 208.522.1983.</p>
]]></content:encoded>
                        <wfw:commentRss>http://thebannockalternative.com/wordpress/idaho-falls-police-looking-for-bank-robber/feed/</wfw:commentRss>
                <slash:comments>0</slash:comments>
                </item>

To myself it seems like it is formatting properly, but I am unsure. This is coming from a WordPress site. the code I am using is as follows...

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://thebannockalternative.com/wordpress/wp-rss.php"
--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( "thebannockalternative.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( "thebannockalternative.com", MyNetworkReachabilityListener )
    else
        native.showAlert("network", "not supported", {"OK"})
        print("network reachability not supported on this platform")
    end
end
 
displayFeed(feedName, feedURL)

I am not looking for someone to rewrite and do this for me, but any help pointing me in the right directions so that I can get this working is very much appreciated. I have tried this with a few different feeds out there and some work great, others do the same thing. what I need to figure out is why and find a solution.

Thanks in advance for any help or advice on this!

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

There likely some un-escaped UTF-8 character showing up some where that's breaking the XML.

You can turn on the debugging prints in xml.lua and the same with rss.lua and see if you get any additional console messages that might point to the problem.

be careful turning on the print_r' statements because they dump a lot of data.

eroth's picture
eroth
User offline. Last seen 3 days 3 hours ago. Offline
Joined: 31 Aug 2011

@robmiracle

thanks - i enabled the XML debug and got this...

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
Parsing the feed
XmlParser:      openTag =rss
XmlParser:      openTag =channel
XmlParser:      openTag =title
XmlParser:      closeTag=title
XmlParser:      openTag =link
XmlParser:      closeTag=link
XmlParser:      openTag =description
XmlParser:      closeTag=description
XmlParser:      openTag =lastBuildDate
XmlParser:      closeTag=lastBuildDate
XmlParser:      openTag =language
XmlParser:      closeTag=language
XmlParser:      openTag =sy:updatePeriod
XmlParser:      closeTag=sy:updatePeriod
XmlParser:      openTag =sy:updateFrequency
XmlParser:      closeTag=sy:updateFrequency
XmlParser:      openTag =generator
XmlParser:      closeTag=generator
XmlParser:      openTag =item
XmlParser:      openTag =title
XmlParser:      closeTag=title
XmlParser:      openTag =link
XmlParser:      closeTag=link
XmlParser:      openTag =comments
XmlParser:      closeTag=comments
XmlParser:      openTag =pubDate
XmlParser:      closeTag=pubDate
XmlParser:      openTag =dc:creator
XmlParser:      closeTag=dc:creator
XmlParser:      openTag =category
XmlParser:      Found CDATA start tag   1278
XmlParser:      Found CDATA end tag     1284
XmlParser:      closeTag=category
XmlParser:      openTag =category
XmlParser:      Found CDATA start tag   1318
XmlParser:      Found CDATA end tag     1330
XmlParser:      closeTag=category
XmlParser:      openTag =guid
XmlParser:      closeTag=guid
XmlParser:      openTag =description
XmlParser:      Found CDATA start tag   1454
XmlParser:      Found CDATA end tag     1781
XmlParser:      closeTag=description
XmlParser:      openTag =content:encoded
XmlParser:      Found CDATA start tag   1826
XmlParser:      Found CDATA end tag     3218
XmlParser:      closeTag=content:encoded
XmlParser:      openTag =wfw:commentRss
XmlParser:      closeTag=wfw:commentRss
XmlParser:      openTag =slash:comments
XmlParser:      closeTag=slash:comments
XmlParser:      closeTag=item
XmlParser:      openTag =item
XmlParser:      openTag =title
XmlParser:      closeTag=title
XmlParser:      openTag =link
XmlParser:      closeTag=link
XmlParser:      openTag =comments
XmlParser:      closeTag=comments
XmlParser:      openTag =pubDate
XmlParser:      closeTag=pubDate
XmlParser:      openTag =dc:creator
XmlParser:      closeTag=dc:creator
XmlParser:      openTag =category
XmlParser:      Found CDATA start tag   3829
XmlParser:      Found CDATA end tag     3835

Thats only a brief part of it , took me a bit of time to go through it but it didn't throw any errors, and everything seems to open and close properly. I will next try the rss parser debug as you suggested.

Also, on a side note I thought maybe the feed was corrupt, so I ran it in the online vallidator and it checked out fine for RSS 2.0, so in theory it should all work :/

Thanks for helping a noob :)

eroth's picture
eroth
User offline. Last seen 3 days 3 hours ago. Offline
Joined: 31 Aug 2011

So, I turned off the DEBUG in the Xml file and enabled all the print_r statements in the RSS file..

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
Copyright (C) 2009-2012  A n s c a ,  I n c .
        Version: 2.0.0
        Build: 2012.781
The file sandbox for this project is located at the following folder:
        (/Users/EvilE/Library/Application Support/Corona Simulator/16-0CD4FF3926D00E17A4F592005238A642)
entering displayFeed    index.rss       http://www.thebannockalternative.com/wordpress/feed
 
isReachable     true
 
table: 0x53de40 {
  [name] => "networkRequest"
  [status] => 200
  [url] => "/Users/EvilE/Library/Application Support/Corona Simulator/16-0CD4FF3926D00E17A4F592005238A642/Caches/index.rss"
  [isError] => false
  [response] => "/Users/EvilE/Library/Application Support/Corona Simulator/16-0CD4FF3926D00E17A4F592005238A642/Caches/index.rss"
}
 
calling processRSSFeed because the feed is avaialble
Parsing the feed
table: 0x1842840 {
  [entries] => table: 0x1842840 {
               }
}
 
Num stories: 0
Got     0        stories

That is the same thing as having them all disabled so something is missing here. I will start in a while going through the parser and see if there is a child field or something that is spelled different, or something out of sequence after I get back from lunch. I still think there is something in the feed causing this, and now I'm bound and determined to find it - LOL - Figures this would happen on Friday the 13th - LOL

If you think of anything let me know - and thanks!!

robmiracle's picture
robmiracle
User offline. Last seen 4 hours 10 min ago. Offline
Joined: 18 Jan 2011

My prints may just be commented out in the rss.lua file.

eroth's picture
eroth
User offline. Last seen 3 days 3 hours ago. Offline
Joined: 31 Aug 2011

So after a ton of research and trial and error last night..

@robmiracle - your code works like a dream!

For anyone else that wants to use this to pull from a WordPress Site...

All the issues were in the XML output from the web end. Make sure you hard code the php script on the blog to pull down the 'ATOM' feed and not the RSS2. Apparently (at least I could not find a way) there is no way to get the RSS 2 to display without the XSL info, and for some reason the new WP sites have a slightly different child structure, causing the parser to report 0 entries. If you use the ATOM feed as follows it will work...

1
local feedURL = "http://www.thebannockalternative.com/wordpress/wp-atom.php"

The key was to reference the actual php file that generates the feed xml.

Now to get all this to display and figure out how to remove the HTML formatting in the [CDATA] blocks :)

eroth's picture
eroth
User offline. Last seen 3 days 3 hours ago. Offline
Joined: 31 Aug 2011

Ok so a couple of things on this I am now trying to figure out... If anyone has advice or directions to point me to I would be appreciative :)

First...
when returning the 'title' the xml file gives me this

1
[title] => "Disturbing details emerge in Afton Allison&#8217;s death"

I am trying to get rid of the &#8217; and replace it with a '
I tried adding this to the xml file in the function(h)
1
if h == "8217" then return "'" end      

But it does not seem to work. Am I missing something?

Also when returning the data is there a way to parse and strip all the html tags and just output plain text? Then when I have the text output truncate it.

Any ideas of help is greatly appreciated! Thanks in Advance!