forked from sleikang/media_server_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.py
1678 lines (1609 loc) · 73.9 KB
/
media.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import time
from concurrent.futures import ThreadPoolExecutor
from threading import BoundedSemaphore
import zhconv
from api.douban import douban
from api.mediasql import mediasql
from api.nastools import nastools
from api.server.emby import emby
from api.server.jellyfin import jellyfin
from api.server.plex import plex
from api.tmdb import tmdb
from system.config import config
from system.log import log
class media:
mediaservertype = None
nastoolsclient = None
meidiaserverclient = None
tmdbclient = None
doubanclient = None
languagelist = None
threadpool = None
updatepeople = None
updateoverview = None
updateseasongroup = None
taskdonespace = None
delnotimagepeople = None
checkmediasearch = None
configload = None
sqlclient = None
configinfo = None
threadnum = None
semaphore = None
def __init__(self, configinfo: config) -> None:
"""
:param configinfo 配置
"""
try:
self.mediaservertype = configinfo.systemdata["mediaserver"]
self.configload = False
self.configinfo = configinfo
self.sqlclient = mediasql(configinfo=configinfo)
self.nastoolsclient = nastools(
host=configinfo.apidata["nastools"]["host"],
authorization=configinfo.apidata["nastools"]["authorization"],
username=configinfo.apidata["nastools"]["username"],
passwd=configinfo.apidata["nastools"]["passwd"],
)
self.tmdbclient = tmdb(
key=configinfo.apidata["tmdb"]["key"],
host=configinfo.apidata["tmdb"].get("host", 'https://api.themoviedb.org/3'),
proxy=configinfo.apidata["tmdb"].get("proxy", None),
)
self.doubanclient = douban(
key=configinfo.apidata["douban"]["key"],
cookie=configinfo.apidata["douban"]["cookie"],
)
self.languagelist = ["zh-CN", "zh-SG", "zh-TW", "zh-HK"]
self.threadnum = configinfo.systemdata["threadnum"]
self.semaphore = BoundedSemaphore(self.threadnum)
self.threadpool = ThreadPoolExecutor(max_workers=self.threadnum)
self.updatepeople = configinfo.systemdata["updatepeople"]
self.updateoverview = configinfo.systemdata["updateoverview"]
self.taskdonespace = configinfo.systemdata["taskdonespace"]
self.delnotimagepeople = configinfo.systemdata["delnotimagepeople"]
self.updateseasongroup = configinfo.systemdata["updateseasongroup"]
self.checkmediasearch = configinfo.systemdata["checkmediasearch"]
if "Emby" in self.mediaservertype:
self.meidiaserverclient = emby(
host=configinfo.apidata["emby"]["host"],
userid=configinfo.apidata["emby"]["userid"],
key=configinfo.apidata["emby"]["key"],
)
self.configload = True
elif "Jellyfin" in self.mediaservertype:
self.meidiaserverclient = jellyfin(
host=configinfo.apidata["jellyfin"]["host"],
userid=configinfo.apidata["jellyfin"]["userid"],
key=configinfo.apidata["jellyfin"]["key"],
)
self.configload = True
elif "Plex" in self.mediaservertype:
self.meidiaserverclient = plex(
host=configinfo.apidata["plex"]["host"],
userid="",
key=configinfo.apidata["plex"]["key"],
)
self.configload = True
else:
log().info("当前设置媒体服务器[{}]不支持".format(self.mediaservertype))
self.configload = False
except Exception as result:
log().info("配置异常错误, {}".format(result))
def start_scan_media(self):
"""
开始扫描媒体
:return True or False
"""
if not self.configload:
log().info("配置未正常加载")
return False
# 获取媒体库根文件夹
ret, itmes = self.meidiaserverclient.get_items()
if not ret:
log().info(
"获取{}媒体总列表失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
return False
ret, iteminfo = self.meidiaserverclient.get_items_count()
log().info("总媒体数量[{}]".format(iteminfo["MovieCount"] + iteminfo["SeriesCount"]))
if not ret:
log().info(
"获取{}媒体数量失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
return False
for item in self.__check_media_info__(itemlist=itmes):
if not item:
continue
self.__submit_task__(item=item)
while self.semaphore._value != self.threadnum:
time.sleep(0.1)
return ret
def __submit_task__(self, item):
"""
提交任务
:param item
"""
self.semaphore.acquire()
self.threadpool.submit(self.__start_task__, item)
def __start_task__(self, item):
"""
开始任务
:param item
"""
ret, name = self.__to_deal_with_item__(item=item)
if ret:
log().info("媒体[{}]处理完成".format(name))
else:
log().info("媒体[{}]处理失败".format(name))
self.semaphore.release()
def __check_media_info__(self, itemlist):
"""
检查媒体信息
:param itemlist 项目列表
:return True or False
"""
try:
for item in itemlist["Items"]:
if "Folder" in item["Type"] and (
"CollectionType" not in item
or "boxsets" not in item["CollectionType"]
):
ret, items = self.meidiaserverclient.get_items(parentid=item["Id"])
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]列表失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
self.meidiaserverclient.err,
)
)
continue
for tmpitem in self.__check_media_info__(itemlist=items):
yield tmpitem
else:
if "Series" in item["Type"] or "Movie" in item["Type"]:
yield item
except Exception as result:
log().info(
"文件[{}]行[{}]异常错误:{}".format(
result.__traceback__.tb_frame.f_globals["__file__"],
result.__traceback__.tb_lineno,
result,
)
)
yield {}
def __check_media_info_search__(self, item):
"""
检查媒体信息搜索是否正确
:param item 媒体信息
"""
try:
ret, iteminfo = self.meidiaserverclient.get_item_info(itemid=item["Id"])
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
self.meidiaserverclient.err,
)
)
return False
tmdbid = ""
if "Tmdb" in iteminfo["ProviderIds"]:
tmdbid = iteminfo["ProviderIds"]["Tmdb"]
elif "tmdb" in iteminfo["ProviderIds"]:
tmdbid = iteminfo["ProviderIds"]["tmdb"]
name = re.sub(
pattern="\s+-\s+\d{1,4}[k|K|p|P]|\s+\(\d{4}\)|\.\S{1,4}$",
repl="",
string=iteminfo["FileName"],
)
year = None
redata = re.search(pattern="\((\d{4})\)", string=iteminfo["FileName"])
if redata:
year = redata.group(1)
if (
name in item["Name"]
and (
year
and "ProductionYear" in iteminfo
and year in str(iteminfo["ProductionYear"])
)
and tmdbid
):
return True
mediatype = "MOV"
if "Movie" in item["Type"]:
mediatype = "MOV"
else:
mediatype = "TV"
ret, mediainfo = self.nastoolsclient.media_info(
name=name, year=year, type=mediatype
)
if not ret:
log().info(
"[{}]媒体名称[{}]与原始名称[{}]不一致可能识别错误, NasTools识别媒体[{}]失败, {}".format(
self.mediaservertype,
item["Name"],
iteminfo["FileName"],
name,
self.nastoolsclient.err,
)
)
return False
testtmdbid = None
if year and year != mediainfo["year"]:
return False
if mediainfo["tmdbid"] > 0:
testtmdbid = str(mediainfo["tmdbid"])
if not testtmdbid or tmdbid == testtmdbid:
return True
if "Series" in item["Type"]:
ret, tvinfo = self.__get_tmdb_media_info__(
mediatype=1, name=item["Name"], id=testtmdbid
)
if not ret:
return False
if len(tvinfo["seasons"]) < iteminfo["ChildCount"]:
return False
ret, searchinfo = self.meidiaserverclient.search_movie(
itemid=item["Id"], tmdbid=testtmdbid, name=name, year=year
)
if not ret:
log().info(
"{}搜索媒体[{}]ID[{}]TMDB[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
testtmdbid,
self.meidiaserverclient.err,
)
)
return False
for info in searchinfo:
if "Plex" not in self.mediaservertype:
info["Type"] = iteminfo["Type"]
info["IsFolder"] = iteminfo["IsFolder"]
ret = self.meidiaserverclient.apply_search(
itemid=item["Id"], iteminfo=info
)
if not ret:
log().info(
"{}更新媒体[{}]ID[{}]TMDB[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
testtmdbid,
self.meidiaserverclient.err,
)
)
return False
log().info(
"{}更新媒体[{}]ID[{}]TMDB[{}]更新为媒体[{}]TMDB[{}]".format(
self.mediaservertype,
item["Name"],
item["Id"],
tmdbid,
mediainfo["title"],
testtmdbid,
)
)
item["Name"] = mediainfo["title"]
break
return True
except Exception as result:
log().info(
"文件[{}]行[{}]异常错误:{}".format(
result.__traceback__.tb_frame.f_globals["__file__"],
result.__traceback__.tb_lineno,
result,
)
)
return False
def __to_deal_with_item__(self, item):
"""
处理媒体
:param item
:return True or False
"""
try:
if self.checkmediasearch:
_ = self.__check_media_info_search__(item=item)
updatename = False
updatepeople = False
updateoverview = False
ret, iteminfo = self.meidiaserverclient.get_item_info(itemid=item["Id"])
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
self.meidiaserverclient.err,
)
)
return False, item["Name"]
if "LockedFields" not in iteminfo:
iteminfo["LockedFields"] = []
tmdbid = None
imdbid = None
if "Tmdb" in iteminfo["ProviderIds"]:
tmdbid = iteminfo["ProviderIds"]["Tmdb"]
elif "tmdb" in iteminfo["ProviderIds"]:
tmdbid = iteminfo["ProviderIds"]["tmdb"]
if "Imdb" in iteminfo["ProviderIds"]:
imdbid = iteminfo["ProviderIds"]["Imdb"]
elif "imdb" in iteminfo["ProviderIds"]:
imdbid = iteminfo["ProviderIds"]["imdb"]
doubanmediainfo = None
if not self.__is_chinese__(string=item["Name"]):
if not tmdbid and not imdbid:
log().info(
"{}媒体[{}]ID[{}]Tmdb|Imdb不存在".format(
self.mediaservertype, item["Name"], item["Id"]
)
)
return False, item["Name"]
name = None
if tmdbid:
if "Series" in item["Type"]:
ret, name = self.__get_tmdb_media_name__(
mediatype=1, datatype=1, name=item["Name"], id=tmdbid
)
else:
ret, name = self.__get_tmdb_media_name__(
mediatype=2, datatype=1, name=item["Name"], id=tmdbid
)
if imdbid and not name:
if "Series" in item["Type"]:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=1, name=item["Name"], id=imdbid
)
else:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=2, name=item["Name"], id=imdbid
)
if ret:
if self.__is_chinese__(string=doubanmediainfo["title"], mode=2):
name = doubanmediainfo["title"]
if name:
originalname = iteminfo["Name"]
iteminfo["Name"] = name
if "Name" not in iteminfo["LockedFields"]:
iteminfo["LockedFields"].append("Name")
updatename = True
if self.updatepeople and "People" in iteminfo:
updatepeople = self.__update_people__(
item=item, iteminfo=iteminfo, imdbid=imdbid
)
if "Series" in item["Type"]:
ret, seasons = self.meidiaserverclient.get_items(
parentid=item["Id"], type="Season"
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
self.meidiaserverclient.err,
)
)
else:
for season in seasons["Items"]:
if "Jellyfin" in self.mediaservertype:
ret, seasoninfo = self.meidiaserverclient.get_item_info(
itemid=season["Id"]
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
season["Name"],
season["Id"],
self.meidiaserverclient.err,
)
)
continue
ret = self.__update_people__(
item=season, iteminfo=seasoninfo, imdbid=imdbid
)
if not ret:
continue
if ret:
_ = self.__refresh_people__(
item=season, iteminfo=seasoninfo
)
log().info(
"原始媒体名称[{}] 第[{}]季更新人物".format(
iteminfo["Name"], season["IndexNumber"]
)
)
ret = self.meidiaserverclient.set_item_info(
itemid=seasoninfo["Id"], iteminfo=seasoninfo
)
ret, episodes = self.meidiaserverclient.get_items(
parentid=season["Id"], type="Episode"
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
season["Name"],
season["Id"],
self.meidiaserverclient.err,
)
)
continue
for episode in episodes["Items"]:
(
ret,
episodeinfo,
) = self.meidiaserverclient.get_item_info(
itemid=episode["Id"]
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
episode["Name"],
episode["Id"],
self.meidiaserverclient.err,
)
)
else:
ret = self.__update_people__(
item=episode,
iteminfo=episodeinfo,
imdbid=imdbid,
)
if not ret:
continue
ret = self.meidiaserverclient.set_item_info(
itemid=episodeinfo["Id"], iteminfo=episodeinfo
)
if ret:
if "Jellyfin" in self.mediaservertype:
_ = self.__refresh_people__(
item=episode, iteminfo=episodeinfo
)
log().info(
"原始媒体名称[{}] 第[{}]季 第[{}]集更新人物".format(
iteminfo["Name"],
season["IndexNumber"],
episode["IndexNumber"],
)
)
if self.updateoverview:
if "Overview" not in iteminfo or not self.__is_chinese__(
string=iteminfo["Overview"]
):
if not tmdbid and not imdbid:
log().info(
"{}媒体[{}]ID[{}]Tmdb|Imdb不存在".format(
self.mediaservertype, item["Name"], item["Id"]
)
)
return False, item["Name"]
ret = False
if tmdbid:
if "Series" in item["Type"]:
ret, overview = self.__get_tmdb_media_name__(
mediatype=1, datatype=2, name=item["Name"], id=tmdbid
)
else:
ret, overview = self.__get_tmdb_media_name__(
mediatype=2, datatype=2, name=item["Name"], id=tmdbid
)
if ret:
iteminfo["Overview"] = overview
if "Overview" not in iteminfo["LockedFields"]:
iteminfo["LockedFields"].append("Overview")
updateoverview = True
elif imdbid:
if not doubanmediainfo:
if "Series" in item["Type"]:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=1, name=item["Name"], id=imdbid
)
else:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=2, name=item["Name"], id=imdbid
)
if doubanmediainfo:
if doubanmediainfo["intro"]:
iteminfo["Overview"] = doubanmediainfo["intro"]
if "Overview" not in iteminfo["LockedFields"]:
iteminfo["LockedFields"].append("Overview")
updateoverview = True
if "Series" in item["Type"]:
ret, seasons = self.meidiaserverclient.get_items(
parentid=item["Id"], type="Season"
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
self.meidiaserverclient.err,
)
)
else:
seasongroupinfo = None
groupid = None
if self.updateseasongroup:
for groupinfo in self.configinfo.systemdata["seasongroup"]:
infolist = groupinfo.split("|")
if len(infolist) < 2:
continue
if infolist[0] == item["Name"]:
groupid = infolist[1]
break
for season in seasons["Items"]:
ret, episodes = self.meidiaserverclient.get_items(
parentid=season["Id"], type="Episode"
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
season["Name"],
season["Id"],
self.meidiaserverclient.err,
)
)
continue
for episode in episodes["Items"]:
(
ret,
episodeinfo,
) = self.meidiaserverclient.get_item_info(
itemid=episode["Id"]
)
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
episode["Name"],
episode["Id"],
self.meidiaserverclient.err,
)
)
else:
imageurl = None
name = None
overview = None
ommunityrating = None
if not groupid:
if (
"Overview" not in episodeinfo
or not self.__is_chinese__(
string=episodeinfo["Overview"]
)
):
(
ret,
name,
overview,
ommunityrating,
imageurl,
) = self.__get_tmdb_tv_season_info__(
name=item["Name"],
tvid=tmdbid,
seasonid=season["IndexNumber"],
episodeid=episode["IndexNumber"],
)
if not ret:
continue
else:
if not seasongroupinfo:
(
ret,
seasongroupinfo,
) = self.__get_tmdb_tv_season_group_info__(
name=item["Name"], groupid=groupid
)
if not ret:
continue
tmdbepisodeinfo = None
for seasondata in seasongroupinfo["groups"]:
if (
seasondata["order"]
!= season["IndexNumber"]
):
continue
for episodedata in seasondata["episodes"]:
if (
episodedata["order"] + 1
!= episode["IndexNumber"]
):
continue
tmdbepisodeinfo = episodedata
break
if tmdbepisodeinfo:
break
if not tmdbepisodeinfo:
log().info(
"原始媒体名称[{}] 第[{}]季 第[{}]集未匹配到TMDB剧集组数据".format(
iteminfo["Name"],
season["IndexNumber"],
episode["IndexNumber"],
)
)
continue
if (
tmdbepisodeinfo["name"]
!= episodeinfo["Name"]
):
name = tmdbepisodeinfo["name"]
if (
"Overview" not in episodeinfo
or tmdbepisodeinfo["overview"]
!= episodeinfo["Overview"]
):
overview = tmdbepisodeinfo["overview"]
if (
"still_path" in tmdbepisodeinfo
and tmdbepisodeinfo["still_path"]
):
imageurl = "https://www.themoviedb.org/t/p/original{}".format(
tmdbepisodeinfo["still_path"]
)
if (
"CommunityRating" not in episodeinfo
or episodeinfo["CommunityRating"]
!= tmdbepisodeinfo["vote_average"]
):
ommunityrating = tmdbepisodeinfo[
"vote_average"
]
if not name and not overview and not ommunityrating:
continue
if "LockedFields" not in episodeinfo:
episodeinfo["LockedFields"] = []
if name:
episodeinfo["Name"] = name
if overview:
episodeinfo["Overview"] = overview
if ommunityrating:
episodeinfo["CommunityRating"] = ommunityrating
if "Name" not in episodeinfo["LockedFields"]:
episodeinfo["LockedFields"].append("Name")
if "Overview" not in episodeinfo["LockedFields"]:
episodeinfo["LockedFields"].append("Overview")
ret = self.meidiaserverclient.set_item_info(
itemid=episodeinfo["Id"], iteminfo=episodeinfo
)
if ret:
if overview:
log().info(
"原始媒体名称[{}] 第[{}]季 第[{}]集更新概述".format(
iteminfo["Name"],
season["IndexNumber"],
episode["IndexNumber"],
)
)
if ommunityrating:
log().info(
"原始媒体名称[{}] 第[{}]季 第[{}]集更新评分".format(
iteminfo["Name"],
season["IndexNumber"],
episode["IndexNumber"],
)
)
if imageurl:
ret = self.meidiaserverclient.set_item_image(
itemid=episodeinfo["Id"], imageurl=imageurl
)
if ret:
log().info(
"原始媒体名称[{}] 第[{}]季 第[{}]集更新图片".format(
iteminfo["Name"],
season["IndexNumber"],
episode["IndexNumber"],
)
)
if not updatename and not updatepeople and not updateoverview:
return True, item["Name"]
ret = self.meidiaserverclient.set_item_info(
itemid=iteminfo["Id"], iteminfo=iteminfo
)
if ret:
if updatename:
log().info(
"原始媒体名称[{}]更新为[{}]".format(originalname, iteminfo["Name"])
)
if updatepeople:
if "Jellyfin" in self.mediaservertype:
_ = self.__refresh_people__(item=item, iteminfo=iteminfo)
log().info("原始媒体名称[{}]更新人物".format(iteminfo["Name"]))
if updateoverview:
log().info("原始媒体名称[{}]更新概述".format(iteminfo["Name"]))
time.sleep(self.taskdonespace)
return True, item["Name"]
except Exception as result:
log().info(
"文件[{}]行[{}]异常错误:{}".format(
result.__traceback__.tb_frame.f_globals["__file__"],
result.__traceback__.tb_lineno,
result,
)
)
return False, item["Name"]
def __refresh_people__(self, item, iteminfo):
"""
刷新人物元数据
:param iteminfo 项目信息
:return True of False
"""
try:
ret, newiteminfo = self.meidiaserverclient.get_item_info(itemid=item["Id"])
if not ret:
log().info(
"获取{}媒体[{}]ID[{}]信息失败, {}".format(
self.mediaservertype,
item["Name"],
item["Id"],
self.meidiaserverclient.err,
)
)
return False
for people, newpeople in zip(iteminfo["People"], newiteminfo["People"]):
if (
people["Id"] in newpeople["Id"]
or people["Name"] not in newpeople["Name"]
):
continue
ret, peopleinfo = self.meidiaserverclient.get_item_info(
itemid=people["Id"]
)
if not ret:
log().info(
"获取{}人物信息失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
continue
ret, newpeopleinfo = self.meidiaserverclient.get_item_info(
itemid=newpeople["Id"]
)
if not ret:
log().info(
"获取{}人物信息失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
continue
newpeopleinfo["ProviderIds"] = peopleinfo["ProviderIds"]
ret = self.meidiaserverclient.set_item_info(
itemid=newpeopleinfo["Id"], iteminfo=newpeopleinfo
)
if not ret:
log().info(
"更新{}人物信息失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
continue
ret, newpeopleinfo = self.meidiaserverclient.get_item_info(
itemid=newpeople["Id"]
)
if not ret:
log().info(
"获取{}人物信息失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
continue
"""
ret = self.meidiaserverclient.refresh(peopleinfo['Id'])
if not ret:
log().info('刷新{}人物信息失败, {}'.format(self.mediaservertype, self.meidiaserverclient.err))
"""
time.sleep(self.taskdonespace)
return True
except Exception as result:
log().info("异常错误: {}".format(result))
return False
def __update_people__(self, item, iteminfo, imdbid):
"""
更新人物
:param item 项目
:param iteminfo 项目信息
:param imdbid IMDB ID
:return True of False
"""
updatepeople = False
try:
doubanmediainfo = None
doubancelebritiesinfo = None
needdelpeople = []
for people in iteminfo["People"]:
ret, peopleinfo = self.meidiaserverclient.get_item_info(
itemid=people["Id"]
)
if not ret:
log().info(
"获取{}人物信息失败, {}".format(
self.mediaservertype, self.meidiaserverclient.err
)
)
continue
peopleimdbid = None
peopletmdbid = None
peoplename = None
if "Tmdb" in peopleinfo["ProviderIds"]:
peopletmdbid = peopleinfo["ProviderIds"]["Tmdb"]
elif "tmdb" in peopleinfo["ProviderIds"]:
peopletmdbid = peopleinfo["ProviderIds"]["tmdb"]
if "Imdb" in peopleinfo["ProviderIds"]:
peopleimdbid = peopleinfo["ProviderIds"]["Imdb"]
elif "imdb" in peopleinfo["ProviderIds"]:
peopleimdbid = peopleinfo["ProviderIds"]["imdb"]
if not self.__is_chinese__(string=people["Name"], mode=1):
if "LockedFields" not in peopleinfo:
peopleinfo["LockedFields"] = []
if not peopletmdbid and not peopleimdbid:
log().info(
"{}人物[{}]ID[{}]Tmdb|Imdb不存在".format(
self.mediaservertype,
peopleinfo["Name"],
peopleinfo["Id"],
)
)
needdelpeople.append(peopleinfo["Id"])
continue
if peopleimdbid and imdbid:
if not doubanmediainfo:
if "Series" in item["Type"]:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=1, name=item["Name"], id=imdbid
)
else:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=2, name=item["Name"], id=imdbid
)
if doubanmediainfo and not doubancelebritiesinfo:
if "Series" in item["Type"]:
(
ret,
doubancelebritiesinfo,
) = self.__get_douban_media_celebrities_info__(
mediatype=1,
name=item["Name"],
id=doubanmediainfo["id"],
)
else:
(
ret,
doubancelebritiesinfo,
) = self.__get_douban_media_celebrities_info__(
mediatype=2,
name=item["Name"],
id=doubanmediainfo["id"],
)
if doubancelebritiesinfo:
ret, celebrities = self.__get_people_info__(
celebritiesinfo=doubancelebritiesinfo,
people=people,
imdbid=peopleimdbid,
)
if ret and self.__is_chinese__(string=celebrities["name"]):
peoplename = re.sub(
pattern="\s+", repl="", string=celebrities["name"]
)
if not peoplename:
if self.__is_chinese__(string=peopleinfo["Name"], mode=2):
peoplename = re.sub(
pattern="\s+", repl="", string=peopleinfo["Name"]
)
peoplename = zhconv.convert(peopleinfo["Name"], "zh-cn")
elif peopletmdbid:
ret, peoplename = self.__get_tmdb_person_name(
name=peopleinfo["Name"], personid=peopletmdbid
)
if peoplename:
originalpeoplename = people["Name"]
peopleinfo["Name"] = peoplename
if "Name" not in peopleinfo["LockedFields"]:
peopleinfo["LockedFields"].append("Name")
people["Name"] = peoplename
if "Emby" in self.mediaservertype:
ret = self.meidiaserverclient.set_item_info(
itemid=peopleinfo["Id"], iteminfo=peopleinfo
)
else:
ret = True
if ret:
log().info(
"原始人物名称[{}]更新为[{}]".format(
originalpeoplename, peoplename
)
)
updatepeople = True
elif "Role" not in people or not self.__is_chinese__(
string=people["Role"], mode=2
):
if imdbid:
if not doubanmediainfo:
if "Series" in item["Type"]:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=1, name=item["Name"], id=imdbid
)
else:
ret, doubanmediainfo = self.__get_douban_media_info__(
mediatype=2, name=item["Name"], id=imdbid
)
if doubanmediainfo and not doubancelebritiesinfo:
if "Series" in item["Type"]:
(
ret,
doubancelebritiesinfo,
) = self.__get_douban_media_celebrities_info__(
mediatype=1,
name=item["Name"],
id=doubanmediainfo["id"],
)
else:
(
ret,
doubancelebritiesinfo,