logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

yahoo_news.py (3618B)


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