forked from timmy0209/plex-pinyin-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplex-pinyin-sort.py
104 lines (85 loc) · 3.6 KB
/
plex-pinyin-sort.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Description: Automatically generate pinyin sort title for Chinese media library
Author: timmy0209
Maintainer: sjtuross
Requires: plexapi, pypinyin
Usage: plex-pinyin-sort.py [-h] [-u URL] [-t TOKEN] [-s SECTION]
optional arguments:
-h, --help show this help message and exit
-u URL, --url URL
-t TOKEN, --token TOKEN
-s SECTION, --section SECTION
Tautulli script trigger:
* Notify on recently added
Tautulli script arguments:
* Recently Added:
--section {section_id}
'''
import argparse
import os
import pypinyin
from plexapi.server import PlexServer
PLEX_URL = ""
PLEX_TOKEN = ""
def check_contain_chinese(check_str):
for ch in check_str:
if '\u4e00' <= ch <= '\u9fff':
return True
return False
def changepinyin (title):
a = pypinyin.pinyin(title, style=pypinyin.FIRST_LETTER, errors='ignore')
b = []
for i in range(len(a)):
b.append(str(a[i][0]).upper())
c = ''.join(b)
return c
def loopThroughAllItems(plex, sectionId, regenerate):
section=plex.library.sectionByID(int(sectionId))
if section.type in ('movie', 'show'):
for item in section.all():
if check_contain_chinese(item.title if regenerate else item.titleSort):
titleSort=changepinyin(item.title if regenerate else item.titleSort)
item.editSortTitle(titleSort, True)
print(section.type.capitalize(), "-", item.title)
for collection in section.collections():
if collection.content==None and check_contain_chinese(collection.title if regenerate else collection.titleSort):
titleSort=changepinyin(collection.title if regenerate else collection.titleSort)
collection.editSortTitle(titleSort, True)
print("Collection", "-", collection.title)
if section.type == 'artist':
for artist in section.all():
if check_contain_chinese(artist.title if regenerate else artist.titleSort):
titleSort=changepinyin(artist.title if regenerate else artist.titleSort)
artist.editSortTitle(titleSort, True)
print(section.type.capitalize(), "-", artist.title)
for album in artist.albums():
if check_contain_chinese(album.title if regenerate else album.titleSort):
titleSort=changepinyin(album.title if regenerate else album.titleSort)
album.editSortTitle(titleSort, True)
print(section.type.capitalize(), "-", artist.title, "-", album.title)
print("\nSuccess!")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url')
parser.add_argument('-t', '--token')
parser.add_argument('-s', '--section')
parser.add_argument('-r', '--regenerate', nargs='?', type=int, const=1, default=0)
opts = parser.parse_args()
PLEX_URL = opts.url or os.getenv('PLEX_URL', PLEX_URL)
PLEX_TOKEN = opts.token or os.getenv('PLEX_TOKEN', PLEX_TOKEN)
if not PLEX_URL:
PLEX_URL = input('Enter Plex Server Url:')
if not PLEX_TOKEN:
PLEX_TOKEN = input('Enter Plex Server Token:')
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
if not opts.section:
for section in plex.library.sections():
if section.type in ('movie', 'show', 'artist'):
print(section)
sectionId = input('Enter Media Library Section Id:')
else:
sectionId = opts.section
regenerate = False if opts.regenerate==0 else True
loopThroughAllItems(plex, sectionId, regenerate)