-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnytapi.py
More file actions
108 lines (94 loc) · 3.83 KB
/
nytapi.py
File metadata and controls
108 lines (94 loc) · 3.83 KB
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
import simplejson as json
import hashlib
import urllib2
from urllib import quote, quote_plus, urlencode
def fetch_data(url):
data = urllib2.urlopen(url).read()
return data
class NYT(object):
def __init__(self, key):
self.key = key
self.uri = 'api.nytimes.com/svc'
self.version = 'v1'
self.response_format = 'json'
class TimesTags(NYT):
"""A wrapper around the NYTimes TimesTags API
tags = TimesTags(TIMES_TAGS_KEY)
filter = ['Per','Des']
data = tags.suggest('obama', filter)
"""
def __init__(self, key):
super(TimesTags, self).__init__(key)
self.baseURI = 'http://%s/timestags/suggest' % self.uri
def suggest(self, query, filter=[], max=100):
reqarg = urlencode({
'query': query,
'filter': ','.join(['(%s)' % f for f in filter]),
'max': max,
'api-key': self.key
})
url = ''.join([self.baseURI, '?', reqarg])
data = fetch_data(url)
payload = json.loads(data)
return payload
class TimesPeople(NYT):
"""A wrapper around the NYTimes TimesPeople API
tp = TimesPeople(TIMES_PEOPLE_KEY)
uid = tp.get_user_id('email@example.com')
data = tp.get_user_data(uid, 'activities')
"""
def __init__(self, key):
super(TimesPeople, self).__init__(key)
self.baseURI = 'http://%s/timespeople/api/%s/user/' % (self.uri, self.version)
def get_user_id(self, email_address):
email = email_address.lower()
email_hash = hashlib.md5(email).hexdigest()
url = self.baseURI+'%s/id.%s?api-key=%s'% (email_hash, self.response_format, self.key)
data = fetch_data(url)
payload = json.loads(data)
return payload['results']['user_id']
def get_user_data(self, user_id, data_type):
url = self.baseURI+'%s/%s.%s?api-key=%s'% (user_id, data_type, self.response_format, self.key)
data = fetch_data(url)
payload = json.loads(data)
return payload['results']
class ArticleSearch(NYT):
"""A wrapper around the NYTimes Article Search API
nyt = ArticleSearch(ARTICLE_SEARCH_KEY)
terms = 'health care'
query_fields = {
'title': ['debate'],
'body': ['economy', '2009'],
}
query_facets = {
'per_facet':['OBAMA, BARACK'],
'des_facet':['UNITED STATES POLITICS AND GOVERNMENT','UNITED STATES ECONOMY'],
}
facets = ['des_facet','per_facet','geo_facet']
response_fields = ['body','byline','date','title','url','lead_paragraph','desk_facet']
results = nyt.search(
terms,
query_fields=query_fields,
query_facets=query_facets,
facets=facets,
response_fields=response_fields,
rank='oldest',
)
"""
def __init__(self, key):
super(ArticleSearch, self).__init__(key)
self.baseURI = 'http://%s/search/%s/article' % (self.uri, self.version)
def search(self, terms=None, query_facets={}, query_fields={}, response_fields=[], facets=[], **kwargs):
q = terms.strip()
q += ''.join([' %s:%s' % (k,val) for k,v in query_fields.items() for val in v])
q += ''.join([' %s:[%s]' % (k,val) for k,v in query_facets.items() for val in v])
reqarg = urlencode({'query':q,
'facets': ','.join(facets),
'fields':','.join(response_fields),
'api-key': self.key})
reqarg += ''.join(['&%s=%s' % (k,v) for k,v in kwargs.items()])
url = ''.join([self.baseURI, '?', reqarg])
print url
data = fetch_data(url)
payload = json.loads(data)
return payload