tagesschau.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """ARD: `Tagesschau API`_
  4. The Tagesschau is a news program of the ARD. Via the `Tagesschau API`_, current
  5. news and media reports are available in JSON format. The `Bundesstelle für Open
  6. Data`_ offers a `OpenAPI`_ portal at bundDEV_ where APIs are documented an can
  7. be tested.
  8. This SearXNG engine uses the `/api2u/search`_ API.
  9. .. _/api2u/search: http://tagesschau.api.bund.dev/
  10. .. _bundDEV: https://bund.dev/apis
  11. .. _Bundesstelle für Open Data: https://github.com/bundesAPI
  12. .. _Tagesschau API: https://github.com/AndreasFischer1985/tagesschau-api/blob/main/README_en.md
  13. .. _OpenAPI: https://swagger.io/specification/
  14. """
  15. from typing import TYPE_CHECKING
  16. from datetime import datetime
  17. from urllib.parse import urlencode
  18. import re
  19. if TYPE_CHECKING:
  20. import logging
  21. logger: logging.Logger
  22. about = {
  23. 'website': "https://tagesschau.de",
  24. 'wikidata_id': "Q703907",
  25. 'official_api_documentation': None,
  26. 'use_official_api': True,
  27. 'require_api_key': False,
  28. 'results': 'JSON',
  29. 'language': 'de',
  30. }
  31. categories = ['general', 'news']
  32. paging = True
  33. results_per_page = 10
  34. base_url = "https://www.tagesschau.de"
  35. def request(query, params):
  36. args = {
  37. 'searchText': query,
  38. 'pageSize': results_per_page,
  39. 'resultPage': params['pageno'] - 1,
  40. }
  41. params['url'] = f"{base_url}/api2u/search?{urlencode(args)}"
  42. return params
  43. def response(resp):
  44. results = []
  45. json = resp.json()
  46. for item in json['searchResults']:
  47. item_type = item.get('type')
  48. if item_type in ('story', 'webview'):
  49. results.append(_story(item))
  50. elif item_type == 'video':
  51. results.append(_video(item))
  52. else:
  53. logger.error("unknow result type: %s", item_type)
  54. return results
  55. def _story(item):
  56. return {
  57. 'title': item['title'],
  58. 'thumbnail': item.get('teaserImage', {}).get('imageVariants', {}).get('16x9-256'),
  59. 'publishedDate': datetime.strptime(item['date'][:19], '%Y-%m-%dT%H:%M:%S'),
  60. 'content': item['firstSentence'],
  61. 'url': item['shareURL'],
  62. }
  63. def _video(item):
  64. video_url = item['streams']['h264s']
  65. title = item['title']
  66. if "_vapp.mxf" in title:
  67. title = title.replace("_vapp.mxf", "")
  68. title = re.sub(r"APP\d+ (FC-)?", "", title, count=1)
  69. return {
  70. 'template': 'videos.html',
  71. 'title': title,
  72. 'thumbnail': item.get('teaserImage', {}).get('imageVariants', {}).get('16x9-256'),
  73. 'publishedDate': datetime.strptime(item['date'][:19], '%Y-%m-%dT%H:%M:%S'),
  74. 'content': item.get('firstSentence', ''),
  75. 'iframe_src': video_url,
  76. 'url': video_url,
  77. }