Get Twitch API (HTTPS)

Just starting out? Need help? Post your questions and find answers here.
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

Get Twitch API (HTTPS)

Post by Opcode »

So you're forced to use SSL when calling the Twitch API which renders ReceiveHTTPFile() useless. I also tried using the webgadget to view the API but it doesn't work (it spurts to save a .json file). What are some easy ways to get around the webgadget not working with the Twitch API? Or a HTTPS variant of ReceiveHTTPFile. I have found a couple of solutions that work but I am trying to keep code length to an absolute minimum.

Code: Select all

If OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_SystemMenu)
  
  WebGadget(0, 0, 0, 800, 600, "https://api.twitch.tv/kraken/streams/inexpensivegamer/")
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
API => https://api.twitch.tv/kraken/streams/inexpensivegamer/
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Twitch API (HTTPS)

Post by infratec »

Hi,

windows only:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


Procedure.i ReceiveHTTPsFile(URL$, Filename$)
  
  #INTERNET_OPEN_TYPE_DIRECT = 1
  #INTERNET_FLAG_DONT_CACHE = $04000000
  
  Protected.l BytesRead
  Protected.i hOpen, hConnect
  Protected.s szHead
  Protected.i Result, File
  Protected *Buffer
  
  
  hOpen = InternetOpen_(#NULL$, #INTERNET_OPEN_TYPE_DIRECT, #NULL$, #NULL$, 0)
  If hOpen
    
    szHead = "Accept: */*" + #CRLF$
    szHead + #CRLF$
    
    hConnect = InternetOpenUrl_(hOpen, URL$, szHead, Len(szHead), #INTERNET_FLAG_DONT_CACHE, 0)
    If hConnect
      
      File = CreateFile(#PB_Any, Filename$)
      If File
      
        *Buffer = AllocateMemory(2048)
        If *Buffer
          Repeat
            If InternetReadFile_(hConnect, *Buffer, MemorySize(*Buffer), @BytesRead)
              WriteData(File, *Buffer, BytesRead)
            EndIf
            Result + BytesRead
          Until BytesRead = 0
          FreeMemory(*Buffer)
        EndIf
        
        CloseFile(File)
      EndIf
      InternetCloseHandle_(hConnect)
    EndIf
    InternetCloseHandle_(hOpen)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




Procedure.s ReceiveHTTPsString(URL$)
  
  #INTERNET_OPEN_TYPE_DIRECT = 1
  #INTERNET_FLAG_DONT_CACHE = $04000000
  
  Protected.l BytesRead
  Protected.i hOpen, hConnect
  Protected.s szHead, Result
  Protected *Buffer
  
  
  hOpen = InternetOpen_(#NULL$, #INTERNET_OPEN_TYPE_DIRECT, #NULL$, #NULL$, 0)
  If hOpen
    
    szHead = "Accept: */*" + #CRLF$
    szHead + #CRLF$
    
    hConnect = InternetOpenUrl_(hOpen, URL$, szHead, Len(szHead), #INTERNET_FLAG_DONT_CACHE, 0)
    If hConnect
      
      *Buffer = AllocateMemory(2048)
      If *Buffer
        Repeat
          If InternetReadFile_(hConnect, *Buffer, MemorySize(*Buffer), @BytesRead)
            Result + PeekS(*Buffer, BytesRead, #PB_UTF8)
          EndIf
        Until BytesRead = 0
        FreeMemory(*Buffer)
      EndIf
      
      InternetCloseHandle_(hConnect)
    EndIf
    InternetCloseHandle_(hOpen)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




CompilerIf #PB_Compiler_IsMainFile
  If ReceiveHTTPsFile("https://api.twitch.tv/kraken/streams/inexpensivegamer/", "c:\tmp\test.file")
    MessageRequester("Info", "Received")
    
    Debug ReceiveHTTPsString("https://api.twitch.tv/kraken/streams/inexpensivegamer/")
    
  Else
    MessageRequester("Info", "Failed")
  EndIf
CompilerEndIf
Bernd
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Twitch API (HTTPS)

Post by infratec »

Extended the Listing above.
Maybe ReceiveHTTPSString() is easier for you :wink:

Bernd
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Get Twitch API (HTTPS)

Post by firace »

Now if you really want the shortest code possible, for Windows:

Code: Select all

url$="https://www.test.com/file1.jpg"
URLDownloadToFile_(0,url$,"temp.jpg",0,0)
Of course you may want to do some error handling around that.
Last edited by firace on Sat Apr 11, 2015 11:11 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Twitch API (HTTPS)

Post by infratec »

You are right, but ...
not to string :D
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

Re: Get Twitch API (HTTPS)

Post by Opcode »

infratec wrote:Extended the Listing above.
Maybe ReceiveHTTPSString() is easier for you :wink:

Bernd
Exactly what I was looking for, thanks :D
Post Reply