|  | @@ -1,50 +1,86 @@
 | 
	
		
			
			|  | 1 | +## Bing (News)
 | 
	
		
			
			|  | 2 | +# 
 | 
	
		
			
			|  | 3 | +# @website     https://www.bing.com/news
 | 
	
		
			
			|  | 4 | +# @provide-api yes (http://datamarket.azure.com/dataset/bing/search), max. 5000 query/month
 | 
	
		
			
			|  | 5 | +# 
 | 
	
		
			
			|  | 6 | +# @using-api   no (because of query limit)
 | 
	
		
			
			|  | 7 | +# @results     HTML (using search portal)
 | 
	
		
			
			|  | 8 | +# @stable      no (HTML can change)
 | 
	
		
			
			|  | 9 | +# @parse       url, title, content, publishedDate
 | 
	
		
			
			|  | 10 | +
 | 
	
		
			
			| 1 | 11 |  from urllib import urlencode
 | 
	
		
			
			| 2 | 12 |  from cgi import escape
 | 
	
		
			
			| 3 | 13 |  from lxml import html
 | 
	
		
			
			|  | 14 | +from datetime import datetime, timedelta
 | 
	
		
			
			|  | 15 | +from dateutil import parser
 | 
	
		
			
			|  | 16 | +import re
 | 
	
		
			
			| 4 | 17 |  
 | 
	
		
			
			|  | 18 | +# engine dependent config
 | 
	
		
			
			| 5 | 19 |  categories = ['news']
 | 
	
		
			
			| 6 |  | -
 | 
	
		
			
			| 7 |  | -base_url = 'http://www.bing.com/'
 | 
	
		
			
			| 8 |  | -search_string = 'news/search?{query}&first={offset}'
 | 
	
		
			
			| 9 | 20 |  paging = True
 | 
	
		
			
			| 10 | 21 |  language_support = True
 | 
	
		
			
			| 11 | 22 |  
 | 
	
		
			
			|  | 23 | +# search-url
 | 
	
		
			
			|  | 24 | +base_url = 'https://www.bing.com/'
 | 
	
		
			
			|  | 25 | +search_string = 'news/search?{query}&first={offset}'
 | 
	
		
			
			| 12 | 26 |  
 | 
	
		
			
			|  | 27 | +# do search-request
 | 
	
		
			
			| 13 | 28 |  def request(query, params):
 | 
	
		
			
			| 14 | 29 |      offset = (params['pageno'] - 1) * 10 + 1
 | 
	
		
			
			|  | 30 | +
 | 
	
		
			
			| 15 | 31 |      if params['language'] == 'all':
 | 
	
		
			
			| 16 | 32 |          language = 'en-US'
 | 
	
		
			
			| 17 | 33 |      else:
 | 
	
		
			
			| 18 | 34 |          language = params['language'].replace('_', '-')
 | 
	
		
			
			|  | 35 | +
 | 
	
		
			
			| 19 | 36 |      search_path = search_string.format(
 | 
	
		
			
			| 20 | 37 |          query=urlencode({'q': query, 'setmkt': language}),
 | 
	
		
			
			| 21 | 38 |          offset=offset)
 | 
	
		
			
			| 22 | 39 |  
 | 
	
		
			
			| 23 | 40 |      params['cookies']['SRCHHPGUSR'] = \
 | 
	
		
			
			| 24 | 41 |          'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
 | 
	
		
			
			| 25 |  | -    #if params['category'] == 'images':
 | 
	
		
			
			| 26 |  | -    # params['url'] = base_url + 'images/' + search_path
 | 
	
		
			
			|  | 42 | +
 | 
	
		
			
			| 27 | 43 |      params['url'] = base_url + search_path
 | 
	
		
			
			| 28 | 44 |      return params
 | 
	
		
			
			| 29 | 45 |  
 | 
	
		
			
			| 30 | 46 |  
 | 
	
		
			
			|  | 47 | +# get response from search-request
 | 
	
		
			
			| 31 | 48 |  def response(resp):
 | 
	
		
			
			| 32 | 49 |      results = []
 | 
	
		
			
			|  | 50 | +
 | 
	
		
			
			| 33 | 51 |      dom = html.fromstring(resp.content)
 | 
	
		
			
			| 34 |  | -    for result in dom.xpath('//div[@class="sa_cc"]'):
 | 
	
		
			
			| 35 |  | -        link = result.xpath('.//h3/a')[0]
 | 
	
		
			
			|  | 52 | +
 | 
	
		
			
			|  | 53 | +    # parse results
 | 
	
		
			
			|  | 54 | +    for result in dom.xpath('//div[@class="sn_r"]'):
 | 
	
		
			
			|  | 55 | +        link = result.xpath('.//div[@class="newstitle"]/a')[0]
 | 
	
		
			
			| 36 | 56 |          url = link.attrib.get('href')
 | 
	
		
			
			| 37 | 57 |          title = ' '.join(link.xpath('.//text()'))
 | 
	
		
			
			| 38 |  | -        content = escape(' '.join(result.xpath('.//p//text()')))
 | 
	
		
			
			| 39 |  | -        results.append({'url': url, 'title': title, 'content': content})
 | 
	
		
			
			|  | 58 | +        content = escape(' '.join(result.xpath('.//div[@class="sn_txt"]/div//span[@class="sn_snip"]//text()')))
 | 
	
		
			
			|  | 59 | +        
 | 
	
		
			
			|  | 60 | +        # parse publishedDate
 | 
	
		
			
			|  | 61 | +        publishedDate = escape(' '.join(result.xpath('.//div[@class="sn_txt"]/div//span[@class="sn_ST"]//span[@class="sn_tm"]//text()')))
 | 
	
		
			
			| 40 | 62 |  
 | 
	
		
			
			| 41 |  | -    if results:
 | 
	
		
			
			| 42 |  | -        return results
 | 
	
		
			
			|  | 63 | +        if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
 | 
	
		
			
			|  | 64 | +            timeNumbers = re.findall(r'\d+', publishedDate)
 | 
	
		
			
			|  | 65 | +            publishedDate = datetime.now()\
 | 
	
		
			
			|  | 66 | +                - timedelta(minutes=int(timeNumbers[0]))
 | 
	
		
			
			|  | 67 | +        elif re.match("^[0-9]+ hour(s|) ago$", publishedDate):
 | 
	
		
			
			|  | 68 | +            timeNumbers = re.findall(r'\d+', publishedDate)
 | 
	
		
			
			|  | 69 | +            publishedDate = datetime.now()\
 | 
	
		
			
			|  | 70 | +                - timedelta(hours=int(timeNumbers[0]))
 | 
	
		
			
			|  | 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 | +            publishedDate = parser.parse(publishedDate)  
 | 
	
		
			
			| 43 | 78 |  
 | 
	
		
			
			| 44 |  | -    for result in dom.xpath('//li[@class="b_algo"]'):
 | 
	
		
			
			| 45 |  | -        link = result.xpath('.//h2/a')[0]
 | 
	
		
			
			| 46 |  | -        url = link.attrib.get('href')
 | 
	
		
			
			| 47 |  | -        title = ' '.join(link.xpath('.//text()'))
 | 
	
		
			
			| 48 |  | -        content = escape(' '.join(result.xpath('.//p//text()')))
 | 
	
		
			
			| 49 |  | -        results.append({'url': url, 'title': title, 'content': content})
 | 
	
		
			
			|  | 79 | +        # append result
 | 
	
		
			
			|  | 80 | +        results.append({'url': url, 
 | 
	
		
			
			|  | 81 | +                        'title': title, 
 | 
	
		
			
			|  | 82 | +                        'publishedDate': publishedDate,
 | 
	
		
			
			|  | 83 | +                        'content': content})
 | 
	
		
			
			|  | 84 | +
 | 
	
		
			
			|  | 85 | +    # return results
 | 
	
		
			
			| 50 | 86 |      return results
 |