#include "JSON.au3" #cs ***************************************************************************** Name: TMDB.au3 AutoIt Version: 3.3.14.5 Author: Rix68 JSON library author: Ward Script Function: Access movie data from TMDB.com Date: Oct. 16, 2020 #ce ***************************************************************************** Global $ApiKey = "" #Region *** Functions *** #cs*************************************************************** Function: _TmdbGetBackdrops($Tmdb_Id) Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = 2D Array() Array[x][0]=aspect_ratio Array[x][1]=file_path Array[x][2]=height Array[x][3]=iso_639_1 Array[x][4]=vote_average Array[x][5]=vote_count Array[x][6]=width Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetBackdrops($Tmdb_id) If $Tmdb_id = "" Then SetError (1) Return EndIf $BData = InetRead("https://api.themoviedb.org/3/movie/" & $Tmdb_id & "/images?api_key=" & $ApiKey & "&language=en", 27) $SData = BinaryToString($BData) $Obj = Json_Decode($SData) $Backdrops = Json_ObjGet($Obj, "backdrops") $Elements = UBound($Backdrops, 1) Local $BackdropsResults[$Elements][7] For $L = 0 To ($Elements - 1) $Obj2 = $Backdrops[$L] $AspectRatio = Json_ObjGet($Obj2, "aspect_ratio") $FilePath = Json_ObjGet($Obj2, "file_path") $Height = Json_ObjGet($Obj2, "height") $Language = Json_ObjGet($Obj2, "iso_639_1") $VoteAverage = Json_ObjGet($Obj2, "vote_average") $VoteCount = Json_ObjGet($Obj2, "vote_count") $Width = Json_ObjGet($Obj2, "width") $BackdropsResults[$L][0] = $AspectRatio $BackdropsResults[$L][1] = $FilePath $BackdropsResults[$L][2] = $Height $BackdropsResults[$L][3] = $Language $BackdropsResults[$L][4] = $VoteAverage $BackdropsResults[$L][5] = $VoteCount $BackdropsResults[$L][6] = $Width Next Return ($BackdropsResults) EndFunc #cs*************************************************************** Function: _TmdbGetCast($Tmdb_Id) Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = 2D Array() Array[x][0]=cast_id Array[x][1]=character Array[x][2]=credit_id Array[x][3]=gender Array[x][4]=id Array[x][5]=name Array[x][6]=order Array[x][7]=profile_path Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetCast($Tmdb_id) If $Tmdb_id = "" Then SetError (1) Return EndIf $BData = InetRead("https://api.themoviedb.org/3/movie/" & $Tmdb_id & "/credits?api_key=" & $ApiKey & "&language=en-US", 27) $SData = BinaryToString($BData) $Obj = Json_Decode($SData) $Cast = Json_ObjGet($Obj, "cast") $Elements = UBound($Cast, 1) Local $CastResults[$Elements][8] For $L = 0 To ($Elements - 1) $Obj2 = $Cast[$L] $CastID = Json_ObjGet($Obj2, "cast_id") $Character = Json_ObjGet($Obj2, "character") $CreditID = Json_ObjGet($Obj2, "credit_id") $Gender = Json_ObjGet($Obj2, "gender") $ID = Json_ObjGet($Obj2, "id") $Name = Json_ObjGet($Obj2, "name") $Order = Json_ObjGet($Obj2, "order") $ProfilePath = Json_ObjGet($Obj2, "profile_path") $CastResults[$L][0] = $CastID $CastResults[$L][1] = $Character $CastResults[$L][2] = $CreditID $CastResults[$L][3] = $Gender $CastResults[$L][4] = $ID $CastResults[$L][5] = $Name $CastResults[$L][6] = $Order $CastResults[$L][7] = $ProfilePath Next Return ($CastResults) EndFunc #cs*************************************************************** Function: _TmdbGetCrew($Tmdb_Id) Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = 2D Array() Array[x][0]=credit_id Array[x][1]=department Array[x][2]=gender Array[x][3]=id Array[x][4]=job Array[x][5]=name Array[x][6]=profile_path Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetCrew($Tmdb_id) If $Tmdb_id = "" Then SetError (1) Return EndIf $BData = InetRead("https://api.themoviedb.org/3/movie/" & $Tmdb_id & "/credits?api_key=" & $ApiKey & "&language=en-US", 27) $SData = BinaryToString($BData) $Obj = Json_Decode($SData) $Crew = Json_ObjGet($Obj, "crew") $Elements = UBound($Crew, 1) Local $CrewResults[$Elements][7] For $L = 0 To ($Elements - 1) $Obj2 = $Crew[$L] $CreditID = Json_ObjGet($Obj2, "credit_id") $Department = Json_ObjGet($Obj2, "department") $Gender = Json_ObjGet($Obj2, "gender") $ID = Json_ObjGet($Obj2, "id") $Job = Json_ObjGet($Obj2, "job") $Name = Json_ObjGet($Obj2, "name") $ProfilePath = Json_ObjGet($Obj2, "profile_path") $CrewResults[$L][0] = $CreditID $CrewResults[$L][1] = $Department $CrewResults[$L][2] = $Gender $CrewResults[$L][3] = $ID $CrewResults[$L][4] = $Job $CrewResults[$L][5] = $Name $CrewResults[$L][6] = $ProfilePath Next Return ($CrewResults) EndFunc #cs*************************************************************** Function: _TmdbGetGenres() Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = 2D Array() Array[x][0]=id Array[x][1]=name Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetGenres() $BData = InetRead("https://api.themoviedb.org/3/genre/movie/list?api_key=" & $ApiKey & "&language=en", 27) $SData = BinaryToString($BData) $Obj = Json_Decode($SData) $Genres = Json_ObjGet($Obj, "genres") $Elements = UBound($Genres, 1) Local $GenresResults[$Elements][2] For $L = 0 To ($Elements - 1) $Obj2 = $Genres[$L] $ID = Json_ObjGet($Obj2, "id") $Name = Json_ObjGet($Obj2, "name") $GenresResults[$L][0] = $ID $GenresResults[$L][1] = $Name Next Return ($GenresResults) EndFunc #cs*************************************************************** Function: _TmdbGetMovie($Tmdb_Id) Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = Array() Array[1]=adult Array[2]=backdrop_path Array[3]=belong_to_collection Array[4]=budget Array[5]=genres Array[6]=homepage Array[7]=id Array[8]=imdb_id Array[9]=original_language Array[10]=original_title Array[11]=overview Array[12]=popularity Array[13]=poster_path Array[14]=release_date Array[15]=revenue Array[16]=runtime Array[17]=status Array[18]=tagline Array[19]=title Array[20]=video Array[21]=vote_average Array[22]=vote_count Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetMovie($Tmdb_id) Local $Array[23] If $Tmdb_id = "" Then SetError (1) Return EndIf Local $BData = InetRead("https://api.themoviedb.org/3/movie/" & $Tmdb_id & "?api_key=" & $ApiKey & "&language=en-US", 27) Local $SData = BinaryToString($BData) Local $Obj = Json_Decode($SData) $Array[1] = Json_ObjGet($Obj, "adult") If Json_ObjGet($Obj, "backdrop_path") = "" Then $Array[2] = "n/a" Else $Array[2] = Json_ObjGet($Obj, "backdrop_path") EndIf Local $BTC = Json_ObjGet($Obj, "belongs_to_collection") If $BTC = Null Then $Array[3] = "n/a" Else Local $BTC_id = Json_ObjGet($BTC, "id") Local $BTC_name = Json_ObjGet($BTC, "name") Local $BTC_poster_path = Json_ObjGet($BTC, "poster_path") Local $BTC_backdrop_path = Json_ObjGet($BTC, "backdrop_path") $Array[3] = ($BTC_id & "|" & $BTC_name & "|" & $BTC_poster_path & "|" & $BTC_backdrop_path) EndIf If Json_ObjGet($Obj, "budget") = "" Then $Array[4] = "n/a" Else $Array[4] = Json_ObjGet($Obj, "budget") EndIf Local $GenreResult Local $Genres = Json_ObjGet($Obj, "genres") $Elements = UBound($Genres, 1) For $L = 0 To ($Elements - 1) Local $Obj2 = $Genres[$L] Local $GenreName = Json_ObjGet($Obj2, "name") $GenreResult = ($GenreResult & $GenreName & ", ") Next $Array[5] = StringTrimRight($GenreResult, 2) If Json_ObjGet($Obj, "homepage") = "" Then $Array[6] = "n/a" Else $Array[6] = Json_ObjGet($Obj, "homepage") EndIf If Json_ObjGet($Obj, "id") = "" Then $Array[7] = "n/a" Else $Array[7] = Json_ObjGet($Obj, "id") EndIf If Json_ObjGet($Obj, "imdb_id") = "" Then $Array[8] = "n/a" Else $Array[8] = Json_ObjGet($Obj, "imdb_id") EndIf If Json_ObjGet($Obj, "original_language") = "" Then $Array[9] = "n/a" Else $Array[9] = Json_ObjGet($Obj, "original_language") EndIf If Json_ObjGet($Obj, "original_title") = "" Then $Array[10] = "n/a" Else $Array[10] = Json_ObjGet($Obj, "original_title") EndIf If Json_ObjGet($Obj, "overview") = "" Then $Array[11] = "n/a" Else $Array[11] = Json_ObjGet($Obj, "overview") EndIf If Json_ObjGet($Obj, "popularity") = "" Then $Array[12] = "n/a" Else $Array[12] = Json_ObjGet($Obj, "popularity") EndIf If Json_ObjGet($Obj, "poster_path") = "" Then $Array[13] = "n/a" Else $Array[13] = Json_ObjGet($Obj, "poster_path") EndIf If Json_ObjGet($Obj, "release_date") = "" Then $Array[14] = "n/a" Else $Array[14] = Json_ObjGet($Obj, "release_date") EndIf If Json_ObjGet($Obj, "revenue") = "" Then $Array[15] = "n/a" Else $Array[15] = Json_ObjGet($Obj, "revenue") EndIf If Json_ObjGet($Obj, "runtime") = "" Then $Array[16] = "n/a" Else $Array[16] = Json_ObjGet($Obj, "runtime") EndIf If Json_ObjGet($Obj, "status") = "" Then $Array[17] = "n/a" Else $Array[17] = Json_ObjGet($Obj, "status") EndIf If Json_ObjGet($Obj, "tagline") = "" Then $Array[18] = "n/a" Else $Array[18] = Json_ObjGet($Obj, "tagline") EndIf If Json_ObjGet($Obj, "title") = "" Then $Array[19] = "n/a" Else $Array[19] = Json_ObjGet($Obj, "title") EndIf If Json_ObjGet($Obj, "video") = "" Then $Array[20] = "n/a" Else $Array[20] = Json_ObjGet($Obj, "video") EndIf If Json_ObjGet($Obj, "vote_average") = "" Then $Array[21] = "n/a" Else $Array[21] = Json_ObjGet($Obj, "vote_average") EndIf If Json_ObjGet($Obj, "vote_count") = "" Then $Array[22] = "n/a" Else $Array[22] = Json_ObjGet($Obj, "vote_count") EndIf Return $Array EndFunc #cs***************************************************** Function: _TmdbGetMovieID($Title, $Year) Requires: $Title = Title of movie to search for $Year = Year movie was released (Optional) Returns: Success = TMDB ID number for given movie Error = Sets @Error flag No results found returns 0 #ce***************************************************** Func _TmdbGetMovieID($Title = "", $Year = "") If $Title = "" Then SetError (1) Return EndIf $Tmdb_Id = _TmdbFindMovie($Title, $Year) Return $Tmdb_Id EndFunc #cs*************************************************************** Function: _TmdbGetPosters($Tmdb_Id) Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = 2D Array() Array[x][0]=aspect_ratio Array[x][1]=file_path Array[x][2]=height Array[x][3]=iso_639_1 Array[x][4]=vote_average Array[x][5]=vote_count Array[x][6]=width Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetPosters($Tmdb_id) If $Tmdb_id = "" Then SetError (1) Return EndIf $BData = InetRead("https://api.themoviedb.org/3/movie/" & $Tmdb_id & "/images?api_key=" & $ApiKey & "&language=en", 27) $SData = BinaryToString($BData) $Obj = Json_Decode($SData) $Posters = Json_ObjGet($Obj, "posters") $Elements = UBound($Posters, 1) Local $PostersResults[$Elements][7] For $L = 0 To ($Elements - 1) $Obj2 = $Posters[$L] $AspectRatio = Json_ObjGet($Obj2, "aspect_ratio") $FilePath = Json_ObjGet($Obj2, "file_path") $Height = Json_ObjGet($Obj2, "height") $Language = Json_ObjGet($Obj2, "iso_639_1") $VoteAverage = Json_ObjGet($Obj2, "vote_average") $VoteCount = Json_ObjGet($Obj2, "vote_count") $Width = Json_ObjGet($Obj2, "width") $PostersResults[$L][0] = $AspectRatio $PostersResults[$L][1] = $FilePath $PostersResults[$L][2] = $Height $PostersResults[$L][3] = $Language $PostersResults[$L][4] = $VoteAverage $PostersResults[$L][5] = $VoteCount $PostersResults[$L][6] = $Width Next Return ($PostersResults) EndFunc #cs*************************************************************** Function: _TmdbGetVideos($Tmdb_Id) Requires: $Tmdb_Id = ID number returned from _TmdbGetMovieID() Returns: Success = 2D Array() Array[x][0]=id Array[x][1]=iso_639_1 Array[x][2]=iso_3166_1 Array[x][3]=key Array[x][4]=name Array[x][5]=site Array[x][6]=size Array[x][7]=type Error = Sets @error to 1 #ce*************************************************************** Func _TmdbGetVideos($Tmdb_id) If $Tmdb_id = "" Then SetError (1) Return EndIf $BData = InetRead("https://api.themoviedb.org/3/movie/" & $Tmdb_id & "/videos?api_key=" & $ApiKey & "&language=en", 27) $SData = BinaryToString($BData) $Obj = Json_Decode($SData) $Videos = Json_ObjGet($Obj, "results") $Elements = UBound($Videos, 1) Local $VideosResults[$Elements][8] For $L = 0 To ($Elements - 1) $Obj2 = $Videos[$L] $ID = Json_ObjGet($Obj2, "id") $Language = Json_ObjGet($Obj2, "iso_639_1") $Locale = Json_ObjGet($Obj2, "iso_3166_1") $Key = Json_ObjGet($Obj2, "key") $Name = Json_ObjGet($Obj2, "name") $Site = Json_ObjGet($Obj2, "site") $Size = Json_ObjGet($Obj2, "size") $Type = Json_ObjGet($Obj2, "type") $VideosResults[$L][0] = $ID $VideosResults[$L][1] = $Language $VideosResults[$L][2] = $Locale $VideosResults[$L][3] = $Key $VideosResults[$L][4] = $Name $VideosResults[$L][5] = $Site $VideosResults[$L][6] = $Size $VideosResults[$L][7] = $Type Next Return ($VideosResults) EndFunc #EndRegion #Region *** Internal Functions *** Func _TmdbFindMovie($Title, $Year) ; Returns the tmdb_id of given search. If $Title = "" Or $Year = "" Then SetError (1) Return EndIf Local $BData = InetRead("http://api.themoviedb.org/3/search/movie?api_key=" & $ApiKey & "&language=en-US&query=" & $Title & "&page=1&include_adult=false&year=" & $Year, 27) Local $SData = BinaryToString($BData) Local $Obj = Json_Decode($SData) Local $TotalResults = Json_ObjGet($Obj, "total_results") If $TotalResults = 0 Then Return 0 Local $Results = Json_ObjGet($Obj, "results") Local $objJson2 = $Results[0] Local $ID = Json_ObjGet($objJson2, "id") Return $ID EndFunc #EndRegion