imdb.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 = []
  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 = {"nm": "name", "tt": "title", "kw": "keyword", "co": "company", "ep": "episode"}
  27. def request(query, params):
  28. query = query.replace(" ", "_").lower()
  29. params['url'] = suggestion_url.format(letter=query[0], query=query)
  30. return params
  31. def response(resp):
  32. suggestions = json.loads(resp.text)
  33. results = []
  34. for entry in suggestions.get('d', []):
  35. # https://developer.imdb.com/documentation/key-concepts#imdb-ids
  36. entry_id = entry['id']
  37. categ = search_categories.get(entry_id[:2])
  38. if categ is None:
  39. logger.error('skip unknown category tag %s in %s', entry_id[:2], entry_id)
  40. continue
  41. title = entry['l']
  42. if 'q' in entry:
  43. title += " (%s)" % entry['q']
  44. content = ''
  45. if 'rank' in entry:
  46. content += "(%s) " % entry['rank']
  47. if 'y' in entry:
  48. content += str(entry['y']) + " - "
  49. if 's' in entry:
  50. content += entry['s']
  51. # imageUrl is the image itself, it is not a thumb!
  52. image_url = entry.get('i', {}).get('imageUrl')
  53. if image_url:
  54. # get thumbnail
  55. image_url_name, image_url_prefix = image_url.rsplit('.', 1)
  56. # recipe to get the magic value:
  57. # * search on imdb.com, look at the URL of the thumbnail on the right side of the screen
  58. # * search using the imdb engine, compare the imageUrl and thumbnail URL
  59. # QL75 : JPEG quality (?)
  60. # UX280 : resize to width 320
  61. # 280,414 : size of the image (add white border)
  62. magic = 'QL75_UX280_CR0,0,280,414_'
  63. if not image_url_name.endswith('_V1_'):
  64. magic = '_V1_' + magic
  65. image_url = image_url_name + magic + '.' + image_url_prefix
  66. results.append(
  67. {
  68. "title": title,
  69. "url": href_base.format(category=categ, entry_id=entry_id),
  70. "content": content,
  71. "img_src": image_url,
  72. }
  73. )
  74. return results