Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

API manager.py 2.88 KB
Newer Older
SoleneDc's avatar
SoleneDc committed
1 2 3
import requests
import datetime
import time
SoleneDc's avatar
SoleneDc committed
4
import ast
SoleneDc's avatar
SoleneDc committed
5 6 7 8 9 10

url = 'https://api.themoviedb.org/3/discover/movie?api_key=e4c6a6f5fbd60b0316b7ff30e73bec74&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&'

#il faut ajouter l'année après
url_get_video = "http://api.themoviedb.org/3/movie/" #157336/videos?api_key=e4c6a6f5fbd60b0316b7ff30e73bec74

11 12
#API = 'api_key=e4c6a6f5fbd60b0316b7ff30e73bec74&language=en-US'
API = 'api_key=a9075982d1f7ce05cc45adec0e5f5358&language=en-US'
SoleneDc's avatar
SoleneDc committed
13 14 15

def get_movie_ids(year):
    now1 = datetime.datetime.now()
SoleneDc's avatar
SoleneDc committed
16 17
    # url_final = url + 'page=1&year=' + str(year)
    # req = requests.get(url_final)
SoleneDc's avatar
SoleneDc committed
18
    dict_movies = {}
SoleneDc's avatar
SoleneDc committed
19 20 21 22 23 24
    nb_of_pages = 550
    # nb_of_pages = req.json()['total_pages']
    # for item in req.json()['results']:
    #     movie_id = item['id']
    #     video_link = get_video_link(movie_id)
    #     dict_movies[movie_id] = [item['vote_average'], item["genre_ids"], video_link]
SoleneDc's avatar
SoleneDc committed
25
    #     f = open("./Link-dictionaries/Link-dictionary" + str(year) + ".txt", "w")
SoleneDc's avatar
SoleneDc committed
26 27 28 29
    #     f.write(str(dict_movies))
    #     f.close()

    for page in range(163, nb_of_pages+1): #TODO: changer la page if needed
30
        time.sleep(7)
SoleneDc's avatar
SoleneDc committed
31 32
        print(dict_movies)
        print(len(dict_movies))
33
        print(f"taking care of page # {page}... / ", nb_of_pages)
SoleneDc's avatar
SoleneDc committed
34 35 36 37 38 39 40
        url_final = url + 'page=' + str(page) +'&year=' + str(year)
        req = requests.get(url_final)
        for item in req.json()['results']:
            movie_id = item['id']
            video_link = get_video_link(movie_id)
            if video_link != '':
                dict_movies[movie_id] = [item['vote_average'], item["genre_ids"], video_link]
SoleneDc's avatar
SoleneDc committed
41
                f = open("./Link-dictionaries/Link-dictionary" + str(year) + "-2.txt", "w")
SoleneDc's avatar
SoleneDc committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
                f.write(str(dict_movies))
                f.close()
    return dict_movies

def get_video_link(movie_id):
    url = url_get_video + str(movie_id) + '/videos?' + API
    req = requests.get(url)
    #print(req.json())
    if req.json()['results'] != []:
        if req.json()['results'][0]['site'] == 'YouTube':
            return req.json()['results'][0]['key']
    else:
        return ''


SoleneDc's avatar
SoleneDc committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
# Helpers

def concatenate_dict():
    path = "./Link-dictionaries/Link-dictionary2005-1.txt"
    file = open(path, "r").read()
    dictyear = ast.literal_eval(file)
    print(len(dictyear))
    path2 = "./Link-dictionaries/Link-dictionary2005-2.txt"
    file2 = open(path2, "r").read()
    dict2 = ast.literal_eval(file2)
    print(len(dict2), 'dict2')
    dictyear.update(dict2)
    print(len(dictyear), 'new dictyear')
    path3 = "./Link-dictionaries/Link-dictionary2005-3.txt"
    file3 = open(path3, "r").read()
    dict3 = ast.literal_eval(file3)
    print(len(dict3))
    dictyear.update(dict3)
    print(len(dictyear))
    f = open("./Link-dictionaries/Link-dictionary2005.txt", "w")
    f.write(str(dictyear))
    f.close()
    return (dictyear)

concatenate_dict()





SoleneDc's avatar
SoleneDc committed
87