Page 1 of 1

Weather ? How Can i get Weather Forecast with PB?

Posted: Thu Sep 02, 2010 6:48 pm
by John-G
I would like to read or get the Weather from some place in a now$ so i can play it back using a voice?
Anyone do this Yet? or know how to get the weather info?

Thanks John G.

Re: Weather ? How Can i get Weather Forecast with PB?

Posted: Thu Sep 02, 2010 8:05 pm
by cas
I can only suggest you to look at http://www.accuweather.com/. Most mobile phone applications/widgets for weather information fetch data from their server. Also, there are many gadgets/widgets for Windows Vista/7 and Mac OS that use their service.

Re: Weather ? How Can i get Weather Forecast with PB?

Posted: Fri Sep 03, 2010 7:57 pm
by infratec
Hi John,

a short search after "weatherforecast httpget" in a famous internet search engine results in
http://developer.yahoo.com/weather/

Here is an PB example

Code: Select all

;
; Yahoo weather;
;
; http://developer.yahoo.com/weather/
;


InitNetwork()

Host$ = "weather.yahooapis.com"
Location$ = "707991"
Unit$ = "c"

ConnectionID = OpenNetworkConnection(Host$, 80)
If ConnectionID
  
  Send$ = "GET /forecastrss?w=" + Location$ 
  Send$ + "&u=" + Unit$
  Send$ + " HTTP/1.0" + Chr(13) + Chr(10)
  Send$ + "User-Agent: Mozilla/2.0 (Win95; I)" + Chr(13) + Chr(10)
  Send$ + "Pragma: no-cache" + Chr(13) + Chr(10)
  Send$ + "Host: " + Host$ + Chr(13) + Chr(10)
  Send$ + "Accept: */*" + Chr(13) + Chr(10)
  Send$ + Chr(13) + Chr(10)
 
  SendNetworkString(ConnectionID, Send$)
  
  Answer = #False
  Timeout = 100
 
  While Not Answer And Timeout > 0
   Delay(10)
   If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
   
    *Buffer = AllocateMemory(3000)
    If *Buffer 
     Length = ReceiveNetworkData(ConnectionID, *Buffer, 3000)
     If Length > 0
       Answer$ = PeekS(*Buffer, 3000, #PB_Ascii)
;       Debug Answer$
       EndPos = 1
       Repeat
         StartPos = FindString(Answer$, "<yweather:", Endpos)
         If StartPos
           Startpos + 10
           EndPos = FindString(Answer$, "/>", Startpos)
           Debug Mid(Answer$, StartPos, EndPos - StartPos)
         EndIf
       Until StartPos = 0
     EndIf
     FreeMemory(*Buffer)
    EndIf 
    Answer = #True
   EndIf 
  Timeout - 1
  Wend  
  
  CloseNetworkConnection(ConnectionID)
EndIf
Best regards,

Bernd

Re: Weather ? How Can i get Weather Forecast with PB?

Posted: Fri Sep 03, 2010 8:23 pm
by WilliamL
You could also just download a weather page data and find the data you want as in the example (last post) in this thread http://www.purebasic.fr/english/viewtop ... 13&t=39065. I used this method and it was easy.

Re: Weather ? How Can i get Weather Forecast with PB?

Posted: Tue Sep 07, 2010 1:49 am
by John-G
Thanks .. thanks so much. This was just what i was looking for .. :wink:

but ( sorry ) :shock: .. I have some more questions .. :oops:

I have been playing around with this ..First time I have played with OpenNetworkConnection stuff .. thanks for the help .. I have it working and running .. But i have it talking the Weather info and the ( forecast day = "sun" low = "61" and so on ) ..
It's killing me, i guess i am not that good with strings stuff

[20:43:02] location city="Monongahela" region="PA" country="United States"
[20:43:02] units temperature="F" distance="mi" pressure="in" speed="mph"
[20:43:02] wind chill="75" direction="0" speed="0"
[20:43:02] atmosphere humidity="32" visibility="10" pressure="30.11" rising="0"
[20:43:02] astronomy sunrise="6:51 am" sunset="7:43 pm"
[20:43:02] condition text="Fair" code="33" temp="75" date="Mon, 06 Sep 2010 7:53 pm EDT"
[20:43:02] forecast day="Mon" date="6 Sep 2010" low="60" high="81" text="Clouds Early/Clearing Late" code="29"


I need to clean up this last 4 lines of the output so i can have the computer's voice read it for me?

Got any Ideas :|

Re: Weather ? How Can i get Weather Forecast with PB?

Posted: Tue Sep 07, 2010 10:17 am
by infratec
Hi John,

have a look here

http://www.purebasic.fr/english/viewtop ... 12&t=43504

I did a lot of stringstuff inside :)

Bernd

Re: Weather ? How Can i get Weather Forecast with PB?

Posted: Tue Sep 07, 2010 5:04 pm
by TerryHough
Here is a different way using TELNET. It may be of some use to you.

Be sure to replace the three digit city code with your desired city code. Then clean up the resulting string and read as desired.

Code: Select all

; TELNET
; by Pantcho
; from PB Forum topic http://purebasic.myforums.net/viewtopic.php?t=4455&highlight=telnet
; Modified by TerryHough - November 11, 2004

Enumeration
#Window
#EdGadget
#StrGadget
EndEnumeration

;#CR   = Chr(13)
;#LF   = Chr(10)
#CRLF = Chr(13) + Chr(10)

;Variables
Row  = 1
Col  = 1
fore = 7
back = 0

;command.s = LCase(ProgramParameter())  ; For use with Command Tail
Global Command.s : Command.s = "rainmaker.wunderground.com"  :  Global Port.l : Port.l = 23

; The following are the defined TELNET commands from RFC854
SE.s        = Chr(240)        ; End of subnegotiation parameters
NOP.s       = Chr(241)        ; No operation
DM.s        = Chr(242)        ; The data stream portion of a Synch (DataMark)
                              ; always accompanied by a TCP Urgent notification
BRK.s       = Chr(243)        ; NVT character BRK = Break
IP.s        = Chr(244)        ; Function IP  = Interrupt Process
AO.s        = Chr(245)        ; Function AO  = Abort output
AYT.s       = Chr(246)        ; Function AYT = Are You There?
EC.s        = Chr(247)        ; Function EC  = Erase Character
EL.s        = Chr(248)        ; Function EL  = Erase Line
GA.s        = Chr(249)        ; The Go Ahead signal
SB.s        = Chr(250)        ; Indicates the following is a subnegotiation
                              ; of the indicated option
WILL.s      = Chr(251)        ; Indicates the desire to begin performing or
                              ; confirms you are performing the indicated option
WONT.s      = Chr(252)        ; Indicates the refusal to perform or continue
                              ; to perform the indicated option
DO.s        = Chr(253)        ; Requests that the other party perform or confirms
                              ; that you are expecting the other party to perform
                              ; the indicated option
DONT.S      = Chr(254)        ; Demands that the other party stop performing or
                              ; or confirms that you are no longer expecting the
                              ; other party to perform the indicated option
IAC.s       = Chr(255)        ; Data Byte 255

TermType.s      = Chr(24)
TermTypeSB.s    = SB + TermType + Chr(1) + IAC + SE
TermTypeInfo.s  = IAC + SB +TermType + Chr(0) + "ANSI" + IAC + SE

*Buffer= AllocateMemory(5000)

B.s ;Read Byte Buffer

;- Begin program action
If InitKeyboard()
  If OpenWindow(#Window,0,0,500,540,"TELNET",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
    AddKeyboardShortcut(#Window, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
    AddKeyboardShortcut(#Window, #PB_Shortcut_Return, #PB_Shortcut_Return)
    EditorGadget(#EdGadget ,10, 10,480,480)
    StringGadget(#StrGadget,10,500,480, 25,"")
    
    AddGadgetItem(#EdGadget,-1,"PureBasic Telnet Client")
    AddGadgetItem(#EdGadget,-1,"-----------------------")
    AddGadgetItem(#EdGadget,-1,"")
    While WindowEvent() : Wend
    If command.s = ""
      AddGadgetItem(#EdGadget,-1,"")
      AddGadgetItem(#EdGadget,-1,"Usage - Tel <Host>")
      AddGadgetItem(#EdGadget,-1,"")
      While WindowEvent() : Wend
      Delay(3000)
    EndIf
    
    AddGadgetItem(#EdGadget,-1,"Connecting to - " + command + " ...")
    While WindowEvent() : Wend
    
    If InitNetwork()
      ConnectionID = OpenNetworkConnection(command, Port)
      If ConnectionID
        AddGadgetItem(#EdGadget,-1,"Online with " + Command)
        While WindowEvent() : Wend
        ;          SendNetworkString(ConnectionID, Chr(13)) ; send Carriage Return
        SendNetworkString(ConnectionID, Chr(13)) ; send Carriage Return
        
        ;-Main Loop
        
        ;-NetComm
        Repeat
          SendChk = NetworkClientEvent(ConnectionID)
          Select SendChk
          Case 2 ; Raw data has been received
            RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)
            B.s = PeekS(*Buffer,RequestLength)
            If Mid(B,1,3) = IAC
              MessageRequester("Debug","Got an IAC " + Mid(B,1,3),0)
              Gosub HandleOPT ; Process Telnet Protocol
            Else
              If FindString(B,"Press Return to continue",1)
                SendNetworkString(ConnectionID, Chr(10)) ; send ^J
              Else
                B = ReplaceString(B,#LF$+#LF$,#LF$)
                B = ReplaceString(B,#LF$+#CR$,#CR$)
                AddGadgetItem(#EdGadget,-1, B)
                While WindowEvent() : Wend
              EndIf
            EndIf
            If FindString(B,"enter 3 letter forecast city code",1)
              AddGadgetItem(#EdGadget,-1,"")
              While WindowEvent() : Wend
; >>>> -------Send a 3 digit city code now. -----------------------------<<<<
              SendNetworkString(ConnectionID, "GSO" + Chr(10)) ; send city code + ^J
; >>>> -------Replace with your desired 3 digit city code. --------------<<<<
            ElseIf FindString(B,"Press Return for menu",1)
              SetGadgetText(#StrGadget,"")
              SetActiveGadget(#StrGadget)
            EndIf
            SetActiveGadget(#StrGadget)
            Delay(20)
          Case 1 ; A file has been received - Not used here
          Case 0 ; Nothing has happened
          EndSelect
          SendMessage_(GadgetID(#EdGadget), #EM_SETSEL,-1,-1)  ; scroll the Editor Gadget to last line
          ;SendMessage_(GadgetID(#EdGadget), #EM_SCROLLCARET, #False,#False)   ; scroll the Editor Gadget to last line
          
          Select WindowEvent()
          Case #PB_Event_CloseWindow  ; Window close box clicked
            Quit = #True
          Case #PB_Event_Menu
            Select EventMenu()  ; See which menu has been selected
            Case #PB_Shortcut_Escape  ; ESC key pressed
              Quit = #True
            Case #PB_Shortcut_Return  ; Return key pressed
              Command = UCase(GetGadgetText(#StrGadget))
              SendNetworkString(ConnectionID, Command + #CRLF)
              If Command = "X"
                Quit = #True
              EndIf
              SetGadgetText(#StrGadget,"")
            EndSelect ; End #PB_Event_Menu selection
          EndSelect  ; End WaitWindowEvent selection
        Until quit = #True
        If ConnectionID
          CloseNetworkConnection(ConnectionID)
        EndIf
        
      Else
        AddGadgetItem(#EdGadget,-1,"")
        AddGadgetItem(#EdGadget,-1,"Could not connect to - " + command)
        Delay(5000)
        End
      EndIf
    Else
      MessageRequester("ERROR","Couldn't initialize the network system",#MB_ICONERROR)
    EndIf
    
  Else
    MessageRequester("ERROR","Couldn't create the TELNET window",#MB_ICONERROR)
  EndIf
Else
  MessageRequester("ERROR","Couldn't initialize keyboard handling",#MB_ICONERROR)
EndIf
End

;-HandleOPT
HandleOPT:                          ; Handle the TELNET Option commands
MessageRequester("Debug","Got here",0)
AddGadgetItem(#EdGadget,-1,"RCVD ")
; B.s=""
; RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
; B.s = PeekS(*Buffer)

If Mid(B,2,1) = WILL Or Mid(B,2,1) = WONT Or Mid(B,2,1) = DO Or Mid(B,2,1) = DONT
  ;RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
  ;More.s = PeekS(*Buffer)
  ;B=B+More
  
  Select Mid(B,2,2)
  Case DO + TermType
    AddGadgetItem(#EdGadget,-1,"IAC DO TermType")
    Command.s = IAC + WILL +TermType
    SendNetworkString(ConnectionID,Command)
    AddGadgetItem(#EdGadget,-1,"SENT IAC WILL TermType")
    Delay(2000)
  Default
    If Mid(B,2,1) = DO ;Reply With WONT
      AddGadgetItem(#EdGadget,-1,"IAC ["+Str(Asc(Mid(B,2,1)))+"]["+Str(Asc(Mid(B,3,1)))+"]")
      Command.s = IAC + WONT + Mid(B,3,1)
      SendNetworkString(ConnectionID,Command)
      AddGadgetItem(#EdGadget,-1,"SENT IAC WONT "+Str(Asc(Mid(B,3,1))))
      Delay(2000)
    EndIf
  EndSelect
Else
  Select Mid(B,2,1)
  Case SB ; negotiate sub options
    While Mid(B,2,1) <> SE
      RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
      More.s = PeekS(*Buffer)
      B = B + More
    Wend
    If B = TermTypeSB
      AddGadgetItem(#EdGadget,-1,"REQUEST FOR TermType")
      Command.s = TermTypeInfo
      SendNetworkString(ConnectionID,Command)
      AddGadgetItem(#EdGadget,-1,"SENT TermType INFORMATION")
      Delay(2000)
    Else
      AddGadgetItem(#EdGadget,-1,"Unknown Suboption... Ignoring")
      Delay(20)
    EndIf
    Default
    AddGadgetItem(#EdGadget,-1,"Unknown Option...Ignoring")
    Delay(20)
EndSelect
EndIf
Return