Exemplo n.º 1
0
def update_collection_order(collection):
    rating_key = collection.ratingKey
    count = int(collection.childCount)

    modes = {0: "Release date", 1: "Alphabetical"}

    # by release date
    if (count <= settings.SORT_VIDEOS_BY_RELEASE_MAX_COLLECTION_LENGTH) \
            or (collection.title in settings.ALWAYS_SORT_BY_RELEASE_DATE):
        print("Changing collection order for '{title}' to '{mode}'".format(
            title=collection.title, mode=modes[0]))
        update_settings = utils.generate_url(
            params={
                "base_url":
                f"{settings.PLEX_URL}/library/metadata/{rating_key}/prefs?",
                "collectionSort": 0,
                "X-Plex-Token": settings.PLEX_TOKEN
            })
        requests.put(update_settings)

    # alphabetically
    if (count > settings.SORT_VIDEOS_BY_RELEASE_MAX_COLLECTION_LENGTH) \
            or (collection.title in settings.ALWAYS_SORT_ALPHABETICALLY):
        print("Changing collection order for '{title}' to '{mode}'".format(
            title=collection.title, mode=modes[1]))
        update_settings = utils.generate_url(
            params={
                "base_url":
                f"{settings.PLEX_URL}/library/metadata/{rating_key}/prefs?",
                "collectionSort": 1,
                "X-Plex-Token": settings.PLEX_TOKEN
            })
        requests.put(update_settings)
Exemplo n.º 2
0
def execute():
    """
    Deletes all collections from movie and tv shows.
    """
    plex = PlexServer(settings.PLEX_URL, settings.PLEX_TOKEN)

    sections = plex.library.sections()

    for section in sections:

        if section.type == "movie":
            all_movie_collections = section.collection()

            if all_movie_collections:

                for collection in all_movie_collections:
                    print(f"Removing collection: {collection.title}")
                    collection.delete()

        if section.type == "show":
            all_show_collections = get_all_show_collections()

            for collection_id, collection_title in all_show_collections.items():
                print("Removing collection: {title}".format(title=collection_title))
                url = utils.generate_url(params={
                    "base_url": "{base_url}/library/metadata/{id}?".format(base_url=settings.PLEX_URL,
                                                                           id=collection_id),
                    "X-Plex-Token": settings.PLEX_TOKEN
                })
                requests.delete(url)
Exemplo n.º 3
0
def get_all_show_collections():
    section_id = utils.get_type_id(type="show")
    url = utils.generate_url(params={
        "base_url": "{base_url}/library/sections/{id}/all?".format(base_url=settings.PLEX_URL, id=section_id),
        "type": utils.get_type_id(type="collection"),
        "X-Plex-Token": settings.PLEX_TOKEN
    })
    r = requests.get(url)

    collections = {}
    root = ET.fromstring(r.text)
    for child in root:
        for element in child.getiterator("Directory"):
            collections[element.attrib["ratingKey"]] = element.attrib["title"]
    return collections
Exemplo n.º 4
0
def execute():
    """
    Remove all trophies and medals from all movies.
    """
    plex = PlexServer(settings.PLEX_URL, settings.PLEX_TOKEN)

    plex_sections = plex.library.sections()

    for plex_section in plex_sections:

        if plex_section.type == "movie":
            section = plex.library.section(plex_section.title)

            for video in section.all():
                title = video.title

                trophies = ["🏆", "🥈"]
                if any(trophy in title for trophy in trophies):
                    print("Removing trophy from '{title}'".format(title=title))
                    url = utils.generate_url(
                        params={
                            "base_url":
                            "{base_url}/library/sections/{id}/all?".format(
                                base_url=settings.PLEX_URL,
                                id=video.librarySectionID),
                            "type":
                            1,
                            "id":
                            video.ratingKey,
                            "includeExternalMedia":
                            1,
                            "title.value":
                            utils.clean_title(title),
                            "title.locked":
                            1,
                            "titleSort.locked":
                            1,
                            "X-Plex-Token":
                            settings.PLEX_TOKEN
                        })
                    requests.put(url)
Exemplo n.º 5
0
def execute(genre):
    """
    Remove specific tag from all movies.
    """
    plex = PlexServer(settings.PLEX_URL, settings.PLEX_TOKEN)

    plex_sections = plex.library.sections()

    for plex_section in plex_sections:

        if plex_section.type == "movie":
            section = plex.library.section(plex_section.title)

            for video in section.all():
                video.reload()
                video_genres = [genre.tag.lower() for genre in video.genres]

                if genre.lower() in video_genres:
                    print("Removing {genre} from {title}.".format(
                        genre=genre, title=video.title))
                    url = utils.generate_url(
                        params={
                            "base_url":
                            "{base_url}/library/sections/{id}/all?".format(
                                base_url=settings.PLEX_URL,
                                id=video.librarySectionID),
                            "type":
                            1,
                            "id":
                            video.ratingKey,
                            "includeExternalMedia":
                            1,
                            "genre%5B%5D.tag.tag-":
                            genre,
                            "X-Plex-Token":
                            settings.PLEX_TOKEN
                        })
                    requests.put(url)
Exemplo n.º 6
0
def update_collection_mode(collection):
    rating_key = collection.ratingKey
    count = int(collection.childCount)

    modes = {
        -1: "Library default",
        1: "Hide items in this collection",
        2: "Show this collection and its items",
        0: "Hide collection"
    }

    # hide items in collection
    if (count <= settings.HIDE_VIDEOS_MAX_COLLECTION_LENGTH) or (
            collection.title in settings.IGNORE_COLLECTION_LENGTH):
        expected_mode = 1
        print("Changing collection mode for '{title}' to '{mode}'".format(
            title=collection.title, mode=modes[expected_mode]))
        update_settings = utils.generate_url(
            params={
                "base_url":
                "{base_url}/library/metadata/{id}/prefs?".format(
                    base_url=settings.PLEX_URL, id=rating_key),
                "collectionMode":
                expected_mode,
                "X-Plex-Token":
                settings.PLEX_TOKEN
            })
        requests.put(update_settings)

    # show items in collection
    if (collection.title in settings.ALWAYS_SHOW_VIDEOS_IN_COLLECTION) \
            or (count > settings.HIDE_VIDEOS_MAX_COLLECTION_LENGTH) and (
            collection.title not in settings.IGNORE_COLLECTION_LENGTH):
        expected_mode = 2
        print("Changing collection mode for '{title}' to '{mode}'".format(
            title=collection.title, mode=modes[expected_mode]))
        update_settings = utils.generate_url(
            params={
                "base_url":
                "{base_url}/library/metadata/{id}/prefs?".format(
                    base_url=settings.PLEX_URL, id=rating_key),
                "collectionMode":
                expected_mode,
                "X-Plex-Token":
                settings.PLEX_TOKEN
            })
        requests.put(update_settings)

    # hide collection as a whole
    if collection.title in settings.ALWAYS_HIDE_COLLECTION:
        expected_mode = 0
        print("Changing collection mode for '{title}' to '{mode}'".format(
            title=collection.title, mode=modes[expected_mode]))
        update_settings = utils.generate_url(
            params={
                "base_url":
                "{base_url}/library/metadata/{id}/prefs?".format(
                    base_url=settings.PLEX_URL, id=rating_key),
                "collectionMode":
                expected_mode,
                "X-Plex-Token":
                settings.PLEX_TOKEN
            })
        requests.put(update_settings)

    # hide inactive holiday collections
    if collection.title in settings.HOLIDAY_COLLECTIONS:
        date_ranges = settings.HOLIDAY_COLLECTIONS[collection.title]

        expected_mode = 0
        current_date = datetime.now().date()

        start_date = datetime.strptime(
            str(current_date.year) + '-' + date_ranges[0], '%Y-%m-%d').date()
        end_date = datetime.strptime(
            str(current_date.year) + '-' + date_ranges[1], '%Y-%m-%d').date()

        if settings.DELETE_OUT_OF_SEASON_COLLECTIONS:
            print(f"Removing out of season collection: {collection.title}")
            collection.delete()
        elif not start_date <= current_date <= end_date:
            print("Changing collection mode for '{title}' to '{mode}'".format(
                title=collection.title, mode=modes[expected_mode]))
            update_settings = utils.generate_url(
                params={
                    "base_url":
                    "{base_url}/library/metadata/{id}/prefs?".format(
                        base_url=settings.PLEX_URL, id=rating_key),
                    "collectionMode":
                    expected_mode,
                    "X-Plex-Token":
                    settings.PLEX_TOKEN
                })
            requests.put(update_settings)
        else:
            pass
Exemplo n.º 7
0
def upload_poster_via_dropbox(media, poster_type):
    """
    The Plex API call needs a link to an image so instead of storing them locally, I store them in Dropbox and use the
    display link generated for each image. To use this, you must have a Dropbox token and have your images
    stored in the correct folders. The image name must match the collection name, however, there is an option in
    settings.py for replacing a semi-colon.
    """
    dbx = dropbox.Dropbox(settings.DROPBOX_TOKEN)
    contents = dbx.files_list_folder(path=f"/{poster_type.lower()}").entries

    media_id = media.ratingKey
    clean_media_title = utils.clean_title(media.title).replace(
        ":", settings.SEMICOLON_REPLACEMENT).lower()

    poster_file = None
    for file in contents:
        if poster_type != "collections":
            title = "{title} ({year})".format(title=clean_media_title,
                                              year=media.year)
            if title in file.name.lower():
                poster_file = file
        else:
            # collections don't have years
            if clean_media_title in file.name.lower():
                poster_file = file

    image_file_path = None
    if poster_file:
        image_file_path = poster_file.path_display

    if image_file_path:
        print("Uploading image for '{title}'".format(title=media.title))
        image_link = dbx.files_get_temporary_link(path=image_file_path).link

        # need to upload the photo to Plex's database
        upload_poster_url = utils.generate_url(
            params={
                "base_url":
                "{base_url}/library/metadata/{id}/posters?".format(
                    base_url=settings.PLEX_URL, id=media_id),
                "url":
                urllib.parse.quote(image_link),
                "X-Plex-Token":
                settings.PLEX_TOKEN
            })
        requests.post(upload_poster_url)

        # once the photo is in the database, we need to get the url
        get_poster_url = utils.generate_url(
            params={
                "base_url":
                "{base_url}/library/metadata/{id}/posters?".format(
                    base_url=settings.PLEX_URL, id=media_id),
                "X-Plex-Token":
                settings.PLEX_TOKEN
            })
        r = requests.get(get_poster_url)
        root = ET.fromstring(r.text)

        for child in root:
            rating_key = child.attrib["ratingKey"]
            if rating_key.startswith("upload"):
                upload_url = rating_key

        # once we have that url, we can set it as the poster
        update_poster = utils.generate_url(
            params={
                "base_url":
                "{base_url}/library/metadata/{id}/posters?".format(
                    base_url=settings.PLEX_URL, id=media_id),
                "url":
                upload_url,
                "X-Plex-Token":
                settings.PLEX_TOKEN
            })
        requests.put(update_poster)
        return True

    else:
        print("No image for '{title}'".format(title=media.title))
        return False