This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yahoo.py
166 lines (111 loc) · 4.31 KB
/
yahoo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import oauth2 as oauth
import time
import urllib
import json
from traceback import format_exc
import requests
class GeoCodeError(Exception):
pass
class YahooBoss(object):
def __init__(self, **kwargs):
self.key = 'dj0yJmk9UzhPZlRNbmFwQjdZJmQ9WVdrOWNXSlBRM2hOTm1VbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1lNw--'
self.secret = '8eb66129a2d550779614eb358f3f6bfecac854ef'
class GeoCodeError(Exception):
pass
class BossSearch(YahooBoss):
def __init__(self, **kwargs):
super(BossSearch, self).__init__(**kwargs)
self.params = {
'oauth_version': "1.0",
'format': 'json'
}
self._set_params(**kwargs)
def _set_params( self, **kwargs ):
self.params['count'] = kwargs.get('results_per_page',50)
self.age = kwargs.get('age', '7d')
self.age = kwargs.get('age','7d')
self.urls = kwargs.get('urls',[])
if self.urls:
param_str = ")OR(".join(self.urls)
self.params['url'] = "(" + param_str + ")"
# looks like Yahoo is going away from urls and uses "sites" now
if kwargs.get('sites'):
sites = kwargs.get('sites')
if isinstance(sites, list ):
self.params['sites'] = ','.join(sites)
else:
self.params['sites'] = sites
def search_news(self,q,page_num=1,**kwargs):
self._set_params(**kwargs)
start = (page_num - 1) * self.params.get('count')
return self.make_request('news',q, start)
def search_web(self, q, page_num=1,**kwargs ):
self._set_params(**kwargs)
start = (page_num - 1) * self.params.get('count')
return self.make_request('web',q,start)
def make_request(self,bucket, q,start_at):
url = "http://yboss.yahooapis.com/ysearch/"+bucket
consumer = oauth.Consumer(key=self.key,secret=self.secret)
try:
new_params = {
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': int(time.time()),
'q' : urllib.quote_plus(q),
'start': start_at
}
except:
# weird unicode problems sometimes
return []
r_params = dict( self.params.items() + new_params.items() )
if bucket == 'news':
r_params['age'] = self.age
req = oauth.Request(method="GET",url=url,parameters=r_params)
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, None)
#f = urllib.urlopen(req.to_url())
f = requests.get(req.to_url())
retval = f.json()
self.total_results = retval.get('bossresponse').get(bucket).get('totalresults')
if self.total_results:
results = retval.get('bossresponse').get(bucket).get('results')
else:
results = []
return results
class PlaceFinder(YahooBoss):
# placefinder geocoding
def __init__(self, **kwargs):
super(PlaceFinder,self).__init__(**kwargs)
self.params = {}
def placefinder(self, q):
self.params['q'] = urllib.quote_plus(q)
return self.make_request()
def lookup(self,q):
return self.placefinder(q)
def reverse(self, lat, lon ):
self.params['gflags'] = 'R'
self.params['location'] = urllib.quote_plus(str(lat) + ' ' + str(lon))
return self.make_request()
def make_request(self):
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': int(time.time()),
'count': '1',
'flags': 'J'
}
params.update(self.params)
url = 'https://yboss.yahooapis.com/geo/placespotter?'
consumer = oauth.Consumer(key=self.key,secret=self.secret)
req = oauth.Request(method="GET", url=url, parameters=params)
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, None)
retval = requests.get(req.to_url())
response = retval.json()
print response
if response and response.get('bossresponse'):
return response.get('bossresponse').get('placefinder')
else:
raise GeoCodeError( retval )
pf = PlaceFinder()
print pf
print pf.make_request()