yahoo_news.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Yahoo (News)
  2. #
  3. # @website https://news.yahoo.com
  4. # @provide-api yes (https://developer.yahoo.com/boss/search/)
  5. # $0.80/1000 queries
  6. #
  7. # @using-api no (because pricing)
  8. # @results HTML (using search portal)
  9. # @stable no (HTML can change)
  10. # @parse url, title, content, publishedDate
  11. import re
  12. from datetime import datetime, timedelta
  13. from urllib.parse import urlencode
  14. from lxml import html
  15. from searx.engines.yahoo import (
  16. parse_url, _fetch_supported_languages, supported_languages_url, language_aliases
  17. )
  18. from dateutil import parser
  19. from searx.utils import extract_text, extract_url, match_language
  20. # engine dependent config
  21. categories = ['news']
  22. paging = True
  23. language_support = True
  24. # search-url
  25. search_url = 'https://news.search.yahoo.com/search?{query}&b={offset}&{lang}=uh3_news_web_gs_1&pz=10&xargs=0&vl=lang_{lang}' # noqa
  26. # specific xpath variables
  27. results_xpath = '//ol[contains(@class,"searchCenterMiddle")]//li'
  28. url_xpath = './/h3/a/@href'
  29. title_xpath = './/h3/a'
  30. content_xpath = './/div[@class="compText"]'
  31. publishedDate_xpath = './/span[contains(@class,"tri")]'
  32. suggestion_xpath = '//div[contains(@class,"VerALSOTRY")]//a'
  33. # do search-request
  34. def request(query, params):
  35. offset = (params['pageno'] - 1) * 10 + 1
  36. if params['language'] == 'all':
  37. language = 'en'
  38. else:
  39. language = match_language(params['language'], supported_languages, language_aliases).split('-')[0]
  40. params['url'] = search_url.format(offset=offset,
  41. query=urlencode({'p': query}),
  42. lang=language)
  43. # TODO required?
  44. params['cookies']['sB'] = '"v=1&vm=p&fl=1&vl=lang_{lang}&sh=1&pn=10&rw=new'\
  45. .format(lang=language)
  46. return params
  47. def sanitize_url(url):
  48. if ".yahoo.com/" in url:
  49. return re.sub("\\;\\_ylt\\=.+$", "", url)
  50. else:
  51. return url
  52. # get response from search-request
  53. def response(resp):
  54. results = []
  55. dom = html.fromstring(resp.text)
  56. # parse results
  57. for result in dom.xpath(results_xpath):
  58. urls = result.xpath(url_xpath)
  59. if len(urls) != 1:
  60. continue
  61. url = sanitize_url(parse_url(extract_url(urls, search_url)))
  62. title = extract_text(result.xpath(title_xpath)[0])
  63. content = extract_text(result.xpath(content_xpath)[0])
  64. # parse publishedDate
  65. publishedDate = extract_text(result.xpath(publishedDate_xpath)[0])
  66. # still useful ?
  67. if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
  68. publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group()))
  69. elif re.match("^[0-9]+ days? ago$", publishedDate):
  70. publishedDate = datetime.now() - timedelta(days=int(re.match(r'\d+', publishedDate).group()))
  71. elif re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate):
  72. timeNumbers = re.findall(r'\d+', publishedDate)
  73. publishedDate = datetime.now()\
  74. - timedelta(hours=int(timeNumbers[0]))\
  75. - timedelta(minutes=int(timeNumbers[1]))
  76. else:
  77. try:
  78. publishedDate = parser.parse(publishedDate)
  79. except:
  80. publishedDate = datetime.now()
  81. if publishedDate.year == 1900:
  82. publishedDate = publishedDate.replace(year=datetime.now().year)
  83. # append result
  84. results.append({'url': url,
  85. 'title': title,
  86. 'content': content,
  87. 'publishedDate': publishedDate})
  88. # return results
  89. return results