imdb.py 3.0 KB

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