%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
vetor = ["Sophie Wilson"]
for i in range(len(vetor)):
    historical_scores = pandas.DataFrame(fetch_historical_scores(vetor[i]))

    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')
    
    arquivo = str(vetor[i])+str('.csv')
    historical_scores.to_csv(arquivo)
    ##export_csv = historical_scores.to_csv (r'C:\Users\Ron\Desktop\export_dataframe.csv', index = None, header=True) 

    print (historical_scores)
....................................................................................................
....................................................................................................
....................................................................................................
.......................................................
                        rev_id             timestamp  weighted_sum
time                                                              
2002-06-12 03:00:34     111136  2002-06-12T03:00:34Z      1.108909
2002-07-06 11:10:45     111795  2002-07-06T11:10:45Z      1.106585
2002-07-06 23:24:59     352304  2002-07-06T23:24:59Z      1.125340
2002-10-12 01:41:16     352307  2002-10-12T01:41:16Z      1.124420
2002-10-12 01:42:47     356088  2002-10-12T01:42:47Z      1.169688
2002-10-13 18:49:48     930482  2002-10-13T18:49:48Z      1.170347
2003-05-17 17:07:29    1649330  2003-05-17T17:07:29Z      1.176617
2003-10-30 18:44:56    2074579  2003-10-30T18:44:56Z      1.122786
2004-01-03 08:53:23    5616392  2004-01-03T08:53:23Z      1.152979
2004-09-02 22:42:01    5873546  2004-09-02T22:42:01Z      1.125162
2004-09-14 08:39:08    6141666  2004-09-14T08:39:08Z      1.129012
2004-09-25 11:35:45    6142386  2004-09-25T11:35:45Z      1.130443
2004-09-25 12:35:32    6163221  2004-09-25T12:35:32Z      1.127460
2004-09-26 12:43:27    7906732  2004-09-26T12:43:27Z      1.134890
2004-11-27 20:22:45    7908831  2004-11-27T20:22:45Z      1.134717
2004-11-27 21:44:33   11825257  2004-11-27T21:44:33Z      1.133552
2005-04-03 06:09:06   15950508  2005-04-03T06:09:06Z      1.129616
2005-08-03 22:52:25   20222536  2005-08-03T22:52:25Z      1.204591
2005-09-13 14:30:07   23166448  2005-09-13T14:30:07Z      1.304479
2005-10-09 03:37:03   25108649  2005-10-09T03:37:03Z      1.424085
2005-10-09 03:37:51   25108693  2005-10-09T03:37:51Z      1.447552
2005-10-10 18:50:17   25218820  2005-10-10T18:50:17Z      1.424991
2005-10-19 00:42:46   25876208  2005-10-19T00:42:46Z      1.415742
2006-01-18 04:57:20   35639725  2006-01-18T04:57:20Z      1.468619
2006-02-26 22:12:52   41368028  2006-02-26T22:12:52Z      1.229832
2006-02-26 22:24:34   41370925  2006-02-26T22:24:34Z      1.468619
2006-03-04 20:17:54   42234304  2006-03-04T20:17:54Z      1.570315
2006-03-06 04:38:51   42431107  2006-03-06T04:38:51Z      1.606597
2006-04-10 02:52:21   47743152  2006-04-10T02:52:21Z      1.618235
2006-04-10 06:20:22   47764091  2006-04-10T06:20:22Z      1.714958
...                        ...                   ...           ...
2018-09-17 03:27:10  859914649  2018-09-17T03:27:10Z      3.605798
2018-10-14 00:08:27  863924307  2018-10-14T00:08:27Z      3.611771
2018-10-14 15:38:26  864017000  2018-10-14T15:38:26Z      3.605798
2018-10-20 18:58:26  864959882  2018-10-20T18:58:26Z      3.606253
2018-10-26 15:04:26  865844217  2018-10-26T15:04:26Z      3.599766
2018-11-08 11:30:13  867847753  2018-11-08T11:30:13Z      3.601324
2018-11-08 19:06:39  867907850  2018-11-08T19:06:39Z      3.568938
2018-11-27 04:53:55  870815676  2018-11-27T04:53:55Z      3.583849
2018-11-29 09:37:44  871160853  2018-11-29T09:37:44Z      3.580053
2018-11-29 14:34:22  871186263  2018-11-29T14:34:22Z      3.583849
2018-11-29 14:41:35  871187108  2018-11-29T14:41:35Z      3.589927
2018-11-29 18:18:56  871221962  2018-11-29T18:18:56Z      3.589927
2018-11-30 03:57:58  871300026  2018-11-30T03:57:58Z      3.587274
2018-12-20 22:39:31  874685969  2018-12-20T22:39:31Z      3.618442
2018-12-20 22:40:20  874686063  2018-12-20T22:40:20Z      3.595442
2019-01-06 23:34:05  877159107  2019-01-06T23:34:05Z      3.566672
2019-01-06 23:39:12  877159711  2019-01-06T23:39:12Z      3.578842
2019-02-16 16:44:03  883628260  2019-02-16T16:44:03Z      3.575306
2019-03-05 17:50:30  886337993  2019-03-05T17:50:30Z      3.578842
2019-03-05 17:50:41  886338015  2019-03-05T17:50:41Z      3.575306
2019-03-05 17:50:54  886338035  2019-03-05T17:50:54Z      3.575306
2019-03-05 17:51:19  886338097  2019-03-05T17:51:19Z      3.575306
2019-03-12 00:13:46  887334902  2019-03-12T00:13:46Z      3.596867
2019-03-18 09:42:47  888312347  2019-03-18T09:42:47Z      3.577708
2019-04-15 14:45:59  892583864  2019-04-15T14:45:59Z      3.577708
2019-04-22 09:56:03  893578367  2019-04-22T09:56:03Z      3.578118
2019-04-22 09:56:32  893578411  2019-04-22T09:56:32Z      3.570976
2019-04-22 10:00:19  893578765  2019-04-22T10:00:19Z      3.577153
2019-05-03 09:52:28  895301692  2019-05-03T09:52:28Z      3.570865
2019-05-05 22:38:40  895688645  2019-05-05T22:38:40Z      3.570865

[355 rows x 3 columns]