Created
March 11, 2020 22:59
-
-
Save samlovescoding/9135f59d6d5a05711e7e8a28746350ec to your computer and use it in GitHub Desktop.
Python wrapper for tmdb API to transfer movies to Expo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install tmdbsimple | |
# pip install requests | |
# python3 main.py | |
import tmdbsimple as tmdb | |
import json | |
import os | |
import requests | |
local = False | |
movie_save_url = "http://expo.samlovescoding.com/transfer/movie" | |
show_save_url = "http://expo.samlovescoding.com/transfer/show" | |
if local: | |
movie_save_url = "http://localhost/expo/transfer/movie" | |
show_save_url = "http://localhost/expo/transfer/show" | |
tmdb.API_KEY = "6bd736641465d1731bd67085c7de53f4" | |
def movie_search(query): | |
search = tmdb.Search() | |
response = search.movie(query=query) | |
for s in search.results: | |
try: | |
print("[" + str(s['id']) + "]", s['title'], s['release_date'], s['popularity']) | |
except KeyError as e: | |
pass | |
return search.results | |
def movie_save(id): | |
movie = tmdb.Movies(id).info(append_to_response="images,keywords,similar") | |
movie_json = json.dumps(movie) | |
response = requests.post(movie_save_url, data={ | |
"json": movie_json | |
}) | |
print(response.text) | |
def menu_movie(): | |
query = input("Enter movie name: ") | |
movie_search(query) | |
id = int(input("Enter movie id: ")) | |
if id == -1: | |
return | |
movie_save(id) | |
def show_search(query): | |
search = tmdb.Search() | |
response = search.tv(query=query) | |
for s in search.results: | |
try: | |
print("[" + str(s['id']) + "]", s['name'], s['first_air_date'], s['popularity']) | |
except KeyError as e: | |
pass | |
def show_save(id): | |
show = tmdb.TV(id).info(append_to_response="images,keywords,similar") | |
show["_seasons"] = [] | |
#print(show["name"]) | |
for _season in show["seasons"]: | |
if _season["season_number"] == 0: | |
continue | |
season = tmdb.TV_Seasons(show["id"], _season["season_number"]).info(append_to_response="images") | |
season["_episodes"] = [] | |
#print("|---" + season["name"]) | |
for _episode in season["episodes"]: | |
episode = tmdb.TV_Episodes(show["id"], _season["season_number"], _episode["episode_number"]).info(append_to_response="images") | |
#print("| |---" + episode["name"]) | |
season["_episodes"].append(episode) | |
show["_seasons"].append(season) | |
show_json = json.dumps(show) | |
response = requests.post(show_save_url, data={ | |
"json": show_json | |
}) | |
print(response.text) | |
def menu_show(): | |
query = input("Enter show name: ") | |
show_search(query) | |
id = int(input("Enter show id: ")) | |
if id == -1: | |
return | |
show_save(id) | |
def menu_main(): | |
while True: | |
print() | |
print("1. Search and Add Movie") | |
print("2. Search and Add Show") | |
print("q. Quit") | |
print() | |
ch = input("Enter your choice: ") | |
if ch == "1": | |
menu_movie() | |
elif ch == "2": | |
menu_show() | |
elif ch == "q": | |
quit() | |
else: | |
print("Incorrect choice") | |
def auto_menu(): | |
movies = open("batch.txt", "r").read().split("\n") | |
for _movie in movies: | |
movie_parts = _movie.split(".") | |
real_parts = [] | |
for movie_part in movie_parts: | |
if movie_part == "1080p": | |
break | |
real_parts.append(movie_part) | |
year = real_parts.pop() | |
while True: | |
try: | |
int(year) | |
break | |
except Exception as e: | |
try: | |
year = real_parts.pop() | |
except Exception as e: | |
year = "0000" | |
break | |
movie = " ".join(real_parts) | |
print(year, movie) | |
rat = movie_search(movie) | |
for movie in rat: | |
print(movie["title"]) | |
movie_save(movie["id"]) | |
print("-------------") | |
def single_menu(): | |
q = int(input("Enter movie id : ")) | |
if q == 1: | |
open("s.txt", "a").write(movie + "\n") | |
elif q == 0: | |
movie = rat.pop() | |
movie_save(movie["id"]) | |
else: | |
movie_save(q) | |
if __name__ == "__main__": | |
print("1. Simple Menu") | |
print("2. Simple Batch Menu") | |
print("3. Auto Batch Menu") | |
print("0. Exit") | |
print() | |
ch = int(input("Enter your choice: ")) | |
if ch == 0: | |
quit() | |
elif ch == 1: | |
while True: | |
menu_movie() | |
elif ch == 2: | |
single_menu() | |
elif ch == 3: | |
auto_menu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment