-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2ex.py
49 lines (37 loc) · 1.19 KB
/
v2ex.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
import contextlib
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from util import logger
HOT_URL = "https://www.v2ex.com/api/topics/hot.json"
HEADERS = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
}
RETRIES = Retry(total=3,
backoff_factor=1,
status_forcelist=[k for k in range(400, 600)])
@contextlib.contextmanager
def request_session():
s = requests.session()
try:
s.headers.update(HEADERS)
s.mount("http://", HTTPAdapter(max_retries=RETRIES))
s.mount("https://", HTTPAdapter(max_retries=RETRIES))
yield s
finally:
s.close()
class V2ex:
def get_hot_topic(self):
items = []
resp = None
try:
with request_session() as s:
resp = s.get(HOT_URL)
items.extend(resp.json())
except:
logger.exception('get hot topic failed')
return (items, resp)
if __name__ == "__main__":
v2ex = V2ex()
topics, resp = v2ex.get_hot_topic()
logger.info('%s', topics[0])