사용자:Utoleetest/talk closse auto

개요[편집 | 원본 편집]

토론 페이지의 문서들을 확인한 뒤에 한 달 이상 토론이 진행되지 않은 페이지를 확인하고, 각 문단에 {{완료된 토론 시작}} 틀을 부착하는 스크립트입니다..

사용방법[편집 | 원본 편집]

사용하실 때에는 아래의 스크립트를 Pywikibot 디렉토리/scripts/userscripts 디렉토리에서 "talk_close_auto.py"로 저장하신 뒤에 Pywikibot이 있는 디렉토리에서 다음과 같은 코드를 실행시키면 됩니다.

> python pwb.py talk_close_auto

소스[편집 | 원본 편집]

import pywikibot
import re
import time

# 리브레 위키에서 사용하는 토론 이름공간.
talk_namespace_nums = [
    1, # 토론
    # 5, #리브레 위키 토론 - 역시 예외처리해야 할 문서가 너무 많아서 제외
    7, #파일토론
    # 9, #미디어위키토론 - 예외가 너무 많아서 일단 제외
    11, #틀토론
    13, #도움말토론
    15, #분류토론
    829, #모듈토론
    1601 #시리즈토론
]

site = pywikibot.Site('ko', 'libre')

# 편집에서 예외처리할 문서들 - 특이한 구조 덕분에 오류가 발생할 수 있는 문서들 제외.
page_exception = [
    "토론:개신교발 루머",
    "토론:메갈리아",
    "도움말토론:틀",
    "틀토론:알림바",
    "틀토론:ISBN"
]

# 알림바로 사용하는 애들이 있으니 문구 삽입
txt_end_diss = "[[파일:Ledibug-Discussion.png|60px|right]] 이 문단은 완료된 토론으로, 보존중입니다. 특별한 사유가 없는 한 수정하지 말아주십시오."

page_allow = [

]

for namespace in talk_namespace_nums:
    # 이름공간의 모든 페이지
    pages = site.allpages(namespace=namespace, filterredir=False)

    # 각 페이지마다 조사하기
    for page in pages:

        # 마지막 편집 날짜 확인
        last_edit_timestamp = page.latest_revision['timestamp'].timestamp()
        now_timestamp = time.time() - 3600*9 # 시차 반영.

        # 마지막 편집이 있은지 한 달 이상일 때에만 작업 시도
        # 즉 타임스탬프 값이 30일 차이 이상일 때만...
        if now_timestamp - last_edit_timestamp > 86400*30 and page.title() not in page_exception:
            page_contents = page.text
            cnt = []
            titles = re.finditer(r"==([^=]+?)==\n", page_contents) # 등호 2개짜리만 잡을 수 있게 조건 바꾸기

            previous_end = 0
            page_contents_by_div = [] # 나누기

            for title in titles:
                title_start = title.start() # 제목 시작글자 일치
                title_end = title.end() # 제목 끝글자 위치
                # 앞부분 잡기
                page_contents_by_div.append(page_contents[previous_end:title_start])
                # 제목 부분 넣기
                page_contents_by_div.append(title.group())
                # 숫자 넣기
                previous_end = title_end

            #마지막 문단 뒤 내용 넣기
            page_contents_by_div.append(page_contents[previous_end:])



            # 체크 - page_contents_by_div[0]에서 완료된 토론 시작이 없는지...
            if page_contents_by_div[0].find("{{완료된 토론 시작}}") == -1:

                # 완료된 토론 시작/완료된 토론 끝 부분이 없을 때 양끝에 집어넣기
                idx = 0
                temp_included = False # 완료된 토론 시작 안에 있는지 확인

                while idx < len(page_contents_by_div):
                    cont = page_contents_by_div[idx]
                    # 완료된 토론 시작 ~ 완료된 토론 끝 사이에 있음을 보이기
                    if re.search(r"\{\{(틀:)?완료된(\s+)토론(\s)+시작}}", cont) and not re.search(r"\{\{(틀:)?완료된(\s+)토론(\s+)끝}}", cont):
                        temp_included = True

                    # 조건 - 완료된 토론 시작/완료된 토론 끝 틀이 내용에 있거나 참조/본문 틀만 남아있는 경우는 제외한다.
                    if idx%2 == 0 and idx>1 and cont != "" and not re.search(r"\{\{(틀:)?완료된(\s+)토론(\s)+시작}}", cont) and not temp_included \
                            and not re.search(r"\{\{(틀:)?완료된(\s+)토론(\s+)끝}}", cont) and not re.fullmatch(r"(\s|\n)*\{\{(참조|본문)\|.*?}}(\s|\n)*", cont) \
                            and cont.find(txt_end_diss) == -1:
                        # 완료된 토론 표시
                        page_contents_by_div[idx] = f"{{{{완료된 토론 시작}}}}\n{cont}\n{{{{완료된 토론 끝}}}}\n"
                        cnt.append(idx)

                    # 완료된 토론 끝 부분 보이기
                    if temp_included and re.search(r"\{\{(틀:)?완료된(\s+)토론(\s+)끝}}", cont):
                        temp_included = False

                    idx += 1

                # 내용 붙여서 완성
                if len(cnt)>0:
                    page_contents = "".join(page_contents_by_div)
                    # print(page_contents)
                    # 내용 바꾸기
                    page.text = page_contents
                    page.save('봇:오래된 토론에 완료된 토론 표시 추가')
                    # print(page.title(), cnt, len(page_contents_by_div))