-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathplex-pinyin-sort.py
176 lines (160 loc) · 6.25 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
## python 3
# pip install plexapi
# pip install pypinyin
import urllib
import http.client
import json
import sys
import pypinyin
from plexapi.server import PlexServer
from plexapi.myplex import MyPlexAccount
from plexapi.myplex import MyPlexDevice
PLEX_TOKEN = ""
def fetchPlexApi(path='', method='GET', getFormPlextv=False, token=PLEX_TOKEN, params=None):
"""a helper function that fetches data from and put data to the plex server"""
#print(path)
headers = {'X-Plex-Token': token,
'Accept': 'application/json'}
if getFormPlextv:
url = 'plex.tv'
connection = http.client.HTTPSConnection(url)
else:
url = PLEX_URL.rstrip('/').replace('http://','')
connection = http.client.HTTPConnection(url)
try:
if method.upper() == 'GET':
pass
elif method.upper() == 'POST':
headers.update({'Content-type': 'application/x-www-form-urlencoded'})
pass
elif method.upper() == 'PUT':
pass
elif method.upper() == 'DELETE':
pass
else:
print("Invalid request method provided: {method}".format(method=method))
connection.close()
return
connection.request(method.upper(), path , params, headers)
response = connection.getresponse()
r = response.read()
contentType = response.getheader('Content-Type')
status = response.status
connection.close()
if response and len(r):
if 'application/json' in contentType:
return json.loads(r)
elif 'application/xml' in contentType:
return xmltodict.parse(r)
else:
return r
else:
return r
except Exception as e:
connection.close()
print("Error fetching from Plex API: {err}".format(err=e))
def updateSortTitle(rating,item):
sortQuery =urllib.parse.quote(item.encode('utf-8'))
data = fetchPlexApi("/library/sections/"+sectionNum+"/all?type=1&id=%s&titleSort.value=%s&"%(rating,sortQuery), "PUT",token=PLEX_TOKEN)
def updateShowSortTitle(rating,item):
sortQuery =urllib.parse.quote(item.encode('utf-8'))
data = fetchPlexApi("/library/sections/"+sectionNum+"/all?type=2&id=%s&titleSort.value=%s&"%(rating,sortQuery), "PUT",token=PLEX_TOKEN)
def uniqify(seq):
# Not order preserving
keys = {}
for e in seq:
keys[e] = 1
return keys.keys()
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)
b = []
for i in range(len(a)):
b.append(str(a[i][0]).upper())
c = ''.join(b)
return c
def loopThroughAllMovies():
toDo = True
start = 0
size = 100
while toDo:
if len(sectionNum):
url = "/library/sections/" + sectionNum + "/all?type=1&X-Plex-Container-Start=%i&X-Plex-Container-Size=%i" % (start, size)
metadata = fetchPlexApi(url,token=PLEX_TOKEN)
container = metadata["MediaContainer"]
elements = container["Metadata"]
totalSize = container["totalSize"]
offset = container["offset"]
size = container["size"]
start = start + size
if totalSize-offset-size == 0:
toDo = False
# print(toDo)
# loop through all elements
for movie in elements:
mediaType = movie["type"]
if mediaType != "movie":
continue
if 'titleSort' in movie: #判断是否已经有标题
con = movie["titleSort"]
if (check_contain_chinese(con)):
continue
continue
key = movie["ratingKey"]
title = movie["title"]
SortTitle = changepinyin(title)
print(title)
updateSortTitle(key, SortTitle)
def loopThroughAllShows():
toDo = True
start = 0
size = 100
while toDo:
if len(sectionNum):
url = "/library/sections/" + sectionNum + "/all?type=2&X-Plex-Container-Start=%i&X-Plex-Container-Size=%i" % (start, size)
metadata = fetchPlexApi(url,token=PLEX_TOKEN)
container = metadata["MediaContainer"]
elements = container["Metadata"]
totalSize = container["totalSize"]
offset = container["offset"]
size = container["size"]
start = start + size
if totalSize-offset-size == 0:
toDo = False
# print(toDo)
# loop through all elements
for movie in elements:
mediaType = movie["type"]
if mediaType != "show":
continue
if 'titleSort' in movie: #判断是否已经有标题
con = movie["titleSort"]
if (check_contain_chinese(con)):
continue
continue
key = movie["ratingKey"]
title = movie["title"]
SortTitle = changepinyin(title)
print(title)
updateShowSortTitle(key, SortTitle)
if __name__ == '__main__':
#got token.url
print("欢迎使用PLEX中文排序")
PLEX_URL = input('请输入你的plex服务器地址:')
PLEX_TOKEN = input('请输入你的token:')
# the plex server url
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
for section in plex.library.sections():
if section.type == 'movie' or section.type == 'show':
print(section)
#choose list
sectionNum = input('请输入你要排序的影视库编号:')
# run at startup
try:
loopThroughAllMovies()
except:
loopThroughAllShows()