-
Notifications
You must be signed in to change notification settings - Fork 0
/
devto.py
155 lines (124 loc) · 4.31 KB
/
devto.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
import requests
import json
import pprint
from sqlitedict import SqliteDict
from datetime import datetime
from backports.datetime_fromisoformat import MonkeyPatch
MonkeyPatch.patch_fromisoformat()
cache = SqliteDict("dev.to", autocommit=True)
host = "http://34.125.94.232"
collection = "devto"
def set_schema(collection, schema):
res = requests.post(
f"{host}/api/indexes/{collection}/schema",
json=schema
)
print(res.text)
def get_posts(page=1, pagesize=200, username=None):
if username:
res = requests.get(f"https://dev.to/api/articles?page={page}&per_page={pagesize}&username={username}")
else:
res = requests.get(f"https://dev.to/api/articles?page={page}&per_page={pagesize}")
try:
data = json.loads(res.text)
batch = []
usernames = cache.get("usernames", {})
fields = ["type_of", "id", "title", "description",
"readable_publish_date", "url", "comments_count", "public_reactions_count", "collection_id",
"published_timestamp", "social_image", "created_at", "published_at", "tag_list",
"cover_image", "user"]
for d in data:
_data = {field:d.get(field) for field in fields}
date = datetime.fromisoformat(_data["published_timestamp"][:-1])
_data["published_timestamp"] = date.strftime("%Y-%m-%d")
user = d["user"]["username"]
if user not in usernames:
usernames[user] = {}
batch.append(_data)
cache["usernames"] = usernames
return batch
except json.JSONDecodeError as err:
return
def get_pages(n=10, field="article", username=None):
if username and field=="user":
pages = cache["usernames"].get(username, None)
else:
pages = cache.get(field, {})
max_page = 0
if pages:
max_page = max([int(k) for k in pages])
return [max_page + i for i in range(1, n)]
def add_data(page, batch, username=None):
usernames = cache.get("usernames", {})
if username:
pages = usernames.get(username, {})
else:
pages = cache.get(field, {})
if not batch:
print("batch empty")
return None
res = requests.post(f"{host}/api/indexes/{collection}/objects", json=batch)
try:
jres = json.loads(res.text)
pages[page] = {
"status": "pending",
"task_id": jres["taskId"],
"page": page,
"count": len(batch)
}
if username:
usernames[username] = pages
cache["usernames"] = usernames
else:
cache[field] = pages
print(jres, " => ", username)
except json.JSONDecodeError as err:
print(err, " => add_data", res.text)
pass
def get_username():
usernames = cache.get("usernames", {})
names = sorted(usernames.keys())
u = 0
for name in names:
u += 1
pages = usernames[name]
if pages:
max_page = max([v for k, v in pages.items()], key=lambda x:x["page"])
if max_page["count"] >= 200:
print(f"users: {u} => total users: {len(usernames)}")
return name
else:
print(f"users: {u} => total users: {len(usernames)}")
return name
def do_posts(field):
username = None
n = 5
if field == "user":
username = get_username()
npages = get_pages(n=n, field="user", username=username)
else:
npages = get_pages(n=n, field=field)
print("USER ==> ", username)
for p in npages:
batch = get_posts(p, username=username)
add_data(p, batch, username)
if username:
print(cache.get("usernames", {}).get(username))
schema = [
{"name": "id", "type": "id"},
{"name": "title", "type": "text"},
{"name": "description", "type": "text"},
{"name": "public_reactions_count", "type": "number"},
{"name": "comments_count", "type": "number"},
{"name": "tag_list", "type": "text", "index": False, "facet": True},
{"name": "published_timestamp", "type": "date"}
]
if __name__ == "__main__":
import sys
args = sys.argv
if len(args) < 2:
raise ValueError("Field type needed in arguments")
field = args[1]
set_schema(collection, schema)
do_posts(field=field)
# print(cache.get(field))