Skip to content

Commit

Permalink
Improved readability and maintenance by splitting get_subtitle.py int…
Browse files Browse the repository at this point in the history
…o multiple files
  • Loading branch information
morpheus65535 committed Jan 3, 2022
1 parent c83d661 commit 722014d
Show file tree
Hide file tree
Showing 57 changed files with 2,392 additions and 2,240 deletions.
6 changes: 3 additions & 3 deletions bazarr/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import codecs

from pyga.requests import Event, Page, Tracker, Session, Visitor, Config
from pyga.requests import Event, Tracker, Session, Visitor, Config
from pyga.entities import CustomVariable

from get_args import args
Expand Down Expand Up @@ -39,7 +39,7 @@ def track_event(category=None, action=None, label=None):
else:
visitor = Visitor()
visitor.unique_id = random.randint(0, 0x7fffffff)
except:
except Exception:
visitor = Visitor()
visitor.unique_id = random.randint(0, 0x7fffffff)

Expand All @@ -61,7 +61,7 @@ def track_event(category=None, action=None, label=None):

try:
tracker.track_event(event, session, visitor)
except:
except Exception:
logging.debug("BAZARR unable to track event.")
pass
else:
Expand Down
4 changes: 2 additions & 2 deletions bazarr/api/badges/badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Badges(Resource):
@authenticate
def get(self):
episodes_conditions = [(TableEpisodes.missing_subtitles is not None),
episodes_conditions = [(TableEpisodes.missing_subtitles.is_null(False)),
(TableEpisodes.missing_subtitles != '[]')]
episodes_conditions += get_exclusion_clause('series')
missing_episodes = TableEpisodes.select(TableShows.tags,
Expand All @@ -26,7 +26,7 @@ def get(self):
.where(reduce(operator.and_, episodes_conditions))\
.count()

movies_conditions = [(TableMovies.missing_subtitles is not None),
movies_conditions = [(TableMovies.missing_subtitles.is_null(False)),
(TableMovies.missing_subtitles != '[]')]
movies_conditions += get_exclusion_clause('movie')
missing_movies = TableMovies.select(TableMovies.tags,
Expand Down
2 changes: 1 addition & 1 deletion bazarr/api/episodes/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..utils import authenticate, postprocessEpisode
from utils import blacklist_log, delete_subtitles, blacklist_delete_all, blacklist_delete
from helper import path_mappings
from get_subtitle import episode_download_subtitles
from get_subtitle.mass_download import episode_download_subtitles
from event_handler import event_stream


Expand Down
11 changes: 4 additions & 7 deletions bazarr/api/episodes/episodes_subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from database import TableEpisodes, get_audio_profile_languages, get_profile_id
from ..utils import authenticate
from helper import path_mappings
from get_providers import get_providers, get_providers_auth
from get_subtitle import generate_subtitles, manual_upload_subtitle
from get_subtitle.upload import manual_upload_subtitle
from get_subtitle.download import generate_subtitles
from utils import history_log, delete_subtitles
from notifier import send_notifications
from list_subtitles import store_subtitles
Expand All @@ -36,9 +36,7 @@ def patch(self):

title = episodeInfo['title']
episodePath = path_mappings.path_replace(episodeInfo['path'])
sceneName = episodeInfo['scene_name']
audio_language = episodeInfo['audio_language']
if sceneName is None: sceneName = "None"
sceneName = episodeInfo['scene_name'] or "None"

language = request.form.get('language')
hi = request.form.get('hi').capitalize()
Expand Down Expand Up @@ -94,9 +92,8 @@ def post(self):

title = episodeInfo['title']
episodePath = path_mappings.path_replace(episodeInfo['path'])
sceneName = episodeInfo['scene_name']
sceneName = episodeInfo['scene_name'] or "None"
audio_language = episodeInfo['audio_language']
if sceneName is None: sceneName = "None"

language = request.form.get('language')
forced = True if request.form.get('forced') == 'true' else False
Expand Down
8 changes: 4 additions & 4 deletions bazarr/api/episodes/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get(self):

upgradable_episodes_conditions = [(TableHistory.action.in_(query_actions)),
(TableHistory.timestamp > minimum_timestamp),
(TableHistory.score is not None)]
(TableHistory.score.is_null(False))]
upgradable_episodes_conditions += get_exclusion_clause('series')
upgradable_episodes = TableHistory.select(TableHistory.video_path,
fn.MAX(TableHistory.timestamp).alias('timestamp'),
Expand All @@ -61,7 +61,7 @@ def get(self):
if int(upgradable_episode['score']) < 360:
upgradable_episodes_not_perfect.append(upgradable_episode)

query_conditions = [(TableEpisodes.title is not None)]
query_conditions = [(TableEpisodes.title.is_null(False))]
if episodeid:
query_conditions.append((TableEpisodes.sonarrEpisodeId == episodeid))
query_condition = reduce(operator.and_, query_conditions)
Expand Down Expand Up @@ -100,7 +100,7 @@ def get(self):
item.update({"upgradable": False})
if {"video_path": str(item['path']), "timestamp": float(item['timestamp']), "score": str(item['score']),
"tags": str(item['tags']), "monitored": str(item['monitored']),
"seriesType": str(item['seriesType'])} in upgradable_episodes_not_perfect:
"seriesType": str(item['seriesType'])} in upgradable_episodes_not_perfect: # noqa: E129
if os.path.isfile(path_mappings.path_replace(item['subtitles_path'])):
item.update({"upgradable": True})

Expand Down Expand Up @@ -128,6 +128,6 @@ def get(self):

count = TableHistory.select()\
.join(TableEpisodes, on=(TableHistory.sonarrEpisodeId == TableEpisodes.sonarrEpisodeId))\
.where(TableEpisodes.title is not None).count()
.where(TableEpisodes.title.is_null(False)).count()

return jsonify(data=episode_history, total=count)
2 changes: 1 addition & 1 deletion bazarr/api/movies/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..utils import authenticate, postprocessMovie
from utils import blacklist_log_movie, delete_subtitles, blacklist_delete_all_movie, blacklist_delete_movie
from helper import path_mappings
from get_subtitle import movies_download_subtitles
from get_subtitle.mass_download import movies_download_subtitles
from event_handler import event_stream


Expand Down
10 changes: 5 additions & 5 deletions bazarr/api/movies/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get(self):

upgradable_movies_conditions = [(TableHistoryMovie.action.in_(query_actions)),
(TableHistoryMovie.timestamp > minimum_timestamp),
(TableHistoryMovie.score is not None)]
(TableHistoryMovie.score.is_null(False))]
upgradable_movies_conditions += get_exclusion_clause('movie')
upgradable_movies = TableHistoryMovie.select(TableHistoryMovie.video_path,
fn.MAX(TableHistoryMovie.timestamp).alias('timestamp'),
Expand All @@ -61,7 +61,7 @@ def get(self):
if int(upgradable_movie['score']) < 120:
upgradable_movies_not_perfect.append(upgradable_movie)

query_conditions = [(TableMovies.title is not None)]
query_conditions = [(TableMovies.title.is_null(False))]
if radarrid:
query_conditions.append((TableMovies.radarrId == radarrid))
query_condition = reduce(operator.and_, query_conditions)
Expand Down Expand Up @@ -95,7 +95,7 @@ def get(self):
# Mark movies as upgradable or not
item.update({"upgradable": False})
if {"video_path": str(item['path']), "timestamp": float(item['timestamp']), "score": str(item['score']),
"tags": str(item['tags']), "monitored": str(item['monitored'])} in upgradable_movies_not_perfect:
"tags": str(item['tags']), "monitored": str(item['monitored'])} in upgradable_movies_not_perfect: # noqa: E129
if os.path.isfile(path_mappings.path_replace_movie(item['subtitles_path'])):
item.update({"upgradable": True})

Expand All @@ -117,13 +117,13 @@ def get(self):
if item['action'] not in [0, 4, 5]:
for blacklisted_item in blacklist_db:
if blacklisted_item['provider'] == item['provider'] and blacklisted_item['subs_id'] == item[
'subs_id']:
'subs_id']: # noqa: E125
item.update({"blacklisted": True})
break

count = TableHistoryMovie.select()\
.join(TableMovies, on=(TableHistoryMovie.radarrId == TableMovies.radarrId))\
.where(TableMovies.title is not None)\
.where(TableMovies.title.is_null(False))\
.count()

return jsonify(data=movie_history, total=count)
3 changes: 2 additions & 1 deletion bazarr/api/movies/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from ..utils import authenticate, postprocessMovie, None_Keys
from list_subtitles import list_missing_subtitles_movies, movies_scan_subtitles
from event_handler import event_stream
from get_subtitle import movies_download_subtitles, wanted_search_missing_subtitles_movies
from get_subtitle.wanted import wanted_search_missing_subtitles_movies
from get_subtitle.mass_download import movies_download_subtitles


class Movies(Resource):
Expand Down
13 changes: 4 additions & 9 deletions bazarr/api/movies/movies_subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from database import TableMovies, get_audio_profile_languages, get_profile_id
from ..utils import authenticate
from helper import path_mappings
from get_providers import get_providers, get_providers_auth
from get_subtitle import manual_upload_subtitle, generate_subtitles
from get_subtitle.upload import manual_upload_subtitle
from get_subtitle.download import generate_subtitles
from utils import history_log_movie, delete_subtitles
from notifier import send_notifications_movie
from list_subtitles import store_subtitles_movie
Expand All @@ -36,8 +36,7 @@ def patch(self):
.get()

moviePath = path_mappings.path_replace_movie(movieInfo['path'])
sceneName = movieInfo['sceneName']
if sceneName is None: sceneName = 'None'
sceneName = movieInfo['sceneName'] or 'None'

title = movieInfo['title']
audio_language = movieInfo['audio_language']
Expand All @@ -46,9 +45,6 @@ def patch(self):
hi = request.form.get('hi').capitalize()
forced = request.form.get('forced').capitalize()

providers_list = get_providers()
providers_auth = get_providers_auth()

audio_language_list = get_audio_profile_languages(movie_id=radarrId)
if len(audio_language_list) > 0:
audio_language = audio_language_list[0]['name']
Expand Down Expand Up @@ -97,8 +93,7 @@ def post(self):
.get()

moviePath = path_mappings.path_replace_movie(movieInfo['path'])
sceneName = movieInfo['sceneName']
if sceneName is None: sceneName = 'None'
sceneName = movieInfo['sceneName'] or 'None'

title = movieInfo['title']
audioLanguage = movieInfo['audio_language']
Expand Down
4 changes: 2 additions & 2 deletions bazarr/api/providers/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def get(self):
history = request.args.get('history')
if history and history not in False_Keys:
providers = list(TableHistory.select(TableHistory.provider)
.where(TableHistory.provider != None and TableHistory.provider != "manual")
.where(TableHistory.provider is not None and TableHistory.provider != "manual")
.dicts())
providers += list(TableHistoryMovie.select(TableHistoryMovie.provider)
.where(TableHistoryMovie.provider != None and TableHistoryMovie.provider != "manual")
.where(TableHistoryMovie.provider is not None and TableHistoryMovie.provider != "manual")
.dicts())
providers_list = list(set([x['provider'] for x in providers]))
providers_dicts = []
Expand Down
8 changes: 3 additions & 5 deletions bazarr/api/providers/providers_episodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from database import TableEpisodes, TableShows, get_audio_profile_languages, get_profile_id
from helper import path_mappings
from get_providers import get_providers, get_providers_auth
from get_subtitle import manual_search, manual_download_subtitle
from get_subtitle.manual import manual_search, manual_download_subtitle
from utils import history_log
from config import settings
from notifier import send_notifications
Expand All @@ -31,9 +31,8 @@ def get(self):

title = episodeInfo['title']
episodePath = path_mappings.path_replace(episodeInfo['path'])
sceneName = episodeInfo['scene_name']
sceneName = episodeInfo['scene_name'] or "None"
profileId = episodeInfo['profileId']
if sceneName is None: sceneName = "None"

providers_list = get_providers()
providers_auth = get_providers_auth()
Expand All @@ -58,8 +57,7 @@ def post(self):

title = episodeInfo['title']
episodePath = path_mappings.path_replace(episodeInfo['path'])
sceneName = episodeInfo['scene_name']
if sceneName is None: sceneName = "None"
sceneName = episodeInfo['scene_name'] or "None"

language = request.form.get('language')
hi = request.form.get('hi').capitalize()
Expand Down
9 changes: 3 additions & 6 deletions bazarr/api/providers/providers_movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from database import TableMovies, get_audio_profile_languages, get_profile_id
from helper import path_mappings
from get_providers import get_providers, get_providers_auth
from get_subtitle import manual_search, manual_download_subtitle
from get_subtitle.manual import manual_search, manual_download_subtitle
from utils import history_log_movie
from config import settings
from notifier import send_notifications_movie
Expand All @@ -30,9 +30,8 @@ def get(self):

title = movieInfo['title']
moviePath = path_mappings.path_replace_movie(movieInfo['path'])
sceneName = movieInfo['sceneName']
sceneName = movieInfo['sceneName'] or "None"
profileId = movieInfo['profileId']
if sceneName is None: sceneName = "None"

providers_list = get_providers()
providers_auth = get_providers_auth()
Expand All @@ -57,9 +56,7 @@ def post(self):

title = movieInfo['title']
moviePath = path_mappings.path_replace_movie(movieInfo['path'])
sceneName = movieInfo['sceneName']
if sceneName is None: sceneName = "None"
audio_language = movieInfo['audio_language']
sceneName = movieInfo['sceneName'] or "None"

language = request.form.get('language')
hi = request.form.get('hi').capitalize()
Expand Down
3 changes: 2 additions & 1 deletion bazarr/api/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from database import get_exclusion_clause, TableEpisodes, TableShows
from list_subtitles import list_missing_subtitles, series_scan_subtitles
from get_subtitle import series_download_subtitles, wanted_search_missing_subtitles_series
from get_subtitle.mass_download import series_download_subtitles
from get_subtitle.wanted import wanted_search_missing_subtitles_series
from ..utils import authenticate, postprocessSeries, None_Keys
from event_handler import event_stream

Expand Down
2 changes: 1 addition & 1 deletion bazarr/api/subtitles/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..utils import authenticate
from subsyncer import subsync
from utils import translate_subtitles_file, subtitles_apply_mods
from get_subtitle import store_subtitles, store_subtitles_movie
from list_subtitles import store_subtitles, store_subtitles_movie
from config import settings


Expand Down
1 change: 1 addition & 0 deletions bazarr/api/subtitles/subtitles_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask import request, jsonify
from flask_restful import Resource
from subliminal_patch.core import guessit

from ..utils import authenticate


Expand Down
8 changes: 4 additions & 4 deletions bazarr/api/system/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def get(self):
history = request.args.get('history')
if history and history not in False_Keys:
languages = list(TableHistory.select(TableHistory.language)
.where(TableHistory.language != None)
.where(TableHistory.language.is_null(False))
.dicts())
languages += list(TableHistoryMovie.select(TableHistoryMovie.language)
.where(TableHistoryMovie.language != None)
.where(TableHistoryMovie.language.is_null(False))
.dicts())
languages_list = list(set([l['language'].split(':')[0] for l in languages]))
languages_list = list(set([lang['language'].split(':')[0] for lang in languages]))
languages_dicts = []
for language in languages_list:
code2 = None
Expand All @@ -40,7 +40,7 @@ def get(self):
# Compatibility: Use false temporarily
'enabled': False
})
except:
except Exception:
continue
return jsonify(sorted(languages_dicts, key=itemgetter('name')))

Expand Down
1 change: 0 additions & 1 deletion bazarr/api/system/searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ def get(self):
movies = list(movies)
search_list += movies


return jsonify(search_list)
6 changes: 3 additions & 3 deletions bazarr/api/webhooks/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from bs4 import BeautifulSoup as bso

from database import TableEpisodes, TableShows, TableMovies
from get_subtitle import episode_download_subtitles, movies_download_subtitles
from get_subtitle.mass_download import episode_download_subtitles, movies_download_subtitles
from ..utils import authenticate


Expand Down Expand Up @@ -47,7 +47,7 @@ def post(self):
headers={"User-Agent": os.environ["SZ_USER_AGENT"]})
soup = bso(r.content, "html.parser")
series_imdb_id = soup.find('a', {'class': re.compile(r'SeriesParentLink__ParentTextLink')})['href'].split('/')[2]
except:
except Exception:
return '', 404
else:
sonarrEpisodeId = TableEpisodes.select(TableEpisodes.sonarrEpisodeId) \
Expand All @@ -63,7 +63,7 @@ def post(self):
else:
try:
movie_imdb_id = [x['imdb'] for x in ids if 'imdb' in x][0]
except:
except Exception:
return '', 404
else:
radarrId = TableMovies.select(TableMovies.radarrId)\
Expand Down
2 changes: 1 addition & 1 deletion bazarr/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8

from flask import Flask, redirect, render_template, request, url_for
from flask import Flask
from flask_socketio import SocketIO
import os

Expand Down

0 comments on commit 722014d

Please sign in to comment.