#c.f. http://paws-public.wmflabs.org/paws-public/17347449/Test.ipynb

from ipywidgets import interact_manual, interact
import os
import pymysql
conn = pymysql.connect(
    host=os.environ['MYSQL_HOST'],
    user=os.environ['MYSQL_USERNAME'],
    password=os.environ['MYSQL_PASSWORD'],
    database='enwiki_p',
    #charset='utf16',
    use_unicode=True
)
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""SELECT * from revision LIMIT 10""")
    print(cur.fetchall())
((519207833, 0, 0, b'[[Hilfe:Zusammenfassung und Quellen#Auto-Zusammenfassung|AZ]]: Die Seite wurde neu angelegt: [[Datei:Legobr\xc3\xbccke Wuppertal 1.jpg|miniatur|Die Legobr\xc3\xbccke]] [[Datei:Legobr\xc3\xbccke Wuppertal 3.jpg|min\xe2\x80\xa6', 17565405, b'Morty', b'20121004171819', 0, 0, 2609, 0, b'ja2n5b5mc7b9l2oh71fnko5uf82vcs4', None, None), (519207834, 0, 0, b'', 17565405, b'Morty', b'20121004174221', 0, 0, 2687, 0, b't6e3ilpe31rz15zyeyhfmwn992gechg', None, None), (519207835, 0, 0, b'/* Weblinks */', 576652, b'Gerd Fahrenhorst', b'20121004193302', 0, 0, 2714, 0, b'h6ua4f48shhzlbrox738f1td41amtpb', None, None), (519207836, 0, 0, b'linkfix', 192622, b'Atamari', b'20121004195709', 0, 0, 2756, 0, b'du2pzh49445sp2xyiohulul8vkajgeg', None, None), (519207837, 0, 0, b'wikify', 192622, b'Atamari', b'20121004195727', 0, 0, 2756, 0, b'sh5pvzghzbwyqg83lthygv00oz7hsjw', None, None), (519207838, 0, 0, b'[[WP:HC|HC]]: +[[Kategorie:Pr\xc3\xa4miertes Bauwerk in Wuppertal]]; \xc2\xb1[[Kategorie:Kultur (Wuppertal)]]\xe2\x86\x92[[Kategorie:Bildende Kunst (Wuppertal)]]', 192622, b'Atamari', b'20121004195953', 0, 0, 2810, 0, b'tm27yeqn1khzumaq4ylmrnohywm0983', None, None), (519207839, 0, 0, b'Morty verschob Seite [[Legobr\xc3\xbccke]] nach [[Lego-Br\xc3\xbccke]]', 17565405, b'Morty', b'20121004205524', 1, 0, 2810, 0, b'tm27yeqn1khzumaq4ylmrnohywm0983', None, None), (519207840, 0, 0, b'', 17565405, b'Morty', b'20121004205613', 0, 0, 2814, 0, b'p4zyhqcbdim4yx5hpf7usvrf5zdzh6h', None, None), (519207842, 0, 0, b'/* Beschreibung */', 17565405, b'Morty', b'20121004211247', 0, 0, 2816, 0, b'4kaptnynp4vh6bttymbhx9erxid79i5', None, None), (519207843, 0, 0, b'/* Beschreibung */ Kleinigkeiten', 0, b'Der-wuppertaler', b'20121004222920', 0, 0, 2836, 0, b'dt0qefs4iu6bysy4g3elu33z0w07vn7', None, None))
!pip install git+https://github.com/mediawiki-utilities/python-mwviews.git
Collecting git+https://github.com/mediawiki-utilities/python-mwviews.git
  Cloning https://github.com/mediawiki-utilities/python-mwviews.git to /tmp/pip-cipc18yh-build
  Requirement already satisfied (use --upgrade to upgrade): mwviews==0.0.8 from git+https://github.com/mediawiki-utilities/python-mwviews.git in /srv/paws/lib/python3.4/site-packages
Requirement already satisfied (use --upgrade to upgrade): requests in /srv/paws/lib/python3.4/site-packages (from mwviews==0.0.8)
Requirement already satisfied (use --upgrade to upgrade): futures in /srv/paws/lib/python3.4/site-packages (from mwviews==0.0.8)
from ipywidgets import VBox, jsdlink, IntSlider
s1 = IntSlider(description='Maximum', min=100, max=200, value=100)
s2 = IntSlider(description='Value', value=40)
jsdlink((s1, 'value'), (s2, 'max'))
VBox([s1, s2])
!jupyter nbextension enable --py widgetsnbextension
Unrecognized JSON config file version, assuming version 1
Enabling notebook extension jupyter-js-widgets/extension...
      - Validating: OK
@interact
def wat(x=5,h=""):
    print(x * 50)
    print(h)
250

geo = {
    "type": "Feature",
    "properties": {
        "name": "Coors Field",
        "amenity": "Baseball Stadium",
        "popupContent": "This is where the Rockies play!"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [-104.99404, 39.75621]
    }
}
from ipyleaflet import GeoJSON
conn = pymysql.connect(
    host=os.environ['MYSQL_HOST'],
    user=os.environ['MYSQL_USERNAME'],
    password=os.environ['MYSQL_PASSWORD'],
    database='enwiki_p',
    #charset='utf16',
    use_unicode=True
)
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute('select user_id  from user_groups join user on user_id = ug_user where ug_group="bot"')
    #bots = [r['user_id'] for r in cur.fetchall()]
    bots = [r[0] for r in cur.fetchall()]
def revisions_for_user_in_time(user_id, starttime, endtime):
    with conn.cursor() as cur:
        cur.execute('use enwiki_p')
        cur.execute('select * from revision_userindex where rev_user = %s and rev_timestamp > %s and rev_timestamp < %s', (user_id, starttime, endtime))
        return cur.fetchall()
bot_revs = []
for id in bots:
    bot_revs += revisions_for_user_in_time(id, '20150101000000', '20150108000000')
len(bot_revs)
120505
import random
def sample_random_users(number, starttime, endtime):
    conn.ping(True)
    users = []
    with conn.cursor() as cur:
        cur.execute('use enwiki_p')
        cur.execute('SELECT DISTINCT rev_user FROM revision_userindex WHERE rev_timestamp > %s and rev_timestamp < %s', (starttime, endtime))
        #max_id = cur.fetchone()['id']
        #ids = [random.randint(1, max_id) for _ in range(number)]
        #cur.execute('SELECT * FROM user WHERE user_id IN (%s)' % (','.join([str(id) for id in ids]),))
        return cur.fetchall()
users = sample_random_users(10, '20150101000000', '20150108000000')
user_revs = []
for user in users:
    #user_revs += revisions_for_user_in_time(user['user_id'], '20150101000000', '20150108000000')
    user_revs += revisions_for_user_in_time(user[0], '20150101000000', '20150108000000')
len(user_revs)
1003760