Skip to content

Commit

Permalink
only add to collections if media is in configured library
Browse files Browse the repository at this point in the history
  • Loading branch information
bearlikelion committed Oct 27, 2020
1 parent f1e99f0 commit 03d415b
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions popular_plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ def get_popular():
return _popular


def clear_collections():
def clear_collections(movies_library, tv_library):
""" Clear media items from collections
"""
print("Clear Collections")
movies_collection = plex.library.section(config['Libraries']['movies']).collection()
movies_collection = movies_library.collection()
for m_collection in movies_collection:
if m_collection.title == config['Collections']['movies']:
for movies in m_collection.children:
print(" Removing %s from %s" % (movies.title, config['Collections']['movies']))
movies.removeCollection(config['Collections']['movies'])

tv_collection = plex.library.section(config['Libraries']['tv']).collection()
tv_collection = tv_library.collection()
for t_collection in tv_collection:
if t_collection.title == config['Collections']['tv']:
for show in t_collection.children:
print(" Removing %s from %s" % (show.title, config['Collections']['tv']))
show.removeCollection(config['Collections']['movies'])


def generate_collections(popular_dict):
def generate_collections(popular_dict, movies_library, tv_library):
""" Generate collections based on popular items
Args:
Expand All @@ -57,14 +57,16 @@ def generate_collections(popular_dict):
print("Creating Movie Collection")
for movie in popular_dict['movies']:
_movie = plex.fetchItem(movie['rating_key'])
_movie.addCollection(config['Collections']['movies'])
print(" Added %s to %s" % (_movie.title, config['Collections']['movies']))
if _movie.librarySectionID == movies_library.key:
_movie.addCollection(config['Collections']['movies'])
print(" Added %s to %s" % (_movie.title, config['Collections']['movies']))

print("Creating TV Collection")
for show in popular_dict['tv']:
_show = plex.fetchItem(show['rating_key'])
_show.addCollection(config['Collections']['tv'])
print(" Added %s to %s" % (_show.title, config['Collections']['tv']))
if _show.librarySectionID == tv_library.key:
_show.addCollection(config['Collections']['tv'])
print(" Added %s to %s" % (_show.title, config['Collections']['tv']))


if __name__ == '__main__':
Expand All @@ -89,6 +91,9 @@ def generate_collections(popular_dict):
else:
Exception("[Config file invalid] - Failed to login to Plex")

popular = get_popular()
clear_collections()
generate_collections(popular)
movies_library = plex.library.section(config['Libraries']['movies'])
tv_library = plex.library.section(config['Libraries']['tv'])

clear_collections(movies_library, tv_library)
popular = get_popular()
generate_collections(popular, movies_library, tv_library)

0 comments on commit 03d415b

Please sign in to comment.