forked from abrinsmead/hackernews-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackernews.py
More file actions
63 lines (43 loc) · 1.58 KB
/
Copy pathhackernews.py
File metadata and controls
63 lines (43 loc) · 1.58 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
__version__ = '0.3.2'
from datetime import datetime
import requests
class Item():
def __init__(self, **entries):
self.__dict__.update(entries)
class User():
def __init__(self, **entries):
self.__dict__.update(entries)
class HackerNews():
def __init__(self, timeout=5):
self.url = 'https://hacker-news.firebaseio.com/v0/{uri}'
self.timeout = timeout
def request(self, method, uri):
url = self.url.format(uri=uri)
return requests.request(method, url, timeout=self.timeout)
def item(self, item_id):
uri = 'item/{item_id}.json'.format(item_id=item_id)
response = self.request('GET', uri)
item = response.json()
if item.get('time'):
item['time'] = datetime.fromtimestamp(item['time'])
return Item(**item)
def user(self, user_id):
uri = 'user/{user_id}.json'.format(user_id=user_id)
response = self.request('GET', uri)
user = response.json()
user['created'] = datetime.fromtimestamp(user['created'])
return User(**user)
def top_stories(self):
response = self.request('GET', 'topstories.json')
return response.json()
def newstories(self):
response = self.request('GET', 'newstories.json')
return response.json()
def last_newstories(self):
return self.newstories()[0]
def max_item(self):
response = self.request('GET', 'maxitem.json')
return response.json()
def updates(self):
response = self.request('GET', 'updates.json')
return response.json()