|
1 | 1 | from plexapi.server import PlexServer
|
2 | 2 | from plexapi.media import SubtitleStream
|
3 | 3 | import os
|
| 4 | +import traceback |
4 | 5 |
|
5 |
| -def main(): |
6 |
| - # Connect to Plex Media Server. Replace PLEX_TOKEN below with your Plex token. How to get token: https://www.plexopedia.com/plex-media-server/general/plex-token/ |
7 |
| - baseurl = 'http://localhost:32400' |
8 |
| - token = 'PLEX_TOKEN' |
9 |
| - |
10 |
| - script_dir = os.path.dirname(os.path.abspath(__file__)) |
11 |
| - token_file = os.path.join(script_dir, 'token.txt') |
| 6 | +def report_error(error_message): |
| 7 | + github_issue_url = "https://github.com/RileyXX/PlexPreferNonForcedSubs/issues/new?assignees=&labels=&projects=&template=bug_report.yml" |
| 8 | + traceback_info = traceback.format_exc() |
12 | 9 |
|
13 |
| - try: |
14 |
| - with open(token_file, 'r') as f: |
15 |
| - token = f.read().strip() |
16 |
| - except FileNotFoundError: |
17 |
| - pass |
| 10 | + print("\n--- ERROR ---") |
| 11 | + print(error_message) |
| 12 | + print("Please submit the error to GitHub with the following information:") |
| 13 | + print("-" * 50) |
| 14 | + print(traceback_info) |
| 15 | + print("-" * 50) |
| 16 | + print(f"Submit the error here: {github_issue_url}") |
| 17 | + print("-" * 50) |
18 | 18 |
|
19 |
| - if token == 'PLEX_TOKEN': |
20 |
| - print(f'\nHow to get your Plex token: https://www.plexopedia.com/plex-media-server/general/plex-token/\n') |
21 |
| - token = input("Enter your Plex token: ") |
22 |
| - with open(token_file, 'w') as f: |
23 |
| - f.write(token) |
| 19 | +def main(): |
| 20 | + # Connect to Plex Media Server. Replace PLEX_TOKEN below with your Plex token. How to get token: https://www.plexopedia.com/plex-media-server/general/plex-token/ |
| 21 | + baseurl = 'http://localhost:32400' |
| 22 | + token = 'PLEX_TOKEN' |
24 | 23 |
|
25 |
| - plex = PlexServer(baseurl, token) |
| 24 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 25 | + token_file = os.path.join(script_dir, 'token.txt') |
26 | 26 |
|
27 |
| - # Movies |
28 |
| - table_headers = ['Title', 'Year', 'Status', 'Changes'] |
29 |
| - title_width = 70 |
30 |
| - year_width = 5 |
31 |
| - status_width = 20 |
32 |
| - changes_width = 8 |
| 27 | + try: |
| 28 | + with open(token_file, 'r') as f: |
| 29 | + token = f.read().strip() |
| 30 | + except FileNotFoundError: |
| 31 | + pass |
33 | 32 |
|
34 |
| - print("\n" + "-" * 114 + "\nMovies\n" + "-" * 114) |
35 |
| - print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else status_width if i == 2 else changes_width) for i, h in enumerate(table_headers)])}\033[0m') |
| 33 | + if token == 'PLEX_TOKEN': |
| 34 | + print(f'\nHow to get your Plex token: https://www.plexopedia.com/plex-media-server/general/plex-token/\n') |
| 35 | + token = input("Enter your Plex token: ") |
| 36 | + with open(token_file, 'w') as f: |
| 37 | + f.write(token) |
36 | 38 |
|
37 |
| - for section in plex.library.sections(): |
38 |
| - if section.type == 'movie': |
39 |
| - for movie in section.all(): |
40 |
| - english_subs = movie.subtitleStreams() |
41 |
| - if english_subs is not None: |
42 |
| - english_subs = [stream for stream in english_subs if stream is not None and stream.languageCode == 'eng'] |
43 |
| - non_forced_english_subs = [stream for stream in english_subs if stream is not None and (not stream.forced or ('forced' not in getattr(stream, 'title', '').lower()))] |
44 |
| - forced_english_subs = [stream for stream in english_subs if stream is not None and (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())] |
45 |
| - part = movie.media[0].parts[0] |
46 |
| - partsid = part.id |
47 |
| - if forced_english_subs and non_forced_english_subs: |
48 |
| - non_forced_english_subs[0].setDefault() |
49 |
| - print(f'\033[92m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English (Non-Forced)".ljust(status_width)} | {"Y".ljust(changes_width)}\033[0m') |
50 |
| - elif non_forced_english_subs and not forced_english_subs: |
51 |
| - print(f'{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English".ljust(status_width)} | {"N".ljust(changes_width)}') |
52 |
| - elif not non_forced_english_subs and not forced_english_subs: |
53 |
| - print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"No Subtitles Found".ljust(status_width)} | {"N".ljust(changes_width)}\033[0m') |
54 |
| - else: |
55 |
| - print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English (Forced)".ljust(status_width)} | {"N (Error)".ljust(changes_width)}\033[0m') |
| 39 | + plex = PlexServer(baseurl, token) |
56 | 40 |
|
57 |
| - # Shows |
58 |
| - table_headers = ['Title', 'Year', 'Season #', 'Episode #', 'Status', 'Changes'] |
59 |
| - title_width = 42 |
60 |
| - year_width = 5 |
61 |
| - season_width = 11 |
62 |
| - episode_width = 11 |
63 |
| - status_width = 20 |
64 |
| - changes_width = 8 |
65 |
| - season_row_width = 4 |
66 |
| - episode_row_width = 3 |
| 41 | + # Movies |
| 42 | + table_headers = ['Title', 'Year', 'Status', 'Changes'] |
| 43 | + title_width = 70 |
| 44 | + year_width = 5 |
| 45 | + status_width = 20 |
| 46 | + changes_width = 8 |
67 | 47 |
|
68 |
| - print("\n" + "-" * 114 + "\nShows\n" + "-" * 114) |
69 |
| - print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else season_width if i == 2 else episode_width if i == 3 else status_width if i == 4 else changes_width) for i, h in enumerate(table_headers)])}\033[0m') |
| 48 | + print("\n" + "-" * 114 + "\nMovies\n" + "-" * 114) |
| 49 | + print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else status_width if i == 2 else changes_width) for i, h in enumerate(table_headers)])}\033[0m') |
70 | 50 |
|
71 |
| - for section in plex.library.sections(): |
72 |
| - if section.type == 'show': |
73 |
| - for show in section.all(): |
74 |
| - for episode in show.episodes(): |
75 |
| - english_subs = episode.subtitleStreams() |
| 51 | + for section in plex.library.sections(): |
| 52 | + if section.type == 'movie': |
| 53 | + for movie in section.all(): |
| 54 | + english_subs = movie.subtitleStreams() |
76 | 55 | if english_subs is not None:
|
77 | 56 | english_subs = [stream for stream in english_subs if stream is not None and stream.languageCode == 'eng']
|
78 | 57 | non_forced_english_subs = [stream for stream in english_subs if stream is not None and (not stream.forced or ('forced' not in getattr(stream, 'title', '').lower()))]
|
79 | 58 | forced_english_subs = [stream for stream in english_subs if stream is not None and (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())]
|
80 |
| - part = episode.media[0].parts[0] |
| 59 | + part = movie.media[0].parts[0] |
81 | 60 | partsid = part.id
|
82 | 61 | if forced_english_subs and non_forced_english_subs:
|
83 | 62 | non_forced_english_subs[0].setDefault()
|
84 |
| - print(f'\033[92m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English (Non-Forced)".ljust(status_width)} | {"Y".ljust(changes_width)}\033[0m') |
| 63 | + print(f'\033[92m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English (Non-Forced)".ljust(status_width)} | {"Y".ljust(changes_width)}\033[0m') |
85 | 64 | elif non_forced_english_subs and not forced_english_subs:
|
86 |
| - print(f'{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English".ljust(status_width)} | {"N".ljust(changes_width)}') |
| 65 | + print(f'{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English".ljust(status_width)} | {"N".ljust(changes_width)}') |
87 | 66 | elif not non_forced_english_subs and not forced_english_subs:
|
88 |
| - print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"No Subtitles Found".ljust(status_width)} | {"N".ljust(changes_width)}\033[0m') |
| 67 | + print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"No Subtitles Found".ljust(status_width)} | {"N".ljust(changes_width)}\033[0m') |
89 | 68 | else:
|
90 |
| - print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English (Forced)".ljust(status_width)} | {"N (Error)".ljust(changes_width)}\033[0m') |
| 69 | + print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English (Forced)".ljust(status_width)} | {"N (Error)".ljust(changes_width)}\033[0m') |
| 70 | + |
| 71 | + # Shows |
| 72 | + table_headers = ['Title', 'Year', 'Season #', 'Episode #', 'Status', 'Changes'] |
| 73 | + title_width = 42 |
| 74 | + year_width = 5 |
| 75 | + season_width = 11 |
| 76 | + episode_width = 11 |
| 77 | + status_width = 20 |
| 78 | + changes_width = 8 |
| 79 | + season_row_width = 4 |
| 80 | + episode_row_width = 3 |
| 81 | + |
| 82 | + print("\n" + "-" * 114 + "\nShows\n" + "-" * 114) |
| 83 | + print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else season_width if i == 2 else episode_width if i == 3 else status_width if i == 4 else changes_width) for i, h in enumerate(table_headers)])}\033[0m') |
| 84 | + |
| 85 | + for section in plex.library.sections(): |
| 86 | + if section.type == 'show': |
| 87 | + for show in section.all(): |
| 88 | + for episode in show.episodes(): |
| 89 | + english_subs = episode.subtitleStreams() |
| 90 | + if english_subs is not None: |
| 91 | + english_subs = [stream for stream in english_subs if stream is not None and stream.languageCode == 'eng'] |
| 92 | + non_forced_english_subs = [stream for stream in english_subs if stream is not None and (not stream.forced or ('forced' not in getattr(stream, 'title', '').lower()))] |
| 93 | + forced_english_subs = [stream for stream in english_subs if stream is not None and (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())] |
| 94 | + part = episode.media[0].parts[0] |
| 95 | + partsid = part.id |
| 96 | + if forced_english_subs and non_forced_english_subs: |
| 97 | + non_forced_english_subs[0].setDefault() |
| 98 | + print(f'\033[92m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English (Non-Forced)".ljust(status_width)} | {"Y".ljust(changes_width)}\033[0m') |
| 99 | + elif non_forced_english_subs and not forced_english_subs: |
| 100 | + print(f'{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English".ljust(status_width)} | {"N".ljust(changes_width)}') |
| 101 | + elif not non_forced_english_subs and not forced_english_subs: |
| 102 | + print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"No Subtitles Found".ljust(status_width)} | {"N".ljust(changes_width)}\033[0m') |
| 103 | + else: |
| 104 | + print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English (Forced)".ljust(status_width)} | {"N (Error)".ljust(changes_width)}\033[0m') |
| 105 | + |
| 106 | + except Exception as e: |
| 107 | + error_message = "An error occurred while running the script." |
| 108 | + errorHandling.report_error(error_message) |
91 | 109 |
|
92 | 110 | if __name__ == '__main__':
|
93 | 111 | main()
|
0 commit comments