· 2 years ago · Apr 16, 2023, 07:30 AM
1### omdbapi.com ###
2# API https://www.omdbapi.com/
3# API xml exemple: tt0412142 1408 House md https://www.omdbapi.com/?i=tt0412142
4# API xml exemple: tt0186151 10559 Frequency https://www.omdbapi.com/?i=tt0186151
5
6### Imports ###
7# Python Modules #
8import os
9# HAMA Modules #
10import common
11from common import Log, DictString, Dict, SaveDict # Direct import of heavily used functions
12
13### Variables ###
14OMDB_HTTP_API_URL = "https://www.omdbapi.com/?apikey={api_key}&i="
15
16### Functions ###
17def GetMetadata(movie, IMDbid): # return 200 but not downloaded correctly - IMDB has a single poster, downloading through OMDB xml, prefered by mapping file
18 Log.Info("=== OMDb.GetMetadata() ===".ljust(157, '='))
19 url = OMDB_HTTP_API_URL.format(api_key=Prefs['OMDbApiKey']) #'
20 OMDb_dict = {}
21
22 if Prefs['OMDbApiKey'] in ('None', '', 'N/A'): Log.Info("No api key found - Prefs['OMDbApiKey']: '{}'".format(Prefs['OMDbApiKey'])); return OMDb_dict
23
24 Log.Info("IMDbid: '%s'" % IMDbid)
25 for imdbid_single in IMDbid.split(",") if IMDbid.startswith("tt") else []:
26 Log.Info(("--- %s.series ---" % imdbid_single).ljust(157, '-'))
27 json = common.LoadFile(filename=imdbid_single+".json", relativeDirectory=os.path.join('OMDb', 'json'), url=url + imdbid_single)
28 if json:
29 Log.Info("[ ] title: {}" .format(SaveDict( Dict(json,'Title') , OMDb_dict, 'title' )))
30 Log.Info("[ ] summary: {}" .format(SaveDict( Dict(json,'Plot') , OMDb_dict, 'summary' )))
31 Log.Info("[ ] originally_available_at: {}".format(SaveDict( Dict(json,'Released') , OMDb_dict, 'originally_available_at')))
32 Log.Info("[ ] countries: {}" .format(SaveDict( Dict(json,'Country') , OMDb_dict, 'countries' )))
33 Log.Info("[ ] directors: {}" .format(SaveDict( Dict(json,'Director') , OMDb_dict, 'directors' )))
34 Log.Info("[ ] genres: {}" .format(SaveDict( sorted([x.strip() for x in Dict(json,'Genre').split(',')]), OMDb_dict, 'genres')))
35 Log.Info("[ ] writers: {}" .format(SaveDict( Dict(json,'Writer') , OMDb_dict, 'writers' )))
36 SaveDict( Dict(json,'imdbRating'), OMDb_dict, 'rating')
37 if Dict(json,'Metascore').isdigit() and not Dict(OMDb_dict,'rating'):
38 SaveDict( float(json['Metascore'])/10, OMDb_dict, 'rating')
39 Log.Info("[ ] rating: {}".format(Dict(OMDb_dict,'rating')))
40 if SaveDict( Dict(json,'Rated'), OMDb_dict, 'content_rating') in common.Movie_to_Serie_US_rating and not movie and Dict(json,'Type')=="movie":
41 Log.Info("[ ] content_rating: {}".format(SaveDict(common.Movie_to_Serie_US_rating[json['Rated']], OMDb_dict, 'content_rating')))
42 if Dict(json,'Poster'): Log.Info("[ ] poster: {}".format(json['Poster'])); SaveDict((os.path.join('OMDb', 'poster', imdbid_single+'.jpg'), common.poster_rank('OMDb', 'posters'), None), OMDb_dict, 'posters', json['Poster'])
43 try: Log.Info("[ ] duration: {}".format(SaveDict( int(Dict(json,'Runtime').replace(' min','')) * 60 * 1000, OMDb_dict, 'duration'))) # Plex save duration in millisecs
44 except: pass
45
46 Log.Info("--- return ---".ljust(157, '-'))
47 Log.Info("OMDb_dict: {}".format(DictString(OMDb_dict, 4)))
48 return OMDb_dict
49