bing_news.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. Bing (News)
  3. @website https://www.bing.com/news
  4. @provide-api yes (http://datamarket.azure.com/dataset/bing/search),
  5. max. 5000 query/month
  6. @using-api no (because of query limit)
  7. @results RSS (using search portal)
  8. @stable yes (except perhaps for the images)
  9. @parse url, title, content, publishedDate, thumbnail
  10. """
  11. from datetime import datetime
  12. from dateutil import parser
  13. from lxml import etree
  14. from searx.utils import list_get
  15. from searx.engines.bing import _fetch_supported_languages, supported_languages_url
  16. from searx.url_utils import urlencode, urlparse, parse_qsl
  17. # engine dependent config
  18. categories = ['news']
  19. paging = True
  20. language_support = True
  21. time_range_support = True
  22. # search-url
  23. base_url = 'https://www.bing.com/'
  24. search_string = 'news/search?{query}&first={offset}&format=RSS'
  25. search_string_with_time = 'news/search?{query}&first={offset}&qft=interval%3d"{interval}"&format=RSS'
  26. time_range_dict = {'day': '7',
  27. 'week': '8',
  28. 'month': '9'}
  29. # remove click
  30. def url_cleanup(url_string):
  31. parsed_url = urlparse(url_string)
  32. if parsed_url.netloc == 'www.bing.com' and parsed_url.path == '/news/apiclick.aspx':
  33. query = dict(parse_qsl(parsed_url.query))
  34. return query.get('url', None)
  35. return url_string
  36. # replace the http://*bing4.com/th?id=... by https://www.bing.com/th?id=...
  37. def image_url_cleanup(url_string):
  38. parsed_url = urlparse(url_string)
  39. if parsed_url.netloc.endswith('bing4.com') and parsed_url.path == '/th':
  40. query = dict(parse_qsl(parsed_url.query))
  41. return "https://www.bing.com/th?id=" + query.get('id')
  42. return url_string
  43. def _get_url(query, language, offset, time_range):
  44. if time_range in time_range_dict:
  45. search_path = search_string_with_time.format(
  46. query=urlencode({'q': query, 'setmkt': language}),
  47. offset=offset,
  48. interval=time_range_dict[time_range])
  49. else:
  50. search_path = search_string.format(
  51. query=urlencode({'q': query, 'setmkt': language}),
  52. offset=offset)
  53. return base_url + search_path
  54. # do search-request
  55. def request(query, params):
  56. if params['time_range'] and params['time_range'] not in time_range_dict:
  57. return params
  58. offset = (params['pageno'] - 1) * 10 + 1
  59. if params['language'] == 'all':
  60. language = 'en-US'
  61. else:
  62. language = params['language']
  63. params['url'] = _get_url(query, language, offset, params['time_range'])
  64. return params
  65. # get response from search-request
  66. def response(resp):
  67. results = []
  68. rss = etree.fromstring(resp.content)
  69. ns = rss.nsmap
  70. # parse results
  71. for item in rss.xpath('./channel/item'):
  72. # url / title / content
  73. url = url_cleanup(item.xpath('./link/text()')[0])
  74. title = list_get(item.xpath('./title/text()'), 0, url)
  75. content = list_get(item.xpath('./description/text()'), 0, '')
  76. # publishedDate
  77. publishedDate = list_get(item.xpath('./pubDate/text()'), 0)
  78. try:
  79. publishedDate = parser.parse(publishedDate, dayfirst=False)
  80. except TypeError:
  81. publishedDate = datetime.now()
  82. except ValueError:
  83. publishedDate = datetime.now()
  84. # thumbnail
  85. thumbnail = list_get(item.xpath('./News:Image/text()', namespaces=ns), 0)
  86. if thumbnail is not None:
  87. thumbnail = image_url_cleanup(thumbnail)
  88. # append result
  89. if thumbnail is not None:
  90. results.append({'url': url,
  91. 'title': title,
  92. 'publishedDate': publishedDate,
  93. 'content': content,
  94. 'img_src': thumbnail})
  95. else:
  96. results.append({'url': url,
  97. 'title': title,
  98. 'publishedDate': publishedDate,
  99. 'content': content})
  100. # return results
  101. return results