RSS in LiveCode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
bbhank
Posts: 116
Joined: Thu Mar 17, 2016 6:04 pm

RSS in LiveCode

Post by bbhank » Sun Apr 24, 2016 10:54 pm

Looking for working method, script(s) to load RSS feeds. Found one that only works, and is said to work, with Slashdot.
Posted below is another that starts out better.
Last edited by bbhank on Wed Apr 27, 2016 12:26 am, edited 4 times in total.

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: RSS in LiveCode

Post by theotherbassist » Tue Apr 26, 2016 4:57 pm

Do you mean you'd like to write up an RSS reader? You can "load an RSS feed" with a single line:

Code: Select all

put revXMLCreateTreeFromFile([your feed URL here],false,true,false) into tTREE
It's what you do with it (tTree) afterward that makes all the difference.

Hints: read all the "revXML" entries in the Dictionary API. You'll probably also want to figure out how to display as UTF-8, so check out "uniEncode" too. :wink:

bbhank
Posts: 116
Joined: Thu Mar 17, 2016 6:04 pm

Re: RSS in LiveCode

Post by bbhank » Tue Apr 26, 2016 5:23 pm

Please re-read question.

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: RSS in LiveCode

Post by theotherbassist » Tue Apr 26, 2016 10:23 pm

What I'm saying is that the code block you've given will (roughly) work for any properly formatted XML document, so long as you adapt it for what you need. You would have to configure that block to point to the correct location on the web or locally and direct it to display how you want where you want.

If I were you, I'd drop that code like it's hot and start from scratch. Reading in XML is quite easy so long as you understand the format and how Livecode deals with it, and so long as you know what to do with it (put it into an array--like, always).

If you want to understand why the script isn't doing what you want, build a really basic version of what you're after and work it toward the example code. Start by finding an RSS feed example. Look at the XML code in any modern browser--if it's not displaying, choose "view page source" or similar--you will be able to easily figure out the structure (maybe you already know the structure?). But if you're ever in doubt, check out the RSS standards. Then you can create an XML tree in Livecode from the feed you've chosen. From there, use the various revXML functions to access the content of each of the nodes you're interested in (this is why you have to understand the RSS structure, but if you're clever you can use the revXML functions to list out anything that doesn't fit what you're expecting and handle it appropriately)--you'll have to direct the script to place the node contents into array variables. Do what you want with the information you've gathered, and that's pretty much all there is to it.

You don't need that code block to do what you're trying to do (what exactly *are* you trying to do?)

bbhank
Posts: 116
Joined: Thu Mar 17, 2016 6:04 pm

Re: RSS in LiveCode

Post by bbhank » Tue Apr 26, 2016 10:47 pm

Would like to load various RSS news feeds into a page. Some of the content has images. Would like to load that also.
Also to format the output so it looks like I want it to. Is that asking too much? I tried controlling image size in feeds in web pages and it's painful at best.

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: RSS in LiveCode

Post by theotherbassist » Tue Apr 26, 2016 11:23 pm

Ha, I'm working on a project that's basically what you describe (minus the images, and with some extra analytics). If you suggested that not many Livecoders seem all that interested in RSS today, I'd definitely agree with you.

There are a lot of issues with coding RSS readers, especially if you want to display a mix of articles from feeds that originate in different time zones (see: http://forums.livecode.com/viewtopic.php?f=7&t=27009 ) But I'd say it's still definitely a rewarding path to travel down, given Livecode's affinity for text manipulation.

Also, if you want to pull images for personal use--go for it. Nobody will ever know. But RSS is a really gray area legally speaking. By law, titles can't be copyrighted. But that's about as far as you can get with what's *definitely* legal to retransmit or copy in an aggregator without explicit permission. Yes, "fair use" applies in many circumstances when you clearly denote the source of the info (and don't appear to be affiliated with the source in any way). But--and especially when it comes to embedded images or videos--media companies generally feel you're stealing their web traffic if a user can get everything they need out of your reader app (see: the NYT making Apple pull the Pulse RSS iPad app from the store). I'm no lawyer, but I doubt anyone's going to complain if you pull article titles, descriptions, and pubdates--if they really don't want you accessing that information, why would they provide it? Just don't pull the whole provider's website at any point and put in a browser frame. You can open a link in a user's default browser from within Livecode. Maybe that is the best way to avoid any trouble.

Anyway, I played with the above block of code for a little bit. It's a bit convoluted, so you might have trouble figuring out why it keeps returning errors.

In essence, all you have to do is:
1. Create an XML tree from (each) RSS URL
2. List out the contents of each child node of the "items" node (rss/channel/item[x]) and store each in a variable (an array)
3. Display the contents where you want
4. Delete the XML trees if you're done with them

It sounds simple--and it is--but it might be a bit tricky to get scripting until you really read through the dictionary entries of the revXML library.

bbhank
Posts: 116
Joined: Thu Mar 17, 2016 6:04 pm

Re: RSS in LiveCode

Post by bbhank » Wed Apr 27, 2016 12:09 am

Looking for sample(s) that work. The tutorials, lessons have been moved or deleted.

theotherbassist
Posts: 114
Joined: Thu Mar 06, 2014 9:29 am
Location: UK

Re: RSS in LiveCode

Post by theotherbassist » Wed Apr 27, 2016 12:17 am

Ok. Well I'm on my phone now, but I can try to get you started with something to work with tomorrow if nobody else wants to chime in. I only figured it out from the dictionary myself though. Familiarizing yourself with using xml and utf-8 in Livecode in general will help out down the line. The tutorials on xml and unicode are still up.

If you're still playing with the Slashdot code, you might notice that the item path variable is "rdf" but this is not the standard for rss 2.0. "Rss" is usually the first node, followed by "channel." That should be enough to get you going for now.
Last edited by theotherbassist on Wed Apr 27, 2016 12:25 am, edited 2 times in total.

bbhank
Posts: 116
Joined: Thu Mar 17, 2016 6:04 pm

Re: RSS in LiveCode

Post by bbhank » Wed Apr 27, 2016 12:18 am

Been looking but they are gone.

bbhank
Posts: 116
Joined: Thu Mar 17, 2016 6:04 pm

Re: RSS in LiveCode

Post by bbhank » Wed Apr 27, 2016 12:22 am

Code: Select all

local allErrors

on mouseUp
  put 3 into field "loading"
  show field "loading"
  put url "http://rss.syndicationsite.com/feed" into theXML
  
  subtract one from field loading
  put revCreateXMLTree(theXML, true, true, false) into theID
  if theID is not a number then
    answer "revXML encountered this error:" & return & theID as sheet
    put theXML into field "text"
    hide field "loading"
    exit mouseUp
  end if
  if revXMLRootNode(theID) <> "rss" then
    answer "Sorry, but I don't think this URL is a RSS file." as sheet
    put theXML into field "text"
    hide field "loading"
    exit mouseUp
  end if
  set the label of this stack to "RSS Reader / " & revXMLNodeContents(theID, "rss/channel/title")
  put revXMLNumberOfChildren(theID, "rss/channel", "item", 0) into theCount
  set the custompropertysets of this stack to ""
  repeat for theCount times
    put "" into theData
    add one to x
    error revXMLNodeContents(theID,"rss/channel/item[" & x & "]encoded")
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/title") into theData["name"] --theName
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/link") into theData["link"]
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/pubDate") into theData["pubDate"]
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/author") into theData["author"]
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/creator") into theData["creator"]
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/encoded") into theData["content"]
    put revXMLNodeContents(theID,"rss/channel/item[" & x & "]/description") into theData["description"]
    put x into theData["number"]
    repeat for each key theKey in theData
      if startsWith(theData[theKey] ,"xmlerr") then
        if theKey = "content" then
          --error revXMLAttributes(theID,"rss/channel/item[" & x & "]/content:encoded",": ",return)
        end if
        put "" into theData[theKey]
      end if
    end repeat
    set the customproperties["item_" & x] of this stack to theData
  end repeat
  
  subtract one from field "loading"
  put "" into field "list"
  set the itemdelimiter to "_"
  put the custompropertysets of this stack into allItems
  sort allItems numeric by item 2 of each
  repeat for each line theLine in allItems
    put the customproperties[theLine] of this stack into theItem
    put "<p>" & theItem["name"] & "</p>" & return after theOutput
  end repeat
  set the htmltext of field "list" to theOutput
  
  subtract one from field "loading"
  error revXMLText(theID)
  revDeleteXMLTree theID
  error "End of all Errors"
  select line 1 of field "list"
  send selectionChanged to field "list"
  hide field "loading"
end mouseUp



function startsWith theData, theCompare
  if char 1 to (the number of chars in theCompare) of theData = char 1 to -1 of theCompare then
    return true
  else
    return false
  end if
end startsWith



on error theError
  if theError = "End of all Errors" then
    put allErrors
    delete local allErrors
  else
    put theError & return after allErrors
  end if
end error


put theData["description"] into  rssDescription
set the text of field "ssdescription" to ssDescription
put theData["pubDate"] into pubDate
set the text of field "pubdate" to pubDate
This seems more promising. Working on it.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”