Skip to content

Commit b6ccc85

Browse files
authored
Merge pull request #15 from RileyXX/add-error-handling-with-github-submission-link
Add detailed error handling with Github bug report submission link
2 parents af9a536 + 216cf8d commit b6ccc85

File tree

2 files changed

+88
-69
lines changed

2 files changed

+88
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,111 @@
11
from plexapi.server import PlexServer
22
from plexapi.media import SubtitleStream
33
import os
4+
import traceback
45

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()
129

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)
1818

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'
2423

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')
2626

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
3332

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)
3638

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)
5640

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
6747

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')
7050

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()
7655
if english_subs is not None:
7756
english_subs = [stream for stream in english_subs if stream is not None and stream.languageCode == 'eng']
7857
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()))]
7958
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]
8160
partsid = part.id
8261
if forced_english_subs and non_forced_english_subs:
8362
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')
8564
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)}')
8766
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')
8968
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)
91109

92110
if __name__ == '__main__':
93111
main()

setup.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
1111
long_description = "\n" + fh.read()
1212

13-
VERSION = '2.1.4'
13+
VERSION = '2.1.6'
1414
DESCRIPTION = 'This script will set all movies and shows in your local Plex library to English non forced subtitles by default.'
1515
LONG_DESCRIPTION = 'This python script will set all movies and shows in your local Plex library to English non forced subtitles by default. The subtitle selections will apply to your Plex profile and be remembered on other devices.'
1616

@@ -35,5 +35,6 @@
3535
'console_scripts': [
3636
'PlexPreferNonForcedSubs = PlexPreferNonForcedSubs.PlexPreferNonForcedSubs:main'
3737
]
38-
}
38+
},
39+
python_requires='>=3.5'
3940
)

0 commit comments

Comments
 (0)