import pywikibot
import re
import os
import pymysql
site = pywikibot.Site('zh', 'wikipedia')
site.login()
cat = pywikibot.Page(site, "Category:缺少文件的条目")
cnt = 0
for page in site.categorymembers(cat):
    print(page.title())
    text = page.text
    summary = []
    for image in page.imagelinks():
        if not image.exists():
            try:
                if image.fileIsShared():
                    continue
            except:
                pass
            
            imagename = image.title(with_ns=False)
            
            imageregex = "[" + imagename[0].upper() + imagename[0].lower() + "]" + re.escape(imagename[1:])
            imageregex = imageregex.replace("\ ", "[ _]")
            imageregex = imageregex
            
            existonen = pywikibot.Page(site, "en:File:{}".format(imagename)).exists()
            
            if not existonen:
                print("File:{} not exist".format(imagename))
            
                regex = r"^(\s*\|\s*(?:image|logo|map_image)\s*=\s*(?:(File|Image):)?)"+imageregex+"\s*$"
                text = re.sub(regex, r"\1", text, flags=re.M)

                regex = r"\[\[(File|Image):" + imageregex + r"\s*\|[^\]]+\]\](?:\n(?!\s*\|))?"
                text = re.sub(regex, r"", text)
                
                summary.append("移除不存在檔案 File:{}".format(imagename))
            else:
                print("File:{} exist on en".format(imagename))
                
                regex = r"^(\s*\|\s*(?:image|logo|map_image)\s*=\s*)((?:File:)?"+imageregex+"\s*)$"
                text = re.sub(regex, r"\1<!-- \2 ,可於英文維基百科取得 -->", text, flags=re.M)

                regex = r"(\[\[File:" + imageregex + r"\s*\|[^\]]+\]\])"
                text = re.sub(regex, r"<!-- \1 ,可於英文維基百科取得 -->", text)
                
                summary.append("注釋掉檔案 File:{0} ,不存在於本地,可於[[en:File:{0}|英文維基百科]]取得".format(imagename))
    if page.text == text:
        print("nothing changed")
        continue
    pywikibot.showDiff(page.text, text)
    summary = ";".join(summary)
    print("summary = {}".format(summary))
    save = input("save?")
    if save in ["Yes", "yes", "Y", "y"]:
        page.text = text
        page.save(summary=summary, minor=False)
    cnt += 1
    if cnt > 10:
        break
VERBOSE:pywiki:Found 1 wikipedia:zh processes running, including this one.
1920年美国总统选举
VERBOSE:pywiki:Found 1 wikipedia:en processes running, including this one.
File:Edwardedwards.jpg exist on en
File:Gilbert Monell Hitchcock.jpg not exist
nothing changed
1928年美国总统选举
File:P000420.jpg not exist
nothing changed
1935年-1940年苏联军衔
File:Артюх Владимир Кузьмич.jpg not exist
File:Попов Николай Захарович.jpg not exist
File:Просолов Иван Васильевич.jpg not exist
File:Пятикоп Михаил Евгеньевич.jpg not exist
File:Флёров Иван Андреевич.jpg not exist
File:Ягунов, Павел Максимович.jpg not exist
nothing changed
1955年英國大選
@@ -18 +18 @@ ***
- | image1 = [[File:Sir Anthony-Eden number 10 Official.jpg|160x160px]] ***
+ | image1 =  ***

INFO:pywiki:{aqua}@@ -18 +18 @@{default}
{lightred}- | image1 = [[File:Sir Anthony-Eden number 10 Official.jpg|160x160px]]
{default}{lightgreen}+ | image1 = 
{default}
File:Sir Anthony-Eden number 10 Official.jpg not exist
summary = 移除不存在檔案 File:Sir Anthony-Eden number 10 Official.jpg
save?y
Page [[zh:1955年英國大選]] saved
INFO:pywiki:Page [[zh:1955年英國大選]] saved
1958年國際足協世界盃
@@ -11 +11 @@ ***
- | image           = 1958 Football World Cup poster.jpg ***
+ | image           = <!-- 1958 Football World Cup poster.jpg ,可於英文維基百科取得 --> ***

INFO:pywiki:{aqua}@@ -11 +11 @@{default}
{lightred}- | image           = 1958 Football World Cup poster.jpg
{default}{lightgreen}+{default} | image           ={default;lightgreen} <!--{default} 1958 Football World Cup poster.jpg{default;lightgreen} ,可於英文維基百科取得 -->{default}

File:1958 Football World Cup poster.jpg exist on en
summary = 注釋掉檔案 File:1958 Football World Cup poster.jpg ,不存在於本地,可於[[en:File:1958 Football World Cup poster.jpg|英文維基百科]]取得
save?y
Sleeping for 5.1 seconds, 2018-10-28 12:33:43
INFO:pywiki:Sleeping for 5.1 seconds, 2018-10-28 12:33:43
Page [[zh:1958年國際足協世界盃]] saved
INFO:pywiki:Page [[zh:1958年國際足協世界盃]] saved
1959年東南亞半島運動會
File:1st seap games.png exist on en
nothing changed
1959年英國大選
@@ -18 +18 @@ ***
- | image1 = [[File:Harold Macmillan number 10 official.jpg|160x160px]] ***
+ | image1 =  ***
 ***
@@ -44 +44 @@ ***
- | image3 = [[File:Jo Grimond.jpg|160x160px]] ***
+ | image3 = <!-- [[File:Jo Grimond.jpg|160x160px]] ,可於英文維基百科取得 --> ***

INFO:pywiki:{aqua}@@ -18 +18 @@{default}
{lightred}- | image1 = [[File:Harold Macmillan number 10 official.jpg|160x160px]]
{default}{lightgreen}+ | image1 = 
{default}
{aqua}@@ -44 +44 @@{default}
{lightred}- | image3 = [[File:Jo Grimond.jpg|160x160px]]
{default}{lightgreen}+{default} | image3 ={default;lightgreen} <!--{default} [[File:Jo Grimond.jpg|160x160px]]{default;lightgreen} ,可於英文維基百科取得 -->{default}

File:Harold Macmillan number 10 official.jpg not exist
File:Jo Grimond.jpg exist on en
summary = 移除不存在檔案 File:Harold Macmillan number 10 official.jpg;注釋掉檔案 File:Jo Grimond.jpg ,不存在於本地,可於[[en:File:Jo Grimond.jpg|英文維基百科]]取得
save?y
Page [[zh:1959年英國大選]] saved
INFO:pywiki:Page [[zh:1959年英國大選]] saved
1964年英國大選
File:Harold Wilson Number 10 official.jpg not exist
File:Jo Grimond.jpg exist on en
@@ -18 +18 @@ ***
- | image1 = [[File:Harold Wilson Number 10 official.jpg|160x160px]] ***
+ | image1 =  ***
 ***
@@ -44 +44 @@ ***
- | image3 = [[File:Jo Grimond.jpg|160x160px]] ***
+ | image3 = <!-- [[File:Jo Grimond.jpg|160x160px]] ,可於英文維基百科取得 --> ***

INFO:pywiki:{aqua}@@ -18 +18 @@{default}
{lightred}- | image1 = [[File:Harold Wilson Number 10 official.jpg|160x160px]]
{default}{lightgreen}+ | image1 = 
{default}
{aqua}@@ -44 +44 @@{default}
{lightred}- | image3 = [[File:Jo Grimond.jpg|160x160px]]
{default}{lightgreen}+{default} | image3 ={default;lightgreen} <!--{default} [[File:Jo Grimond.jpg|160x160px]]{default;lightgreen} ,可於英文維基百科取得 -->{default}

summary = 移除不存在檔案 File:Harold Wilson Number 10 official.jpg;注釋掉檔案 File:Jo Grimond.jpg ,不存在於本地,可於[[en:File:Jo Grimond.jpg|英文維基百科]]取得
save?y
Page [[zh:1964年英國大選]] saved
INFO:pywiki:Page [[zh:1964年英國大選]] saved
1965年東南亞半島運動會
File:3rd seap games.png exist on en
nothing changed
1966年英國大選
@@ -18 +18 @@ ***
- | image1 =[[File:Harold Wilson Number 10 official.jpg|160x160px]] ***
+ | image1 = ***
 ***
@@ -43 +43 @@ ***
- | image3 = [[File:Jo Grimond.jpg|108px]] ***
+ | image3 = <!-- [[File:Jo Grimond.jpg|108px]] ,可於英文維基百科取得 --> ***

INFO:pywiki:{aqua}@@ -18 +18 @@{default}
{lightred}- | image1 =[[File:Harold Wilson Number 10 official.jpg|160x160px]]
{default}{lightgreen}+ | image1 =
{default}
{aqua}@@ -43 +43 @@{default}
{lightred}- | image3 = [[File:Jo Grimond.jpg|108px]]
{default}{lightgreen}+{default} | image3 ={default;lightgreen} <!--{default} [[File:Jo Grimond.jpg|108px]]{default;lightgreen} ,可於英文維基百科取得 -->{default}

File:Harold Wilson Number 10 official.jpg not exist
File:Jo Grimond.jpg exist on en
summary = 移除不存在檔案 File:Harold Wilson Number 10 official.jpg;注釋掉檔案 File:Jo Grimond.jpg ,不存在於本地,可於[[en:File:Jo Grimond.jpg|英文維基百科]]取得
save?y
Page [[zh:1966年英國大選]] saved
INFO:pywiki:Page [[zh:1966年英國大選]] saved
1970年英國大選
@@ -35 +35 @@ ***
- | image2 =[[File:Harold Wilson Number 10 official.jpg|160x160px]] ***
+ | image2 = ***

INFO:pywiki:{aqua}@@ -35 +35 @@{default}
{lightred}- | image2 =[[File:Harold Wilson Number 10 official.jpg|160x160px]]
{default}{lightgreen}+ | image2 =
{default}
File:Harold Wilson Number 10 official.jpg not exist
summary = 移除不存在檔案 File:Harold Wilson Number 10 official.jpg
save?y
Sleeping for 4.2 seconds, 2018-10-28 12:35:36
INFO:pywiki:Sleeping for 4.2 seconds, 2018-10-28 12:35:36
Page [[zh:1970年英國大選]] saved
INFO:pywiki:Page [[zh:1970年英國大選]] saved
1973年東南亞半島運動會
File:7th seap games.png exist on en
nothing changed
1974年10月英國大選
@@ -18 +18 @@ ***
- | image1             = [[File:Harold Wilson Number 10 official.jpg|160x160px]] ***
+ | image1             =  ***

INFO:pywiki:{aqua}@@ -18 +18 @@{default}
{lightred}- | image1             = [[File:Harold Wilson Number 10 official.jpg|160x160px]]
{default}{lightgreen}+ | image1             = 
{default}
File:Harold Wilson Number 10 official.jpg not exist
summary = 移除不存在檔案 File:Harold Wilson Number 10 official.jpg
save?y
Page [[zh:1974年10月英國大選]] saved
INFO:pywiki:Page [[zh:1974年10月英國大選]] saved
1974年2月英國大選
@@ -18 +18 @@ ***
- | image1             = [[File:Harold Wilson Number 10 official.jpg|160x160px]] ***
+ | image1             =  ***

INFO:pywiki:{aqua}@@ -18 +18 @@{default}
{lightred}- | image1             = [[File:Harold Wilson Number 10 official.jpg|160x160px]]
{default}{lightgreen}+ | image1             = 
{default}
File:Harold Wilson Number 10 official.jpg not exist
summary = 移除不存在檔案 File:Harold Wilson Number 10 official.jpg