1
0
Fork 0
mirror of https://github.com/morpheus65535/bazarr synced 2024-12-25 01:02:19 +00:00

Reduce to minimum the opened connection time to db

This commit is contained in:
Louis Vézina 2017-11-30 14:31:31 -05:00
parent 9537935a27
commit e77e556610
3 changed files with 12 additions and 10 deletions

View file

@ -41,12 +41,13 @@ logger = logging.getLogger('waitress')
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'))
c = db.cursor() c = db.cursor()
c.execute("SELECT log_level FROM table_settings_general") c.execute("SELECT log_level FROM table_settings_general")
c.close()
log_level = c.fetchone() log_level = c.fetchone()
log_level = log_level[0] log_level = log_level[0]
if log_level is None: if log_level is None:
log_level = "INFO" log_level = "INFO"
log_level = getattr(logging, log_level) log_level = getattr(logging, log_level)
c.close()
class OneLineExceptionFormatter(logging.Formatter): class OneLineExceptionFormatter(logging.Formatter):
def formatException(self, exc_info): def formatException(self, exc_info):
@ -146,11 +147,11 @@ def episodes(no):
tvdbid = series_details[5] tvdbid = series_details[5]
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() 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()
c.close()
episodes = reversed(sorted(episodes, key=operator.itemgetter(2))) episodes = reversed(sorted(episodes, key=operator.itemgetter(2)))
seasons_list = [] seasons_list = []
for key,season in itertools.groupby(episodes,operator.itemgetter(2)): for key,season in itertools.groupby(episodes,operator.itemgetter(2)):
seasons_list.append(list(season)) seasons_list.append(list(season))
c.close()
return template('episodes', no=no, details=series_details, seasons=seasons_list, url_sonarr_short=url_sonarr_short, base_url=base_url, tvdbid=tvdbid) return template('episodes', no=no, details=series_details, seasons=seasons_list, url_sonarr_short=url_sonarr_short, base_url=base_url, tvdbid=tvdbid)
@ -186,8 +187,8 @@ def history():
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,)) 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,))
data = c.fetchall() data = c.fetchall()
data = reversed(sorted(data, key=operator.itemgetter(4)))
c.close() c.close()
data = reversed(sorted(data, key=operator.itemgetter(4)))
return template('history', rows=data, row_count=row_count, page=page, max_page=max_page, base_url=base_url) return template('history', rows=data, row_count=row_count, page=page, max_page=max_page, base_url=base_url)
@route(base_url + 'wanted') @route(base_url + 'wanted')

View file

@ -30,6 +30,9 @@ else:
fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r') fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r')
script = fd.read() script = fd.read()
# Close SQL script file
fd.close()
# Open database connection # Open database connection
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'))
c = db.cursor() c = db.cursor()
@ -39,6 +42,3 @@ else:
# Close database connection # Close database connection
db.close() db.close()
# Close SQL script file
fd.close()

View file

@ -73,6 +73,7 @@ def list_missing_subtitles(*no):
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'))
c_db = conn_db.cursor() c_db = conn_db.cursor()
episodes_subtitles = c_db.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.subtitles, table_shows.languages FROM table_episodes INNER JOIN table_shows on table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId" + query_string).fetchall() episodes_subtitles = c_db.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.subtitles, table_shows.languages FROM table_episodes INNER JOIN table_shows on table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId" + query_string).fetchall()
c_db.close()
missing_subtitles_global = [] missing_subtitles_global = []
@ -93,10 +94,10 @@ def list_missing_subtitles(*no):
missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list))
missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]])) missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]]))
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'))
c_db = conn_db.cursor()
c_db.executemany("UPDATE table_episodes SET missing_subtitles = ? WHERE sonarrEpisodeId = ?", (missing_subtitles_global)) c_db.executemany("UPDATE table_episodes SET missing_subtitles = ? WHERE sonarrEpisodeId = ?", (missing_subtitles_global))
conn_db.commit() conn_db.commit()
c_db.close() c_db.close()
def full_scan_subtitles(): def full_scan_subtitles():