-
Notifications
You must be signed in to change notification settings - Fork 304
/
remove_watched_movies.py
132 lines (105 loc) · 3.84 KB
/
remove_watched_movies.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Find and delete Movies that have been watched by a list of users.
Deletion is prompted
"""
from __future__ import print_function
from __future__ import unicode_literals
from builtins import input
from builtins import object
import requests
import sys
import os
import shutil
# ## EDIT THESE SETTINGS ##
TAUTULLI_APIKEY = 'xxxxxxxx' # Your Tautulli API key
TAUTULLI_URL = 'http://localhost:8181/' # Your Tautulli URL
LIBRARY_NAMES = ['My Movies'] # Whatever your movie libraries are called.
USER_LST = ['Joe', 'Alex'] # Name of users
class UserHIS(object):
def __init__(self, data=None):
d = data or {}
self.rating_key = d['rating_key']
class METAINFO(object):
def __init__(self, data=None):
d = data or {}
self.title = d['title']
media_info = d['media_info'][0]
parts = media_info['parts'][0]
self.file = parts['file']
def get_metadata(rating_key):
# Get the metadata for a media item.
payload = {'apikey': TAUTULLI_APIKEY,
'rating_key': rating_key,
'cmd': 'get_metadata',
'media_info': True}
try:
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
res_data = response['response']['data']
if res_data['library_name'] in LIBRARY_NAMES:
return METAINFO(data=res_data)
except Exception as e:
sys.stderr.write("Tautulli API 'get_metadata' request failed: {0}.".format(e))
pass
def get_history(user, start, length):
# Get the Tautulli history.
payload = {'apikey': TAUTULLI_APIKEY,
'cmd': 'get_history',
'user': user,
'media_type': 'movie',
'start': start,
'length': length}
try:
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
res_data = response['response']['data']['data']
return [UserHIS(data=d) for d in res_data if d['watched_status'] == 1]
except Exception as e:
sys.stderr.write("Tautulli API 'get_history' request failed: {0}.".format(e))
def delete_files(tmp_lst):
del_file = input('Delete all watched files? (yes/no)').lower()
if del_file.startswith('y'):
for x in tmp_lst:
print("Removing {}".format(os.path.dirname(x)))
shutil.rmtree(os.path.dirname(x))
else:
print('Ok. doing nothing.')
movie_dict = {}
movie_lst = []
delete_lst = []
count = 25
for user in USER_LST:
start = 0
while True:
# Getting all watched history for listed users
history = get_history(user, start, count)
try:
if all([history]):
start += count
for h in history:
# Getting metadata of what was watched
movies = get_metadata(h.rating_key)
if not any(d['title'] == movies.title for d in movie_lst):
movie_dict = {
'title': movies.title,
'file': movies.file,
'watched_by': [user]
}
movie_lst.append(movie_dict)
else:
for d in movie_lst:
if d['title'] == movies.title:
d['watched_by'].append(user)
continue
elif not all([history]):
break
start += count
except Exception as e:
print(e)
pass
for movie_dict in movie_lst:
if set(USER_LST) == set(movie_dict['watched_by']):
print(u"{} has been watched by {}".format(movie_dict['title'], " & ".join(USER_LST)))
delete_lst.append(movie_dict['file'])
delete_files(delete_lst)