This commit is contained in:
Louis Vézina 2020-01-19 23:00:03 -05:00
parent f9ba0b0fb8
commit 025ecb941c
3 changed files with 34 additions and 12 deletions

View File

@ -1,7 +1,6 @@
from __future__ import absolute_import
from collections import deque
import json
import time
class EventStream:
@ -26,7 +25,7 @@ class EventStream:
:type movie: str
"""
msg = {"type": type, "action": action, "series": series, "episode": episode, "movie": movie}
self.queue.append("retry: 1000\ndata:" + json.dumps(msg) + "\n\n")
self.queue.append("data:" + json.dumps(msg) + "\n\n")
def read(self):
"""
@ -35,11 +34,8 @@ class EventStream:
"""
while True:
if self.queue:
return self.queue.popleft()
else:
return ':'
time.sleep(0.1)
while self.queue:
yield self.queue.popleft()
event_stream = EventStream()

View File

@ -326,14 +326,15 @@ def movies_full_scan_subtitles():
def series_scan_subtitles(no):
episodes = database.execute("SELECT path FROM table_episodes WHERE sonarrSeriesId=?", (no,))
episodes = database.execute("SELECT path FROM table_episodes WHERE sonarrSeriesId=? ORDER BY sonarrEpisodeId",
(no,))
for episode in episodes:
store_subtitles(episode['path'], path_replace(episode['path']))
def movies_scan_subtitles(no):
movies = database.execute("SELECT path FROM table_movies WHERE radarrId=?", (no,))
movies = database.execute("SELECT path FROM table_movies WHERE radarrId=? ORDER BY radarrId", (no,))
for movie in movies:
store_subtitles_movie(movie['path'], path_replace_movie(movie['path']))

View File

@ -252,6 +252,8 @@
<script src="{{ url_for('static',filename='datatables/dataTables.responsive.min.js') }}"></script>
<script src="{{ url_for('static',filename='plugins/datatables.net-bs4/js/dataTables.bootstrap4.min.js') }}"></script>
<script src="{{ url_for('static',filename='js/custom.js') }}"></script>
<!-- Polyfill for older browsers without native support for the HTML5 EventSource API. -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=EventSource"></script>
<script>
$(document).ready(function () {
@ -261,8 +263,15 @@
$('[data-toggle="tooltip"]').tooltip({html: true});
});
var events = new EventSource('{{ url_for('api.events') }}');
events.onmessage = function (event) {
events = new EventSource('{{ url_for('api.events') }}');
// Handler for when the stream is opened (either the first time or after a reconnect).
events.addEventListener('open', function(event) {
console.log('Stream is open');
}, false);
// Handler for new messages.
events.addEventListener('message', event => {
var event_json = JSON.parse(event.data);
if (event_json.series === {{id}}) {
if (event_json.type === 'series' && event_json.action === 'update' && event_json.episode == null) {
@ -302,7 +311,19 @@
}
}
}
};
}, false);
// Handler for a dropped connection or the server closing the connection.
events.addEventListener('error', function(event) {
switch (event.target.readyState) {
case EventSource.CONNECTING:
console.log('Reconnecting...');
break;
case EventSource.CLOSED:
console.log('Connection failed, will not reconnect');
break;
}
}, false);
function BadgesAjax() {
$.ajax({
@ -328,6 +349,10 @@
})
}
});
$(window).on('beforeunload', function() {
events.close();
});
</script>
{% endblock tail_js %}
{% block tail %}