Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import requests
- from math import ceil
- from plexapi.server import PlexServer
- from lxml.html import parse
- from plexapi.utils import NA
- ### Plex server details ###
- PLEX_URL = 'http://127.0.0.1:32400'
- PLEX_TOKEN = 'XXXXXXX'
- ### Existing movie library details ###
- MOVIE_LIBRARY_NAME = 'Movies'
- LIST_ID = 'ls027955354'
- IMDB_LIST_URL = 'http://www.imdb.com/list/{}/?view=compact&sort=listorian:asc&defaults=1&' \
- 'lists={}&mode=detail&page='.format(LIST_ID, LIST_ID)
- IMDB_COLLECTION_NAME = 'Independent_Spirit_Best_Film'
- def add_collection(library_key, rating_key):
- headers = {"X-Plex-Token": PLEX_TOKEN}
- params = {"type": 1,
- "id": rating_key,
- "collection[0].tag.tag": IMDB_COLLECTION_NAME,
- "collection.locked": 1
- }
- url = "{base_url}/library/sections/{library}/all".format(base_url=PLEX_URL, library=library_key)
- r = requests.put(url, headers=headers, params=params)
- def run_imdb_list():
- try:
- plex = PlexServer(PLEX_URL, PLEX_TOKEN)
- except:
- print("No Plex server found at: {base_url}".format(base_url=PLEX_URL))
- print("Exiting script.")
- return [], 0
- # Get the IMDB list
- print("Retrieving the IMDB list...")
- i = 1
- remaining_pages = 0
- url = IMDB_LIST_URL + str(i)
- tree = parse(url)
- total_tiles = int(tree.xpath("//div[@class='desc lister-total-num-results']/text()")[0].strip().split(' ')[0].replace(",", ""))
- list_picture_titles = tree.xpath("//div[@class='lister-item-content']//h3[@class='lister-item-header']//a/text()")
- list_picture_years = tree.xpath("//div[@class='lister-item-content']//h3[@class='lister-item-header']//span[@class='lister-item-year text-muted unbold']/text()")
- list_picture_ids = [a.split('/')[2] for a in
- tree.xpath("//div[@class='lister-item-content']//h3[@class='lister-item-header']//a//@href")]
- if (total_tiles > 100):
- total_tiles = total_tiles - 100
- remaining_pages = int(ceil(total_tiles / 100.0))
- i = i + 1
- while(remaining_pages > 0):
- url = IMDB_LIST_URL + str(i)
- tree = parse(url)
- list_picture_titles = list_picture_titles + tree.xpath(
- "//div[@class='lister-item-content']//h3[@class='lister-item-header']//a/text()")
- list_picture_years = list_picture_years + tree.xpath(
- "//div[@class='lister-item-content']//h3[@class='lister-item-header']//span[@class='lister-item-year text-muted unbold']/text()")
- list_picture_ids = list_picture_ids + [a.split('/')[2] for a in
- tree.xpath(
- "//div[@class='lister-item-content']//h3[@class='lister-item-header']//a//@href")]
- remaining_pages = remaining_pages - 1
- i = i + 1
- # Get list of movies from the Plex server
- print("Retrieving a list of movies from the '{library}' library in Plex...".format(library=MOVIE_LIBRARY_NAME))
- try:
- movie_library = plex.library.section(MOVIE_LIBRARY_NAME)
- movie_library_key = movie_library.key
- all_movies = movie_library.all()
- except:
- print("The '{library}' library does not exist in Plex.".format(library=MOVIE_LIBRARY_NAME))
- print("Exiting script.")
- return
- # Create a dictionary of {imdb_id: movie}
- imdb_map = {}
- for m in all_movies:
- if m.guid != NA and 'imdb://' in m.guid:
- imdb_id = m.guid.split('imdb://')[1].split('?')[0]
- if imdb_id in list_picture_ids:
- imdb_map[imdb_id] = m
- print("Setting the collection for the '{}' library...".format(MOVIE_LIBRARY_NAME))
- in_library_idx = []
- for i, imdb_id in enumerate(list_picture_ids):
- movie = imdb_map.pop(imdb_id, None)
- if movie:
- add_collection(movie_library_key, movie.ratingKey)
- in_library_idx.append(i)
- # Get list of missing movies
- missing_imdb_list = [(idx, imdb) for idx, imdb in enumerate(zip(list_picture_ids, list_picture_titles, list_picture_years))
- if idx not in in_library_idx]
- return missing_imdb_list, len(list_picture_ids)
- if __name__ == "__main__":
- print("===================================================================")
- import time
- print time.strftime("%d/%m/%Y")
- print("===================================================================\n")
- missing_imdb_list, list_count = run_imdb_list()
- print("\n===================================================================\n")
- print("Number of IMDB movies in the library: {count}".format(count=list_count - len(missing_imdb_list)))
- print("Number of missing IMDB movies: {count}".format(count=len(missing_imdb_list)))
- print("\nList of missing movies:\n")
- for idx, (imdb_id, title, year) in missing_imdb_list:
- print("{idx}\t{imdb_id}\t{title} {year}".format(idx=idx + 1, imdb_id=imdb_id, title=title.encode('utf-8'),
- year=year.encode('utf-8')))
- print("\n===================================================================")
- print(" Done! ")
- print("===================================================================\n")
- f = open('taschen_decadas.txt','w')
- f.write("===================================================================\n")
- f.write(time.strftime("%d/%m/%Y") + '\n')
- f.write("===================================================================\n")
- f.write("\nNumber of IMDB movies in the library: {count}".format(count=list_count - len(missing_imdb_list)))
- f.write("\nNumber of missing IMDB movies: {count}\n".format(count=len(missing_imdb_list)))
- f.write("\nList of missing movies:")
- for idx, (imdb_id, title, year) in missing_imdb_list:
- f.write("\n{idx}\t{imdb_id}\t{title} {year}".format(idx=idx + 1, imdb_id=imdb_id, title=title.encode('utf-8'),
- year=year.encode('utf-8')))
- f.write("\n===================================================================")
- f.write("\n Done! ")
- f.write("\n===================================================================")
- # raw_input("Press Enter to finish...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement