-
Notifications
You must be signed in to change notification settings - Fork 0
/
plex-reanalyze.py
executable file
·51 lines (38 loc) · 1.86 KB
/
plex-reanalyze.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
#!/usr/bin/env python
# This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 Unported License.
# (CC BY-SA-4.0) http://creativecommons.org/licenses/by-sa/4.0/
import argparse
from plexapi.server import PlexServer
from requests import put as PUT
def analyze_item(server, key):
server.query('/library/metadata/%s/analyze' % key, method=PUT)
def process_album(server, album, prefix_path=[], depth=0, dryrun=False):
if prefix_path and album.title != prefix_path[0]:
return
print '\t' * depth + 'album: ' + album.title
for item in album.photos():
if item.TYPE == 'photo':
print '\t' * (depth + 1) + 'photo: ' + item.title
if not dryrun:
analyze_item(server, item.ratingKey)
elif item.TYPE == 'photoalbum':
process_album(server, item, prefix_path[1:] if prefix_path else None, depth=depth + 1, dryrun=dryrun)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Reanalyze plex media items')
parser.add_argument('-b', '--baseurl', dest='baseurl',
help='base url to be used when connecting to Plex')
parser.add_argument('-l', '--library', dest='library',
help='media library to examine')
parser.add_argument('-p', '--prefix', dest='prefix',
help='folder path that should be scanned')
parser.add_argument('-n', '--dry-run', dest='dryrun', action='store_true', default=False,
help='don\'t analyze anything, just print the matching items')
args = parser.parse_args()
plex = PlexServer(baseurl=args.baseurl)
library = plex.library.section(args.library)
if args.prefix:
prefix = args.prefix.split('/')
else:
prefix = None
for album in library.all():
process_album(plex, album, prefix, dryrun=args.dryrun)