Jump to content
Sign in to follow this  
heebijeebi

Using the TMDb API in AutoIt

Recommended Posts

heebijeebi

Hi!

I want to use the TMDb API in my script, but I can't figure out how to use the generated data.

A short explanation of how the API is used:

1. with: "http://api.themoviedb.org/3/search/movie?api_key=APIKEY&query=Looper"

The result (in the browser) looks like this:

{"page":1,"results":[{"adult":false,"backdrop_path":"/vuHG8VlYnvw4gUKtS7GY5kZol69.jpg","id":59967,"original_title":"Looper","release_date":"2012-09-28","poster_path":"/g9FW5mfDN26sNjOcDPB5mIGe8jv.jpg","popularity":9.720885546258774,"title":"Looper","vote_average":7.7,"vote_count":50}],"total_pages":1,"total_results":1}

So my question: How do I read this data to use it in my script?

I already tried:

BinaryToString(InetRead("http://api.themoviedb.org/3/search/movie?api_key=APIKEY&query=Looper"))

But this doesn't give me any data. Is it because I can't choose to display the html-source in the browser? I think its a json file which is displayed in the browser.. maybe that helps.

Note: APIKEY is a string which you get when you register at TMDb. I can't post it here...

PS: I hope you guys understand what I mean. I'm no native speaker in english so its a bit complicated to discribe it right in english ;)

Edited by heebijeebi

Share this post


Link to post
Share on other sites
FireFox

Hi,

The website you need to make a request to wants "a correct header" (which the function InetRead does not provide)

Fast made code :

TCPStartup()

Local $sGet = "", $sHost = "", $iSocket = 0, $sReferer = "", $bRecv = Binary(""), $sRecv = ""

$sGet = "/3/search/movie?api_key=APIKEY&query=Looper"

$sHost = TCPNameToIP("api.themoviedb.org")

$iSocket = TCPConnect($sHost, 80)

$sReferer = "http://api.themoviedb.org/"

TCPSend($iSocket, _
      "GET " & $sGet & " HTTP/1.1" & @CRLF & _
      "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)" & @CRLF & _
      "Host: " & $sHost & @CRLF & _
      "Accept: */*" & @CRLF & _
      "Referer: " & $sReferer & @CRLF & _
      "Connection: close" & @CRLF & _
      @CRLF)

While 1
   $bRecv = TCPRecv($iSocket, 1024, 1)
   If @error Then ExitLoop

   If BinaryLen($bRecv) Then
      $sRecv &= BinaryToString($bRecv)
   EndIf
WEnd

TCPCloseSocket($iSocket)

TCPShutdown()

ConsoleWrite(StringTrimLeft($sRecv, StringInStr($sRecv, @CRLF & @CRLF, 2) + 3) & @CRLF)

Edit: Removed API key on OP demand.

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
heebijeebi

Thank you so much! It works!!

Could you please remove my APIKey from your example. I think it shouldn't be posted in public. Thanks!

Share this post


Link to post
Share on other sites
heebijeebi

HI again!

I have another issue with using data from the API properly.

When searching for a movie and the result is only one match everything works fine, but when there are more movies which could match the query my StringRegExp code doesn't work anymore. I have to confess I'm not very familiar with StringRegExp :/

The result with only one match looks like this:

{"page":1,"results":[{"adult":false,"backdrop_path":"/vuHG8VlYnvw4gUKtS7GY5kZol69.jpg","id":59967,"original_title":"Looper","release_date":"2012-09-28","poster_path":"/g9FW5mfDN26sNjOcDPB5mIGe8jv.jpg","popularity":10.040000000000001,"title":"Looper","vote_average":7.6,"vote_count":50}],"total_pages":1,"total_results":1}

The result with more matching movies looks like this:

{"page":1,"results":[{"adult":false,"backdrop_path":"/lVA7gi0DOJ5UoxNf56UVAml7BY6.jpg","id":74074,"original_title":"The Raid","release_date":"1954-08-04","poster_path":"/mbSuGsYtubSKWHYiqSwvwhKIYQu.jpg","popularity":0.7200000000000001,"title":"The Raid","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"id":172460,"original_title":"The Raid","release_date":"2012-02-06","poster_path":"/hUMLyquzx6DoKTrpQmHAxpdwrbt.jpg","popularity":3.4000000000000004,"title":"The Raid","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/i5ubqxYkkqZpwgw6nHHUudOuGTh.jpg","id":85,"original_title":"Raiders of the Lost Ark","release_date":"1981-06-12","poster_path":"/tjGpsV0vKKHECtdsQoVwIXsDj3.jpg","popularity":3.8000000000000003,"title":"Raiders of the Lost Ark","vote_average":8.7,"vote_count":86},{"adult":false,"backdrop_path":"/fCcWGyNgz4pmCdgqQsYVZfAgT1K.jpg","id":94329,"original_title":"Serbuan maut","release_date":"2011-11-20","poster_path":"/tDYXQLLnDhPmeG7hUEJSQ2unAK8.jpg","popularity":2.2,"title":"The Raid: Redemption","vote_average":7.6,"vote_count":19},{"adult":false,"backdrop_path":"/zsTpK6SnVNRyHn31VUA3MmfE5ff.jpg","id":130267,"original_title":"Seal Team Six: The Raid on Osama Bin Laden","release_date":"2012-11-04","poster_path":"/g7siZfF9Whskke5FqikZ5UWOSI4.jpg","popularity":1.7799000000000003,"title":"Seal Team Six: The Raid on Osama Bin Laden","vote_average":6.3,"vote_count":1},{"adult":false,"backdrop_path":null,"id":74691,"original_title":"Raiders of Wu Tang","release_date":"1982-09-20","poster_path":"/sMZsOgwphbgTiPBRO9yiqlY0AcZ.jpg","popularity":0.8,"title":"Raiders of Wu Tang","vote_average":6.0,"vote_count":1},{"adult":false,"backdrop_path":"/lZh04Sxm6ThidqkKZkG71SQmdJm.jpg","id":53657,"original_title":"Raid on Rommel","release_date":"1971-02-12","poster_path":"/2vmuqqg91rE5N6HDvumaEeLVm5N.jpg","popularity":0.2,"title":"Raid on Rommel","vote_average":7.5,"vote_count":1},{"adult":false,"backdrop_path":null,"id":75285,"original_title":"Womb Raider","release_date":"2003-08-03","poster_path":"/uhNpZkCdtGpxDMPBmxdFZPmQTft.jpg","popularity":0.2,"title":"Womb Raider","vote_average":3.0,"vote_count":1},{"adult":false,"backdrop_path":"/c8DpfU2VjXXGxQIX9ME3S30YB4e.jpg","id":27349,"original_title":"The Great Northfield Minnesota Raid","release_date":"1972-06-14","poster_path":"/43oCljfg3cttXOxquST5mHrDZtn.jpg","popularity":0.6000000000000001,"title":"The Great Northfield Minnesota Raid","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/8U8L12N9MYE1XlnfX79dfRmD37m.jpg","id":39524,"original_title":"De overval","release_date":"1962-12-21","poster_path":"/lhcoW9NBle0IxNhTojVcKEYD0VI.jpg","popularity":0.2,"title":"De overval","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/3bKHlG1KYSdflLYjv0ZtrJ7juEd.jpg","id":46728,"original_title":"Le Raid","release_date":"2002-03-27","poster_path":"/fgueJ3rNlgoYlF6GEIE3soL6AaC.jpg","popularity":0.2,"title":"The Race","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"id":45507,"original_title":"Zhi zun wu shang","release_date":"1989-01-01","poster_path":"/8Cok3CsaAAh97Wcl9dmwvsw3ODp.jpg","popularity":0.2,"title":"Casino Raiders","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/9C3qRg6ZdvjjSnyNWX1Kv2LIcdC.jpg","id":62128,"original_title":"Raiders of the Lost Ark - The Adaptation","release_date":"1989-01-01","poster_path":"/6JwdLafP1tW3RNymMvzNXDKNPGJ.jpg","popularity":0.46,"title":"Raiders of the Lost Ark - The Adaptation","vote_average":8.0,"vote_count":1},{"adult":false,"backdrop_path":null,"id":98237,"original_title":"They Raid by Night","release_date":"1942-06-19","poster_path":"/xanjnY2jOgCHTzq18EBQ4F8mywK.jpg","popularity":0.2,"title":"They Raid by Night","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/gsR15RUFRFQWXJZ1932ZSHQMLLn.jpg","id":61525,"original_title":"Air Raid Wardens","release_date":"1943-04-04","poster_path":"/rK0qNBO7aCDsyWp9G2fjnmSXTVW.jpg","popularity":0.6834000000000001,"title":"Air Raid Wardens","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/dKgZw3SSorKbTmcduzFCDJcxvnC.jpg","id":13954,"original_title":"Alien Raiders","release_date":"2008-09-21","poster_path":"/l7Pua9InXMr4v9KXfFrTH8sbtLW.jpg","popularity":0.2,"title":"Alien Raiders","vote_average":3.6,"vote_count":4},{"adult":false,"backdrop_path":null,"id":82732,"original_title":"America's Game: 1976 Oakland Raiders","release_date":"2006-12-19","poster_path":"/3gUxbKGT0eyXBkCTsSVc3wY7AbJ.jpg","popularity":0.6000000000000001,"title":"America's Game: 1976 Oakland Raiders","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"id":82734,"original_title":"America's Game: 1980 Oakland Raiders","release_date":null,"poster_path":"/dnsG36KiCUmjWZtscucOeoK2sYi.jpg","popularity":0.2,"title":"America's Game: 1980 Oakland Raiders","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"id":82735,"original_title":"America's Game: 1983 Los Angeles Raiders","release_date":"2006-12-19","poster_path":"/89buKbdcfw8IsccR1uXkCIa69zi.jpg","popularity":0.2,"title":"America's Game: 1983 Los Angeles Raiders","vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"id":132939,"original_title":"Arizona Raiders","release_date":"1965-08-01","poster_path":"/xpjibth217ARwz9MMuGXGRj5K9M.jpg","popularity":0.2,"title":"Arizona Raiders","vote_average":0.0,"vote_count":0}],"total_pages":5,"total_results":97}

My StringRegExp Code is as follows:

;TMDbID:
   $array = StringRegExp($data, '"id":(.*?),"', 2)
   If @error Then
   ;nothing
   Else
   $idTMDbTXT = $array[1]
   MsgBox(0,"TMDB ID",$idTMDbTXT)
   ;Jahr:
   $array = StringRegExp($data, '"release_date":"(.*?)"', 2)
   $YearTXT = StringTrimRight($array[1], 6)
   MsgBox(0,"Jahr",$YearTXT)
   EndIf

$data is the string from above!

How do I have to modifiy my StringRegExp Code for not just getting an error when there are more results? Maybe even with a box that shows all results (Movienames + Year) and the user has to select the right one?

Share this post


Link to post
Share on other sites
FireFox

Here you go :

#include <Array.au3>

$array = StringRegExp($data, '"id":(.*?),"', 3)
_ArrayDisplay($array)

$array = StringRegExp($data, '"release_date":"(.*?)"', 3)
_ArrayDisplay($array)

You can use the _StringBetween function which is the same thing ;)

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
heebijeebi

Thanks for the answer: but when I run the script with a search for "The Raid", which gives more than one result, I stil get the error:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\Dropbox\_WIP\MovieManager\_Backups\AutoIt test\TMDB-IMDb abfragen.au3"
D:\Dropbox\_WIP\MovieManager\_Backups\AutoIt test\TMDB-IMDb abfragen.au3 (44) : ==> Subscript used with non-Array variable.:
$idTMDbTXT = $array[0]
$idTMDbTXT = $array^ ERROR
>Exit code: 1 Time: 0.817
Edited by heebijeebi

Share this post


Link to post
Share on other sites
heebijeebi

Hi,

The website you need to make a request to wants "a correct header" (which the function InetRead does not provide)

Fast made code :

TCPStartup()

Local $sGet = "", $sHost = "", $iSocket = 0, $sReferer = "", $bRecv = Binary(""), $sRecv = ""

$sGet = "/3/search/movie?api_key=APIKEY&query=Looper"

$sHost = TCPNameToIP("api.themoviedb.org")

$iSocket = TCPConnect($sHost, 80)

$sReferer = "http://api.themoviedb.org/"

TCPSend($iSocket, _
      "GET " & $sGet & " HTTP/1.1" & @CRLF & _
      "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)" & @CRLF & _
      "Host: " & $sHost & @CRLF & _
      "Accept: */*" & @CRLF & _
      "Referer: " & $sReferer & @CRLF & _
      "Connection: close" & @CRLF & _
      @CRLF)

While 1
   $bRecv = TCPRecv($iSocket, 1024, 1)
   If @error Then ExitLoop

   If BinaryLen($bRecv) Then
      $sRecv &= BinaryToString($bRecv)
   EndIf
WEnd

TCPCloseSocket($iSocket)

TCPShutdown()

ConsoleWrite(StringTrimLeft($sRecv, StringInStr($sRecv, @CRLF & @CRLF, 2) + 3) & @CRLF)
Edit: Removed API key on OP demand.

Br, FireFox.

 

 

Hi again! :)

It seems like this method doesn't work anymore...

Is there something that has changed with the new AutoIt version which may affect this?

Share this post


Link to post
Share on other sites
water

Which "new" version are you talking about?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
heebijeebi

Which "new" version are you talking about?

 

v3.3.10.2

 

The above code works fine with v3.3.8.1

Edited by heebijeebi

Share this post


Link to post
Share on other sites
water

To narrow down your problem you need to do some kind of error checking.

Which function returns an error (check return value and @error)?

Please have a look at the change history of AutoIt and the script breaking changes section.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×