imdb.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """IMDB - Internet Movie Database
  4. Retrieves results from a basic search. Advanced search options are not
  5. supported. IMDB's API is undocumented, here are some posts about:
  6. - https://stackoverflow.com/questions/1966503/does-imdb-provide-an-api
  7. - https://rapidapi.com/blog/how-to-use-imdb-api/
  8. An alternative that needs IMDPro_ is `IMDb and Box Office Mojo
  9. <https://developer.imdb.com/documentation>`_
  10. .. __IMDPro: https://pro.imdb.com/login
  11. """
  12. import json
  13. about = {
  14. "website": 'https://imdb.com/',
  15. "wikidata_id": 'Q37312',
  16. "official_api_documentation": None,
  17. "use_official_api": False,
  18. "require_api_key": False,
  19. "results": 'HTML',
  20. }
  21. categories = ['general', ]
  22. paging = False
  23. # suggestion_url = "https://sg.media-imdb.com/suggestion/{letter}/{query}.json"
  24. suggestion_url = "https://v2.sg.media-imdb.com/suggestion/{letter}/{query}.json"
  25. href_base = 'https://imdb.com/{category}/{entry_id}'
  26. search_categories = {
  27. "nm": "name",
  28. "tt": "title",
  29. "kw": "keyword",
  30. "co": "company",
  31. "ep": "episode"
  32. }
  33. def request(query, params):
  34. query = query.replace(" ", "_").lower()
  35. params['url'] = suggestion_url.format(letter=query[0], query=query)
  36. return params
  37. def response(resp):
  38. suggestions = json.loads(resp.text)
  39. results = []
  40. for entry in suggestions['d']:
  41. # https://developer.imdb.com/documentation/key-concepts#imdb-ids
  42. entry_id = entry['id']
  43. categ = search_categories.get(entry_id[:2])
  44. if categ is None:
  45. logger.error(
  46. 'skip unknown category tag %s in %s', entry_id[:2], entry_id
  47. )
  48. continue
  49. title = entry['l']
  50. if 'q' in entry:
  51. title += " (%s)" % entry['q']
  52. content = ''
  53. if 'rank' in entry:
  54. content += "(%s) " % entry['rank']
  55. if 'y' in entry:
  56. content += str(entry['y']) + " - "
  57. if 's' in entry:
  58. content += entry['s']
  59. # imageUrl is the image itself, it is not a thumb!
  60. image_url = entry.get('i', {}).get('imageUrl')
  61. if image_url:
  62. # get thumbnail
  63. image_url_name, image_url_prefix = image_url.rsplit('.', 1)
  64. # recipe to get the magic value:
  65. # * search on imdb.com, look at the URL of the thumbnail on the right side of the screen
  66. # * search using the imdb engine, compare the imageUrl and thumbnail URL
  67. # QL75 : JPEG quality (?)
  68. # UX280 : resize to width 320
  69. # 280,414 : size of the image (add white border)
  70. magic = 'QL75_UX280_CR0,0,280,414_'
  71. if not image_url_name.endswith('_V1_'):
  72. magic = '_V1_' + magic
  73. image_url = image_url_name + magic + '.' + image_url_prefix
  74. results.append({
  75. "title": title,
  76. "url": href_base.format(category=categ, entry_id=entry_id),
  77. "content": content,
  78. "img_src" : image_url,
  79. })
  80. return results