Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- lang = 'en'
- region = 'US'
- localechanged = False
- api_key = '[enter your themoviedb api key here (and remove the brackets!)]'
- ws = ' '
- try:
- while True:
- try:
- import urllib.request
- from urllib.request import urlopen
- import urllib.parse
- from collections import OrderedDict
- except ImportError:
- from urllib2 import urlopen
- import json
- import sys
- import os
- import codecs
- import languagecodes
- lang_list = [x[0] for x in languagecodes.languages]
- country_list = [x[0] for x in languagecodes.countries]
- def query_yes_no(question, default="yes"):
- valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
- if default is None:
- prompt = " [y/n] "
- elif default == "yes":
- prompt = " [Y/n] "
- elif default == "no":
- prompt = " [y/N] "
- else:
- raise ValueError("invalid default answer: '%s'" % default)
- while True:
- sys.stdout.write(question + prompt)
- choice = input().lower()
- if default is not None and choice == '':
- return valid[default]
- elif choice in valid:
- return valid[choice]
- else:
- sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
- def get_jsonparsed_data(url):
- response = urlopen(url)
- data = response.read().decode("utf-8")
- return json.loads(data)
- crash = False
- print('Current locale: ' + lang + '-' + region + '. Type \'change-locale\' to change.')
- print()
- search_title = input("Movie title: ")
- if search_title == 'change-locale':
- while localechanged == False:
- newlang = input('\nEnter new language code (ISO-639-1) (type list to show available codes): ').lower()
- if newlang == 'list':
- print()
- for code, language, in languagecodes.languages:
- print(code, language)
- elif newlang not in lang_list:
- print("Unknown language code")
- else:
- lang = newlang
- x = 0
- for x in range(0, len(languagecodes.languages)):
- if newlang in languagecodes.languages[x]:
- print('\nLanguage set to: ' + str(languagecodes.languages[x])[8:][:-2])
- newregion = input('\nEnter new country code (ISO-3166-1) (type list to show available codes): ').upper()
- if newregion == 'list':
- print()
- for code, country, in languagecodes.countries:
- print(code, country)
- elif newregion not in country_list:
- print("Unknown country code")
- else:
- region = newregion
- x = 0
- for x in range(0, len(languagecodes.countries)):
- if newregion in languagecodes.countries[x]:
- print('\nCountry set to: ' + str(languagecodes.countries[x])[8:][:-2])
- localechanged = True
- break
- search_title = urllib.parse.quote(search_title)
- try:
- search_result = get_jsonparsed_data("https://api.themoviedb.org/3/search/movie?api_key=" + api_key + "&language=" + lang + "-" + region + "&query=" + str(search_title))
- except:
- print("\nNo title entered!")
- crash = True
- try:
- query = query_yes_no('\nIs ' + search_result['results'][0]['title'] + ' (' + search_result['results'][0]['release_date'][:-6] + ') the title you\'re looking for?')
- if query == False:
- print()
- x = 1
- for x in range(1, len(search_result['results'])):
- search_details = get_jsonparsed_data("https://api.themoviedb.org/3/movie/" + str(search_result['results'][x]['id']) + "?api_key=" + api_key + "&language=" + lang + "-" + region + "®ion=" + region + "&append_to_response=credits")
- print(str(x) + ": " + search_result['results'][x]['title'] + ' (' + search_result['results'][x]['release_date'][:-6] + ')')
- selected_title = int(input("\nSelect correct title ([ENTER] to cancel): "))
- else:
- selected_title = 0
- info_default = get_jsonparsed_data("https://api.themoviedb.org/3/movie/" + str(search_result['results'][selected_title]['id']) + "?api_key=" + api_key + "&language=" + lang + "-" + region + "®ion=" + region + "&append_to_response=credits,release_dates")
- info_english = get_jsonparsed_data("https://api.themoviedb.org/3/movie/" + str(search_result['results'][selected_title]['id']) + "?api_key=" + api_key)
- print('\nSelected title: ' + info_default['title'] + ' (' + info_default['release_date'][:-6] + ') - TMDB ID: ' + str(search_result['results'][selected_title]['id']) + '\n')
- filepath = input("Drag and drop your video file here (or type a path+filename without extension) and press [ENTER]: ").replace("\"", "")[:-4]
- print()
- path = filepath.rsplit('\\', 1)
- with open(filepath + '.xml', 'w', encoding='utf-8') as f:
- f.write('<media type="Movie">\n' + ws + '<title>')
- f.write(info_default['title'])
- print('Added title: ' + info_default['title'])
- f.write('</title>\n' + ws + '<description>')
- f.write(info_default['overview'])
- f.write('</description>\n' + ws + '<cast>\n')
- print('Added overview')
- x = 0
- for x in range(0, len(info_default['credits']['cast'])):
- actor = info_default['credits']['cast'][x]['name']
- f.write(ws*2 + '<name>' + actor + '</name>\n')
- print('Added actor: ' + actor)
- x += 1
- f.write(ws + '</cast>\n' + ws + '<rating>')
- x = 0
- for x in range(0, len(info_default['release_dates']['results'])):
- if region in info_default['release_dates']['results'][x]['iso_3166_1']:
- cert = info_default['release_dates']['results'][x]['release_dates'][0]['certification']
- f.write(cert)
- print('Added certification: ' + cert)
- x += 1
- f.write('</rating>\n' + ws + '<userStarRating>')
- rating = str(info_default['vote_average'])
- f.write(rating)
- print('Added rating: ' + rating)
- f.write('</userStarRating>\n' + ws + '<published>')
- x = 0
- for x in range(0, len(info_default['release_dates']['results'])):
- if region in info_default['release_dates']['results'][x]['iso_3166_1']:
- reldate = info_default['release_dates']['results'][x]['release_dates'][0]['release_date'][:10]
- f.write(reldate)
- print('Added release date: ' + reldate)
- x += 1
- f.write('</published>\n' + ws + '<genres>\n')
- x = 0
- for x in range(0, len(info_english['genres'])):
- genre = info_english['genres'][x]['name']
- f.write(ws*2 + '<genre>' + genre + '</genre>\n')
- print('Added genre: ' + genre)
- f.write(ws + '</genres>\n' + ws + '<directors>\n')
- x = 0
- for x in range(0, len(info_default['credits']['crew'])):
- if 'Director' in info_default['credits']['crew'][x]['job']:
- director = info_default['credits']['crew'][x]['name']
- f.write(ws*2 + '<name>' + str(director) + '</name>\n')
- print('Added director: ' + director)
- f.write(ws + '</directors>\n' + ws + '<writers>\n')
- x = 0
- duplicate_check = []
- for x in range(0, len(info_default['credits']['crew'])):
- if ('Writer' in info_default['credits']['crew'][x]['job']) or ('Story' in info_default['credits']['crew'][x]['job']) or ('Screenplay' in info_default['credits']['crew'][x]['job']) or ('Author' in info_default['credits']['crew'][x]['job']) or ('Novel' in info_default['credits']['crew'][x]['job']):
- writer = info_default['credits']['crew'][x]['name']
- if not writer in duplicate_check:
- duplicate_check.append(writer)
- f.write(ws*2 + '<name>' + writer + '</name>\n')
- print('Added writer: ' + writer)
- else:
- print('Discarded duplicate entry: ' + writer)
- f.write(ws + '</writers>\n</media>')
- f.close
- with open(path[0] + '\.' + str(info_default['id']), 'w') as f:
- f.close
- print('Downloading poster...')
- urllib.request.urlretrieve("http://image.tmdb.org/t/p/original" + info_default['poster_path'], filepath + ".jpg")
- print('Downloading backdrop...')
- urllib.request.urlretrieve("http://image.tmdb.org/t/p/original" + info_default['backdrop_path'], filepath + "-fanart.jpg")
- print('Done!')
- except:
- if localechanged == True:
- print()
- localechanged = False
- elif crash == True:
- print()
- crash = False
- else:
- try:
- selected_title
- except:
- print('\nNo title selected!\n')
- else:
- print('\nNo match!\n')
- except KeyboardInterrupt:
- print('Interrupted!')
Advertisement
Add Comment
Please, Sign In to add comment