moviepilot.py 3.5 KB

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