this repo has no description
at main 3.9 kB view raw
1#!/usr/bin/env python3 2from plexapi.myplex import MyPlexAccount 3import sys 4import os 5 6password = os.popen('security find-internet-password -a "indirect" -s "app.plex.tv" -w').read().rstrip() 7account = MyPlexAccount('indirect', password) 8 9print("Connecting...") 10plex = account.resource('Poe').connect() 11 12shows = plex.library.section("TV Shows") 13 14#shows = None 15#for library in plex.library.sections(): 16# if library.type == "show": 17# if shows == None: 18# shows = library 19# else: 20# print("Multiple show libraries. Please hardcode it with plex.library.sectionByID.") 21# sys.exit(1) 22 23if len(sys.argv) < 2: 24 print("Usage: [series] [subbed/dubbed]") 25 sys.exit(1) 26 27series = sys.argv[1] 28mode = sys.argv[2] 29 30search = shows.search(series) 31 32if len(search) == 1: 33 show = search[0] 34elif len(search) == 0: 35 print("No show found.") 36 sys.exit(1) 37else: 38 for i, show in enumerate(search): 39 print("%s: %s" % (i, show.title)) 40 index = int(input("# ")) 41 show = search[index] 42 43def check_subtitles_full(subtitles, retry_flag=False): 44 for subtitle in subtitles: 45 if subtitle.languageCode != "eng": 46 continue 47 title = (subtitle.title or "").lower() 48 if "sign" in title or "commentary" in title: 49 continue 50 if "Forced" in subtitle._data.get("displayTitle"): 51 continue 52 return subtitle 53 return None 54 55def check_subtitles_forced(subtitles): 56 selected_subtitles = None 57 for subtitle in subtitles: 58 if subtitle.languageCode != "eng": 59 continue 60 if not "Forced" in subtitle._data.get("displayTitle"): 61 continue 62 return subtitle 63 return None 64 65def process_media(part, debug_name, mode): 66 is_english = False 67 english_audio = None 68 japanese_audio = None 69 for audio in part.audioStreams(): 70 if audio.languageCode == "eng" and english_audio == None: 71 is_english = True 72 english_audio = audio 73 elif audio.languageCode == "jpn" and japanese_audio == None: 74 japanese_audio = audio 75 if not is_english: 76 print("Skipping: %s is not in English." % debug_name) 77 return 78 79 if mode == "dubbed": 80 subtitles = check_subtitles_forced(part.subtitleStreams()) 81 if subtitles != None: 82 sid = subtitles.id 83 else: 84 sid = 0 85 print("Set dubbed: %s" % debug_name) 86 plex.query('/library/parts/%s?audioStreamID=%s&allParts=1' % (part.id, english_audio.id),method=plex._session.put) 87 plex.query('/library/parts/%s?subtitleStreamID=%s&allParts=1' % (part.id, sid),method=plex._session.put) 88 elif mode == "subbed": 89 if japanese_audio == "None": 90 print("Skipping: %s is English only." % debug_name) 91 return 92 subtitles = check_subtitles_full(part.subtitleStreams()) 93 if subtitles == None: 94 print("Skipping: %s has no subtitles." % debug_name) 95 return 96 sid = subtitles.id 97 print("Set subbed: %s (%s)" % (debug_name,subtitles.title)) 98 plex.query('/library/parts/%s?audioStreamID=%s&allParts=1' % (part.id, japanese_audio.id),method=plex._session.put) 99 plex.query('/library/parts/%s?subtitleStreamID=%s&allParts=1' % (part.id, sid),method=plex._session.put) 100 101 102for season in show.seasons(): 103 for episode in season.episodes(): 104 debug_name = "S%02iE%02i" % (season.index, episode.index) 105 episode = episode.reload() 106 medias = episode.media 107 if len(medias) > 1: 108 print("Warning: %s has multiple media files." % debug_name) 109 for media in medias: 110 parts = media.parts 111 if len(parts) > 1: 112 print("Warning: %s has multiple media parts." % debug_name) 113 for part in parts: 114 process_media(part, debug_name, mode)