import pywikibot
import re, time
from dateutil.parser import parse
GREEN = '\u001b[32;1m';MAGENTA = '\u001b[35m';NORM = '\u001b[0m'; UNDERLINE = "\u001B[4m"
RED   = "\u001B[31m";  YELLOW  = "\u001B[33m";BLUE = "\u001B[34m";CYAN      = "\u001B[36m"

def clrs(st):
    for s in [['{lightred}',   '\u001b[31;1m'],
              ['{lightgreen}|{default;lightgreen}', GREEN ],
              ['{aqua}.*\n',   ''            ],  #'\u001b[36;1m'],
              ['\n.{0,4}\n', '\n'],
              ['{default}',    NORM  ],
              ['',''],
              ['\n.*@@[^\n]*@@', ''],
              ['\n.{0,4}\n', '\n'],
              ['\n.{0,4}\n', '\n'],
              ['\n.{0,4}\n', '\n'],
             ]:
      st = re.sub(s[0], s[1], st)
    return st

family = 'commons'

if family == 'commons':
    site = pywikibot.Site(family, family)
else:
    site = pywikibot.Site('en',family)

user = 'A.Savin'
print(GREEN, user, NORM)
i = input( "user >> " )
if i != '':
    user = i
user = pywikibot.User(site, user)

pageregex = ".*"
print(GREEN + pageregex, NORM)
i = input( "pageregex >> " )
if i != '':
    pageregex = i
pageregex = re.compile(pageregex)

clip = "stupid|idiot|thick|piss|fuck|[Hh]arass|[Dd]iva|[Tt]hreat|[Bb]lackmail|Prima |meatball|[Vv]andal"
print(GREEN + clip, NORM)
i = input( "clip >> " )
if i != '':
    clip = i
clip = re.compile(clip)

terminus = "20191201"
print(GREEN + terminus, NORM)
i = input( "terminus >> " )
if i != '':
    terminus = i
terminus = parse(terminus)

'''
Contributions is 
Page, revid, timestamp, comment
'''

count = 0
diffs=[]
for p in user.contributions(total=None):
    count += 1
    (page, revid, timestamp, comment) = p
    if timestamp < terminus:
        print ('Pre quem')
        break
    if re.search(pageregex, page.title()):
        B = {'revid':''}
        A = []
        for r in page.revisions(content=False, endtime = terminus):
            #print( r['timestamp'], timestamp)
            if B['revid'] == revid:
                A = r
                da = r['revid']
                break
            else:
                B = r
                db = r['revid']
        
        if A == []:
            #print("PROBLEM")
            continue
        a = page.getOldVersion(oldid = da)
        b = page.getOldVersion(oldid = db)
        try:
            diff = pywikibot.diff.PatchManager(a,b)
        except Exception as e:
            print(str(e))
            continue
        ddiff = clrs('\n'.join(diff._generate_diff(super_hunk) for super_hunk in diff._super_hunks))
        if re.search(clip, ddiff):
            print(UNDERLINE + "{:05d}".format(count), timestamp.isoformat().split('T')[0], revid , CYAN + page.title(), NORM)
            if family == 'commons':
                url = "https://commons.wikimedia.org/w/index.php?type=revision&oldid={}&diff={}".format(da, db)
            if family == 'wikipedia':
                url = "https://en.wikipedia.org/w/index.php?type=revision&oldid={}&diff={}".format(da, db)
            print(MAGENTA, url, NORM)
            print(ddiff)
            comment = B['comment']
            if len(B['comment'])<2:
                comment = 'diff'
            diffs.append([B['timestamp'], comment, page, url])
    if count > 50000:
        print(RED + "More than 50,000 edits examined", NORM)
        break
for d in diffs:
    print("#", GREEN + d[0].isoformat().split('T')[0], d[0].isoformat().split('T')[1][:5], d[2].title(), NORM + "[{} {}]".format(d[3], d[1])  )
print('-'*80)
 A.Savin 
user >> DiegoAma
.* 
pageregex >> 
stupid|idiot|thick|piss|fuck|[Hh]arass|[Dd]iva|[Tt]hreat|[Bb]lackmail|Prima |meatball|[Vv]andal 
clip >> Dost
20191201 
terminus >> 
argument of type 'NoneType' is not iterable
argument of type 'NoneType' is not iterable
argument of type 'NoneType' is not iterable
00958 2020-01-19 388406608 File:Something12345.jpg 
 https://commons.wikimedia.org/w/index.php?type=revision&oldid=388361793&diff=388406608 
+ {{delete|reason=Lolicon/Shotacon pornography, fails the Dost Test|subpage=File:Something12345.jpg|year=2020|month=January|day=19}}

00962 2020-01-19 388406496 File:Threesomegif.png 
 https://commons.wikimedia.org/w/index.php?type=revision&oldid=388361183&diff=388406496 
+ {{delete|reason=Lolicon pornography, fails the Dost Test|subpage=File:Threesomegif.png|year=2020|month=January|day=19}}

00974 2020-01-18 388388001 File:Meme 123.jpg 
 https://commons.wikimedia.org/w/index.php?type=revision&oldid=388362234&diff=388388001 
+ {{delete|reason=Lolicon pornography, fails the Dost test|subpage=File:Meme 123.jpg|year=2020|month=January|day=18}}

00978 2020-01-18 388383941 File:The Pink Bunny by Mister Theo.png 
 https://commons.wikimedia.org/w/index.php?type=revision&oldid=388352326&diff=388383941 
+ {{delete|reason=Lolicon pornography, fails the Dost test|subpage=File:The Pink Bunny by Mister Theo.png|year=2020|month=January|day=18}}

Pre quem
# 2020-01-19 00:05 File:Something12345.jpg [https://commons.wikimedia.org/w/index.php?type=revision&oldid=388361793&diff=388406608 Nominating for deletion]
# 2020-01-19 00:04 File:Threesomegif.png [https://commons.wikimedia.org/w/index.php?type=revision&oldid=388361183&diff=388406496 Nominating for deletion]
# 2020-01-18 22:05 File:Meme 123.jpg [https://commons.wikimedia.org/w/index.php?type=revision&oldid=388362234&diff=388388001 Nominating for deletion]
# 2020-01-18 21:43 File:The Pink Bunny by Mister Theo.png [https://commons.wikimedia.org/w/index.php?type=revision&oldid=388352326&diff=388383941 Nominating for deletion]
--------------------------------------------------------------------------------