diff --git a/bazarr/SSE.py b/bazarr/SSE.py index 4f98674bc..fb0f0e50e 100644 --- a/bazarr/SSE.py +++ b/bazarr/SSE.py @@ -10,15 +10,21 @@ class EventStream: """ def __init__(self): - self.queue = deque(maxlen=10) + self.queue = deque(maxlen=100) - def write(self, msg): + def write(self, type=None, series=None, episode=None, movie=None): """ - :param msg: The message to display. - :type msg: str + :param type: The type of element. + :type type: str + :param type: The series id. + :type type: str + :param type: The episode id. + :type type: str + :param type: The movie id. + :type type: str """ - - self.queue.append("data:" + msg + "\n\n") + msg = {"type": type, "series": series, "episode": episode, "movie": movie} + self.queue.append("data:" + json.dumps(msg) + "\n\n") def read(self): """ @@ -29,6 +35,8 @@ class EventStream: while True: if self.queue: return self.queue.popleft() + else: + return ':' time.sleep(0.1) diff --git a/bazarr/api.py b/bazarr/api.py index 41f9e32ba..5008659b4 100644 --- a/bazarr/api.py +++ b/bazarr/api.py @@ -21,6 +21,7 @@ from list_subtitles import store_subtitles, store_subtitles_movie, series_scan_s list_missing_subtitles, list_missing_subtitles_movies from utils import history_log, history_log_movie from get_providers import get_providers, get_providers_auth, list_throttled_providers +from SSE import event_stream from subliminal_patch.core import SUBTITLE_EXTENSIONS @@ -44,6 +45,11 @@ class Badges(Resource): return jsonify(result) +class Events(Resource): + def get(self): + return Response(event_stream.read(), mimetype="text/event-stream") + + class Series(Resource): def get(self): start = request.args.get('start') or 0 @@ -102,10 +108,17 @@ class Episodes(Resource): length = request.args.get('length') or -1 draw = request.args.get('draw') - seriesId = request.args.get('id') + seriesId = request.args.get('seriesid') + episodeId = request.args.get('episodeid') row_count = database.execute("SELECT COUNT(*) as count FROM table_episodes WHERE sonarrSeriesId=?", (seriesId,), only_one=True)['count'] - if seriesId: + if episodeId: + result = database.execute("SELECT * FROM table_episodes WHERE sonarrEpisodeId=?", (episodeId,)) + desired_languages = database.execute("SELECT languages FROM table_shows WHERE sonarrSeriesId=?", + (seriesId,), only_one=True)['languages'] + if desired_languages == "None": + desired_languages = '[]' + elif seriesId: result = database.execute("SELECT * FROM table_episodes WHERE sonarrSeriesId=? ORDER BY season DESC, " "episode DESC", (seriesId,)) desired_languages = database.execute("SELECT languages FROM table_shows WHERE sonarrSeriesId=?", @@ -115,6 +128,9 @@ class Episodes(Resource): else: return "Series ID not provided", 400 for item in result: + # Add Datatables rowId + item.update({"DT_RowId": 'row_' + str(item['sonarrEpisodeId'])}) + # Parse subtitles if item['subtitles']: item.update({"subtitles": ast.literal_eval(item['subtitles'])}) @@ -612,6 +628,7 @@ class WantedMovies(Resource): api.add_resource(Badges, '/badges') +api.add_resource(Events, '/events') api.add_resource(Series, '/series') api.add_resource(Episodes, '/episodes') api.add_resource(EpisodesSubtitlesDelete, '/episodes_subtitles_delete') diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index abf47920b..9684cc1ec 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -26,6 +26,7 @@ from helper import path_replace, path_replace_movie, path_replace_reverse, \ from queueconfig import notifications from embedded_subs_reader import embedded_subs_reader +from SSE import event_stream import six gc.enable() @@ -90,7 +91,7 @@ def store_subtitles(original_path, reversed_path): database.execute("UPDATE table_episodes SET subtitles=? WHERE path=?", (str(actual_subtitles), original_path)) - matching_episodes = database.execute("SELECT sonarrEpisodeId FROM table_episodes WHERE path=?", + matching_episodes = database.execute("SELECT sonarrEpisodeId, sonarrSeriesId FROM table_episodes WHERE path=?", (original_path,)) for episode in matching_episodes: @@ -104,6 +105,8 @@ def store_subtitles(original_path, reversed_path): logging.debug('BAZARR ended subtitles indexing for this file: ' + reversed_path) + event_stream.write(type='episode', series=episode['sonarrSeriesId'], episode=episode['sonarrEpisodeId']) + return actual_subtitles diff --git a/views/_main.html b/views/_main.html index 20f822334..fe6dda959 100644 --- a/views/_main.html +++ b/views/_main.html @@ -254,38 +254,54 @@ {% endblock tail_js %} diff --git a/views/episodes.html b/views/episodes.html index f50c4f7b6..baaefea04 100644 --- a/views/episodes.html +++ b/views/episodes.html @@ -38,15 +38,22 @@ {% block bcleft %}
+
{% endblock bcleft %} {% block bcright %}
-
Some page settings
+
{% endblock bcright %} @@ -241,14 +248,18 @@ }); var table = $('#episodes').DataTable({ - "processing": false, + "processing": true, "serverSide": true, + language: { + zeroRecords: 'No Episodes Found For This Series', + processing: "Loading Episodes..." + }, "searching": false, "ordering": false, "lengthChange": false, "responsive": true, "pageLength": 100, - "ajax": "{{ url_for('api.episodes') }}?id={{id}}", + "ajax": "{{ url_for('api.episodes') }}?seriesid={{id}}", rowGroup: { dataSrc: function(data) { return 'Season ' + data.season; @@ -340,14 +351,6 @@ data: values, beforeSend: function() { cell.html('
Loading...
'); - }, - complete: function(data) { - table.ajax.reload(null, false); - if (data['responseJSON']) { - console.log(data['responseJSON']); - } else { - console.log("Unable to delete subtitle."); - } } }); }); @@ -373,14 +376,6 @@ data: values, beforeSend: function() { cell.html('
Loading...
'); - }, - complete: function(data) { - table.ajax.reload(null, false); - if (data['responseJSON']) { - console.log(data['responseJSON'][0]); - } else { - console.log("No subtitle found."); - } } }); }); @@ -520,13 +515,7 @@ cell.html('
Loading...
'); }, complete: function(data) { - $('#episodes').DataTable().ajax.reload(null, false); $('#manualSearchModal').modal('hide'); - if (data['responseJSON']) { - console.log(data['responseJSON'][0]); - } else { - console.log("No subtitle downloaded."); - } } }); }); @@ -568,7 +557,6 @@ contentType: false, type: 'POST', success: function(){ - $('#episodes').DataTable().ajax.reload(null, false); $('#uploadModal').modal('hide'); } });