#!/usr/bin/env python 
# -*- coding: UTF-8 -*-

import urllib.request, re, os, time, requests, datetime
def recup_cand (date, cand) : 

    date= str(date)
    cand = str(cand)
    cand = urllib.parse.quote(cand)
    print(date)
    url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia/all-access/user/"+cand+"/daily/2017030100/"+date+"00"
    print(url)
    file = urllib.request.urlopen(url)
    datel = []
    #Récup date
    for i in file :
        i = i.decode()
        j = i.split('"timestamp":"')
        for i in j :
            i = i.split('","')
            if i[0].startswith('20') :
                datel.append(i[0])

    audience = []            
    file = urllib.request.urlopen(url)
    #Récup audience
    for i in file :
        i = i.decode()
        j = i.split('"views":')
        for i in j :
            i = i . split('}')
            if not i[0].startswith('{"items":') :
                audience.append(i[0])
                
    return {"liste_dates" : datel, "liste_audiences" : audience }


liste_cand = ["François_Fillon", "Emmanuel Macron", "Jean-Luc Mélenchon", "Marine Le Pen","Benoît Hamon"] 
date = "20170423"

Fillon = []
Macron = []
Melenchon = []
LePen = []
Hamon = []


pFillon = []
pMacron = []
pMelenchon = []
pLePen = []
pHamon = []


liste_aud = [Fillon, Macron, Melenchon, LePen, Hamon]
liste_pour_aud = [pFillon, pMacron, pMelenchon, pLePen, pHamon]

b = 0
for i in liste_cand :
    cand = i
    retour = recup_cand(date, cand)
    a = 0
    print(i)
    for i in retour["liste_dates"] :
        liste_aud[b].append(retour["liste_audiences"][a] )
        a = a +1
    b = b + 1

a = 0
total = []
for i in Fillon :
    compt = 0
    for j in [0,1,2,3,4] :
        compt = compt + int(liste_aud[j][a])
    a = a + 1
    total.append(compt)

#print(total)
a = 0
for i in retour["liste_dates"] :
    #print(i , ":" , total[a])
    a = a + 1
    
for j in [0,1,2,3,4] :
    b = 0 
    for i in retour["liste_dates"] :
        #print(liste_aud[j][b])
        #print(total[b])
        #print(p)
        p = int(liste_aud[j][b])/int(total[b])
        liste_pour_aud[j].append(round(p,3))
        b = b + 1

#print(retour["liste_dates"])
#print(pFillon)
#print(pMacron)
#print(pLe_Pen)
#print(pHamon)
#print(pPoutou)
#print(pMélenchon)
#print(pDupont)
#print(pArthaud)

y1 = "|y1 = "
y2 = "|y2 = "
y3 = "|y3 = "
y4 = "|y4 = "
y5 = "|y5 = "
y6 = "|y6 = "
y7 = "|y7 = "
y8 = "|y8 = "
y9 = "|y9 = "
y10 = "|y10 = "
y11 = "|y11 = "
liste_ind = [y1,y2,y3,y4,y5] 

a = 0
b = 0

while a < (len(liste_ind)-1) :
    for i in liste_pour_aud :
        b = 0
        
        while b < len(liste_pour_aud[a]) :

            liste_ind[a] = liste_ind[a] + str(liste_pour_aud[a][b]) + ','
            b = b + 1
        print(liste_ind[a])
        a = a + 1
x = "|x = "
a = 0
while a < len(retour["liste_dates"]) :
    l = retour["liste_dates"][a][:4]
    i = retour["liste_dates"][a][4:6]
    j = retour["liste_dates"][a][6:8]
    k = l +"-"+ i +"-"+ j
    #print(k)
    #if k.startswith('01') or k.startswith('15') :
        #x = x + k + ','
    #else :
    x = x + k + ', '   
    a = a + 1

print(x)

    
20170423
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia/all-access/user/Fran%C3%A7ois_Fillon/daily/2017030100/2017042300
François_Fillon
20170423
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia/all-access/user/Emmanuel%20Macron/daily/2017030100/2017042300
Emmanuel Macron
20170423
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia/all-access/user/Jean-Luc%20M%C3%A9lenchon/daily/2017030100/2017042300
Jean-Luc Mélenchon
20170423
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia/all-access/user/Marine%20Le%20Pen/daily/2017030100/2017042300
Marine Le Pen
20170423
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia/all-access/user/Beno%C3%AEt%20Hamon/daily/2017030100/2017042300
Benoît Hamon
|y1 = 0.284,0.182,0.264,0.33,0.351,0.329,0.241,0.153,0.089,0.087,0.06,0.081,0.107,0.233,0.174,0.159,0.145,0.094,0.07,0.075,0.073,0.086,0.122,0.158,0.144,0.103,0.112,0.301,0.159,0.119,0.111,0.089,0.078,0.1,0.087,0.087,0.087,0.075,0.073,0.083,0.093,0.134,0.097,0.084,0.08,0.105,0.095,0.083,0.089,0.098,0.09,0.091,0.091,
|y2 = 0.378,0.559,0.437,0.318,0.286,0.335,0.365,0.415,0.392,0.432,0.526,0.598,0.582,0.408,0.351,0.346,0.341,0.276,0.339,0.272,0.307,0.287,0.313,0.3,0.335,0.26,0.315,0.247,0.328,0.303,0.333,0.443,0.294,0.325,0.327,0.296,0.5,0.369,0.298,0.233,0.207,0.235,0.28,0.29,0.25,0.261,0.261,0.426,0.283,0.269,0.294,0.33,0.329,
|y3 = 0.116,0.082,0.081,0.073,0.092,0.098,0.116,0.117,0.078,0.093,0.167,0.123,0.117,0.114,0.15,0.148,0.245,0.42,0.241,0.317,0.361,0.415,0.354,0.307,0.313,0.369,0.304,0.201,0.238,0.329,0.297,0.241,0.384,0.319,0.283,0.372,0.246,0.369,0.412,0.505,0.506,0.438,0.419,0.444,0.461,0.408,0.438,0.309,0.303,0.313,0.312,0.302,0.338,
|y4 = 0.118,0.089,0.106,0.133,0.128,0.125,0.124,0.159,0.092,0.138,0.131,0.093,0.106,0.131,0.216,0.14,0.113,0.108,0.075,0.139,0.111,0.097,0.11,0.115,0.105,0.143,0.141,0.163,0.132,0.113,0.145,0.117,0.155,0.143,0.174,0.139,0.085,0.103,0.115,0.097,0.113,0.11,0.087,0.094,0.125,0.128,0.108,0.103,0.24,0.224,0.157,0.153,0.129,
|y5 = 0.105,0.088,0.112,0.147,0.144,0.114,0.155,0.156,0.349,0.249,0.116,0.105,0.088,0.113,0.109,0.206,0.155,0.102,0.275,0.197,0.148,0.115,0.101,0.121,0.103,0.126,0.129,0.088,0.143,0.135,0.113,0.11,0.089,0.113,0.129,0.106,0.081,0.084,0.102,0.081,0.08,0.083,0.116,0.088,0.085,0.099,0.098,0.079,0.085,0.097,0.147,0.124,0.114,
|x = 2017-03-01, 2017-03-02, 2017-03-03, 2017-03-04, 2017-03-05, 2017-03-06, 2017-03-07, 2017-03-08, 2017-03-09, 2017-03-10, 2017-03-11, 2017-03-12, 2017-03-13, 2017-03-14, 2017-03-15, 2017-03-16, 2017-03-17, 2017-03-18, 2017-03-19, 2017-03-20, 2017-03-21, 2017-03-22, 2017-03-23, 2017-03-24, 2017-03-25, 2017-03-26, 2017-03-27, 2017-03-28, 2017-03-29, 2017-03-30, 2017-03-31, 2017-04-01, 2017-04-02, 2017-04-03, 2017-04-04, 2017-04-05, 2017-04-06, 2017-04-07, 2017-04-08, 2017-04-09, 2017-04-10, 2017-04-11, 2017-04-12, 2017-04-13, 2017-04-14, 2017-04-15, 2017-04-16, 2017-04-17, 2017-04-18, 2017-04-19, 2017-04-20, 2017-04-21, 2017-04-22,