利用者:メーデー!:エンペディアの真実と真相/bot
ナビゲーションに移動
検索に移動
#PythonでEnpediaにエスケープ転載する時の要約欄を自動生成するやつ 20200428版
#By. 利用者:メーデー!:エンペディアの真実と真相
#This source code is licensed as Public Domain
import requests
import json
import datetime
print ("Escape Manager")
def contributors(pagename):
"""contributors:執筆者を列記する
引数:(str)ページ名
返り値:要約欄用リンク付き執筆者列記、(int)省略された執筆者の数(0あり)、(int)匿名利用者の数(0あり)
指定記事の最新版までの執筆者を列記。
登録利用者のみに限る。
利用者ページへのリンクを含め380文字を超える場合、省略。後段で「ほかn名」との表記を行う前提
匿名利用者(IPユーザー)はユニークユーザーで数値を返す"""
text = "執筆者:"
otherusersnum = 0
anonusersnum = 0
contributorsapi = requests.get('https://ja.wikipedia.org/w/api.php?format=json&action=query&prop=contributors&pclimit=max&pcexcludegroup=bot&titles=' + pagename).json()
if "-1" not in contributorsapi["query"]["pages"]:
for n in contributorsapi["query"]["pages"]:
for m in contributorsapi["query"]["pages"][n]["contributors"]:
if len(text) < 380:
text = text + "[[jawp:user:" + m["name"] + "]], "
else:
otherusersnum+=1
if "anoncontributors" in contributorsapi["query"]["pages"][n]:
anonusersnum = contributorsapi["query"]["pages"][n]["anoncontributors"]
return text, otherusersnum, anonusersnum
else:
print("error")
def allcontributors(pagename):
"""allcontributors:全ての執筆者を列記する
引数:(str)ページ名
返り値:リンク付き執筆者列記"""
contributors = {}
returntext = "執筆者の一覧(版数上位順):\n"
revcontapi = requests.get('https://ja.wikipedia.org/w/api.php?format=json&action=query&prop=revisions&rvlimit=max&rvprop=user&titles=' + pagename).json()
if "-1" not in revcontapi["query"]["pages"]:
for n in revcontapi["query"]["pages"]:
for m in revcontapi["query"]["pages"][n]["revisions"]:
if m["user"] not in contributors:
contributors[m["user"]] = 1
else:
contributors[m["user"]] += 1
contributors = sorted(contributors.items(), key=lambda x:x[1], reverse=True)
for l in contributors:
returntext = returntext + "* [[jawp:special:contributions/" + l[0] + "|]]\n"
return returntext
else:
print("error")
def revisiontime(pagename, revision):
"""revisiontime:指定された版の更新時刻を返す
引数:(str)ページ名、(int)リビジョン数…1で現在版、2で1コ前、…
返り値:(datetime)更新時刻"""
timetext = ""
revisiontimeapi = requests.get('https://ja.wikipedia.org/w/api.php?format=json&action=query&prop=revisions&rvlimit=' + str(revision) + '&rvprop=timestamp|size&titles=' + pagename).json()
if "-1" not in revisiontimeapi["query"]["pages"]:
for n in revisiontimeapi["query"]["pages"]:
for m in revisiontimeapi["query"]["pages"][n]["revisions"]:
timetext = m["timestamp"]
return datetime.datetime.fromisoformat(timetext.replace('Z', '+00:00'))
else:
print("error")
def redirect_summary(pagename, revision):
"""redirect_summary:リダイレクト化されたページ用の要約欄を生成
引数:(str)ページ名、(int)リビジョン数…詳細はrevisiontime参照
返り値:(str)要約欄テキスト"""
revision = revisiontime(pagename, revision)
text, otherusersnum, anonusersnum = contributors(pagename)
text = "escape, リダイレクト化された[[jawp:" + pagename + "]] " + str(revision.year) + "年" + str(revision.month) + "月" + str(revision.day) + "日" + str(revision.hour) + ":" + str(revision.minute) + " (UTC)版を転記 " + text
if otherusersnum + anonusersnum != 0:
text = text + "ほか"
if otherusersnum != 0:
text = text + str(otherusersnum) + "名の登録利用者"
if anonusersnum != 0:
text = text + "と"
if anonusersnum != 0:
text = text + str(anonusersnum) + "名の匿名利用者"
text = text + " 完全なリストは転記元履歴ページを参照"
if len(text) <= 500:
return text
else:
print("error")
print("転載元ページ名は?")
page = input()
print("リダイレクト化されたページ用の要約:")
print(redirect_summary(page, 1))
print("\n\n削除依頼中のページ用の要約:")
revision = revisiontime(page, 1)
print("[[jawp:" + page + "]]の" + str(revision.year) + "年" + str(revision.month) + "月" + str(revision.day) + "日" + str(revision.hour) + ":" + str(revision.minute) + " (UTC)版 より全文をエスケープ転載。著者一覧: [[トーク:" + page + "/移入元情報]]")
print("\n\n移入元情報を作成しました:")
print("以下、移入元情報を記載する。 この記事は [[jawp:" + page + "]]の" + str(revision.year) + "年" + str(revision.month) + "月" + str(revision.day) + "日" + str(revision.hour) + ":" + str(revision.minute) + " (UTC)版 から転記された。\n\n" + allcontributors(page))
使い方[編集]
クソザコソースコードにつき、セキュリティガン無視・誤動作あり。自己責任。移入元情報ページ/要約欄の自動生成。([1]の要約欄で試用)
前提:requests, json, datetime コマンドプロンプト等からpython (pass)/(filename).pyで起動。
- ソース末尾の
redirectescapewrapper()冒頭の#を外す。sakujoescapewrapper()冒頭に#を付ける。 - コマンドプロンプトから起動すると、転載元ページ名を聞かれるので、jawpでのページ名を入力。
- 生成された要約欄(リダイレクト用・削除依頼中用)移入元情報ページが表示される。ここから必要な物をコピペ。
既知の問題
- 版数の多いページには対応できない