%matplotlib inline
import csv
import json
import time
import sys
from concurrent.futures import ThreadPoolExecutor

import requests
import mwapi
import mwtypes
import pandas
import seaborn
session = mwapi.Session("https://en.wikipedia.org", user_agent="carlos_alberto_td@hotmail.com")
WEIGHTS = {'Stub': 1, 'Start': 2, 'C': 3, 'B': 4, 'GA': 5, 'FA': 6}
def score2sum(score_doc):
    if (score_doc is None):
        return None
    weighted_sum = 0
    for cl, proba in score_doc['probability'].items():
        weighted_sum += WEIGHTS[cl] * proba
    return weighted_sum
def fetch_wp10_score(rev_id):
    response = requests.get('https://ores.wikimedia.org/v3/scores/enwiki/{0}/wp10'.format(rev_id))
    try:
        return response.json()['enwiki']['scores'][str(rev_id)]['wp10']['score']
    except:
        print(rev_id)
        return None

def fetch_wp10_scores(rev_ids):
    executor = ThreadPoolExecutor(max_workers=8)
    return executor.map(fetch_wp10_score, rev_ids)

def fetch_historical_scores(page_name):
    historical_scores = []
    for response_doc in session.get(action='query', prop='revisions', titles=page_name, 
                                    rvprop=['ids', 'timestamp'], rvlimit=100, rvdir="newer", 
                                    formatversion=2, continuation=True):
        rev_docs = response_doc['query']['pages'][0]['revisions']
        rev_ids = [d['revid'] for d in rev_docs]
        for rev_doc, score_doc in zip(rev_docs, fetch_wp10_scores(rev_ids)):
            rev_id = rev_doc['revid']
            timestamp = mwtypes.Timestamp(rev_doc['timestamp'])
            weighted_sum = score2sum(score_doc)
            historical_scores.append({'rev_id': rev_id, 'timestamp': timestamp, 'weighted_sum': weighted_sum})
            sys.stderr.write(".")
            sys.stderr.flush()
        sys.stderr.write("\n")
    
    return historical_scores
 
historical_scores = pandas.DataFrame(fetch_historical_scores("Tim Berners-Lee"))
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
.........................
55096207
55482607
55610352
...........................................................................
....................................................................................................
................................................................
90480601
....................................
....................................................................................................
....................................................................................................
....................................................................................................
............................................
157610245
........................................................
....................................................................................................
....................................................................................................
.........................................
197391058
...........................................................
........
207286469
207286809
...........
208273416
208322206
208323781
208878538
208903311
...........
209419214
209419220
......................................................................
....................................................................................................
............................................................................
240705754
240744906
........................
....................................................................................................
........
251847129
251902994
...
252472550
...............
253036015
254034865
254107648
254393933
..................................................
261700516
........................
....................................................................................................
.......................................
279566336
279732152
....................
282117501
......
284082162
..........................
289502123
289502441
289502584
.........
.........................
292521054
.............................................................
304271859
..............
............................
311103604
..........................................
315571683
...........
317038690
317327348
.....
317386636
317442936
317624982
......
318031955
318071668
........
.
319901937
320212152
321655137
..................................
353702133
353704016
.................................
367184940
367189117
374687805
...........
384062534
386428847
...........
387939374
..........
389416206
389416385
389416484
389457480
.....................
391248253
391257414
..................
398488890
..........
399785874
399984055
...
401073441
......
401977731
402361636
............
406824950
....
408250561
..........................
...............................
415685627
............
418281987
418282039
.....................................
425622309
426872685
427367025
....................
....................
434491508
434492179
435073566
....
435273848
435330325
435330539
435413236
............
439284104
.........
441831873
442266488
442453302
...
442904023
...............
443598159
443856670
.........
445767389
446525506
............................
448035601
449155236
449161577
449162235
449622383
...............................
452496308
..................
453894679
..
455021844
455071332
455071497
455071558
..............................
456778289
..............
458663017
.....
.
460425336
460839786
460845600
462327047
462327322
.....
463622708
465798740
466029483
467000044
..........
467573603
468390231
473238021
478887668
.....
479030517
.............
484616546
487395972
488334595
488497090
....
491506183
491917009
493590458
494053465
.......
496590100
499810131
500149118
500904857
..
504496126
504502343
504518948
504523690
504524201
504637235
504665663
...............................
506055587
........
507236853
507882629
..............
511348076
511348227
511348317
511348438
511348911
511348970
515692340
515753222
515753258
516038342
516039540
516040254
..........................
523577966
.............................
537725006
..........
553093624
553094072
553094237
...................
565519632
567376144
567459779
567825827
567826783
.
572825792
572826036
575594395
...............
583488676
584371775
585579739
585673542
.
590257765
591248367
...............
596466967
598682370
....
599369675
599532732
599589152
...
600785659
602913317
603699400
........
607815273
......
609394888
611435347
...
611436934
611514976
613407815
......
614606870
615453359
616580604
617374076
...........................
629971844
.....
641914294
..........
645178775
645178834
650780585
650831102
650831462
............
............
668477327
669726623
670384851
670810668
..................
670810680
670812174
670813125
.........
672151157
672151216
...........
678297037
680610346
682309746
.................................
711160052
.................
722021604
722022062
722359394
722360722
722362120
722362845
722554160
726316149
..............
726328419
726969964
727372182
...................
750231194
.............
758623829
760897744
760921828
770010927
..
773792009
773794533
.............
773858399
773885546
773906475
773966466
..
773982031
773982412
773987167
774054985
774085285
774137284
774193336
774267700
774301750
774415547
.
774564242
774604056
774777075
775040536
....................................
...
794412751
796758458
797745928
799331314
.................
810055460
810056385
810056945
812280297
812280656
...............
815298094
815298325
815300570
815300629
815300880
815303573
815539500
815539535
815715771
........................
831504770
834179028
836462615
836600560
836617822
..............
848247216
849011447
849412044
850883958
853811608
.
854599875
855008292
855061430
857650699
..................
864575774
866465078
........
874107430
877017711
877017912
877018004
877018108
877018219
878027516
878048988
879260980
................
 
historical_scores['time'] =pandas.to_datetime(historical_scores.timestamp, format='%Y-%m-%dT%H:%M:%SZ',errors='ignore')
historical_scores = historical_scores.set_index('time')
historical_scores['weighted_sum'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f4561ec17b8>