moviepilot.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Moviepilot is a German movie database, similar to IMDB or TMDB. It doesn't
  4. have any official API, but it uses JSON requests internally to fetch search
  5. results and suggestions, that's being used in this implementation.
  6. Moviepilot additionally allows to discover movies by certain categories
  7. or filters, hence we provide the following syntax:
  8. - Any normal search query -> Fetch search results by the query
  9. - A query containing one of the category identifiers ``fsk``, ``genre``,
  10. ``jahr``, ``jahrzent``, ``land``, ``online``, ``stimmung`` will be used to
  11. search trending items by the provided filters, which are appended to the
  12. filter category after a ``-``.
  13. Search examples:
  14. - Normal: ``!mp Tom Cruise``
  15. - By filter: ``!mp person-Ryan-Gosling``
  16. - By filter: ``!mp fsk-0 land-deutschland genre-actionfilm``
  17. - By filter: ``!mp jahrzehnt-2020er online-netflix``
  18. For a list of all public filters, observe the url path when browsing
  19. - https://www.moviepilot.de/filme/beste.
  20. """
  21. from urllib.parse import urlencode
  22. from searx.utils import html_to_text
  23. about = {
  24. 'website': "https://www.moviepilot.de",
  25. 'official_api_documentation': None,
  26. 'use_official_api': False,
  27. 'require_api_key': False,
  28. 'results': 'JSON',
  29. 'language': 'de',
  30. }
  31. paging = True
  32. categories = ["movies"]
  33. base_url = "https://www.moviepilot.de"
  34. image_url = "https://assets.cdn.moviepilot.de/files/{image_id}/fill/155/223/{filename}"
  35. filter_types = ["fsk", "genre", "jahr", "jahrzehnt", "land", "online", "stimmung", "person"]
  36. def request(query, params):
  37. query_parts = query.split(" ")
  38. discovery_filters = []
  39. for query_part in query_parts:
  40. filter_category_and_value = query_part.split("-", 1)
  41. if len(filter_category_and_value) < 2:
  42. continue
  43. filter_category = filter_category_and_value[0]
  44. if filter_category in filter_types:
  45. discovery_filters.append(query_part)
  46. params['discovery'] = len(discovery_filters) != 0
  47. if params['discovery']:
  48. args = {
  49. 'page': params['pageno'],
  50. 'order': 'beste',
  51. }
  52. params["url"] = f"{base_url}/api/discovery?{urlencode(args)}"
  53. for discovery_filter in discovery_filters:
  54. params["url"] += f"&filters[]={discovery_filter}"
  55. else:
  56. args = {
  57. 'q': query,
  58. 'page': params['pageno'],
  59. 'type': 'suggest',
  60. }
  61. params["url"] = f"{base_url}/api/search?{urlencode(args)}"
  62. return params
  63. def response(resp):
  64. results = []
  65. json = resp.json()
  66. json_results = []
  67. if resp.search_params['discovery']:
  68. json_results = json['results']
  69. else:
  70. json_results = json
  71. for result in json_results:
  72. item = {'title': result['title']}
  73. if resp.search_params['discovery']:
  74. content_list = [result.get(x) for x in ['abstract', 'summary']]
  75. item['url'] = base_url + result['path']
  76. item['content'] = html_to_text(' | '.join([x for x in content_list if x]))
  77. item['metadata'] = html_to_text(result.get('meta_short', ''))
  78. if result.get('image'):
  79. item['img_src'] = image_url.format(image_id=result['image'], filename=result['image_filename'])
  80. else:
  81. item['url'] = result['url']
  82. item['content'] = ', '.join([result['class'], result['info'], result['more']])
  83. item['img_src'] = result['image']
  84. results.append(item)
  85. return results