-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-content.py
59 lines (49 loc) · 1.75 KB
/
get-content.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
#!/usr/bin/env python3
import os
import requests
import json
from configparser import ConfigParser
from datetime import datetime
from time import sleep
dir_path = os.path.dirname(os.path.realpath(__file__))
file_name = os.path.basename(__file__)
config = ConfigParser()
config.read(dir_path + "/config.ini")
def get_now_time():
"""
取得現在的時間字串
"""
return datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
if __name__ == "__main__":
URL_ROOT = "https://tfc-taiwan.org.tw"
token = config["TFC"]["token"]
now_time = get_now_time()
print("Run @ {}".format(now_time))
endpoint = "{0}/{1}".format(URL_ROOT, "article-api")
page = 0
condition = True
while condition:
print("Prepare to get page: {}".format(page))
payload = {
"token": token,
"page": page,
}
try:
response = requests.get(endpoint, params=payload, timeout=15)
data = json.loads(response.content)
filename = "data/page-{}.json".format(page)
with open(filename, "w", encoding="utf8") as pagefile:
json.dump(data, pagefile, indent=4, ensure_ascii=False)
# for node in data["nodes"]:
# print(node["node"]["nid"])
except requests.exceptions.HTTPError as err:
print("HTTP exception error: {}".format(err))
except requests.exceptions.RequestException as e:
print("Exception error {}".format(e))
if data.get("pager"):
if int(data["pager"]["page"]) <= int(data["pager"]["pages"]) - 2:
# if int(data["pager"]["page"]) <= 2:
page = int(data["pager"]["page"]) + 1
else:
condition = False
sleep(2)