-
Notifications
You must be signed in to change notification settings - Fork 2
/
popular_plex.py
113 lines (93 loc) · 4.17 KB
/
popular_plex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Generate Plex Playlists based on most popular from Tatulli
"""
import configparser
import requests
import os
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
def get_popular():
"""Get Home Statistics from Tautulli API
"""
print("Getting popular media")
stats = requests.get(config['Tautulli']['url'] + '/api/v2?apikey=' +
config['Tautulli']['apikey'] + '&cmd=get_home_stats&time_range=' +
config['Tautulli']['timerange'] + "&stats_count=" +
config['Tautulli']['count'])
response = stats.json()
_popular = {}
for entry in response['response']['data']:
if entry['stat_id'] == 'popular_movies':
_popular['movies'] = entry['rows']
if entry['stat_id'] == 'popular_tv':
_popular['tv'] = entry['rows']
return _popular
def clear_collections(movies_library, tv_library):
""" Clear media items from collections
"""
print("Clear Collections")
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 = 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, movies_library, tv_library):
""" Generate collections based on popular items
Args:
popular_dict (dict): Popular Movies and TV Dictionary
"""
current_int = 0
limit = int(config['Collections']['limit'])
print("Creating Movie Collection")
for movie in popular_dict['movies']:
if current_int < limit:
_movie = plex.fetchItem(movie['rating_key'])
if _movie.librarySectionID == movies_library.key:
_movie.addCollection(config['Collections']['movies'])
print(" Added %s to %s" % (_movie.title, config['Collections']['movies']))
current_int += 1
else:
print("Movie Collection limit reached")
break
print("Creating TV Collection")
current_int = 0
for show in popular_dict['tv']:
if current_int < limit:
_show = plex.fetchItem(show['rating_key'])
if _show.librarySectionID == tv_library.key:
_show.addCollection(config['Collections']['tv'])
print(" Added %s to %s" % (_show.title, config['Collections']['tv']))
current_int += 1
else:
print("TV Collection limit reached")
break
if __name__ == '__main__':
config = configparser.ConfigParser()
if (os.path.exists('config.ini')):
config.read('config.ini')
else:
config.read(os.path.dirname(__file__) + os.path.sep + 'config.ini')
# Login to Plex
if (config['Plex']['url'] != '' and config['Plex']['token'] != ''):
print("Logging in using token")
plex = PlexServer(config['Plex']['url'], config['Plex']['token'])
elif (config['Plex']['username'] != '' and config['Plex']['password'] != ''):
print("Logging in using username/password")
account = MyPlexAccount(config['Plex']['username'], config['Plex']['password'])
if config['Plex']['server'] != '':
plex = account.resource(config['Plex']['server']).connect()
else:
Exception("[Config file invalid] - Missing [Plex] Server")
else:
Exception("[Config file invalid] - Failed to login to Plex")
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)