사용자:하늘/양식

< 사용자:하늘
하늘 (토론 | 기여)님의 2021년 11월 24일 (수) 00:00 판 (Cerulean님이 사용자:Cerulean/소스/cwb 문서를 넘겨주기를 만들지 않고 사용자:Cerulean/소스/ceruleanbot 문서로 이동했습니다)

ceruleanbot

https://www.geeksforgeeks.org/how-to-use-html-in-tkinter-python/

import requests
S = requests.Session()

class wiki:
	def __init__(self, URL: str):
		self.url = URL
		self.csrftoken = False

	def page(self, pages: str):
		return page(self.url, pages, self.csrftoken)

	def get_csrftoken(self):
		PARAMS = {"action": "query", "meta": "tokens", "format": "json"}
		DATA = S.get(url=self.url, params=PARAMS).json()
		self.csrftoken = DATA['query']['tokens']['csrftoken']

	def login(self, name: str, password: str):
		# Step 1: Retrieve login token first
		PARAMS = {
			'action': "query",
			'meta': "tokens",
			'type': "login",
			'format': "json"
		}
		DATA = 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 = S.post(self.url, data=PARAMS).json()
		if DATA["login"]["result"] == "Success":
			print("로그인 성공")
			self.get_csrftoken()
		else:
			print("로그인 실패")
			exit()

	def logout(self):
		DATA = S.post(self.url, data={"action": "logout",
									  "token": self.csrftoken, "format": "json"}).json()
		print("로그아웃")
		print(DATA)

	def pagesInCategory(self, title: str, namespace:str=''):
		PARAMS = {
			"action": "query",
			"format": "json",
			"list": "categorymembers",
			"utf8": 1,
			"cmnamespace": namespace,
			"cmtitle": title,
			"cmprop": "title|timestamp",
			"cmlimit": "max",
			"cmsort": "timestamp",
		}
		DATA = S.get(url=self.url, params=PARAMS).json()
		return DATA['query']['categorymembers']

	def editpage(self, title: str, TEXT: str, summary: str='', namespace=''):
		print(namespace+title+' 편집')
		PARAMS = {
			"action": "edit",
			"format": "json",
			"title": namespace+title,
			"text": TEXT,
			"summary": summary,
			"token": self.csrftoken,
			"utf8": 1,
			"formatversion": "latest",
			"bot": "true"
		}
		DATA = S.post(url=self.url, data=PARAMS).json()
		print(DATA)

	def removecategory(self):
		''
class page:
	def __init__(self, url, title, tokens):
		self.url = url
		self.title = title
		self.csrftoken = tokens
		self.get()
	def get(self):
		PARAMS = {
			"action": "query",
			"format": "json",
			"prop": "revisions",
			"formatversion": "2",
			"rvprop": "content",
			"rvslots": "*",
			"titles": self.title
		}
		DATA = S.get(url= self.url, params=PARAMS).json()
		self.cont = DATA['query']['pages'][0]['revisions'][0]['slots']['main']['content']

	def print(self):
		print(self.cont)

	def remove(self, toremove: str):
		self.cont = self.cont.replace(toremove, '')

	def replace(self, toreplace: str, replace: str) -> None:
		self.cont = self.cont.replace(toreplace, replace)

	def save(self, summary:str =''):
		if self.csrftoken == False:
			print('csrftoken is ungetted')
			return
		print(self.title+' 편집')
		PARAMS = {
			"action": "edit",
			"format": "json",
			"title": self.title,
			"text": self.cont,
			"summary": summary,
			"token": self.csrftoken,
			"utf8": 1,
			"formatversion": "latest",
			"bot": "true"
		}
		DATA = S.post(url=self.url, data=PARAMS).json()
		print(DATA)

위의 봇과 통합 예정 GUI로도 CLI로도 사용할 수 있게

import ceruleanbot
import time
import requests
import tkinter

window = tkinter.Tk()
window.title("cerulwikibot")

S = requests.Session()
libre = cerulwikibot.wiki('https://librewiki.net/api.php')

def searchdoc():
	title = entry.get()
	if title == '' :
		label.configure(text='?')
		text.delete(1.0, tkinter.END)
		text.insert(1.0, '?')
	else:
		label.configure(text=title)
		p = libre.page(title)
		p.get()
		text.delete(1.0, tkinter.END)
		text.insert(1.0, p.cont)

label = tkinter.Label(window, text = '문서 제목 입력하고 소스보기', height=2)
label.pack()

text = tkinter.Text(window)
text.focus()
text.pack()

entry = tkinter.Entry(window, width = 50)
entry.pack()

viewsource = tkinter.Button(window, text='소스 보기', command=searchdoc)
viewsource.pack(pady=0)

savebutton = tkinter.Button(window, text='편집 저장', command=searchdoc) #미구현
savebutton.pack(pady=0)

window.mainloop()