「利用者:芯/サンドボックス/6」の版間の差分
ナビゲーションに移動
検索に移動
細編集の要約なし |
|||
10行目: | 10行目: | ||
import time | import time | ||
import datetime | import datetime | ||
import os | |||
import sys | import sys | ||
S = requests.Session() | S = requests.Session() | ||
URL = "https://ja.wikiwiki.li/api.php" | URL = "https://ja.wikiwiki.li/api.php" | ||
def getSize(page): | def getSize(page): | ||
params = { | |||
"action": "query", | "action": "query", | ||
"prop": "revisions", | "prop": "revisions", | ||
79行目: | 25行目: | ||
"format": "json" | "format": "json" | ||
} | } | ||
data = S.get(url=URL, params=params).json() | |||
return list(data["query"]["pages"].items())[0][1]["revisions"][0]["size"] | |||
params = { | |||
"action": "query", | |||
"list": "logevents", | |||
"leprop": "title|user|timestamp", | |||
"leaction": "create/create", | |||
"lenamespace": 0, | |||
"lelimit": 500, | |||
"format": "json" | |||
} | |||
data = S.get(url=URL, params=params).json() | |||
logEvents = data["query"]["logevents"] | |||
while "continue" in data: | |||
time.sleep(0.5) | |||
params["lecontinue"] = data["continue"]["lecontinue"] | |||
data = S.get(url=URL, params=params).json() | |||
logEvents.append(data["query"]["logevents"]) | |||
logEventsLen = len(logEvents) | |||
f = open("lastCheckedTimestamp.txt", 'w') | |||
f.write(logEvents[0]["timestamp"]) | |||
f.close() | |||
IPUsers = { | IPUsers = { | ||
"172.70.223.86": "ひしょう", "60.145.16.169": "芯" | "172.70.223.86": "ひしょう", "60.145.16.169": "芯" | ||
} | } | ||
byteCntArrsByUser = { | |||
"Yuito": [], | |||
"MediaWiki default": [], | |||
"キュアラプラプ": [], | |||
"Yuito": [], "MediaWiki default": [], "キュアラプラプ": [], "せうゆ": [], | "せうゆ": [], | ||
"Mapilaplap": [], "芯": [], "Popbob": [], "Notorious": [], "しんたろう": [], "Long谷": [], | "Mapilaplap": [], | ||
"神座麟": [], "ケツアゴコロロ": [], "ひしょう": [], "いせ": [], "デデ二オン": [], | "芯": [], | ||
"MagnoliaWoolery": [], "210.151.113.170": [] | "Popbob": [], | ||
"Notorious": [], | |||
"しんたろう": [], | |||
"Long谷": [], | |||
"神座麟": [], | |||
"ケツアゴコロロ": [], | |||
"ひしょう": [], | |||
"いせ": [], | |||
"デデ二オン": [], | |||
"MagnoliaWoolery": [], | |||
"210.151.113.170": [] | |||
} | } | ||
processed = 0 | processed = 0 | ||
for | for le in logEvents: | ||
creator = | creator = le["user"] | ||
if creator not in | if creator not in byteCntArrsByUser: | ||
if creator in IPUsers: | if creator in IPUsers: | ||
creator = IPUsers[creator] | creator = IPUsers[creator] | ||
else: | else: | ||
raise Exception("An unknown IP user.") | raise Exception("An unknown IP user.") | ||
byteCntArrsByUser[creator].append(getSize(le["title"])) | |||
time.sleep(0.5) | |||
processed += 1 | processed += 1 | ||
print("\ | print("\rlogEvents to byteCntArrsByUser: {:.2f}%".format( | ||
processed / logEventsLen * 100), end="") | |||
minMaxTtlAvg = {} | minMaxTtlAvg = {} | ||
for | for user, data in byteCntArrsByUser.items(): | ||
if data: | if data: | ||
ttl = sum(data) | ttl = sum(data) | ||
avg = round(ttl/len(data),2) | avg = round(ttl/len(data), 2) | ||
sortedData = sorted(data) | sortedData = sorted(data) | ||
min, max = sortedData[0], sortedData[-1] | |||
minMaxTtlAvg[ | minMaxTtlAvg[user] = [ | ||
str(min) + " バイト", | |||
str(max) + " バイト", | |||
str(ttl) + " バイト", | |||
str(avg) + " バイト" | |||
] | ] | ||
else: | else: | ||
minMaxTtlAvg[ | minMaxTtlAvg[user] = None | ||
wTable = "{| class=\"wikitable sortable\" style=\"text-align: right; | wTable = "{| class=\"wikitable sortable\" style=\"text-align: right;\"\n! 利用者名 !! style=\"width:7em;\" | 最小値 !! style=\"width:7em;\" | 最大値 !! style=\"width:7em;\" | 合計値 !! style=\"width:7em;\" | 平均値" | ||
for user, data in minMaxTtlAvg.items(): | |||
for | wTable += "\n|-\n! [[利用者:" + user + "|" + user + "]]" | ||
wTable += "\n|-\n! [[利用者:" + | |||
if data: | if data: | ||
wTable += "\n| " + data[0] + "\n| " + data[1] + "\n| " + data[2] + "\n| " + data[3] | wTable += "\n| " + data[0] + "\n| " + \ | ||
data[1] + "\n| " + data[2] + "\n| " + data[3] | |||
else: | else: | ||
for i in range(4): | for i in range(4): | ||
144行目: | 115行目: | ||
now = datetime.datetime.now() | now = datetime.datetime.now() | ||
revisionNote = "最終更新日時: " + str(now.year) + "/" + str(now.month) + "/" + str( | |||
now.day) + "/" + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + " 頃" | |||
wText = "==利用者別バイト数データ==\n本節では、各々の利用者が作成してきた標準記事(リダイレクトを除く)のバイト数について、その代表値を表示する。ソース: [[利用者:芯/サンドボックス/6#利用者別バイト数データ]]\n" | wText = "==利用者別バイト数データ==\n本節では、各々の利用者が作成してきた標準記事(リダイレクトを除く)のバイト数について、その代表値を表示する。ソース: [[利用者:芯/サンドボックス/6#利用者別バイト数データ]]\n" + \ | ||
wTable + "\n" + revisionNote | |||
params = { | |||
"action": "query", | "action": "query", | ||
"meta": "tokens", | "meta": "tokens", | ||
155行目: | 127行目: | ||
"format": "json" | "format": "json" | ||
} | } | ||
LOGIN_TOKEN = S.get(url=URL, params=params).json()[ | |||
"query"]["tokens"]["logintoken"] | |||
params = { | |||
"action": "login", | "action": "login", | ||
"lgname": | "lgname": "芯@bot芯", | ||
"lgpassword": | "lgpassword": os.environ["BotPass"], | ||
"lgtoken": LOGIN_TOKEN, | "lgtoken": LOGIN_TOKEN, | ||
"format": "json" | "format": "json" | ||
} | } | ||
if S.post(URL, data=params).json()["login"]["result"] == "Failed": | |||
choice = input( | |||
"\rLogin faild. Still continue with your IP? [y/N]: ").lower() | |||
choice = input("\rLogin faild. Still continue with your IP? [y/N]: ").lower() | |||
if choice in ["yes", "ye", "y"]: | if choice in ["yes", "ye", "y"]: | ||
pass | pass | ||
elif choice in ["no", "n"]: | elif choice in ["no", "n"]: | ||
print(" | print("\nProcessing has been aborted.") | ||
sys.exit() | sys.exit() | ||
params = { | |||
"action": "query", | "action": "query", | ||
"meta": "tokens", | "meta": "tokens", | ||
"format": "json" | "format": "json" | ||
} | } | ||
CSRF_TOKEN = S.get(url=URL, params=params).json()[ | |||
"query"]["tokens"]["csrftoken"] | |||
params = { | |||
"action": "edit", | "action": "edit", | ||
"title": "麻薬:データ", | "title": "麻薬:データ", | ||
"section":1, | "section": 1, | ||
"text": wText, | "text": wText, | ||
"summary": "利用者別バイト数データを更新", | "summary": "利用者別バイト数データを更新", | ||
"bot": True, | |||
"token": CSRF_TOKEN, | "token": CSRF_TOKEN, | ||
"format": "json" | "format": "json" | ||
} | } | ||
print("\n" + str(S.post(URL, data=params).json())) | |||
</pre>}} | </pre>}} |
3年6月20日 (K) 00:30時点における版
利用者:芯/サンドボックス/6は、ソースコードを公開するページです。
API リクエスト
利用者別バイト数データ
利用者別に集計された執筆バイト数のデータを表で出力するリクエストです。最新のデータを確認したいときは、ソースコードの 153 行めと 154 行めを修正したうえで、これを Python ファイルとして実行してください。
ソースコード |
import requests import time import datetime import os import sys S = requests.Session() URL = "https://ja.wikiwiki.li/api.php" def getSize(page): params = { "action": "query", "prop": "revisions", "titles": page, "rvprop": "size", "format": "json" } data = S.get(url=URL, params=params).json() return list(data["query"]["pages"].items())[0][1]["revisions"][0]["size"] params = { "action": "query", "list": "logevents", "leprop": "title|user|timestamp", "leaction": "create/create", "lenamespace": 0, "lelimit": 500, "format": "json" } data = S.get(url=URL, params=params).json() logEvents = data["query"]["logevents"] while "continue" in data: time.sleep(0.5) params["lecontinue"] = data["continue"]["lecontinue"] data = S.get(url=URL, params=params).json() logEvents.append(data["query"]["logevents"]) logEventsLen = len(logEvents) f = open("lastCheckedTimestamp.txt", 'w') f.write(logEvents[0]["timestamp"]) f.close() IPUsers = { "172.70.223.86": "ひしょう", "60.145.16.169": "芯" } byteCntArrsByUser = { "Yuito": [], "MediaWiki default": [], "キュアラプラプ": [], "せうゆ": [], "Mapilaplap": [], "芯": [], "Popbob": [], "Notorious": [], "しんたろう": [], "Long谷": [], "神座麟": [], "ケツアゴコロロ": [], "ひしょう": [], "いせ": [], "デデ二オン": [], "MagnoliaWoolery": [], "210.151.113.170": [] } processed = 0 for le in logEvents: creator = le["user"] if creator not in byteCntArrsByUser: if creator in IPUsers: creator = IPUsers[creator] else: raise Exception("An unknown IP user.") byteCntArrsByUser[creator].append(getSize(le["title"])) time.sleep(0.5) processed += 1 print("\rlogEvents to byteCntArrsByUser: {:.2f}%".format( processed / logEventsLen * 100), end="") minMaxTtlAvg = {} for user, data in byteCntArrsByUser.items(): if data: ttl = sum(data) avg = round(ttl/len(data), 2) sortedData = sorted(data) min, max = sortedData[0], sortedData[-1] minMaxTtlAvg[user] = [ str(min) + " バイト", str(max) + " バイト", str(ttl) + " バイト", str(avg) + " バイト" ] else: minMaxTtlAvg[user] = None wTable = "{| class=\"wikitable sortable\" style=\"text-align: right;\"\n! 利用者名 !! style=\"width:7em;\" | 最小値 !! style=\"width:7em;\" | 最大値 !! style=\"width:7em;\" | 合計値 !! style=\"width:7em;\" | 平均値" for user, data in minMaxTtlAvg.items(): wTable += "\n|-\n! [[利用者:" + user + "|" + user + "]]" if data: wTable += "\n| " + data[0] + "\n| " + \ data[1] + "\n| " + data[2] + "\n| " + data[3] else: for i in range(4): wTable += "\n| style=\"text-align: center;\" | -" wTable += "\n|}" now = datetime.datetime.now() revisionNote = "最終更新日時: " + str(now.year) + "/" + str(now.month) + "/" + str( now.day) + "/" + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + " 頃" wText = "==利用者別バイト数データ==\n本節では、各々の利用者が作成してきた標準記事(リダイレクトを除く)のバイト数について、その代表値を表示する。ソース: [[利用者:芯/サンドボックス/6#利用者別バイト数データ]]\n" + \ wTable + "\n" + revisionNote params = { "action": "query", "meta": "tokens", "type": "login", "format": "json" } LOGIN_TOKEN = S.get(url=URL, params=params).json()[ "query"]["tokens"]["logintoken"] params = { "action": "login", "lgname": "芯@bot芯", "lgpassword": os.environ["BotPass"], "lgtoken": LOGIN_TOKEN, "format": "json" } if S.post(URL, data=params).json()["login"]["result"] == "Failed": choice = input( "\rLogin faild. Still continue with your IP? [y/N]: ").lower() if choice in ["yes", "ye", "y"]: pass elif choice in ["no", "n"]: print("\nProcessing has been aborted.") sys.exit() params = { "action": "query", "meta": "tokens", "format": "json" } CSRF_TOKEN = S.get(url=URL, params=params).json()[ "query"]["tokens"]["csrftoken"] params = { "action": "edit", "title": "麻薬:データ", "section": 1, "text": wText, "summary": "利用者別バイト数データを更新", "bot": True, "token": CSRF_TOKEN, "format": "json" } print("\n" + str(S.post(URL, data=params).json())) |