writing a program using a website API

Just starting out? Need help? Post your questions and find answers here.
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

writing a program using a website API

Post by wmorton »

I have no idea how to do this. Using this API, how do I get PB to use these commands? http://www.freesound.org/docs/api/
Thanks!
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: writing a program using a website API

Post by infratec »

Hi,

you have to use 'normal' http get or post requests.
For Soundsearch a get request

like

Code: Select all

Send$ = "GET /sounds/search?q=bass -drum HTTP/1.0" + #CRLF$
Send$ + #CRLF$ 
Bernd
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: writing a program using a website API

Post by infratec »

As a first start:

Code: Select all

Procedure.s FreeSoundRequest(KEY$, API$="", Format$="xml")
  
  Result$ = ""
  
    
  ConnectionID = OpenNetworkConnection("www.freesound.org", 80, #PB_Network_TCP)
  
  If ConnectionID
    Send$ = "GET /api/sounds/search/?api_key=" + KEY$ + "&"+ API$ + "&format=" + Format$ + " HTTP/1.0" + #CRLF$
    Send$ + "Accept: */*" + #CRLF$
    Send$ + #CRLF$
    
    Debug Send$
    
    If SendNetworkString(ConnectionID, Send$)
      
      Timeout = 100
      Ptr = 0
      
      *Buffer = AllocateMemory(1501)
      If *Buffer
      
        While Len(Result$) = 0 And Timeout > 0
          Delay(10)
          If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
            
            Length = ReceiveNetworkData(ConnectionID, *Buffer, 1500)
            If Length > 0
              Buffer$ = PeekS(*Buffer, -1, #PB_Ascii)
              Pos = FindString(Buffer$, #CRLF$ + #CRLF$, 1)
              If Pos
                Buffer$ = Mid(Buffer$, Pos + 4)
                Debug Buffer$
              EndIf
            EndIf
          EndIf
          Timeout - 1
        Wend
        FreeMemory(*Buffer)
      EndIf
      
    EndIf
  EndIf
    
  ProcedureReturn Result$
  
EndProcedure  






If InitNetwork() = #False : End : EndIf


FreeSoundRequest("12d6dc5486554e278e370cdc49935908","p=3&q=dogs&s=downloads_desc")
But since I have no key ....

Bernd
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Re: writing a program using a website API

Post by wmorton »

thank you both so much, I understand this now!

Thanks!
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: writing a program using a website API

Post by infratec »

I'm not a multiple personality :mrgreen: :mrgreen: :mrgreen:

Bernd
Post Reply