편집 전 과거 문서의 위키텍스트 (old_wikitext) | '<syntaxhighlight lang="python">
import requests
import datetime
import os
import sys
import re
import time
S = requests.Session()
URL = "https://librewiki.net/api.php"
class bot:
def __init__(self):
self.S = requests.Session()
self.csrftoken = ''
self.url = "https://librewiki.net/api.php"
def get_csrftoken(self):
PARAMS = {"action": "query", "meta": "tokens", "format": "json"}
DATA = self.S.get(url=self.url, params=PARAMS).json()
self.csrftoken = DATA['query']['tokens']['csrftoken']
def login(self, name, password):
# Step 1: Retrieve login token first
PARAMS = {
'action': "query",
'meta': "tokens",
'type': "login",
'format': "json"
}
DATA = self.S.get(url=self.url, params=PARAMS).json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
# Step 2: Send a POST request to login. Using the main account for login is not
# supported. Obtain credentials via Special:BotPasswords
# 봇 비밀번호 설정 권장, 형식 : Caeruleum@asdfqwerzxcvpoiulkjhmnbv
PARAMS = {
"action": "login",
"format": "json",
"lgname": name,
"lgpassword": password,
"lgtoken": LOGIN_TOKEN,
"utf8": 1
}
DATA = self.S.post(self.url, data=PARAMS).json()
if DATA["login"]["result"] == "Success":
print("로그인 성공")
self.get_csrftoken()
else:
print("로그인 실패")
exit()
def logout(self):
DATA = self.S.post(self.url, data={"action": "logout",
"token": self.csrftoken, "format": "json"}).json()
print("로그아웃")
print(DATA)
def download(url, file_name):
with open(file_name, "wb") as file:
response = requests.get(url)
file.write(response.content)
# OS = platform.system()
def GW_download(folder, hanja, lang, fontType):
if lang == 'j':
fontType = fontType + '-일본'
elif lang == 'g':
fontType = fontType + '-중국'
elif lang == 't':
fontType = fontType + '-대만'
elif lang == 'h':
fontType = fontType + '-홍콩'
elif lang == 'v':
fontType = fontType + '-베트남'
charcode = hex(ord(hanja)).replace('0x', 'u') +'-'+lang
filename = hanja + '-' + fontType + '.svg'
GW_query_URL = "https://glyphwiki.org/json?name="+charcode
GW_download_URL = "https://glyphwiki.org/glyph/"+charcode + '.svg'
R = bot.S.get(GW_query_URL).json()
if R['version'] != None:
print(charcode)
download(GW_download_URL, folder + '/' + filename)
else:
print(charcode + ' 없음')
def api_upload(source, filename, category, licence): # 파일 소스, 파일 이름, 분류, 라이선스
#수동으로 파일을 확인해야 하기 때문에 api 업로드하지 않음
uploadPARAMS = {
"action": "upload",
"filename": filename,
"format": "json",
# PD-author # 한자/문자/명조
"text": "== 파일의 설명 ==\n{{파일 정보\n|설명 = \n|출처 = 글리프위키\n|날짜 = \n|만든이 = \n|기타 = \n}}\n[[분류:"+category+"]]\n== 라이선스 ==\n{{"+licence+"}}",
"async": 1,
"token": bot.csrftoken,
"ignorewarnings": 1
}
FILE = {'file': (filename, open(source, 'rb'), 'multipart/form-data')}
DATA = S.post(URL, files=FILE, data=uploadPARAMS).json()
print(DATA)
def GW_upload(folder, hanja, fontType):
filename = hanja + '-' + fontType + '.svg'
GW_download(folder, hanja, fontType)
time.sleep(10)
api_upload(folder + '/' + filename, filename,
'한자/문자/'+fontType, 'PD-author')
bot = bot()
#bot.login("Caeruleum", "Caeruleum@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
currentDate = str(datetime.datetime.now())
currentDate = currentDate.split('.')[0]
temp = open('date.txt', 'r')
preDate = temp.readline().replace('-', ':')
temp.close()
print(preDate)
date_file = open('date.txt', 'w')
date_file.write(currentDate)
date_file.close()
if not(os.path.isdir(currentDate)):
os.makedirs(os.path.join(currentDate))
PARAMS = {
"action": "query",
"format": "json",
"list": "categorymembers",
"utf8": 1,
"cmnamespace": "1600",
"cmtitle": "분류:한자/문자",
"cmprop": "title|timestamp",
"cmtype": "page",
"cmlimit": "max",
"cmsort": "timestamp",
#"cmstart": preDate
}
DATA = S.get(url=URL, params=PARAMS).json()
PAGES = DATA['query']['categorymembers']
hanjaList = open(currentDate + '/list.csv', 'a')
for page in PAGES:
hanja = page['title'].split('/')[1] + '\n'
hanjaList.write(hanja)
hanjaList.close()
hanjaList = open(currentDate + '/list.csv', 'r')
while True:
hanja = hanjaList.readline().replace('\n', '')
if not hanja:
break
PARAMS = {
"action": "query",
"format": "json",
"prop": "revisions",
"formatversion": "2",
"rvprop": "content",
"rvslots": "*",
"titles": '시리즈:리브레 한자사전/' + hanja
}
DATA = S.get(url=URL, params=PARAMS).json()
PAGE = DATA['query']['pages'][0]['revisions'][0]['slots']['main']['content']
hanja = re.search('한자 =(.+?)\n', PAGE).group(1).strip()
print('------------------')
print(hanja)
japanHanja = re.search('신자체 =(.+?)\n', PAGE)
chineseHanja = re.search('간체자 =(.+?)\n', PAGE)
fontType = '명조'
GW_download(currentDate, hanja, 'k', fontType)
GW_download(currentDate, hanja, 't', fontType)
GW_download(currentDate, hanja, 'h', fontType)
GW_download(currentDate, hanja, 'v', fontType)
if japanHanja == None:
japanHanja = hanja
else:
japanHanja = japanHanja.group(1).strip()
if japanHanja == "":
japanHanja = hanja
if chineseHanja == None:
chineseHanja = hanja
else:
chineseHanja = chineseHanja.group(1).strip()
if chineseHanja == "":
chineseHanja = hanja
GW_download(currentDate, japanHanja, 'j', fontType)
GW_download(currentDate, chineseHanja, 'g', fontType)
#bot.logout()
</syntaxhighlight>' |
편집 전후의 차이 (edit_diff) | '@@ -1,195 +1,1 @@
-<syntaxhighlight lang="python">
-import requests
-import datetime
-import os
-import sys
-import re
-import time
-
-S = requests.Session()
-URL = "https://librewiki.net/api.php"
-
-class bot:
- def __init__(self):
- self.S = requests.Session()
- self.csrftoken = ''
- self.url = "https://librewiki.net/api.php"
-
- def get_csrftoken(self):
- PARAMS = {"action": "query", "meta": "tokens", "format": "json"}
- DATA = self.S.get(url=self.url, params=PARAMS).json()
- self.csrftoken = DATA['query']['tokens']['csrftoken']
-
- def login(self, name, password):
- # Step 1: Retrieve login token first
- PARAMS = {
- 'action': "query",
- 'meta': "tokens",
- 'type': "login",
- 'format': "json"
- }
- DATA = self.S.get(url=self.url, params=PARAMS).json()
- LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
- # Step 2: Send a POST request to login. Using the main account for login is not
- # supported. Obtain credentials via Special:BotPasswords
- # 봇 비밀번호 설정 권장, 형식 : Caeruleum@asdfqwerzxcvpoiulkjhmnbv
- PARAMS = {
- "action": "login",
- "format": "json",
- "lgname": name,
- "lgpassword": password,
- "lgtoken": LOGIN_TOKEN,
- "utf8": 1
- }
- DATA = self.S.post(self.url, data=PARAMS).json()
- if DATA["login"]["result"] == "Success":
- print("로그인 성공")
- self.get_csrftoken()
- else:
- print("로그인 실패")
- exit()
-
- def logout(self):
- DATA = self.S.post(self.url, data={"action": "logout",
- "token": self.csrftoken, "format": "json"}).json()
- print("로그아웃")
- print(DATA)
-
-
-def download(url, file_name):
- with open(file_name, "wb") as file:
- response = requests.get(url)
- file.write(response.content)
-# OS = platform.system()
-
-
-def GW_download(folder, hanja, lang, fontType):
- if lang == 'j':
- fontType = fontType + '-일본'
- elif lang == 'g':
- fontType = fontType + '-중국'
- elif lang == 't':
- fontType = fontType + '-대만'
- elif lang == 'h':
- fontType = fontType + '-홍콩'
- elif lang == 'v':
- fontType = fontType + '-베트남'
- charcode = hex(ord(hanja)).replace('0x', 'u') +'-'+lang
- filename = hanja + '-' + fontType + '.svg'
- GW_query_URL = "https://glyphwiki.org/json?name="+charcode
- GW_download_URL = "https://glyphwiki.org/glyph/"+charcode + '.svg'
- R = bot.S.get(GW_query_URL).json()
- if R['version'] != None:
- print(charcode)
- download(GW_download_URL, folder + '/' + filename)
- else:
- print(charcode + ' 없음')
-
-
-
-def api_upload(source, filename, category, licence): # 파일 소스, 파일 이름, 분류, 라이선스
- #수동으로 파일을 확인해야 하기 때문에 api 업로드하지 않음
- uploadPARAMS = {
- "action": "upload",
- "filename": filename,
- "format": "json",
- # PD-author # 한자/문자/명조
- "text": "== 파일의 설명 ==\n{{파일 정보\n|설명 = \n|출처 = 글리프위키\n|날짜 = \n|만든이 = \n|기타 = \n}}\n[[분류:"+category+"]]\n== 라이선스 ==\n{{"+licence+"}}",
- "async": 1,
- "token": bot.csrftoken,
- "ignorewarnings": 1
- }
- FILE = {'file': (filename, open(source, 'rb'), 'multipart/form-data')}
- DATA = S.post(URL, files=FILE, data=uploadPARAMS).json()
- print(DATA)
-
-def GW_upload(folder, hanja, fontType):
- filename = hanja + '-' + fontType + '.svg'
- GW_download(folder, hanja, fontType)
- time.sleep(10)
- api_upload(folder + '/' + filename, filename,
- '한자/문자/'+fontType, 'PD-author')
-
-bot = bot()
-#bot.login("Caeruleum", "Caeruleum@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
-
-currentDate = str(datetime.datetime.now())
-currentDate = currentDate.split('.')[0]
-
-temp = open('date.txt', 'r')
-preDate = temp.readline().replace('-', ':')
-temp.close()
-print(preDate)
-date_file = open('date.txt', 'w')
-date_file.write(currentDate)
-date_file.close()
-
-if not(os.path.isdir(currentDate)):
- os.makedirs(os.path.join(currentDate))
-
-PARAMS = {
- "action": "query",
- "format": "json",
- "list": "categorymembers",
- "utf8": 1,
- "cmnamespace": "1600",
- "cmtitle": "분류:한자/문자",
- "cmprop": "title|timestamp",
- "cmtype": "page",
- "cmlimit": "max",
- "cmsort": "timestamp",
- #"cmstart": preDate
-}
-DATA = S.get(url=URL, params=PARAMS).json()
-PAGES = DATA['query']['categorymembers']
-
-hanjaList = open(currentDate + '/list.csv', 'a')
-for page in PAGES:
- hanja = page['title'].split('/')[1] + '\n'
- hanjaList.write(hanja)
-hanjaList.close()
-hanjaList = open(currentDate + '/list.csv', 'r')
-
-while True:
- hanja = hanjaList.readline().replace('\n', '')
- if not hanja:
- break
- PARAMS = {
- "action": "query",
- "format": "json",
- "prop": "revisions",
- "formatversion": "2",
- "rvprop": "content",
- "rvslots": "*",
- "titles": '시리즈:리브레 한자사전/' + hanja
- }
- DATA = S.get(url=URL, params=PARAMS).json()
- PAGE = DATA['query']['pages'][0]['revisions'][0]['slots']['main']['content']
- hanja = re.search('한자 =(.+?)\n', PAGE).group(1).strip()
- print('------------------')
- print(hanja)
- japanHanja = re.search('신자체 =(.+?)\n', PAGE)
- chineseHanja = re.search('간체자 =(.+?)\n', PAGE)
- fontType = '명조'
- GW_download(currentDate, hanja, 'k', fontType)
- GW_download(currentDate, hanja, 't', fontType)
- GW_download(currentDate, hanja, 'h', fontType)
- GW_download(currentDate, hanja, 'v', fontType)
- if japanHanja == None:
- japanHanja = hanja
- else:
- japanHanja = japanHanja.group(1).strip()
- if japanHanja == "":
- japanHanja = hanja
- if chineseHanja == None:
- chineseHanja = hanja
- else:
- chineseHanja = chineseHanja.group(1).strip()
- if chineseHanja == "":
- chineseHanja = hanja
- GW_download(currentDate, japanHanja, 'j', fontType)
- GW_download(currentDate, chineseHanja, 'g', fontType)
-
-#bot.logout()
-
-</syntaxhighlight>
+{{삭제|}}
' |