Created a nice software using SpiderBasic ? Post you link here !
User avatar
eddy
 
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

RSS Feed (using YQL)

by eddy Sun Apr 27, 2014 8:01 pm

- reading XML from RSS feed
- using YQL service as proxy for bypassing cross-domain problem

Code: Select all
Procedure.s URLToQueryYahoo(Query.s, FormatJSON=#True, YahooQueryURL.s="https://query.yahooapis.com/v1/public/yql?diagnostics=false&callback=&q=")
  Protected url.s=YahooQueryURL+URLEncoder(Query)
  If FormatJSON   
    url+"&format=json" ;Results will be formatted in JSON
  Else     
    url+"&format=xml" ;Results will be formatted in XML
  EndIf   
  ProcedureReturn url
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
 
  ; *************************
  ; EXAMPLE - RSS FEED
  ; *************************
 
  Enumeration
    #RSS
    #Window     
    #TreeGadget
    #StringGadget
    #ButtonGadget
  EndEnumeration
 
 
  Procedure FillRSSTree(CurrentNode, CurrentSublevel=0)
    If XMLNodeType(CurrentNode) = #PB_XML_Normal
     
      Name$ = GetXMLNodeName(CurrentNode)
      Select Name$
        Case "item"
          TitleNode=XMLNodeFromPath(MainXMLNode(#RSS),XMLNodePath(CurrentNode)+"/title")
          If TitleNode
            AddGadgetItem(#TreeGadget, -1, GetXMLNodeText(TitleNode),0,0)
          Else             
            AddGadgetItem(#TreeGadget, -1, "RSS",0,0)
          EndIf           
        Case "title"
        Default
          If GetXMLNodeName(ParentXMLNode(CurrentNode)) ="item"
            RSSField$=Name$
           
            Value$= GetXMLNodeText(CurrentNode)
            If Value$ : RSSField$+": "+Value$ : EndIf
           
            If ExamineXMLAttributes(CurrentNode)
              Attr$= ""           
              While NextXMLAttribute(CurrentNode)
                Attr$ + XMLAttributeName(CurrentNode) + "=" + #DQUOTE$ + XMLAttributeValue(CurrentNode) + #DQUOTE$ + " "
              Wend
              RSSField$+" (Attributes: "+Attr$ + ")"
            EndIf
            AddGadgetItem(#TreeGadget, -1, RSSField$, 0, 1)
          EndIf         
      EndSelect   
     
     
      ; Loop through all available child nodes and call this procedure again
      ;
      ChildNode = ChildXMLNode(CurrentNode)
      While ChildNode <> 0
        FillRSSTree(ChildNode, CurrentSublevel+1)     
        ChildNode = NextXMLNode(ChildNode)
      Wend       
    EndIf   
  EndProcedure
 
  Procedure Loading(Type, Filename$)
    Debug Filename$ + " loaded"   
   
    ; Display an error message if there was a markup error
    If XMLStatus(#RSS) <> #PB_XML_Success
      Debug "Error in the XML file:" + Chr(13)
      Debug "Message: " + XMLError(#RSS) + Chr(13)
      Debug "Line: " + Str(XMLErrorLine(#RSS)) + "   Character: " + Str(XMLErrorPosition(#RSS))
    EndIf   
   
    MainNode = MainXMLNode(#RSS)
    If MainNode 
      ; Clear previous RSS tree
      ClearGadgetItems(#TreeGadget)
      ; Get the main XML node, and call the FillRSSTree() procedure with it
      FillRSSTree(MainNode)
      ; Expand all nodes for a nicer view
      For i = 0 To CountGadgetItems(#TreeGadget) - 1
        SetGadgetItemState(#TreeGadget, i, #PB_Tree_Expanded)
      Next
    EndIf
  EndProcedure
 
  Procedure ReadingRSS()
    ; for my test, I have no server script to execute the cross-domain AJAX
    ; that's why I used YQL service (provided by YAHOO) as proxy
   
    ;URLToByPassCrossDomain.s="GetFile.php?file="+URLEncoder(GetGadgetText(#StringGadget)) 
    URLToByPassCrossDomain.s=URLToQueryYahoo("select * from rss where url='"+GetGadgetText(#StringGadget)+"'",#False)
    LoadXML(#RSS, URLToByPassCrossDomain)
  EndProcedure
 
  If OpenWindow(#Window, 0, 0, 800, 600, "RSS Feed", #PB_Window_ScreenCentered)   
    StringGadget(#StringGadget,10,10,670,20,"http://rss.news.yahoo.com/rss/topstories")
    ButtonGadget(#ButtonGadget,690,10,100,20,"Read")
    TreeGadget(#TreeGadget, 10, 40, 780, 550)   
    BindEvent(#PB_Event_Loading,@Loading())
    BindGadgetEvent(#ButtonGadget, @ReadingRSS())
  EndIf
CompilerEndIf
Return to Showcase

Who is online

Users browsing this forum: No registered users and 4 guests