import os
import pymysql
import getpass
pw = getpass.getpass()
········
def connect_to_db():
    """
    Connect to the public enwiki db using quarry.wmflabs.org credentials
    """
    conn = pymysql.connect(
        host=os.environ['MYSQL_HOST'],
        user='u2402',

#         user=os.environ['MYSQL_USERNAME'],
#         password=os.environ['MYSQL_PASSWORD'], #don't print me!
        password=pw,
        
        database='enwiki_p',
        charset='utf8',
        use_unicode=True
    )
    print(conn.user)
    print(conn.host)
    return conn
conn = connect_to_db()
b'u2402'
192.168.0.122
with conn.cursor() as cur:
    cur.execute('use p50380g50728__hostbot_p;') #which database to use
    cur.execute("""select * from td_host_rename_logs;""")
    revs = cur.fetchall()
for r in revs:
    print(r)
(48448382, b'renameuser', b'renameuser', b'20130417214837', 92123, 2, b'29611670.x', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:10:"29611670.x";s:10:"5::newuser";s:5:"296.x";s:8:"6::edits";s:3:"878";}', 0, b'MBisanz', 39052868)
(64581921, b'renameuser', b'renameuser', b'20150421122124', 24794120, 2, b'APL', b'[[m:Special:MyLanguage/Single User Login finalisation announcement|SUL finalization]]', b'a:3:{s:10:"4::olduser";s:3:"APL";s:10:"5::newuser";s:10:"APL~enwiki";s:8:"6::edits";i:6840;}', 0, b'Maintenance script', 9629855)
(44991540, b'renameuser', b'renameuser', b'20120930171954', 92123, 2, b'Activism1234', b'7,259 edits. Reason: WP:CHU', b'Jethro B', 0, b'MBisanz', 36364753)
(69770444, b'renameuser', b'renameuser', b'20151014013228', 11801436, 2, b'Adjwilley', b'per [[m:Special:GlobalRenameQueue/request/18477|request]]', b'a:3:{s:10:"4::olduser";s:9:"Adjwilley";s:10:"5::newuser";s:7:"Awilley";s:8:"6::edits";i:9332;}', 0, b'K6ka', 31542725)
(50440917, b'renameuser', b'renameuser', b'20130814003741', 92123, 2, b'Adoracionpuntual', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:16:"Adoracionpuntual";s:10:"5::newuser";s:12:"Salmista1983";s:8:"6::edits";i:74;}', 0, b'MBisanz', 40178284)
(50317630, b'renameuser', b'renameuser', b'20130806212916', 3516226, 2, b'Amadscientist', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:13:"Amadscientist";s:10:"5::newuser";s:11:"Mark Miller";s:8:"6::edits";i:27639;}', 0, b'Maxim', 9131778)
(78629422, b'renameuser', b'renameuser', b'20161110070931', 4511460, 2, b'Amatulic', b'Per [[:w:en:Special:Permalink/748765538|:en:WP:CHUS]]', b'a:3:{s:10:"4::olduser";s:8:"Amatulic";s:10:"5::newuser";s:11:"Anachronist";s:8:"6::edits";i:38197;}', 0, b'Ks0stm', 6488566)
(45743940, b'renameuser', b'renameuser', b'20121110151518', 92123, 2, b'Anonymouse321', b'4,186 edits. Reason: WP:CHU', b'The Anonymouse', 0, b'MBisanz', 37133347)
(49221031, b'renameuser', b'renameuser', b'20130606094808', 92123, 2, b'AppleJack-7', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:11:"AppleJack-7";s:10:"5::newuser";s:5:"Woona";s:8:"6::edits";s:3:"851";}', 0, b'MBisanz', 39519705)
(53669180, b'renameuser', b'renameuser', b'20131223152428', 1795359, 2, b'Boogerpatrol', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:12:"Boogerpatrol";s:10:"5::newuser";s:10:"Roberticus";s:8:"6::edits";i:3004;}', 0, b'Xeno', 31936975)
(57219531, b'renameuser', b'renameuser', b'20140624071555', 2530149, 2, b'BrandonWu', b'[[Wikipedia:Changing username/Simple]]', b'a:3:{s:10:"4::olduser";s:9:"BrandonWu";s:10:"5::newuser";s:8:"MrWooHoo";s:8:"6::edits";i:612;}', 0, b'Acalamari', 40227829)
(74583776, b'renameuser', b'renameuser', b'20160501192309', 36647, 2, b'Chesnaught555', b'per [[m:Special:GlobalRenameQueue/request/23755|request]]', b'a:3:{s:10:"4::olduser";s:13:"Chesnaught555";s:10:"5::newuser";s:12:"Patient Zero";s:8:"6::edits";i:7108;}', 0, b'C\xc3\xa9r\xc3\xa9ales Killer', 41606464)
(46936626, b'renameuser', b'renameuser', b'20130118132104', 92123, 2, b'EngGerm12', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:9:"EngGerm12";s:10:"5::newuser";s:13:"Wikignome1213";s:8:"6::edits";s:3:"234";}', 0, b'MBisanz', 0)
(76761929, b'renameuser', b'renameuser', b'20160816121829', 19261965, 2, b'For_(;;)', b'per [[m:Special:GlobalRenameQueue/request/26214|request]]', b'a:3:{s:10:"4::olduser";s:8:"For (;;)";s:10:"5::newuser";s:6:"Cabayi";s:8:"6::edits";i:42602;}', 0, b'Jianhui67', 17354172)
(52391126, b'renameuser', b'renameuser', b'20131105035008', 13732, 2, b'Gtwfan52', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:8:"Gtwfan52";s:10:"5::newuser";s:16:"John from Idegon";s:8:"6::edits";i:20525;}', 0, b'Andrevan', 35162681)
(50634806, b'renameuser', b'renameuser', b'20130824022355', 713860, 2, b'I_Jethrobot', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:11:"I Jethrobot";s:10:"5::newuser";s:10:"I JethroBT";s:8:"6::edits";i:11419;}', 0, b'Wizardman', 6424560)
(53244788, b'renameuser', b'renameuser', b'20131209155105', 1795359, 2, b'Jebus989', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:8:"Jebus989";s:10:"5::newuser";s:9:"Ben Moore";s:8:"6::edits";i:9616;}', 0, b'Xeno', 17476836)
(62497374, b'renameuser', b'renameuser', b'20150215030910', 92123, 2, b'John_Smith_the_Gamer', b'Fix #2', b'a:3:{s:10:"4::olduser";s:20:"John Smith the Gamer";s:10:"5::newuser";s:5:"Banak";s:8:"6::edits";i:339;}', 0, b'MBisanz', 44919606)
(80659920, b'renameuser', b'renameuser', b'20170206171257', 4511460, 2, b'JudgeRM', b'Per [[:w:en:Special:Permalink/763906345|:en:WP:CHUU]]', b'a:3:{s:10:"4::olduser";s:7:"JudgeRM";s:10:"5::newuser";s:10:"SkyWarrior";s:8:"6::edits";i:7985;}', 0, b'Ks0stm', 49886953)
(46203286, b'renameuser', b'renameuser', b'20121206212842', 3030741, 2, b'Keristrasza', b'2,861 edits. Reason: WP:USURP', b'Keri', 0, b'28bytes', 25586063)
(42278848, b'renameuser', b'renameuser', b'20120419230142', 92123, 2, b'Kgorman-ucb', b'5,139 edits. Reason: WP:CHU', b'Kevin Gorman', 0, b'MBisanz', 30709345)
(54332722, b'renameuser', b'renameuser', b'20140121150442', 1795359, 2, b'King_jakob_c_2', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:14:"King jakob c 2";s:10:"5::newuser";s:5:"Jakec";s:8:"6::edits";i:7267;}', 0, b'Xeno', 37660107)
(44291972, b'renameuser', b'renameuser', b'20120823203704', 3030741, 2, b'Loriski', b'2,705 edits. Reason: WP:USURP', b'Lorelei', 0, b'28bytes', 35799170)
(41441883, b'renameuser', b'renameuser', b'20120307040203', 92123, 2, b'Lukep913', b'9,263 edits. Reason: WP:USURP', b'LuK3', 0, b'MBisanz', 32234472)
(54676551, b'renameuser', b'renameuser', b'20140206232021', 1795359, 2, b'Michaelzeng7', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:12:"Michaelzeng7";s:10:"5::newuser";s:3:"Mz7";s:8:"6::edits";i:14391;}', 0, b'Xeno', 27191065)
(42290663, b'renameuser', b'renameuser', b'20120420150847', 92123, 2, b'Monareal', b'436 edits. Reason: WP:CHU', b'Mir Almaat 1 S1', 0, b'MBisanz', 35215224)
(63948729, b'renameuser', b'renameuser', b'20150327063606', 7447705, 2, b'Muad', b'see [[Special:GlobalRenameQueue/request/7786]]', b'a:3:{s:10:"4::olduser";s:4:"Muad";s:10:"5::newuser";s:5:"Muadd";s:8:"6::edits";i:1309;}', 0, b'Itti', 9335269)
(49276865, b'renameuser', b'renameuser', b'20130609173637', 3030741, 2, b'My76Strat', b'per request on my talk page', b'a:3:{s:10:"4::olduser";s:9:"My76Strat";s:10:"5::newuser";s:10:"John Cline";s:8:"6::edits";s:5:"37995";}', 0, b'28bytes', 39440391)
(49590293, b'renameuser', b'renameuser', b'20130628014515', 92123, 2, b'Nerdfighter', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:11:"Nerdfighter";s:10:"5::newuser";s:9:"Ross Hill";s:8:"6::edits";s:4:"4428";}', 0, b'MBisanz', 37632868)
(50537585, b'renameuser', b'renameuser', b'20130819161648', 1795359, 2, b'Newjerseyliz', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:12:"Newjerseyliz";s:10:"5::newuser";s:3:"Liz";s:8:"6::edits";i:1245;}', 0, b'Xeno', 40078790)
(74867258, b'renameuser', b'renameuser', b'20160512112708', 14836860, 2, b'Ochilov', b'Per [[:m:Special:Permalink/15595911|:m:SRUC]]', b'a:3:{s:10:"4::olduser";s:7:"Ochilov";s:10:"5::newuser";s:8:"Lingveno";s:8:"6::edits";i:1502;}', 0, b'Cyberpower678', 0)
(49746513, b'renameuser', b'renameuser', b'20130707012649', 21804, 2, b'PBASH607', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:8:"PBASH607";s:10:"5::newuser";s:9:"Prabash.A";s:8:"6::edits";s:4:"6100";}', 0, b'Pakaran', 37874329)
(52458336, b'renameuser', b'renameuser', b'20131107165358', 1795359, 2, b'PrivatePublicity', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:16:"PrivatePublicity";s:10:"5::newuser";s:11:"JaconaFrere";s:8:"6::edits";i:109;}', 0, b'Xeno', 0)
(69518429, b'renameuser', b'renameuser', b'20151005142223', 1795359, 2, b'Riggr_Mortis', b'Per [[:en:Special:Permalink/684249191|:en:WP:CHUS]]', b'a:3:{s:10:"4::olduser";s:12:"Riggr Mortis";s:10:"5::newuser";s:8:"Outriggr";s:8:"6::edits";i:4799;}', 0, b'Xeno', 47064711)
(80180174, b'renameuser', b'renameuser', b'20170119232837', 11903602, 2, b'SSTflyer', b'Per [[:m:en:Special:PermanentLink/760940283#SSTflyer_.E2.86.92_Feminist|request]]. See also [[phab:T154262]]', b'a:3:{s:10:"4::olduser";s:8:"SSTflyer";s:10:"5::newuser";s:8:"Feminist";s:8:"6::edits";i:112421;}', 0, b'Savh', 50658112)
(71694227, b'renameuser', b'renameuser', b'20160109202407', 11801436, 2, b'SamBrown98121', b'Per [[:w:en:Special:Permalink/699025712|:en:WP:CHUS]]', b'a:3:{s:10:"4::olduser";s:13:"SamBrown98121";s:10:"5::newuser";s:9:"Sam Brown";s:8:"6::edits";i:109;}', 0, b'K6ka', 48853814)
(57448249, b'renameuser', b'renameuser', b'20140707155238', 1795359, 2, b'SarahStierch', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:12:"SarahStierch";s:10:"5::newuser";s:8:"Missvain";s:8:"6::edits";i:109178;}', 0, b'Xeno', 5221388)
(65836265, b'renameuser', b'renameuser', b'20150428091447', 16956574, 2, b'SatuSuro', b'per [[:m:Special:GlobalRenameQueue/request/14236|request]]', b'a:3:{s:10:"4::olduser";s:8:"SatuSuro";s:10:"5::newuser";s:10:"JarrahTree";s:8:"6::edits";i:107142;}', 0, b'Steinsplitter', 2055808)
(57096090, b'renameuser', b'renameuser', b'20140616170436', 1795359, 2, b'Schoolskater', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:12:"Schoolskater";s:10:"5::newuser";s:10:"SkaterLife";s:8:"6::edits";i:357;}', 0, b'Xeno', 42946160)
(48990343, b'renameuser', b'renameuser', b'20130523203339', 2803535, 2, b'Shaun9876', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:9:"Shaun9876";s:10:"5::newuser";s:5:"Shaun";s:8:"6::edits";s:4:"5575";}', 0, b'WJBscribe', 33964693)
(52965197, b'renameuser', b'renameuser', b'20131129054835', 92123, 2, b'Sohambanerjee1998', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:17:"Sohambanerjee1998";s:10:"5::newuser";s:5:"Soham";s:8:"6::edits";i:5367;}', 0, b'MBisanz', 38753430)
(65681979, b'renameuser', b'renameuser', b'20150422194609', 2092344, 2, b'Superm401', b'see [[:m:Special:GlobalRenameQueue/request/13290]]', b'a:3:{s:10:"4::olduser";s:9:"Superm401";s:10:"5::newuser";s:12:"Mattflaschen";s:8:"6::edits";i:21624;}', 0, b'DerHexer', 1636654)
(55039581, b'renameuser', b'renameuser', b'20140224235125', 1795359, 2, b'TheOriginalSoni', b'WP:USURP', b'a:3:{s:10:"4::olduser";s:15:"TheOriginalSoni";s:10:"5::newuser";s:4:"Soni";s:8:"6::edits";i:8064;}', 0, b'Xeno', 35486746)
(71463721, b'renameuser', b'renameuser', b'20151230033807', 13164839, 2, b'Wikiisawesome', b'see [[:m:Special:GlobalRenameQueue/request/20334]]', b'a:3:{s:10:"4::olduser";s:13:"Wikiisawesome";s:10:"5::newuser";s:4:"Wiae";s:8:"6::edits";i:44238;}', 0, b'Jmvkrecords', 48464636)
(50889464, b'renameuser', b'renameuser', b'20130907184850', 92123, 2, b'Xerooz', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:6:"Xerooz";s:10:"5::newuser";s:6:"Xenomm";s:8:"6::edits";i:258;}', 0, b'MBisanz', 40393531)
(56481161, b'renameuser', b'renameuser', b'20140515130416', 1795359, 2, b'Zince34', b'WP:CHU', b'a:3:{s:10:"4::olduser";s:7:"Zince34";s:10:"5::newuser";s:10:"HgandVenus";s:8:"6::edits";i:798;}', 0, b'Xeno', 39350412)
conn = connect_to_db()

user_name_changes = []

with conn.cursor() as cur:
    cur.execute('use p50380g50728__hostbot_p;') #which database to use
    cur.execute("""select REPLACE(log_title,"_"," "), log_params from td_host_rename_logs;""")
    revs = cur.fetchall()
for r in revs:
    user_name_changes.append([x.decode("utf-8") for x in r])
b'u2402'
192.168.0.122
from pprint import pprint
import re

old_new_names = []

for n in user_name_changes:
    new_name = []
    if n[1].startswith("a:3"):
        new_name = re.search('(newuser";s:[0-9]+:")(.+?)(";s:)',n[1])
        old_new_names.append([n[0],new_name.group(2)])
    else:
        old_new_names.append([n[0],n[1]])


pprint(old_new_names)
[['29611670.x', '296.x'],
 ['APL', 'APL~enwiki'],
 ['Activism1234', 'Jethro B'],
 ['Adjwilley', 'Awilley'],
 ['Adoracionpuntual', 'Salmista1983'],
 ['Amadscientist', 'Mark Miller'],
 ['Amatulic', 'Anachronist'],
 ['Anonymouse321', 'The Anonymouse'],
 ['AppleJack-7', 'Woona'],
 ['Boogerpatrol', 'Roberticus'],
 ['BrandonWu', 'MrWooHoo'],
 ['Chesnaught555', 'Patient Zero'],
 ['EngGerm12', 'Wikignome1213'],
 ['For (;;)', 'Cabayi'],
 ['Gtwfan52', 'John from Idegon'],
 ['I Jethrobot', 'I JethroBT'],
 ['Jebus989', 'Ben Moore'],
 ['John Smith the Gamer', 'Banak'],
 ['JudgeRM', 'SkyWarrior'],
 ['Keristrasza', 'Keri'],
 ['Kgorman-ucb', 'Kevin Gorman'],
 ['King jakob c 2', 'Jakec'],
 ['Loriski', 'Lorelei'],
 ['Lukep913', 'LuK3'],
 ['Michaelzeng7', 'Mz7'],
 ['Monareal', 'Mir Almaat 1 S1'],
 ['Muad', 'Muadd'],
 ['My76Strat', 'John Cline'],
 ['Nerdfighter', 'Ross Hill'],
 ['Newjerseyliz', 'Liz'],
 ['Ochilov', 'Lingveno'],
 ['PBASH607', 'Prabash.A'],
 ['PrivatePublicity', 'JaconaFrere'],
 ['Riggr Mortis', 'Outriggr'],
 ['SSTflyer', 'Feminist'],
 ['SamBrown98121', 'Sam Brown'],
 ['SarahStierch', 'Missvain'],
 ['SatuSuro', 'JarrahTree'],
 ['Schoolskater', 'SkaterLife'],
 ['Shaun9876', 'Shaun'],
 ['Sohambanerjee1998', 'Soham'],
 ['Superm401', 'Mattflaschen'],
 ['TheOriginalSoni', 'Soni'],
 ['Wikiisawesome', 'Wiae'],
 ['Xerooz', 'Xenomm'],
 ['Zince34', 'HgandVenus']]
# conn = connect_to_db()

for n in old_new_names:
    with conn.cursor() as cur:
        cur.execute('use p50380g50728__hostbot_p;') #which database to use
        cur.execute("""UPDATE td_answerers_current_usernames set uname_cur = "{}" where uname_old = "{}";""".format(n[1], n[0]))
import csv

with open("renamed_users.csv", "w") as fout:
    writer = csv.writer(fout)
    writer.writerow(("old_username","new_username"))
    for n in old_new_names:
        writer.writerow((n[0],n[1]))