Created
January 1, 2021 05:27
-
-
Save Xzonn/443bd8d793f77a9a30ca3525b379e0b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: UTF-8 -*- | |
import datetime | |
import json | |
import os | |
import requests | |
os.chdir(os.path.dirname(__file__)) | |
apiAddress = 'https://zh.moegirl.org.cn/api.php?format=json&utf8=1' | |
data = [] | |
kw = { | |
"action": "query", | |
"format": "json", | |
"list": "usercontribs", | |
"uclimit": "max", | |
"ucstart": "2020-12-31T15:59:59.999Z", | |
"ucend": "2019-12-31T16:00:00.000Z", # 如果需要分析截至2020年的编辑数,请注释掉此行 | |
"ucuser": "Xzonn", # 请替换此处的用户名 | |
"ucprop": "title|timestamp|sizediff" | |
} | |
r = requests.post(apiAddress, | |
data = kw, | |
timeout = 100 | |
) | |
j = r.json() | |
while len(j["query"]["usercontribs"]): | |
data += j["query"]["usercontribs"] | |
with open("UserContribs.json", "w", -1, "utf-8") as f: | |
json.dump(data, f, ensure_ascii = False) | |
if "continue" in j: | |
kw.update(j["continue"]) | |
r = requests.post(apiAddress, | |
data = kw, | |
timeout = 100 | |
) | |
j = r.json() | |
else: | |
break | |
timePeriod = ["深夜", "清晨", "上午", "下午", "傍晚", "夜晚"] | |
editIn2020, editInMainNamespace, editIn2020InMainNamespace = 0, 0, 0 | |
diffIn2020, diffInMainNamespace, diffIn2020InMainNamespace = 0, 0, 0 | |
totalDiff = 0 | |
days = {} | |
months = { i: 0 for i in range(1, 13) } | |
periods = { i: 0 for i in range(6) } | |
for i in data: | |
totalDiff += i["sizediff"] | |
timestamp = datetime.datetime.fromisoformat(i["timestamp"][:-1] + '+00:00').astimezone(datetime.timezone(datetime.timedelta(hours=8))) | |
if timestamp.year == 2020: | |
editIn2020 += 1 | |
diffIn2020 += i["sizediff"] | |
day, month = f"{timestamp.month:02}-{timestamp.day:02}", timestamp.month | |
period = timestamp.hour // 4 | |
if day not in days: | |
days[day] = 1 | |
else: | |
days[day] += 1 | |
months[month] += 1 | |
periods[period] += 1 | |
if i["ns"] == 0: | |
editInMainNamespace += 1 | |
diffInMainNamespace += i["sizediff"] | |
editIn2020InMainNamespace += 1 | |
diffIn2020InMainNamespace += i["sizediff"] | |
elif i["ns"] == 0: | |
editInMainNamespace += 1 | |
diffInMainNamespace += i["sizediff"] | |
days = { k: days[k] for k in sorted(days.keys(), key = lambda x: days[x], reverse = True) } | |
months = { k: months[k] for k in sorted(months.keys(), key = lambda x: months[x], reverse = True) } | |
periods = { k: periods[k] for k in sorted(periods.keys(), key = lambda x: periods[x], reverse = True) } | |
beautifulDay = "{0}月{1}日".format(*[int(i) for i in list(days.keys())[0].split("-")]) | |
beautifulMonth = "{0}月".format(list(months.keys())[0]) | |
with open("UserContribsResult.txt", "w", -1, "utf-8") as f: | |
f.write(f"""截至2020年结束,在萌娘百科共进行了'''{len(data):,}次'''有效编辑(不计已删除的编辑,下同),为萌娘百科增加了'''{totalDiff:,}字节''';其中主空间'''{editInMainNamespace:,}次'''(占{editInMainNamespace * 100 / len(data):.2f}%),增加了'''{diffInMainNamespace:,}字节'''。2020年共进行了'''{editIn2020:,}次'''有效编辑,增加了'''{diffIn2020:,}字节''';其中主空间'''{editIn2020InMainNamespace:,}次'''(占{editIn2020InMainNamespace * 100 / editIn2020:.2f}%),增加了'''{diffIn2020InMainNamespace:,}字节'''。 | |
在2020年,编辑次数最多的一天是'''{beautifulDay}''',共进行了'''{list(days.values())[0]:,}次'''编辑;编辑次数最多的一个月是'''{beautifulMonth}''',共进行了'''{list(months.values())[0]:,}次'''编辑。最经常进行编辑的时段是'''{timePeriod[list(periods.keys())[0]]}''',共进行了'''{list(periods.values())[0]:,}'''次编辑。""") | |
with open("UserContribsResultByMonth.json", "w", -1, "utf-8") as f: | |
echart = { | |
"title": { | |
"text": "有效编辑历史(按月份分类)" | |
}, | |
"xAxis": { | |
"type": "category", | |
"data": [f"{i}月" for i in range(1, 13)] | |
}, | |
"yAxis": { | |
"type": "value" | |
}, | |
"series": [{ | |
"data": [months[i] for i in range(1, 13)], | |
"type": "line", | |
"label": { | |
"show": True | |
} | |
}] | |
} | |
json.dump(echart, f, ensure_ascii=False, indent = 4) | |
with open("UserContribsResultByPeriod.json", "w", -1, "utf-8") as f: | |
echart = { | |
"title": { | |
"text": "有效编辑历史(按时段分类)" | |
}, | |
"xAxis": { | |
"type": "category", | |
"data": [f"{timePeriod[i]}\n({i * 4:02}:00~{i * 4 + 3:02}:59)" for i in range(6)] | |
}, | |
"yAxis": { | |
"type": "value" | |
}, | |
"series": [{ | |
"data": [periods[i] for i in range(6)], | |
"type": "line", | |
"label": { | |
"show": True | |
} | |
}] | |
} | |
json.dump(echart, f, ensure_ascii=False, indent = 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment