imdb.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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['i']['imageUrl']
  61. results.append({
  62. "title": title,
  63. "url": href_base.format(category=categ, entry_id=entry_id),
  64. "content": content,
  65. # "thumbnail" : image_url,
  66. # "template": "videos.html",
  67. })
  68. return results