imdb.py 3.0 KB

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