Commit 25fc34f9 authored by echel0n's avatar echel0n

Refactored code for searching providers, was causing issues with final results...

Refactored code for searching providers, was causing issues with final results correctly, results where being removed when not matching exact quality.
Rename resultType property to type for SearchResult class.
parent 9c20647f
......@@ -113,7 +113,7 @@ class NZBGet(object):
dupe_score += 10
nzbcontent64 = None
if nzb.resultType == "nzbdata":
if nzb.type == "nzbdata":
data = nzb.extraInfo[0]
nzbcontent64 = standard_b64encode(data)
......@@ -129,7 +129,7 @@ class NZBGet(object):
if nzbcontent64 is not None:
nzbget_result = nzbget_rpc_client.append(nzb.name + ".nzb", category, addToTop, nzbcontent64)
else:
if nzb.resultType == "nzb":
if nzb.type == "nzb":
try:
nzbcontent64 = standard_b64encode(WebSession().get(nzb.url).text)
except Exception:
......
......@@ -72,11 +72,11 @@ class SabNZBd(object):
try:
jdata = None
if nzb.resultType == 'nzb':
if nzb.type == 'nzb':
params['mode'] = 'addurl'
params['name'] = nzb.url
jdata = WebSession().get(url, params=params, verify=False).json()
elif nzb.resultType == 'nzbdata':
elif nzb.type == 'nzbdata':
params['mode'] = 'addfile'
multiPartParams = {'nzbfile': (nzb.name + '.nzb', nzb.extraInfo[0])}
jdata = WebSession().get(url, params=params, file=multiPartParams, verify=False).json()
......
......@@ -325,7 +325,7 @@ class TVCache(object):
result.name = curResult["name"]
result.url = curResult["url"]
sickrage.app.log.info("Found result " + result.name + " at " + result.url)
sickrage.app.log.info("Found cached {} result {}".format(result.type, result.name))
result.seeders = curResult.get("seeders", -1)
result.leechers = curResult.get("leechers", -1)
......
......@@ -81,7 +81,7 @@ class SearchResult(object):
self.ratio = None
# result type
self.resultType = ''
self.type = ''
# dict of files and their sizes
self.files = {}
......@@ -116,7 +116,7 @@ class NZBSearchResult(SearchResult):
def __init__(self, season, episodes):
super(NZBSearchResult, self).__init__(season, episodes)
self.resultType = "nzb"
self.type = "nzb"
class NZBDataSearchResult(SearchResult):
......@@ -126,7 +126,7 @@ class NZBDataSearchResult(SearchResult):
def __init__(self, season, episodes):
super(NZBDataSearchResult, self).__init__(season, episodes)
self.resultType = "nzbdata"
self.type = "nzbdata"
class TorrentSearchResult(SearchResult):
......@@ -136,7 +136,7 @@ class TorrentSearchResult(SearchResult):
def __init__(self, season, episodes):
super(TorrentSearchResult, self).__init__(season, episodes)
self.resultType = "torrent"
self.type = "torrent"
class UIError(object):
......
......@@ -68,7 +68,7 @@ def snatch_episode(result, end_status=SNATCHED, session=None):
result.content = result.provider.get_content(result.url)
dlResult = False
if result.resultType in ("nzb", "nzbdata"):
if result.type in ("nzb", "nzbdata"):
if sickrage.app.config.nzb_method == "blackhole":
dlResult = result.provider.download_result(result)
elif sickrage.app.config.nzb_method == "sabnzbd":
......@@ -78,7 +78,7 @@ def snatch_episode(result, end_status=SNATCHED, session=None):
dlResult = NZBGet.sendNZB(result, is_proper, session=session)
else:
sickrage.app.log.error("Unknown NZB action specified in config: " + sickrage.app.config.nzb_method)
elif result.resultType in ("torrent", "torznab"):
elif result.type in ("torrent", "torznab"):
# add public trackers to torrent result
if not result.provider.private:
result = result.provider.add_trackers(result)
......@@ -92,7 +92,7 @@ def snatch_episode(result, end_status=SNATCHED, session=None):
else:
sickrage.app.log.warning("Torrent file content is empty")
else:
sickrage.app.log.error("Unknown result type, unable to download it (%r)" % result.resultType)
sickrage.app.log.error("Unknown result type, unable to download it (%r)" % result.type)
# no download results found
if not dlResult:
......@@ -220,7 +220,7 @@ def pick_best_result(results, season_pack=False, session=None):
# verify result content
if not cur_result.provider.private:
if cur_result.resultType in ["nzb", "torrent"] and not cur_result.provider.get_content(cur_result.url):
if cur_result.type in ["nzb", "torrent"] and not cur_result.provider.get_content(cur_result.url):
if sickrage.app.config.download_unverified_magnet_link and cur_result.url.startswith('magnet'):
# Attempt downloading unverified torrent magnet link
pass
......@@ -267,7 +267,7 @@ def is_final_result(result):
any_qualities, best_qualities = Quality.split_quality(show_obj.quality)
# if there is a redownload that's higher than this then we definitely need to keep looking
# if there is a download that's higher than this then we definitely need to keep looking
if best_qualities and result.quality < max(best_qualities):
return False
......@@ -326,6 +326,8 @@ def search_providers(show_id, season, episode, manualSearch=False, downCurQualit
# build name cache for show
sickrage.app.name_cache.build(show_object)
final_results = []
for providerID, providerObj in sickrage.app.search_providers.sort(randomize=sickrage.app.config.randomize_providers).items():
# check if provider is enabled
if not providerObj.isEnabled:
......@@ -535,7 +537,7 @@ def search_providers(show_id, season, episode, manualSearch=False, downCurQualit
del found_results[multi_result_episode]
# of all the single ep results narrow it down to the best one
final_results = set(multi_results.values())
final_results += list(set(multi_results.values()))
for curEp, curResults in found_results.items():
if curEp in (MULTI_EP_RESULT, SEASON_RESULT):
continue
......@@ -549,22 +551,21 @@ def search_providers(show_id, season, episode, manualSearch=False, downCurQualit
continue
# add result
final_results.add(best_result)
final_results.append(best_result)
# narrow results by comparing quality
if len(final_results) > 1:
final_results = set([a for a, b in itertools.product(final_results, repeat=len(final_results)) if a.quality >= b.quality])
final_results = list(set([a for a, b in itertools.product(final_results, repeat=len(final_results)) if a.quality >= b.quality]))
# narrow results by comparing seeders for torrent results
if len(final_results) > 1:
final_results = set(
[a for a, b in itertools.product(final_results, repeat=len(final_results)) if a.provider.type == NZBProvider.type or a.seeders > b.seeders])
final_results = list(set(
[a for a, b in itertools.product(final_results, repeat=len(final_results)) if a.provider.type == NZBProvider.type or a.seeders > b.seeders]))
# check that we got all the episodes we wanted first before doing a match and snatch
for result in final_results.copy():
if not all([episode in result.episodes and is_final_result(result)]):
final_results.remove(result)
if all([episode in result.episodes and is_final_result(result)]):
return result
# exit loop if we have a result
if len(final_results) == 1:
return next(iter(final_results))
if len(final_results) == 1:
return next(iter(final_results))
......@@ -716,7 +716,7 @@ class NZBProvider(GenericProvider):
Returns a result of the correct type for this provider
"""
result = NZBSearchResult(season, episodes)
result.resultType = ('nzb', 'torznab')[self.torznab]
result.type = ('nzb', 'torznab')[self.torznab]
result.provider = self
return result
......@@ -740,7 +740,7 @@ class NZBProvider(GenericProvider):
if (result.url.endswith('torrent') or result.url.startswith('magnet')) and self.type in ['nzb', 'newznab']:
filename = "{}.torrent".format(filename.rsplit('.', 1)[0])
if result.resultType == "nzb":
if result.type == "nzb":
sickrage.app.log.info("Saving NZB to " + filename)
# write content to torrent file
......@@ -748,7 +748,7 @@ class NZBProvider(GenericProvider):
f.write(result.content)
return True
elif result.resultType == "nzbdata":
elif result.type == "nzbdata":
filename = os.path.join(sickrage.app.config.nzb_dir, result.name + ".nzb")
sickrage.app.log.info("Saving NZB to " + filename)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment