sepiasearch.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. SepiaSearch (Videos)
  4. """
  5. from json import loads
  6. from dateutil import parser, relativedelta
  7. from urllib.parse import urlencode
  8. from datetime import datetime
  9. # about
  10. about = {
  11. "website": 'https://sepiasearch.org',
  12. "wikidata_id": None,
  13. "official_api_documentation": "https://framagit.org/framasoft/peertube/search-index/-/tree/master/server/controllers/api", # NOQA
  14. "use_official_api": True,
  15. "require_api_key": False,
  16. "results": 'JSON',
  17. }
  18. categories = ['videos']
  19. paging = True
  20. time_range_support = True
  21. safesearch = True
  22. supported_languages = [
  23. 'en', 'fr', 'ja', 'eu', 'ca', 'cs', 'eo', 'el',
  24. 'de', 'it', 'nl', 'es', 'oc', 'gd', 'zh', 'pt',
  25. 'sv', 'pl', 'fi', 'ru'
  26. ]
  27. base_url = 'https://sepiasearch.org/api/v1/search/videos'
  28. safesearch_table = {
  29. 0: 'both',
  30. 1: 'false',
  31. 2: 'false'
  32. }
  33. time_range_table = {
  34. 'day': relativedelta.relativedelta(),
  35. 'week': relativedelta.relativedelta(weeks=-1),
  36. 'month': relativedelta.relativedelta(months=-1),
  37. 'year': relativedelta.relativedelta(years=-1)
  38. }
  39. embedded_url = '<iframe width="540" height="304" src="{url}" frameborder="0" allowfullscreen></iframe>'
  40. def minute_to_hm(minute):
  41. if isinstance(minute, int):
  42. return "%d:%02d" % (divmod(minute, 60))
  43. return None
  44. def request(query, params):
  45. params['url'] = base_url + '?' + urlencode({
  46. 'search': query,
  47. 'start': (params['pageno'] - 1) * 10,
  48. 'count': 10,
  49. 'sort': '-match',
  50. 'nsfw': safesearch_table[params['safesearch']]
  51. })
  52. language = params['language'].split('-')[0]
  53. if language in supported_languages:
  54. params['url'] += '&languageOneOf[]=' + language
  55. if params['time_range'] in time_range_table:
  56. time = datetime.now().date() + time_range_table[params['time_range']]
  57. params['url'] += '&startDate=' + time.isoformat()
  58. return params
  59. def response(resp):
  60. results = []
  61. search_results = loads(resp.text)
  62. if 'data' not in search_results:
  63. return []
  64. for result in search_results['data']:
  65. title = result['name']
  66. content = result['description']
  67. thumbnail = result['thumbnailUrl']
  68. publishedDate = parser.parse(result['publishedAt'])
  69. embedded = embedded_url.format(url=result.get('embedUrl'))
  70. author = result.get('account', {}).get('displayName')
  71. length = minute_to_hm(result.get('duration'))
  72. url = result['url']
  73. results.append({'url': url,
  74. 'title': title,
  75. 'content': content,
  76. 'author': author,
  77. 'length': length,
  78. 'template': 'videos.html',
  79. 'publishedDate': publishedDate,
  80. 'embedded': embedded,
  81. 'thumbnail': thumbnail})
  82. return results