-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
243 lines (204 loc) · 7.37 KB
/
fetch.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# GraphQL
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
import asyncio
from os import environ as env
# Pandas
import pandas as pd
# Stdlib
from pathlib import Path
import string
import json
ENV_BOT_USER = "BOORU_BOT_USER"
ENV_BOT_PASS = "BOORU_BOT_PASS"
def get_bot_session(client: Client):
session_query = gql(
"""
mutation BotLogin {{
login(username: "{user}", password: "{password}") {{
session,
error
}}
}}
""".format(
user=env.get(ENV_BOT_USER, ""),
password=env.get(ENV_BOT_PASS, ""),
)
)
return client.execute_async(session_query)
def save_data_to_json(path: Path, filename: str, data: dict):
Path.mkdir(path, exist_ok=True)
with open(Path(path, filename), "w", encoding="utf-8") as f:
f.write(json.dumps(data))
async def get_all_tag_data(client: Client):
first_chars = string.ascii_lowercase + string.digits
tag_data = []
for c in first_chars:
tag_query = gql(
"""
query GetArtistTags {
"""
+ """
tags(limit: 10000, search: "{c}")
""".format(
c=c
)
+ """{
tag,
uses
}
}
"""
)
print(f'Fetching tag data for tags starting with "{c}"...')
c_tags = await client.execute_async(tag_query)
c_tag_data = c_tags["tags"]
tag_data += c_tag_data
return tag_data
def get_posts_data(client: Client):
posts_query = gql(
"""
query GetPosts {
posts(limit: 50000, offset: 0) {
post_id,
posted,
image_link,
mime,
ext,
info,
locked,
owner {
name
}
height,
width,
filesize,
source,
tags,
}
}
"""
)
print("Fetching posts data...")
return client.execute_async(posts_query)
def get_post_counts_per_day(posts_df: pd.DataFrame):
day_grouper = pd.Grouper(key="posted", freq="D")
return posts_df.groupby(day_grouper)["post_id"].count()
def get_post_counts_per_month(posts_df: pd.DataFrame):
month_grouper = pd.Grouper(key="posted", freq="ME")
return posts_df.groupby(month_grouper)["post_id"].count()
if __name__ == "__main__":
try:
env.get(ENV_BOT_USER)
env.get(ENV_BOT_PASS)
except KeyError:
print(
f"Both {ENV_BOT_USER} and {ENV_BOT_PASS} environment variables must be set."
)
exit(1)
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url="https://fanworks.wanderinginn.com/graphql")
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
try:
print("Retrieving session key...", flush=True)
session = asyncio.run(get_bot_session(client))
session_key = session.get("login").get("session")
if session_key is None:
print(
'The user could not be found. Error: "{}"'.format(
session.get("login").get("error")
)
)
exit(1)
except KeyError:
print(f"Could not retrieve session key for user {env.get(ENV_BOT_USER)}")
exit(1)
except Exception as e:
print(f'An error occurred: "{e}"')
cookies = {
"shm_session": session_key,
"shm_user": env.get(ENV_BOT_USER),
}
transport = AIOHTTPTransport(
url="https://fanworks.wanderinginn.com/graphql", cookies=cookies
)
client = Client(transport=transport, fetch_schema_from_transport=True)
data_path = Path("stats/static/js/data")
# Posts
posts = asyncio.run(get_posts_data(client))
posts_df = pd.DataFrame(posts["posts"])
posts_df["posted"] = pd.to_datetime(posts_df["posted"], format="mixed")
# Posts per day
posts_per_day = get_post_counts_per_day(posts_df)
posts_per_day_cumulative = posts_per_day.cumsum()
posts_per_day.to_json(
Path(data_path, "posts_per_day.json"), date_format="iso", indent=2
)
print(f"> Posts per day data saved to {data_path}")
posts_per_day_cumulative.to_json(
Path(data_path, "posts_per_day_cumulative.json"), date_format="iso", indent=2
)
print(f"> Posts per day (cumulative) data saved to {data_path}")
# Posts by month
posts_per_month = get_post_counts_per_month(posts_df)
posts_per_month_cumulative = posts_per_month.cumsum()
posts_per_month.to_json(
Path(data_path, "posts_per_month.json"), date_format="iso", indent=2
)
print(f"> Posts per month data saved to {data_path}")
posts_per_month_cumulative.to_json(
Path(data_path, "posts_per_month_cumulative.json"), date_format="iso", indent=2
)
print(f"> Posts per month (cumulative) data saved to {data_path}")
# Post filetype distribution
posts_df.groupby("ext")["ext"].count().sort_values(ascending=True).to_json(
Path(data_path, "post_filetypes.json"), indent=2
)
print(f"> Post filetype data saved to {data_path}")
# Post filesize data
posts_df["filesize"].to_json(
Path(data_path, "post_filesizes.json"), orient="values", indent=2
)
print(f"> Post filesize data saved to {data_path}")
# Post uploader counts
posts_df["uploader"] = posts_df["owner"].apply(lambda x: pd.Series(x["name"]))
uploader_counts = posts_df.groupby("uploader")["uploader"].count()
# Manually reallocate uploads to lightningowl per Ayutac's request
uploader_counts["Ayutac"] -= 336
if uploader_counts.get("lightningowl", None):
uploader_counts["lightningowl"] += 336
else:
uploader_counts["lightningowl"] = 336
uploader_counts.sort_values(ascending=False).to_json(
Path(data_path, "post_uploader_counts.json"), indent=2
)
print(f"> Post uploader counts data saved to {data_path}")
# Tags
tags = pd.DataFrame(asyncio.run(get_all_tag_data(client)))
# Artist tags
artist_tags = tags.query("tag.str.startswith('artist:')")
artist_tags.sort_values(["uses", "tag"], ascending=False).set_index("tag").to_json(
Path(data_path, "artist_tags.json"), indent=2
)
print(f"> Artist tag data saved to {data_path}")
# Character tags
character_tags = tags.query("tag.str.startswith('character:')")
character_tags.sort_values(["uses", "tag"], ascending=False).set_index(
"tag"
).to_json(Path(data_path, "character_tags.json"), indent=2)
print(f"> Character tag data saved to {data_path}")
# Book tags
book_tags = tags.query("tag.str.startswith('spoiler:book')")
book_tags.sort_values(["uses", "tag"], ascending=False).set_index("tag").to_json(
Path(data_path, "book_tags.json"), indent=2
)
print(f"> Book tag data saved to {data_path}")
# Volume tags
volume_tags = tags.query(
"tag.str.startswith('spoiler:volum') or tag == 'spoiler:book1' or tag == 'spoiler:book2'"
)
volume_tags.sort_values(["uses", "tag"], ascending=False).set_index("tag").to_json(
Path(data_path, "volume_tags.json"), indent=2
)
print(f"> Volume tag data saved to {data_path}")