Create Plex Collection based on IMDB List

Hi,

here i have a short Python script (Tested with python 2.7 on a qnap x64 plex installation)

For this script to work you need to get a plex token and fill in the first few lines.

This Script retrieves the IMDB CSV Export of a List and matches the entries to a Plex Library and Tags the Entries in the library

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
from math import ceil
from plexapi.server import PlexServer
from lxml.html import parse
import re
import csv
import urllib2

### Plex server details ###
PLEX_URL = 'http://127.0.0.1:32400'
PLEX_TOKEN = ''

### Existing movie library details ###
MOVIE_LIBRARY_NAME = 'EN_Movies'
LIST_ID = 'ls074285945'
IMDB_LIST_URL = 'https://www.imdb.com/list/{}/export'.format(LIST_ID)
IMDB_COLLECTION_NAME = 'Hallmark'

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 find_imdb_id(imdb_url):
    """Extract id from imdb_url"""
    re1 = '.*?'
    re2 = '(\\d+)'
    regex = re.compile(re1 + re2, re.IGNORECASE|re.DOTALL)
    match = regex.search(imdb_url)
    if not match:
        return False
    imdb_id = "tt" + match.group(1)
    return imdb_id

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
    list_picture_ids = []
    list_picture_titles = []
    list_picture_years = []
    title_lookup = {}
    response = urllib2.urlopen(IMDB_LIST_URL)
    reader = csv.reader(response)
    titles = reader.next()
    reader = csv.DictReader(response, titles)
    for row in reader:
        imdburl = row['URL']
        imdb_id = find_imdb_id(imdburl)
        if imdb_id:
            list_picture_ids.append(imdb_id)
            list_picture_titles.append(row['Title'])
            list_picture_years.append(row['Year'])
            title_lookup[imdb_id] = row['Title'] + " (" + row['Year'] + ")"
        else:
            print "ERROR: Unable to find IMDB ID for %s" % (row['Title'])
            continue
    response.close()

    # 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 '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)

    return len(list_picture_ids)

if __name__ == "__main__":
    print("===================================================================")
    import time
    print time.strftime("%d/%m/%Y")
    print("===================================================================\n")

    list_count = run_imdb_list()
   # raw_input("Press Enter to finish...")

Hope this helps you.

The Script is based on the following but modified to Download the CSV instead of parsing the HTML. Here the source