2017-09-16 00:49:46 +00:00
from bottle import route , run , template , static_file , request , redirect
import bottle
bottle . debug ( True )
bottle . TEMPLATES . clear ( )
2017-10-22 01:09:04 +00:00
import os
2017-10-22 00:43:32 +00:00
bottle . TEMPLATE_PATH . insert ( 0 , os . path . join ( os . path . dirname ( __file__ ) , ' views/ ' ) )
2017-09-16 00:49:46 +00:00
import sqlite3
import itertools
import operator
import requests
2017-09-17 13:02:16 +00:00
import pycountry
2017-10-23 03:00:11 +00:00
import pretty
import datetime
2017-09-16 00:49:46 +00:00
from PIL import Image
from io import BytesIO
from fdsend import send_file
import urllib
from init_db import *
from get_languages import *
2017-09-19 10:43:14 +00:00
from get_providers import *
2017-10-03 02:59:45 +00:00
from get_series import *
from get_episodes import *
2017-09-16 00:49:46 +00:00
from get_general_settings import *
from get_sonarr_settings import *
2017-11-09 21:13:14 +00:00
from check_update import *
2017-09-16 00:49:46 +00:00
from list_subtitles import *
2017-09-17 00:11:47 +00:00
from get_subtitle import *
2017-09-17 13:02:16 +00:00
from utils import *
2017-10-23 03:00:11 +00:00
from scheduler import *
2017-09-16 00:49:46 +00:00
2017-10-16 23:27:19 +00:00
import logging
from logging . handlers import TimedRotatingFileHandler
logger = logging . getLogger ( ' waitress ' )
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-10-16 23:27:19 +00:00
c = db . cursor ( )
c . execute ( " SELECT log_level FROM table_settings_general " )
log_level = c . fetchone ( )
log_level = log_level [ 0 ]
if log_level is None :
log_level = " WARNING "
log_level = getattr ( logging , log_level )
c . close ( )
class OneLineExceptionFormatter ( logging . Formatter ) :
def formatException ( self , exc_info ) :
"""
Format an exception so that it prints on a single line .
"""
result = super ( OneLineExceptionFormatter , self ) . formatException ( exc_info )
return repr ( result ) # or format into one line however you want to
def format ( self , record ) :
s = super ( OneLineExceptionFormatter , self ) . format ( record )
if record . exc_text :
s = s . replace ( ' \n ' , ' ' ) + ' | '
return s
def configure_logging ( ) :
2017-10-20 12:59:21 +00:00
fh = TimedRotatingFileHandler ( os . path . join ( os . path . dirname ( __file__ ) , ' data/log/bazarr.log ' ) , when = " midnight " , interval = 1 , backupCount = 7 )
2017-10-16 23:27:19 +00:00
f = OneLineExceptionFormatter ( ' %(asctime)s | %(levelname)s | %(message)s | ' ,
' %d / % m/ % Y % H: % M: % S ' )
fh . setFormatter ( f )
root = logging . getLogger ( )
root . setLevel ( log_level )
root . addHandler ( fh )
configure_logging ( )
2017-11-09 21:13:14 +00:00
@route ( ' / ' )
def redirect_root ( ) :
redirect ( base_url )
@route ( base_url + ' static/:path#.+# ' , name = ' static ' )
2017-09-16 00:49:46 +00:00
def static ( path ) :
2017-10-22 01:25:31 +00:00
return static_file ( path , root = os . path . join ( os . path . dirname ( __file__ ) , ' static ' ) )
2017-09-16 00:49:46 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' image_proxy/<url:path> ' , method = ' GET ' )
2017-09-16 00:49:46 +00:00
def image_proxy ( url ) :
img_pil = Image . open ( BytesIO ( requests . get ( url_sonarr_short + ' / ' + url ) . content ) )
img_buffer = BytesIO ( )
img_pil . tobytes ( )
img_pil . save ( img_buffer , img_pil . format )
img_buffer . seek ( 0 )
return send_file ( img_buffer , ctype = img_pil . format )
2017-11-09 21:13:14 +00:00
@route ( base_url )
2017-09-16 00:49:46 +00:00
def series ( ) :
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-09-16 00:49:46 +00:00
db . create_function ( " path_substitution " , 1 , path_replace )
c = db . cursor ( )
c . execute ( " SELECT tvdbId, title, path_substitution(path), languages, hearing_impaired, sonarrSeriesId, poster FROM table_shows ORDER BY title " )
data = c . fetchall ( )
c . execute ( " SELECT code2, name FROM table_settings_languages WHERE enabled = 1 " )
languages = c . fetchall ( )
c . close ( )
2017-10-18 18:54:24 +00:00
output = template ( ' series ' , rows = data , languages = languages , base_url = base_url )
2017-09-16 00:49:46 +00:00
return output
2017-11-09 21:13:14 +00:00
@route ( base_url + ' edit_series/<no:int> ' , method = ' POST ' )
2017-09-16 00:49:46 +00:00
def edit_series ( no ) :
2017-10-18 18:54:24 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-09-16 00:49:46 +00:00
lang = request . forms . getall ( ' languages ' )
if len ( lang ) > 0 :
if lang [ 0 ] == ' ' :
lang = None
else :
pass
else :
lang = None
hi = request . forms . get ( ' hearing_impaired ' )
if hi == " on " :
hi = " True "
else :
hi = " False "
2017-10-20 12:59:21 +00:00
conn = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-09-16 00:49:46 +00:00
c = conn . cursor ( )
c . execute ( " UPDATE table_shows SET languages = ?, hearing_impaired = ? WHERE tvdbId LIKE ? " , ( str ( lang ) , hi , no ) )
conn . commit ( )
c . close ( )
2017-10-16 23:27:19 +00:00
list_missing_subtitles ( no )
2017-10-18 18:54:24 +00:00
redirect ( ref )
2017-09-16 00:49:46 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' update_series ' )
2017-10-03 02:59:45 +00:00
def update_series_list ( ) :
2017-10-18 18:54:24 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-10-03 02:59:45 +00:00
update_series ( )
2017-10-18 18:54:24 +00:00
redirect ( ref )
2017-10-03 02:59:45 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' update_all_episodes ' )
2017-10-03 02:59:45 +00:00
def update_all_episodes_list ( ) :
2017-10-18 18:54:24 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-10-03 02:59:45 +00:00
update_all_episodes ( )
2017-10-18 18:54:24 +00:00
redirect ( ref )
2017-10-03 02:59:45 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' add_new_episodes ' )
2017-10-16 23:27:19 +00:00
def add_new_episodes_list ( ) :
2017-10-18 18:54:24 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-10-16 23:27:19 +00:00
add_new_episodes ( )
2017-10-18 18:54:24 +00:00
redirect ( ref )
2017-10-16 23:27:19 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' episodes/<no:int> ' , method = ' GET ' )
2017-09-16 00:49:46 +00:00
def episodes ( no ) :
2017-10-20 12:59:21 +00:00
conn = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-09-16 00:49:46 +00:00
conn . create_function ( " path_substitution " , 1 , path_replace )
c = conn . cursor ( )
series_details = [ ]
2017-09-17 00:11:47 +00:00
series_details = c . execute ( " SELECT title, overview, poster, fanart, hearing_impaired FROM table_shows WHERE sonarrSeriesId LIKE ? " , ( str ( no ) , ) ) . fetchone ( )
2017-09-16 00:49:46 +00:00
2017-10-16 23:27:19 +00:00
episodes = c . execute ( " SELECT title, path_substitution(path), season, episode, subtitles, sonarrSeriesId, missing_subtitles, sonarrEpisodeId FROM table_episodes WHERE sonarrSeriesId LIKE ? ORDER BY episode ASC " , ( str ( no ) , ) ) . fetchall ( )
2017-09-17 13:02:16 +00:00
episodes = reversed ( sorted ( episodes , key = operator . itemgetter ( 2 ) ) )
2017-09-16 00:49:46 +00:00
seasons_list = [ ]
for key , season in itertools . groupby ( episodes , operator . itemgetter ( 2 ) ) :
seasons_list . append ( list ( season ) )
c . close ( )
2017-10-18 18:54:24 +00:00
return template ( ' episodes ' , no = no , details = series_details , seasons = seasons_list , url_sonarr_short = url_sonarr_short , base_url = base_url )
2017-10-16 23:27:19 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' scan_disk/<no:int> ' , method = ' GET ' )
2017-10-16 23:27:19 +00:00
def scan_disk ( no ) :
ref = request . environ [ ' HTTP_REFERER ' ]
series_scan_subtitles ( no )
redirect ( ref )
2017-11-09 21:13:14 +00:00
@route ( base_url + ' search_missing_subtitles/<no:int> ' , method = ' GET ' )
2017-10-16 23:27:19 +00:00
def search_missing_subtitles ( no ) :
ref = request . environ [ ' HTTP_REFERER ' ]
series_download_subtitles ( no )
redirect ( ref )
2017-09-16 00:49:46 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' history ' )
2017-09-16 00:49:46 +00:00
def history ( ) :
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-09-16 00:49:46 +00:00
c = db . cursor ( )
2017-10-03 02:59:45 +00:00
c . execute ( " SELECT COUNT(*) FROM table_history " )
row_count = c . fetchone ( )
row_count = row_count [ 0 ]
page = request . GET . page
if page == " " :
page = " 1 "
offset = ( int ( page ) - 1 ) * 15
max_page = ( row_count / 15 ) + 1
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 OFFSET ? " , ( offset , ) )
2017-09-16 00:49:46 +00:00
data = c . fetchall ( )
2017-09-17 13:02:16 +00:00
data = reversed ( sorted ( data , key = operator . itemgetter ( 4 ) ) )
2017-09-16 00:49:46 +00:00
c . close ( )
2017-10-18 18:54:24 +00:00
return template ( ' history ' , rows = data , row_count = row_count , page = page , max_page = max_page , base_url = base_url )
2017-10-03 02:59:45 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' wanted ' )
2017-10-03 02:59:45 +00:00
def wanted ( ) :
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-10-03 02:59:45 +00:00
db . create_function ( " path_substitution " , 1 , path_replace )
c = db . cursor ( )
c . execute ( " SELECT COUNT(*) FROM table_episodes WHERE missing_subtitles != ' [] ' " )
missing_count = c . fetchone ( )
missing_count = missing_count [ 0 ]
page = request . GET . page
if page == " " :
page = " 1 "
offset = ( int ( page ) - 1 ) * 15
max_page = ( missing_count / 15 ) + 1
2017-10-16 23:27:19 +00:00
c . execute ( " SELECT table_shows.title, table_episodes.season || ' x ' || table_episodes.episode, table_episodes.title, table_episodes.missing_subtitles, table_episodes.sonarrSeriesId, path_substitution(table_episodes.path), table_shows.hearing_impaired, table_episodes.sonarrEpisodeId FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.missing_subtitles != ' [] ' ORDER BY table_episodes._rowid_ DESC LIMIT 15 OFFSET ? " , ( offset , ) )
2017-10-03 02:59:45 +00:00
data = c . fetchall ( )
c . close ( )
2017-10-18 18:54:24 +00:00
return template ( ' wanted ' , rows = data , missing_count = missing_count , page = page , max_page = max_page , base_url = base_url )
2017-09-16 00:49:46 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' wanted_search_missing_subtitles ' )
2017-10-23 21:24:22 +00:00
def wanted_search_missing_subtitles_list ( ) :
2017-10-16 23:27:19 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-10-23 03:00:11 +00:00
wanted_search_missing_subtitles ( )
2017-10-16 23:27:19 +00:00
redirect ( ref )
2017-11-09 21:13:14 +00:00
@route ( base_url + ' settings ' )
2017-09-16 00:49:46 +00:00
def settings ( ) :
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-09-16 00:49:46 +00:00
c = db . cursor ( )
c . execute ( " SELECT * FROM table_settings_general " )
settings_general = c . fetchone ( )
2017-09-19 10:43:14 +00:00
c . execute ( " SELECT * FROM table_settings_languages ORDER BY name " )
2017-09-16 00:49:46 +00:00
settings_languages = c . fetchall ( )
2017-09-19 10:43:14 +00:00
c . execute ( " SELECT * FROM table_settings_providers ORDER BY name " )
2017-09-16 00:49:46 +00:00
settings_providers = c . fetchall ( )
c . execute ( " SELECT * FROM table_settings_sonarr " )
settings_sonarr = c . fetchone ( )
c . close ( )
2017-10-18 18:54:24 +00:00
return template ( ' settings ' , settings_general = settings_general , settings_languages = settings_languages , settings_providers = settings_providers , settings_sonarr = settings_sonarr , base_url = base_url )
2017-09-19 10:43:14 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' save_settings ' , method = ' POST ' )
2017-09-19 10:43:14 +00:00
def save_settings ( ) :
2017-10-18 18:54:24 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-10-20 12:59:21 +00:00
conn = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-10-03 02:59:45 +00:00
c = conn . cursor ( )
2017-09-19 10:43:14 +00:00
2017-10-03 02:59:45 +00:00
settings_general_ip = request . forms . get ( ' settings_general_ip ' )
settings_general_port = request . forms . get ( ' settings_general_port ' )
settings_general_baseurl = request . forms . get ( ' settings_general_baseurl ' )
2017-10-16 23:27:19 +00:00
settings_general_loglevel = request . forms . get ( ' settings_general_loglevel ' )
2017-10-03 02:59:45 +00:00
settings_general_sourcepath = request . forms . getall ( ' settings_general_sourcepath ' )
settings_general_destpath = request . forms . getall ( ' settings_general_destpath ' )
settings_general_pathmapping = [ ]
settings_general_pathmapping . extend ( [ list ( a ) for a in zip ( settings_general_sourcepath , settings_general_destpath ) ] )
2017-10-28 01:41:24 +00:00
settings_general_branch = request . forms . get ( ' settings_general_branch ' )
settings_general_automatic = request . forms . get ( ' settings_general_automatic ' )
2017-10-28 02:09:03 +00:00
if settings_general_automatic is None :
settings_general_automatic = ' False '
else :
settings_general_automatic = ' True '
2017-10-28 02:00:39 +00:00
c . execute ( " UPDATE table_settings_general SET ip = ?, port = ?, base_url = ?, path_mapping = ?, log_level = ?, branch=?, auto_update=? " , ( settings_general_ip , settings_general_port , settings_general_baseurl , str ( settings_general_pathmapping ) , settings_general_loglevel , settings_general_branch , settings_general_automatic ) )
2017-10-03 02:59:45 +00:00
settings_sonarr_ip = request . forms . get ( ' settings_sonarr_ip ' )
settings_sonarr_port = request . forms . get ( ' settings_sonarr_port ' )
settings_sonarr_baseurl = request . forms . get ( ' settings_sonarr_baseurl ' )
settings_sonarr_ssl = request . forms . get ( ' settings_sonarr_ssl ' )
if settings_sonarr_ssl is None :
settings_sonarr_ssl = ' False '
2017-09-19 10:43:14 +00:00
else :
2017-10-03 02:59:45 +00:00
settings_sonarr_ssl = ' True '
settings_sonarr_apikey = request . forms . get ( ' settings_sonarr_apikey ' )
c . execute ( " UPDATE table_settings_sonarr SET ip = ?, port = ?, base_url = ?, ssl = ?, apikey = ? " , ( settings_sonarr_ip , settings_sonarr_port , settings_sonarr_baseurl , settings_sonarr_ssl , settings_sonarr_apikey ) )
settings_subliminal_providers = request . forms . getall ( ' settings_subliminal_providers ' )
c . execute ( " UPDATE table_settings_providers SET enabled = 0 " )
for item in settings_subliminal_providers :
c . execute ( " UPDATE table_settings_providers SET enabled = ' 1 ' WHERE name = ? " , ( item , ) )
settings_subliminal_languages = request . forms . getall ( ' settings_subliminal_languages ' )
c . execute ( " UPDATE table_settings_languages SET enabled = 0 " )
for item in settings_subliminal_languages :
c . execute ( " UPDATE table_settings_languages SET enabled = ' 1 ' WHERE code2 = ? " , ( item , ) )
2017-09-19 10:43:14 +00:00
conn . commit ( )
c . close ( )
2017-10-03 02:59:45 +00:00
2017-10-18 18:54:24 +00:00
redirect ( ref )
2017-09-16 00:49:46 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' check_update ' )
2017-11-11 05:54:19 +00:00
def check_update ( ) :
2017-11-09 21:13:14 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-11-11 05:54:19 +00:00
check_and_apply_update ( )
2017-11-09 21:13:14 +00:00
redirect ( ref )
@route ( base_url + ' system ' )
2017-09-16 00:49:46 +00:00
def system ( ) :
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-09-16 00:49:46 +00:00
c = db . cursor ( )
c . execute ( " SELECT * FROM table_scheduler " )
2017-10-16 23:27:19 +00:00
tasks = c . fetchall ( )
2017-09-16 00:49:46 +00:00
c . close ( )
2017-10-16 23:27:19 +00:00
logs = [ ]
2017-10-20 12:59:21 +00:00
for line in reversed ( open ( os . path . join ( os . path . dirname ( __file__ ) , ' data/log/bazarr.log ' ) ) . readlines ( ) ) :
2017-10-16 23:27:19 +00:00
logs . append ( line . rstrip ( ) )
2017-10-23 03:00:11 +00:00
task_list = [ ]
for job in scheduler . get_jobs ( ) :
task_list . append ( [ job . name , job . trigger . interval . __str__ ( ) , pretty . date ( job . next_run_time . replace ( tzinfo = None ) ) ] )
2017-10-16 23:27:19 +00:00
2017-10-23 03:00:11 +00:00
return template ( ' system ' , tasks = tasks , logs = logs , base_url = base_url , task_list = task_list )
2017-10-16 23:27:19 +00:00
2017-11-09 21:13:14 +00:00
@route ( base_url + ' remove_subtitles ' , method = ' POST ' )
2017-09-16 00:49:46 +00:00
def remove_subtitles ( ) :
2017-10-16 23:27:19 +00:00
episodePath = request . forms . get ( ' episodePath ' )
language = request . forms . get ( ' language ' )
subtitlesPath = request . forms . get ( ' subtitlesPath ' )
sonarrSeriesId = request . forms . get ( ' sonarrSeriesId ' )
sonarrEpisodeId = request . forms . get ( ' sonarrEpisodeId ' )
2017-09-16 00:49:46 +00:00
try :
os . remove ( subtitlesPath )
2017-09-17 13:02:16 +00:00
result = pycountry . languages . lookup ( language ) . name + " subtitles deleted from disk. "
history_log ( 0 , sonarrSeriesId , sonarrEpisodeId , result )
2017-09-16 00:49:46 +00:00
except OSError :
2017-09-17 00:11:47 +00:00
pass
store_subtitles ( episodePath )
2017-10-16 23:27:19 +00:00
list_missing_subtitles ( sonarrSeriesId )
2017-11-09 21:13:14 +00:00
@route ( base_url + ' get_subtitle ' , method = ' POST ' )
2017-09-17 00:11:47 +00:00
def get_subtitle ( ) :
2017-10-03 02:59:45 +00:00
ref = request . environ [ ' HTTP_REFERER ' ]
2017-10-16 23:27:19 +00:00
episodePath = request . forms . get ( ' episodePath ' )
language = request . forms . get ( ' language ' )
hi = request . forms . get ( ' hi ' )
sonarrSeriesId = request . forms . get ( ' sonarrSeriesId ' )
sonarrEpisodeId = request . forms . get ( ' sonarrEpisodeId ' )
2017-10-03 02:59:45 +00:00
2017-10-20 12:59:21 +00:00
db = sqlite3 . connect ( os . path . join ( os . path . dirname ( __file__ ) , ' data/db/bazarr.db ' ) )
2017-10-03 02:59:45 +00:00
c = db . cursor ( )
c . execute ( " SELECT name FROM table_settings_providers WHERE enabled = 1 " )
providers = c . fetchall ( )
c . close ( )
providers_list = [ ]
for provider in providers :
providers_list . append ( provider [ 0 ] )
2017-09-17 00:11:47 +00:00
2017-09-16 01:33:49 +00:00
try :
2017-10-03 02:59:45 +00:00
result = download_subtitle ( episodePath , language , hi , providers_list )
if result is not None :
history_log ( 1 , sonarrSeriesId , sonarrEpisodeId , result )
store_subtitles ( episodePath )
2017-10-16 23:27:19 +00:00
list_missing_subtitles ( sonarrSeriesId )
2017-10-03 02:59:45 +00:00
redirect ( ref )
2017-09-16 01:33:49 +00:00
except OSError :
2017-10-03 02:59:45 +00:00
redirect ( ref + ' ?error=2 ' )
2017-09-16 01:33:49 +00:00
2017-09-28 01:55:21 +00:00
run ( host = ip , port = port , server = ' waitress ' )