Skip to content

Commit

Permalink
Merge pull request #896 from thezoggy/dev--logging_tweaks
Browse files Browse the repository at this point in the history
logging tweaks - port from conjuro branch
  • Loading branch information
midgetspy committed Nov 16, 2014
2 parents 5f3f677 + 9a08c0f commit 6670e47
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 39 deletions.
6 changes: 3 additions & 3 deletions sickbeard/databases/mainDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def execute(self):
self.incDBVersion()


# included in build 507 (2014-0#-##)
# included in build 507 (2014-11-16)
class CleanupHistoryAndSpecials(AddRequireAndIgnoreWords):
""" Cleanup older history entries and set specials from wanted to skipped """

Expand Down Expand Up @@ -560,7 +560,7 @@ def execute(self):
self.connection.action("VACUUM")


# included in build 507 (2014-0#-##)
# included in build 507 (2014-11-16)
class AddSkipNotifications(CleanupHistoryAndSpecials):
""" Adding column skip_notices to tv_shows """

Expand All @@ -577,7 +577,7 @@ def execute(self):
self.incDBVersion()


# included in build 507 (2014-0#-##)
# included in build 507 (2014-11-16)
class AddHistorySource(AddSkipNotifications):
""" Adding column source to history """

Expand Down
2 changes: 1 addition & 1 deletion sickbeard/nzbget.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def sendNZB(nzb):

url = urlparse.urlunsplit((scheme, netloc, u"/xmlrpc", "", ""))

logger.log(u"Sending NZB to NZBGet")
logger.log(u"Sending NZB to NZBGet: %s" % nzb.name)
logger.log(u"NZBGet URL: " + url, logger.DEBUG)

nzbGetRPC = xmlrpclib.ServerProxy(url.encode("utf-8", 'ignore'))
Expand Down
49 changes: 27 additions & 22 deletions sickbeard/sab.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.



import urllib, httplib
import urllib
import httplib
import datetime

import sickbeard

from lib import MultipartPostHandler
import urllib2, cookielib
import urllib2
import cookielib
try:
import json
except ImportError:
Expand All @@ -34,22 +35,23 @@
from sickbeard import logger
from sickbeard.exceptions import ex


def sendNZB(nzb):
"""
Sends an NZB to SABnzbd via the API.
nzb: The NZBSearchResult object to send to SAB
"""

# set up a dict with the URL params in it
params = {}
if sickbeard.SAB_USERNAME != None:
if sickbeard.SAB_USERNAME is not None:
params['ma_username'] = sickbeard.SAB_USERNAME
if sickbeard.SAB_PASSWORD != None:
if sickbeard.SAB_PASSWORD is not None:
params['ma_password'] = sickbeard.SAB_PASSWORD
if sickbeard.SAB_APIKEY != None:
if sickbeard.SAB_APIKEY is not None:
params['apikey'] = sickbeard.SAB_APIKEY
if sickbeard.SAB_CATEGORY != None:
if sickbeard.SAB_CATEGORY is not None:
params['cat'] = sickbeard.SAB_CATEGORY

# if it aired recently make it high priority
Expand All @@ -69,14 +71,14 @@ def sendNZB(nzb):

url = sickbeard.SAB_HOST + "api?" + urllib.urlencode(params)

logger.log(u"Sending NZB to SABnzbd")
logger.log(u"URL: " + url, logger.DEBUG)
logger.log(u"Sending NZB to SABnzbd: %s" % nzb.name)
logger.log(u"SABnzbd URL: " + url, logger.DEBUG)

try:
# if we have the URL to an NZB then we've built up the SAB API URL already so just call it
# if we have the URL to an NZB then we've built up the SAB API URL already so just call it
if nzb.resultType == "nzb":
f = urllib.urlopen(url)

# if we are uploading the NZB data to SAB then we need to build a little POST form and send it
elif nzb.resultType == "nzbdata":
cookies = cookielib.CookieJar()
Expand All @@ -97,7 +99,7 @@ def sendNZB(nzb):
return False

# this means we couldn't open the connection or something just as bad
if f == None:
if f is None:
logger.log(u"No data returned from SABnzbd, NZB not sent", logger.ERROR)
return False

Expand All @@ -108,7 +110,7 @@ def sendNZB(nzb):
logger.log(u"Error trying to get result from SAB, NZB not sent: " + ex(e), logger.ERROR)
return False

# SAB shouldn't return a blank result, this most likely (but not always) means that it timed out and didn't recieve the NZB
# SAB shouldn't return a blank result, this most likely (but not always) means that it timed out and didn't receive the NZB
if len(result) == 0:
logger.log(u"No data returned from SABnzbd, NZB not sent", logger.ERROR)
return False
Expand All @@ -129,6 +131,7 @@ def sendNZB(nzb):
logger.log(u"Unknown failure sending NZB to sab. Return text is: " + sabText, logger.ERROR)
return False


def _checkSabResponse(f):
try:
result = f.readlines()
Expand Down Expand Up @@ -156,6 +159,7 @@ def _checkSabResponse(f):
else:
return True, sabText


def _sabURLOpenSimple(url):
try:
f = urllib.urlopen(url)
Expand All @@ -165,15 +169,16 @@ def _sabURLOpenSimple(url):
except httplib.InvalidURL, e:
logger.log(u"Invalid SAB host, check your config: " + ex(e), logger.ERROR)
return False, "Invalid SAB host"
if f == None:
if f is None:
logger.log(u"No data returned from SABnzbd", logger.ERROR)
return False, "No data returned from SABnzbd"
else:
return True, f


def getSabAccesMethod(host=None, username=None, password=None, apikey=None):
url = host + "api?mode=auth"

result, f = _sabURLOpenSimple(url)
if not result:
return False, f
Expand All @@ -184,18 +189,19 @@ def getSabAccesMethod(host=None, username=None, password=None, apikey=None):

return True, sabText


def testAuthentication(host=None, username=None, password=None, apikey=None):
"""
Sends a simple API request to SAB to determine if the given connection information is connect
host: The host where SAB is running (incl port)
username: The username to use for the HTTP request
password: The password to use for the HTTP request
apikey: The API key to provide to SAB
Returns: A tuple containing the success boolean and a message
"""

# build up the URL parameters
params = {}
params['mode'] = 'queue'
Expand All @@ -204,7 +210,7 @@ def testAuthentication(host=None, username=None, password=None, apikey=None):
params['ma_password'] = password
params['apikey'] = apikey
url = host + "api?" + urllib.urlencode(params)

# send the test request
logger.log(u"SABnzbd test URL: " + url, logger.DEBUG)
result, f = _sabURLOpenSimple(url)
Expand All @@ -215,6 +221,5 @@ def testAuthentication(host=None, username=None, password=None, apikey=None):
result, sabText = _checkSabResponse(f)
if not result:
return False, sabText

return True, "Success"

37 changes: 25 additions & 12 deletions sickbeard/searchBacklog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from sickbeard import search_queue
from sickbeard import logger
from sickbeard import ui
#from sickbeard.common import *


class BacklogSearchScheduler(scheduler.Scheduler):

Expand All @@ -41,6 +41,7 @@ def nextRun(self):
else:
return datetime.date.fromordinal(self.action._lastBacklog + self.action.cycleTime)


class BacklogSearcher:

def __init__(self):
Expand All @@ -65,7 +66,7 @@ def getProgressIndicator(self):
return None

def am_running(self):
logger.log(u"amWaiting: "+str(self.amWaiting)+", amActive: "+str(self.amActive), logger.DEBUG)
logger.log(u"amWaiting: " + str(self.amWaiting) + ", amActive: " + str(self.amActive), logger.DEBUG)
return (not self.amWaiting) and self.amActive

def searchBacklog(self, which_shows=None):
Expand All @@ -75,7 +76,20 @@ def searchBacklog(self, which_shows=None):
else:
show_list = sickbeard.showList

if self.amActive == True:
def titler(x):
if not x:
return x
if not x.lower().startswith('a to ') and x.lower().startswith('a '):
x = x[2:]
elif x.lower().startswith('an '):
x = x[3:]
elif x.lower().startswith('the '):
x = x[4:]
return x
# sort shows the same way we show them, makes it easier to follow along
show_list = sorted(show_list, lambda x, y: cmp(titler(x.name), titler(y.name)))

if self.amActive is True:
logger.log(u"Backlog is still running, not starting it again", logger.DEBUG)
return

Expand All @@ -101,9 +115,9 @@ def searchBacklog(self, which_shows=None):
# figure out how many segments of air by date shows we're going to do
air_by_date_segments = []
for cur_id in [x.tvdbid for x in air_by_date_shows]:
air_by_date_segments += self._get_air_by_date_segments(cur_id, fromDate)
air_by_date_segments += self._get_air_by_date_segments(cur_id, fromDate)

logger.log(u"Air-by-date segments: "+str(air_by_date_segments), logger.DEBUG)
logger.log(u"Air-by-date segments: " + str(air_by_date_segments), logger.DEBUG)

#totalSeasons = float(len(numSeasonResults) + len(air_by_date_segments))
#numSeasonsDone = 0.0
Expand All @@ -121,14 +135,14 @@ def searchBacklog(self, which_shows=None):

for cur_segment in segments:

self.currentSearchInfo = {'title': curShow.name + " Season "+str(cur_segment)}
self.currentSearchInfo = {'title': curShow.name + " Season " + str(cur_segment)}

backlog_queue_item = search_queue.BacklogQueueItem(curShow, cur_segment)

if not backlog_queue_item.wantSeason:
logger.log(u"Nothing in season "+str(cur_segment)+" needs to be downloaded, skipping this season", logger.DEBUG)
logger.log(u"Nothing in season " + str(cur_segment) + " needs to be downloaded, skipping this season", logger.DEBUG)
else:
sickbeard.searchQueueScheduler.action.add_item(backlog_queue_item) #@UndefinedVariable
sickbeard.searchQueueScheduler.action.add_item(backlog_queue_item) # @UndefinedVariable

# don't consider this an actual backlog search if we only did recent eps
# or if we only did certain shows
Expand All @@ -147,7 +161,7 @@ def _get_lastBacklog(self):

if len(sqlResults) == 0:
lastBacklog = 1
elif sqlResults[0]["last_backlog"] == None or sqlResults[0]["last_backlog"] == "":
elif sqlResults[0]["last_backlog"] is None or sqlResults[0]["last_backlog"] == "":
lastBacklog = 1
else:
lastBacklog = int(sqlResults[0]["last_backlog"])
Expand All @@ -174,11 +188,11 @@ def _get_air_by_date_segments(self, tvdb_id, fromDate):
cur_date = datetime.date.fromordinal(int(cur_result["airdate"]))
cur_date_str = str(cur_date)[:7]
cur_tvdb_id = int(cur_result["showid"])

cur_result_tuple = (cur_tvdb_id, cur_date_str)
if cur_result_tuple not in air_by_date_segments:
air_by_date_segments.append(cur_result_tuple)

return air_by_date_segments

def _set_lastBacklog(self, when):
Expand All @@ -193,7 +207,6 @@ def _set_lastBacklog(self, when):
else:
myDB.action("UPDATE info SET last_backlog=" + str(when))


def run(self):
try:
self.searchBacklog()
Expand Down
3 changes: 2 additions & 1 deletion sickbeard/search_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def execute(self):

def _changeMissingEpisodes(self):

logger.log(u"Changing all old missing episodes to status WANTED")
logger.log(u"Changing all old missing episodes (UNAIRED) to status WANTED")

curDate = datetime.date.today().toordinal()

Expand Down Expand Up @@ -215,6 +215,7 @@ def execute(self):
search.snatchEpisode(curResult)
time.sleep(5)

logger.log(u"Finished searching for episodes from " + self.show.name + " season " + str(self.segment))
self.finish()

def _need_any_episodes(self, statusResults, bestQualities):
Expand Down

0 comments on commit 6670e47

Please sign in to comment.