mirror of
https://github.com/rembo10/headphones.git
synced 2026-05-02 09:49:36 +01:00
Various small cleanups
This commit is contained in:
@@ -96,11 +96,16 @@ class DBConnection:
|
|||||||
|
|
||||||
genParams = lambda myDict: [x + " = ?" for x in myDict.keys()]
|
genParams = lambda myDict: [x + " = ?" for x in myDict.keys()]
|
||||||
|
|
||||||
query = "UPDATE " + tableName + " SET " + ", ".join(genParams(valueDict)) + " WHERE " + " AND ".join(genParams(keyDict))
|
update_query = "UPDATE " + tableName + " SET " + ", ".join(genParams(valueDict)) + " WHERE " + " AND ".join(genParams(keyDict))
|
||||||
|
|
||||||
self.action(query, valueDict.values() + keyDict.values())
|
self.action(update_query, valueDict.values() + keyDict.values())
|
||||||
|
|
||||||
if self.connection.total_changes == changesBefore:
|
if self.connection.total_changes == changesBefore:
|
||||||
query = "INSERT INTO " + tableName + " (" + ", ".join(valueDict.keys() + keyDict.keys()) + ")" + \
|
insert_query = (
|
||||||
" VALUES (" + ", ".join(["?"] * len(valueDict.keys() + keyDict.keys())) + ")"
|
"INSERT INTO " + tableName + " (" + ", ".join(valueDict.keys() + keyDict.keys()) + ")" +
|
||||||
self.action(query, valueDict.values() + keyDict.values())
|
" VALUES (" + ", ".join(["?"] * len(valueDict.keys() + keyDict.keys())) + ")"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
self.action(insert_query, valueDict.values() + keyDict.values())
|
||||||
|
except sqlite3.IntegrityError:
|
||||||
|
logger.info('Queries failed: %s and %s', update_query, insert_query)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
Locking-related classes
|
Locking-related classes
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import headphones.logger
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import Queue
|
import Queue
|
||||||
@@ -38,10 +39,12 @@ class TimedLock(object):
|
|||||||
sleep_amount = self.minimum_delta - delta
|
sleep_amount = self.minimum_delta - delta
|
||||||
if sleep_amount >= 0:
|
if sleep_amount >= 0:
|
||||||
# zero sleeps give the cpu a chance to task-switch
|
# zero sleeps give the cpu a chance to task-switch
|
||||||
|
headphones.logger.info('Sleeping %s (interval)', sleep_amount)
|
||||||
time.sleep(sleep_amount)
|
time.sleep(sleep_amount)
|
||||||
while not self.queue.empty():
|
while not self.queue.empty():
|
||||||
try:
|
try:
|
||||||
seconds = self.queue.get(False)
|
seconds = self.queue.get(False)
|
||||||
|
headphones.logger.info('Sleeping %s (queued)', seconds)
|
||||||
time.sleep(seconds)
|
time.sleep(seconds)
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
continue
|
continue
|
||||||
@@ -63,6 +66,7 @@ class TimedLock(object):
|
|||||||
"""
|
"""
|
||||||
# we use a queue so that we don't have to synchronize
|
# we use a queue so that we don't have to synchronize
|
||||||
# across threads and with or without locks
|
# across threads and with or without locks
|
||||||
|
headphones.logger.info('Adding %s to queue', seconds)
|
||||||
self.queue.add(seconds)
|
self.queue.add(seconds)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,8 @@ def request_response(url, method="get", auto_raise=True,
|
|||||||
try:
|
try:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except:
|
except:
|
||||||
logger.debug("Response status code %d is not white " \
|
logger.debug(
|
||||||
|
"Response status code %d is not white "
|
||||||
"listed, raised exception", response.status_code)
|
"listed, raised exception", response.status_code)
|
||||||
raise
|
raise
|
||||||
elif auto_raise:
|
elif auto_raise:
|
||||||
@@ -81,7 +82,7 @@ def request_response(url, method="get", auto_raise=True,
|
|||||||
"host is up and running.")
|
"host is up and running.")
|
||||||
except requests.Timeout:
|
except requests.Timeout:
|
||||||
logger.error(
|
logger.error(
|
||||||
"Request timed out. The remote host did not respeond timely.")
|
"Request timed out. The remote host did not respond timely.")
|
||||||
except requests.HTTPError as e:
|
except requests.HTTPError as e:
|
||||||
if e.response is not None:
|
if e.response is not None:
|
||||||
if e.response.status_code >= 500:
|
if e.response.status_code >= 500:
|
||||||
|
|||||||
@@ -593,7 +593,7 @@ def searchNZB(album, new=False, losslessOnly=False, albumlength=None):
|
|||||||
logger.info("Album type is audiobook/spokenword. Using audiobook category")
|
logger.info("Album type is audiobook/spokenword. Using audiobook category")
|
||||||
|
|
||||||
# Request results
|
# Request results
|
||||||
logger.info('Parsing results from nzbs.org')
|
logger.info('Requesting from nzbs.org')
|
||||||
|
|
||||||
headers = {'User-Agent': USER_AGENT}
|
headers = {'User-Agent': USER_AGENT}
|
||||||
params = {
|
params = {
|
||||||
@@ -606,9 +606,11 @@ def searchNZB(album, new=False, losslessOnly=False, albumlength=None):
|
|||||||
|
|
||||||
data = request.request_feed(
|
data = request.request_feed(
|
||||||
url='http://beta.nzbs.org/api',
|
url='http://beta.nzbs.org/api',
|
||||||
params=params, headers=headers
|
params=params, headers=headers,
|
||||||
|
timeout=5
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger.info('Parsing results from nzbs.org')
|
||||||
# Process feed
|
# Process feed
|
||||||
if data:
|
if data:
|
||||||
if not len(data.entries):
|
if not len(data.entries):
|
||||||
|
|||||||
Reference in New Issue
Block a user