コンテンツにスキップ

利用者:Bcxfubot/テレビ朝日/prog

# URL張替
# http://www.tv-asahi.co.jp/
# ↓
# https://www.tv-asahi.co.jp/
import re
import time
import pywikibot
import requests
from requests.exceptions import Timeout

#target = "http://ameblo.jp/"
target = "http://www.tv-asahi.co.jp/"
#max = 10
#max = 10
max = 1
sleepsec = 60

######################################################
# 処理モード
procmode = 0
#procmode = 1
######################################################

# 指定されたURLの最終的なリダイレクト先を返す。
# エラーの場合は""を返す
def get_final_url(url):
    #response = requests.head(url)
    try:
        response = requests.get(url,timeout=2.0)
    except Timeout:
        return ""
    print(response.status_code)
    print(response.url)
    #print(response.headers)
    if response.status_code != 200:
        return ""
    if re.search("404Error",response.url):
        return ""
    return response.url

# ページを置換する
def replace_page(pagetitle):
    site = pywikibot.Site()
    page = pywikibot.Page(site, pagetitle)
    #text = page.text
    #print(text)
    linelist = page.text.split('\n')
    #print(linelist)

    comment_target_https = ""
    gaibu = 0
    modflag = 0
    outtext = ""
    for line in linelist:
        if re.search("<ref",line):
            continue
        if re.search("web.archive.org",line):
            continue
        if re.search("Wayback",line):
            continue
        #print(gaibu,line)
        #if "==外部リンク" in line:
        #if re.search("==[ ]*外部リンク",line):
        #    gaibu = 1
        #if gaibu == 1:
        if target in line:
            #print(gaibu,line)
            #line = line.replace( target, target_https)
            #print(gaibu,line)
            pattern = r"http://[^ \|\]]+"
            matchedlist = re.findall( pattern, line)
            if matchedlist:
                for url in matchedlist:
                    print( "url=" + url )
                    httpsurl = url.replace( "http:" , "https:")
                    print( "httpsurl=" + httpsurl )
                    if target in url:
                        finalurl = get_final_url( httpsurl ) 
                        print( "finalurl=" + finalurl )
                        if finalurl != "":
                            if url != finalurl:
                                line = line.replace( url, finalurl)
                                comment_target_https = comment_target_https + finalurl
                                print(gaibu,line)
                                modflag = 1
                        
        outtext += line + "\n"

    if modflag == 1:
        page.text = outtext
        #print(page.text)
        if procmode == 1:
            page.save("外部リンクの修正 " + comment_target_https + " ([[Wikipedia:Bot|Bot]]による編集)")

# 処理対象のページ名をひとつ返す
# 処理対象がない場合は""を返す
def get_pagetitle():
    path = "list"
    with open(path) as f:
        for s_line in f:
            s_line = s_line.rstrip("\n")
            #print(s_line)
            #if not re.search(",sumi", s_line):
            if not s_line.endswith(",sumi"):
                return s_line
    return ""

# 処理した行にsumiをつける
def done_pagetitle(pagetitle):
    path = "list"
    alltext = ""
    with open(path) as f:
        for s_line in f:
            s_line = s_line.rstrip("\n")
            #print(s_line + "\n")
            #if re.search(pagetitle, s_line):
            if pagetitle == s_line:
                s_line = s_line + ",sumi"
            alltext += s_line + "\n"
    with open(path, mode='w') as f:
        f.write(alltext)
    return ""

def sub():
    num = 0
    for i in range(max):
        num = num + 1
        pagetitle = get_pagetitle()
        print("[" + str(num) + "/" + str(max) + "]" + ":" + "pagetitle=" + pagetitle)
        if pagetitle == "":
            break
        replace_page(pagetitle)
        done_pagetitle(pagetitle)
        
        if ( i < (max - 1) ):
            print("sleep(" + str(sleepsec) + ")")
            time.sleep(sleepsec)

def main():
    sub()
    print("done.")

if __name__ == '__main__':
    main()