diff --git a/bazarr.py b/bazarr.py index 8b078261b..f440da735 100644 --- a/bazarr.py +++ b/bazarr.py @@ -7,6 +7,7 @@ import sqlite3 import itertools import operator import requests +import pycountry from PIL import Image from io import BytesIO from fdsend import send_file @@ -18,6 +19,7 @@ from get_general_settings import * from get_sonarr_settings import * from list_subtitles import * from get_subtitle import * +from utils import * @route('/static/:path#.+#', name='static') def static(path): @@ -81,8 +83,8 @@ def episodes(no): series_details = c.execute("SELECT title, overview, poster, fanart, hearing_impaired FROM table_shows WHERE sonarrSeriesId LIKE ?", (str(no),)).fetchone() sqlite3.enable_callback_tracebacks(True) - episodes = c.execute("SELECT title, path_substitution(path), season, episode, subtitles, sonarrSeriesId, missing_subtitles(path) FROM table_episodes WHERE sonarrSeriesId LIKE ?", (str(no),)).fetchall() - episodes=reversed(sorted(episodes, key=operator.itemgetter(2))) + episodes = c.execute("SELECT title, path_substitution(path), season, episode, subtitles, sonarrSeriesId, missing_subtitles(path), sonarrEpisodeId FROM table_episodes WHERE sonarrSeriesId LIKE ?", (str(no),)).fetchall() + episodes = reversed(sorted(episodes, key=operator.itemgetter(2))) seasons_list = [] for key,season in itertools.groupby(episodes,operator.itemgetter(2)): seasons_list.append(list(season)) @@ -94,8 +96,9 @@ def episodes(no): def history(): db = sqlite3.connect('bazarr.db') c = db.cursor() - c.execute("SELECT table_history.action, table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_history.timestamp, table_history.description, table_history.sonarrSeriesId FROM table_history INNER JOIN table_shows on table_shows.sonarrSeriesId = table_history.sonarrSeriesId INNER JOIN table_episodes on table_episodes.sonarrEpisodeId = table_history.sonarrEpisodeId ORDER BY id LIMIT 15") + c.execute("SELECT table_history.action, table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_history.timestamp, table_history.description, table_history.sonarrSeriesId FROM table_history INNER JOIN table_shows on table_shows.sonarrSeriesId = table_history.sonarrSeriesId INNER JOIN table_episodes on table_episodes.sonarrEpisodeId = table_history.sonarrEpisodeId ORDER BY id DESC LIMIT 15") data = c.fetchall() + data = reversed(sorted(data, key=operator.itemgetter(4))) c.close() return template('history', rows=data) @@ -128,11 +131,15 @@ def system(): @route('/remove_subtitles', method='GET') def remove_subtitles(): episodePath = request.GET.episodePath + language = request.GET.language subtitlesPath = request.GET.subtitlesPath sonarrSeriesId = request.GET.sonarrSeriesId + sonarrEpisodeId = request.GET.sonarrEpisodeId try: os.remove(subtitlesPath) + result = pycountry.languages.lookup(language).name + " subtitles deleted from disk." + history_log(0, sonarrSeriesId, sonarrEpisodeId, result) except OSError: pass store_subtitles(episodePath) @@ -144,9 +151,11 @@ def get_subtitle(): language = request.GET.language hi = request.GET.hi sonarrSeriesId = request.GET.sonarrSeriesId + sonarrEpisodeId = request.GET.sonarrEpisodeId try: - download_subtitle(episodePath, language, hi, None) + result = download_subtitle(episodePath, language, hi, None) + history_log(1, sonarrSeriesId, sonarrEpisodeId, result) store_subtitles(episodePath) redirect('/episodes/' + sonarrSeriesId) except OSError: diff --git a/get_subtitle.py b/get_subtitle.py index f90160f43..f76495dc2 100644 --- a/get_subtitle.py +++ b/get_subtitle.py @@ -1,6 +1,7 @@ import os from babelfish import * from subliminal import * +from pycountry import * # configure the cache region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'}) @@ -10,6 +11,9 @@ def download_subtitle(path, language, hi, providers): best_subtitles = download_best_subtitles([video], {Language(language)}, providers=providers, hearing_impaired=hi) best_subtitle = best_subtitles[video][0] - return save_subtitles(video, [best_subtitle]) + result = save_subtitles(video, [best_subtitle]) + downloaded_provider = str(result[0]).strip('<>').split(' ')[0][:-8] + downloaded_language = pycountry.languages.lookup(str(str(result[0]).strip('<>').split(' ')[2].strip('[]'))).name + message = downloaded_language + " subtitles downloaded from " + downloaded_provider + "." -#download_subtitle('Z:\Series TV\Vikings\Season 03\Vikings.S03E03.720p.HDTV.x264-KILLERS.mkv', 'fra', False, None) + return message diff --git a/utils.py b/utils.py index 83971c341..7d1620f54 100644 --- a/utils.py +++ b/utils.py @@ -1,44 +1,16 @@ -import datetime +import sqlite3 +import time -def pretty_date(time=False): - """ - Get a datetime object or a int() Epoch timestamp and return a - pretty string like 'an hour ago', 'Yesterday', '3 months ago', - 'just now', etc - """ - from datetime import datetime - now = datetime.now() - if type(time) is int: - diff = now - datetime.fromtimestamp(time) - elif isinstance(time,datetime): - diff = now - time - elif not time: - diff = now - now - second_diff = diff.seconds - day_diff = diff.days +def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): + # Open database connection + db = sqlite3.connect('bazarr.db') + c = db.cursor() - if day_diff < 0: - return '' + # Get Sonarr API URL from database config table + history = c.execute('''INSERT INTO table_history(action, sonarrSeriesId, sonarrEpisodeId, timestamp, description) VALUES (?, ?, ?, ?, ?)''', (action, sonarrSeriesId, sonarrEpisodeId, time.time(), description)) - if day_diff == 0: - if second_diff < 10: - return "just now" - if second_diff < 60: - return str(second_diff) + " seconds ago" - if second_diff < 120: - return "a minute ago" - if second_diff < 3600: - return str(second_diff / 60) + " minutes ago" - if second_diff < 7200: - return "an hour ago" - if second_diff < 86400: - return str(second_diff / 3600) + " hours ago" - if day_diff == 1: - return "Yesterday" - if day_diff < 7: - return str(day_diff) + " days ago" - if day_diff < 31: - return str(day_diff / 7) + " weeks ago" - if day_diff < 365: - return str(day_diff / 30) + " months ago" - return str(day_diff / 365) + " years ago" + # Commit changes to DB + db.commit() + + # Close database connection + db.close() diff --git a/views/episodes.tpl b/views/episodes.tpl index e1611de5f..fd320d2d4 100644 --- a/views/episodes.tpl +++ b/views/episodes.tpl @@ -158,7 +158,7 @@ %if actual_languages is not None: %for language in actual_languages: %if language[1] is not None: - + {{language[0]}} @@ -174,9 +174,9 @@ %missing_languages = ast.literal_eval(episode[6]) %if missing_languages is not None: %for language in missing_languages: - + {{language}} - + %end %end diff --git a/views/history.tpl b/views/history.tpl index c446e6306..ab8d9adda 100644 --- a/views/history.tpl +++ b/views/history.tpl @@ -82,7 +82,7 @@ %import time - %from utils import * + %import pretty %for row in rows: @@ -104,7 +104,7 @@ {{row[3]}}
- {{pretty_date(row[4])}} + {{pretty.date(int(row[4]))}}
{{row[5]}}