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

import urllib.request, re, os, time, requests

def recup_program(url) :
    file = urllib.request.urlopen(url)
    print (url)

    list_section_name = []
    a=0
    b = 0
    n = 0
    donnees = dict()
    liste =[]
    for i in file :
        if (i.startswith(b'        <span>Programme<br>') and a < 2800):
            i = i.decode('utf-8')
            i = i.split('>')
            i = i[2]
            i = i.split('<')
            i = i[0]
            i = "'".join(i.split("&#039;"))
            i = ">".join(i.split("&gt;"))
            #print (i)
            #print ('a', a)
            b = a
            liste.append(i)
        if (a != 15 and a == b+15) or (a != 16 and a == b+16) :
            if i != b'    \n':
                if not i.startswith(b'    <span class='):
                    i = i.decode('utf-8')
                    #.decode('ascii')
                    #
                    i = i.split('>')
                    i = i[1]
                    i = i.split('<')
                    i = i[0]
                    i = "'".join(i.split("&#039;"))
                    #print (i)
                    donnees[liste[n]] = i
                    n = n + 1
                    #print ('a', a)
                    
       
                    
        a = a + 1
    return donnees 

def recup_audiences(donnees) :
    donnees_aud = dict()
   
    for i in donnees :
        if donnees[i] == 'Zone interdite' : donnees[i] = 'Zone interdite (émission de télévision)'
        titre = urllib.parse.quote(donnees[i])
        url = 'https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/'+titre+'/daily/20160617/20160617'
        #url ='https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Monuments%20Men/daily/20160529/20160529'
        print (url)
        
        try :
            file = urllib.request.urlopen(url)
            for j in file :
                j = j.decode('utf-8')
                j = j[len(i)-13:]
                j = j.split(":")
                j = j[1]
                j = j.split('}')
                j = j[0]
                
                
                print(j)
                donnees_aud[donnees[i]] = j
                
        except : 
            #print(donnees[i])
            donnees_aud[donnees[i]] = 0
        
        
        #print (url)
        time.sleep(1)
    print (donnees_aud)

        
url = 'http://www.programme-tv.net/programme/programme-tnt/2016-06-17/'
donnees = recup_program(url)  
recup_audiences(donnees)
http://www.programme-tv.net/programme/programme-tnt/2016-06-17/
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Ink%20Master%20%3A%20le%20meilleur%20tatoueur/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Barbie%20%3A%20la%20princesse%20et%20la%20popstar/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Les%20experts/daily/20160617/20160617
6
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/La%20maison%20France%205/daily/20160617/20160617
3
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Clermont-Auvergne%20/%20Racing%2092/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Enqu%C3%AAte%20d%27action/daily/20160617/20160617
11
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Le%20grand%20b%C3%AAtisier%20de%20l%27%C3%A9t%C3%A9/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/L%27%C3%89quipe%20Type/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Soci%C3%A9t%C3%A9s%20secr%C3%A8tes/daily/20160617/20160617
[{"project"
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Super%20Nanny/daily/20160617/20160617
917
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/L%27histoire%20en%20direct/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Le%20monde%20magique%20des%20Leprechauns/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Femmes%20de%20loi/daily/20160617/20160617
598
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Au%20bas%20de%20l%27%C3%A9chelle/daily/20160617/20160617
1571
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/La%20m%C3%A9diasph%C3%A8re/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Espagne%20/%20Turquie/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/American%20Pickers%2C%20chasseurs%20de%20tr%C3%A9sors/daily/20160617/20160617
239
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/CQFD/daily/20160617/20160617
342
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Du%20c%C3%B4t%C3%A9%20des%20%C2%ABanges%C2%BB/daily/20160617/20160617
[{"project"
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Pardonne-moi/daily/20160617/20160617
19
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/La%20chanson%20de%20l%27ann%C3%A9e%20f%C3%AAte%20la%20musique/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Candice%20Renoir/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Dolmen/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/ID%20Voyance%2C%20le%20Prime/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Le%20Titanic/daily/20160617/20160617
22
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Thalassa/daily/20160617/20160617
https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/fr.wikipedia.org/all-access/all-agents/Meurtres%20au%20paradis/daily/20160617/20160617
{'Candice Renoir': 0, 'Du côté des «anges»': '[{"project"', 'American Pickers, chasseurs de trésors': '239', 'Thalassa': 0, 'Sociétés secrètes': '[{"project"', 'Dolmen': 0, 'La maison France 5': '3', 'Le monde magique des Leprechauns': 0, 'Meurtres au paradis': 0, "L'Équipe Type": 0, 'La médiasphère': 0, 'CQFD': '342', 'Espagne / Turquie': 0, 'Barbie : la princesse et la popstar': 0, "Le grand bêtisier de l'été": 0, 'Pardonne-moi': '19', 'Femmes de loi': '598', 'Ink Master : le meilleur tatoueur': 0, 'ID Voyance, le Prime': 0, 'Les experts': '6', "L'histoire en direct": 0, 'Le Titanic': '22', "Enquête d'action": '11', "La chanson de l'année fête la musique": 0, "Au bas de l'échelle": '1571', 'Super Nanny': '917', 'Clermont-Auvergne / Racing 92': 0}