2014-04-22 08:02:43 +00:00
|
|
|
# coding=utf-8
|
2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
import time
|
2014-03-10 05:18:05 +00:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
import re
|
2014-07-22 00:55:45 +00:00
|
|
|
import itertools
|
2015-04-18 04:55:04 +00:00
|
|
|
from base64 import b16encode, b32decode
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
import sickbeard
|
2014-07-24 04:44:11 +00:00
|
|
|
import requests
|
2015-07-13 09:39:20 +00:00
|
|
|
import requests.cookies
|
2015-06-19 23:34:56 +00:00
|
|
|
from sickbeard import helpers, classes, logger, db, tvcache, encodingKludge as ek
|
|
|
|
from sickbeard.common import Quality, MULTI_EP_RESULT, SEASON_RESULT, USER_AGENT
|
|
|
|
from sickbeard.exceptions import SickBeardException, AuthException, ex
|
2015-09-15 20:22:58 +00:00
|
|
|
from sickbeard.helpers import maybe_plural, _remove_file_failed as remove_file_failed
|
2014-07-06 13:11:04 +00:00
|
|
|
from sickbeard.name_parser.parser import NameParser, InvalidNameException, InvalidShowException
|
2015-06-19 23:34:56 +00:00
|
|
|
from sickbeard.show_name_helpers import allPossibleShowNames
|
2015-09-15 20:22:58 +00:00
|
|
|
from hachoir_parser import guessParser
|
|
|
|
from hachoir_core.stream import FileInputStream
|
2015-04-18 04:55:04 +00:00
|
|
|
|
2014-09-07 05:25:01 +00:00
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
class HaltParseException(SickBeardException):
|
|
|
|
"""Something requires the current processing to abort"""
|
|
|
|
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class GenericProvider:
|
2015-07-13 09:39:20 +00:00
|
|
|
NZB = 'nzb'
|
|
|
|
TORRENT = 'torrent'
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
def __init__(self, name, supports_backlog=False, anime_only=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
# these need to be set in the subclass
|
|
|
|
self.providerType = None
|
|
|
|
self.name = name
|
2015-05-27 00:00:01 +00:00
|
|
|
self.supportsBacklog = supports_backlog
|
2015-04-18 04:55:04 +00:00
|
|
|
self.anime_only = anime_only
|
2014-03-10 05:18:05 +00:00
|
|
|
self.url = ''
|
|
|
|
|
2014-04-29 13:14:19 +00:00
|
|
|
self.show = None
|
2014-05-17 05:23:11 +00:00
|
|
|
|
2014-05-16 09:16:01 +00:00
|
|
|
self.search_mode = None
|
|
|
|
self.search_fallback = False
|
2015-04-15 06:16:27 +00:00
|
|
|
self.enabled = False
|
2014-12-22 18:30:53 +00:00
|
|
|
self.enable_recentsearch = False
|
2014-08-29 05:16:25 +00:00
|
|
|
self.enable_backlog = False
|
2014-05-27 07:44:23 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.cache = tvcache.TVCache(self)
|
|
|
|
|
2014-04-28 09:15:29 +00:00
|
|
|
self.session = requests.session()
|
2014-07-27 14:04:37 +00:00
|
|
|
|
2014-07-27 10:59:21 +00:00
|
|
|
self.headers = {
|
2014-09-07 05:25:01 +00:00
|
|
|
# Using USER_AGENT instead of Mozilla to keep same user agent along authentication and download phases,
|
2015-07-13 09:39:20 +00:00
|
|
|
# otherwise session might be broken and download fail, asking again for authentication
|
|
|
|
# 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36'}
|
2014-09-06 00:16:04 +00:00
|
|
|
'User-Agent': USER_AGENT}
|
2014-04-28 09:15:29 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def get_id(self):
|
|
|
|
return GenericProvider.make_id(self.name)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2015-07-13 09:39:20 +00:00
|
|
|
def make_id(name):
|
|
|
|
return re.sub('[^\w\d_]', '_', name.strip().lower())
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def image_name(self, *default_name):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
for name in ['%s.%s' % (self.get_id(), image_ext) for image_ext in ['png', 'gif', 'jpg']]:
|
2015-06-19 23:34:56 +00:00
|
|
|
if ek.ek(os.path.isfile,
|
|
|
|
ek.ek(os.path.join, sickbeard.PROG_DIR, 'gui', sickbeard.GUI_NAME, 'images', 'providers', name)):
|
|
|
|
return name
|
|
|
|
|
|
|
|
return '%s.png' % ('newznab', default_name[0])[any(default_name)]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def _check_auth(self):
|
2014-08-12 10:09:11 +00:00
|
|
|
return True
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def _do_login(self):
|
2014-07-27 10:59:21 +00:00
|
|
|
return True
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def is_active(self):
|
|
|
|
if GenericProvider.NZB == self.providerType and sickbeard.USE_NZBS:
|
|
|
|
return self.is_enabled()
|
|
|
|
elif GenericProvider.TORRENT == self.providerType and sickbeard.USE_TORRENTS:
|
|
|
|
return self.is_enabled()
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def is_enabled(self):
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
This should be overridden and should return the config setting eg. sickbeard.MYPROVIDER
|
|
|
|
"""
|
2015-04-15 06:16:27 +00:00
|
|
|
return self.enabled
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def get_result(self, episodes, url):
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
Returns a result of the correct type for this provider
|
|
|
|
"""
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if GenericProvider.NZB == self.providerType:
|
2014-03-10 05:18:05 +00:00
|
|
|
result = classes.NZBSearchResult(episodes)
|
2015-07-13 09:39:20 +00:00
|
|
|
elif GenericProvider.TORRENT == self.providerType:
|
2014-03-10 05:18:05 +00:00
|
|
|
result = classes.TorrentSearchResult(episodes)
|
|
|
|
else:
|
|
|
|
result = classes.SearchResult(episodes)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
result.provider = self
|
2015-07-13 09:39:20 +00:00
|
|
|
result.url = url
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return result
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def get_url(self, url, post_data=None, params=None, timeout=30, json=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
By default this is just a simple urlopen call but this method should be overridden
|
|
|
|
for providers with special URL requirements (like cookies)
|
|
|
|
"""
|
|
|
|
|
2014-07-27 10:59:21 +00:00
|
|
|
# check for auth
|
2015-07-13 09:39:20 +00:00
|
|
|
if not self._do_login():
|
2014-07-27 10:59:21 +00:00
|
|
|
return
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-27 10:59:21 +00:00
|
|
|
return helpers.getURL(url, post_data=post_data, params=params, headers=self.headers, timeout=timeout,
|
|
|
|
session=self.session, json=json)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def download_result(self, result):
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
Save the result to disk.
|
|
|
|
"""
|
|
|
|
|
2014-07-27 10:59:21 +00:00
|
|
|
# check for auth
|
2015-07-13 09:39:20 +00:00
|
|
|
if not self._do_login():
|
2014-07-27 14:04:37 +00:00
|
|
|
return False
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if GenericProvider.TORRENT == self.providerType:
|
2015-09-16 23:52:18 +00:00
|
|
|
final_dir = sickbeard.TORRENT_DIR
|
|
|
|
link_type = 'magnet'
|
2014-07-27 14:04:37 +00:00
|
|
|
try:
|
2015-08-14 23:02:05 +00:00
|
|
|
torrent_hash = re.findall('(?i)urn:btih:([0-9a-f]{32,40})', result.url)[0].upper()
|
2014-09-11 17:26:17 +00:00
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
if 32 == len(torrent_hash):
|
2014-09-11 17:26:17 +00:00
|
|
|
torrent_hash = b16encode(b32decode(torrent_hash)).lower()
|
|
|
|
|
2014-07-27 14:04:37 +00:00
|
|
|
if not torrent_hash:
|
2015-07-13 09:39:20 +00:00
|
|
|
logger.log('Unable to extract torrent hash from link: ' + ex(result.url), logger.ERROR)
|
2014-07-27 14:04:37 +00:00
|
|
|
return False
|
|
|
|
|
2015-08-14 23:02:05 +00:00
|
|
|
urls = ['http%s://%s/%s.torrent' % (u + (torrent_hash,))
|
|
|
|
for u in (('s', 'torcache.net/torrent'), ('s', 'getstrike.net/torrents/api/download'),
|
2015-09-15 20:22:58 +00:00
|
|
|
('s', 'itorrents.org/torrent'))]
|
2014-07-27 14:04:37 +00:00
|
|
|
except:
|
2015-09-16 23:52:18 +00:00
|
|
|
link_type = 'torrent'
|
2014-07-27 14:04:37 +00:00
|
|
|
urls = [result.url]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
elif GenericProvider.NZB == self.providerType:
|
2015-09-16 23:52:18 +00:00
|
|
|
final_dir = sickbeard.NZB_DIR
|
|
|
|
link_type = 'nzb'
|
2014-07-27 10:59:21 +00:00
|
|
|
urls = [result.url]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-27 10:59:21 +00:00
|
|
|
else:
|
|
|
|
return
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-15 20:22:58 +00:00
|
|
|
ref_state = 'Referer' in self.session.headers and self.session.headers['Referer']
|
|
|
|
saved = False
|
2014-07-27 10:59:21 +00:00
|
|
|
for url in urls:
|
2015-08-14 23:02:05 +00:00
|
|
|
cache_dir = sickbeard.CACHE_DIR or helpers._getTempDir()
|
|
|
|
base_name = '%s.%s' % (helpers.sanitizeFileName(result.name), self.providerType)
|
|
|
|
cache_file = ek.ek(os.path.join, cache_dir, base_name)
|
2014-07-27 10:59:21 +00:00
|
|
|
|
2015-09-15 20:22:58 +00:00
|
|
|
self.session.headers['Referer'] = url
|
2015-08-14 23:02:05 +00:00
|
|
|
if helpers.download_file(url, cache_file, session=self.session):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-08-14 23:02:05 +00:00
|
|
|
if self._verify_download(cache_file):
|
2015-09-15 20:22:58 +00:00
|
|
|
logger.log(u'Downloaded %s result from %s' % (self.name, url))
|
2015-08-14 23:02:05 +00:00
|
|
|
final_file = ek.ek(os.path.join, final_dir, base_name)
|
2015-09-15 20:22:58 +00:00
|
|
|
try:
|
|
|
|
helpers.moveFile(cache_file, final_file)
|
|
|
|
msg = 'moved'
|
|
|
|
except:
|
|
|
|
msg = 'copied cached file'
|
|
|
|
logger.log(u'Saved %s link and %s to %s' % (link_type, msg, final_file))
|
|
|
|
saved = True
|
|
|
|
break
|
2015-08-14 23:02:05 +00:00
|
|
|
|
2015-09-15 20:22:58 +00:00
|
|
|
remove_file_failed(cache_file)
|
2015-08-14 23:02:05 +00:00
|
|
|
|
2015-09-15 20:22:58 +00:00
|
|
|
if 'Referer' in self.session.headers:
|
|
|
|
if ref_state:
|
|
|
|
self.session.headers['Referer'] = ref_state
|
|
|
|
else:
|
|
|
|
del(self.session.headers['Referer'])
|
2014-08-25 15:55:16 +00:00
|
|
|
|
2015-09-15 20:22:58 +00:00
|
|
|
if not saved:
|
|
|
|
logger.log(u'All torrent cache servers failed to return a downloadable result', logger.ERROR)
|
|
|
|
|
|
|
|
return saved
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _verify_download(self, file_name=None):
|
|
|
|
"""
|
|
|
|
Checks the saved file to see if it was actually valid, if not then consider the download a failure.
|
|
|
|
"""
|
2015-09-15 20:22:58 +00:00
|
|
|
result = True
|
2014-03-10 05:18:05 +00:00
|
|
|
# primitive verification of torrents, just make sure we didn't get a text file or something
|
2015-07-13 09:39:20 +00:00
|
|
|
if GenericProvider.TORRENT == self.providerType:
|
2015-09-15 20:22:58 +00:00
|
|
|
parser = stream = None
|
|
|
|
try:
|
|
|
|
stream = FileInputStream(file_name)
|
|
|
|
parser = guessParser(stream)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
result = parser and 'application/x-bittorrent' == parser.mime_type
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-15 20:22:58 +00:00
|
|
|
try:
|
|
|
|
stream._input.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return result
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def search_rss(self, episodes):
|
2014-09-20 12:03:48 +00:00
|
|
|
return self.cache.findNeededEpisodes(episodes)
|
2014-05-15 04:16:46 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def get_quality(self, item, anime=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
Figures out the quality of the given RSS item node
|
|
|
|
|
|
|
|
item: An elementtree.ElementTree element representing the <item> tag of the RSS feed
|
|
|
|
|
|
|
|
Returns a Quality value obtained from the node's data
|
|
|
|
"""
|
2014-03-25 05:57:24 +00:00
|
|
|
(title, url) = self._get_title_and_url(item) # @UnusedVariable
|
2014-05-26 06:29:22 +00:00
|
|
|
quality = Quality.sceneQuality(title, anime)
|
2014-03-10 05:18:05 +00:00
|
|
|
return quality
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def _do_search(self, search_params, search_mode='eponly', epcount=0, age=0):
|
2014-03-10 05:18:05 +00:00
|
|
|
return []
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _get_season_search_strings(self, episode):
|
2014-03-10 05:18:05 +00:00
|
|
|
return []
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def _get_episode_search_strings(self, *args, **kwargs):
|
2014-03-10 05:18:05 +00:00
|
|
|
return []
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _get_title_and_url(self, item):
|
|
|
|
"""
|
2015-05-27 00:00:01 +00:00
|
|
|
Retrieves the title and URL data from the item
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
item: An elementtree.ElementTree element representing the <item> tag of the RSS feed, or a two part tup
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
Returns: A tuple containing two strings representing title and URL respectively
|
|
|
|
"""
|
2014-05-05 13:26:02 +00:00
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
title, url = None, None
|
2014-08-23 21:39:10 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
try:
|
|
|
|
if isinstance(item, tuple):
|
|
|
|
title = item[0]
|
|
|
|
url = item[1]
|
|
|
|
else:
|
|
|
|
if 'title' in item:
|
|
|
|
title = item.title
|
2014-08-23 21:39:10 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
if 'link' in item:
|
|
|
|
url = item.link
|
|
|
|
except Exception:
|
|
|
|
pass
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
if title:
|
2015-07-13 09:39:20 +00:00
|
|
|
title = re.sub(r'\s+', '.', u'%s' % title)
|
2014-08-23 21:39:10 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if url:
|
2015-07-13 09:39:20 +00:00
|
|
|
url = str(url).replace('&', '&')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-08-23 21:39:10 +00:00
|
|
|
return title, url
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def find_search_results(self, show, episodes, search_mode, manual_search=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
self._check_auth()
|
2014-05-01 01:20:53 +00:00
|
|
|
self.show = show
|
|
|
|
|
2014-04-27 10:31:54 +00:00
|
|
|
results = {}
|
2015-07-13 09:39:20 +00:00
|
|
|
item_list = []
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-05-16 05:40:49 +00:00
|
|
|
searched_scene_season = None
|
2015-07-13 09:39:20 +00:00
|
|
|
for ep_obj in episodes:
|
2014-08-30 08:47:00 +00:00
|
|
|
# search cache for episode result
|
2015-07-13 09:39:20 +00:00
|
|
|
cache_result = self.cache.searchCache(ep_obj, manual_search)
|
|
|
|
if cache_result:
|
|
|
|
if ep_obj.episode not in results:
|
|
|
|
results[ep_obj.episode] = cache_result
|
2014-08-30 08:47:00 +00:00
|
|
|
else:
|
2015-07-13 09:39:20 +00:00
|
|
|
results[ep_obj.episode].extend(cache_result)
|
2014-08-30 08:47:00 +00:00
|
|
|
|
|
|
|
# found result, search next episode
|
2014-07-21 05:07:53 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# skip if season already searched
|
2015-07-13 09:39:20 +00:00
|
|
|
if 1 < len(episodes) and ep_obj.scene_season == searched_scene_season:
|
2014-07-21 05:07:53 +00:00
|
|
|
continue
|
2014-05-16 05:40:49 +00:00
|
|
|
|
2014-05-16 09:16:01 +00:00
|
|
|
# mark season searched for season pack searches so we can skip later on
|
2015-07-13 09:39:20 +00:00
|
|
|
searched_scene_season = ep_obj.scene_season
|
2014-05-16 09:16:01 +00:00
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
if 'sponly' == search_mode:
|
2014-07-21 05:07:53 +00:00
|
|
|
# get season search results
|
2015-07-13 09:39:20 +00:00
|
|
|
for curString in self._get_season_search_strings(ep_obj):
|
|
|
|
item_list += self._do_search(curString, search_mode, len(episodes))
|
2014-05-16 05:16:35 +00:00
|
|
|
else:
|
2014-07-21 05:07:53 +00:00
|
|
|
# get single episode search results
|
2015-07-13 09:39:20 +00:00
|
|
|
for curString in self._get_episode_search_strings(ep_obj):
|
|
|
|
item_list += self._do_search(curString, 'eponly', len(episodes))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-21 05:07:53 +00:00
|
|
|
# if we found what we needed already from cache then return results and exit
|
|
|
|
if len(results) == len(episodes):
|
2014-05-30 07:36:47 +00:00
|
|
|
return results
|
2014-05-12 19:48:47 +00:00
|
|
|
|
2014-07-21 06:43:52 +00:00
|
|
|
# sort list by quality
|
2015-07-13 09:39:20 +00:00
|
|
|
if len(item_list):
|
2014-07-21 06:43:52 +00:00
|
|
|
items = {}
|
2015-07-13 09:39:20 +00:00
|
|
|
items_unknown = []
|
|
|
|
for item in item_list:
|
|
|
|
quality = self.get_quality(item, anime=show.is_anime)
|
|
|
|
if Quality.UNKNOWN == quality:
|
|
|
|
items_unknown += [item]
|
2014-07-21 06:59:57 +00:00
|
|
|
else:
|
2014-07-21 15:38:56 +00:00
|
|
|
if quality not in items:
|
|
|
|
items[quality] = [item]
|
|
|
|
else:
|
|
|
|
items[quality].append(item)
|
2014-07-22 00:55:45 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
item_list = list(itertools.chain(*[v for (k, v) in sorted(items.items(), reverse=True)]))
|
|
|
|
item_list += items_unknown if items_unknown else []
|
2014-07-21 06:43:52 +00:00
|
|
|
|
2014-07-21 05:07:53 +00:00
|
|
|
# filter results
|
2014-07-22 06:00:58 +00:00
|
|
|
cl = []
|
2015-07-13 09:39:20 +00:00
|
|
|
for item in item_list:
|
2014-07-21 05:07:53 +00:00
|
|
|
(title, url) = self._get_title_and_url(item)
|
2014-05-08 14:03:50 +00:00
|
|
|
|
2014-07-21 05:07:53 +00:00
|
|
|
# parse the file name
|
|
|
|
try:
|
2015-07-13 09:39:20 +00:00
|
|
|
parser = NameParser(False, convert=True)
|
|
|
|
parse_result = parser.parse(title)
|
2014-07-21 05:07:53 +00:00
|
|
|
except InvalidNameException:
|
2015-07-13 09:39:20 +00:00
|
|
|
logger.log(u'Unable to parse the filename ' + title + ' into a valid episode', logger.DEBUG)
|
2014-07-21 05:07:53 +00:00
|
|
|
continue
|
|
|
|
except InvalidShowException:
|
2015-06-09 14:43:54 +00:00
|
|
|
logger.log(u'No show name or scene exception matched the parsed filename ' + title, logger.DEBUG)
|
2014-07-21 05:07:53 +00:00
|
|
|
continue
|
2014-05-04 12:05:27 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
show_obj = parse_result.show
|
2014-07-21 05:07:53 +00:00
|
|
|
quality = parse_result.quality
|
|
|
|
release_group = parse_result.release_group
|
2014-07-22 04:53:32 +00:00
|
|
|
version = parse_result.version
|
2014-07-21 05:07:53 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
add_cache_entry = False
|
2015-08-14 23:02:05 +00:00
|
|
|
if not (show_obj.air_by_date or show_obj.is_sports):
|
2015-07-13 09:39:20 +00:00
|
|
|
if 'sponly' == search_mode:
|
2014-09-24 11:04:56 +00:00
|
|
|
if len(parse_result.episode_numbers):
|
2015-07-13 09:39:20 +00:00
|
|
|
logger.log(u'This is supposed to be a season pack search but the result ' + title
|
|
|
|
+ u' is not a valid season pack, skipping it', logger.DEBUG)
|
|
|
|
add_cache_entry = True
|
|
|
|
if len(parse_result.episode_numbers)\
|
|
|
|
and (parse_result.season_number not in set([ep.season for ep in episodes])
|
|
|
|
or not [ep for ep in episodes if ep.scene_episode in parse_result.episode_numbers]):
|
|
|
|
logger.log(u'The result ' + title + u' doesn\'t seem to be a valid episode that we are trying'
|
|
|
|
+ u' to snatch, ignoring', logger.DEBUG)
|
|
|
|
add_cache_entry = True
|
2014-07-22 06:00:58 +00:00
|
|
|
else:
|
2015-07-13 09:39:20 +00:00
|
|
|
if not len(parse_result.episode_numbers)\
|
|
|
|
and parse_result.season_number\
|
|
|
|
and not [ep for ep in episodes
|
|
|
|
if ep.season == parse_result.season_number
|
|
|
|
and ep.episode in parse_result.episode_numbers]:
|
|
|
|
logger.log(u'The result ' + title + u' doesn\'t seem to be a valid season that we are trying'
|
|
|
|
+ u' to snatch, ignoring', logger.DEBUG)
|
|
|
|
add_cache_entry = True
|
2014-09-07 05:25:01 +00:00
|
|
|
elif len(parse_result.episode_numbers) and not [ep for ep in episodes if
|
|
|
|
ep.season == parse_result.season_number and ep.episode in parse_result.episode_numbers]:
|
2015-07-13 09:39:20 +00:00
|
|
|
logger.log(u'The result ' + title + ' doesn\'t seem to be a valid episode that we are trying'
|
|
|
|
+ u' to snatch, ignoring', logger.DEBUG)
|
|
|
|
add_cache_entry = True
|
2014-07-22 06:00:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if not add_cache_entry:
|
2014-07-22 06:00:58 +00:00
|
|
|
# we just use the existing info for normal searches
|
2014-09-07 05:25:01 +00:00
|
|
|
actual_season = parse_result.season_number
|
2014-07-22 06:00:58 +00:00
|
|
|
actual_episodes = parse_result.episode_numbers
|
2014-07-21 05:07:53 +00:00
|
|
|
else:
|
2015-07-13 09:39:20 +00:00
|
|
|
if not parse_result.is_air_by_date:
|
|
|
|
logger.log(u'This is supposed to be a date search but the result ' + title
|
|
|
|
+ u' didn\'t parse as one, skipping it', logger.DEBUG)
|
|
|
|
add_cache_entry = True
|
2014-07-22 06:00:58 +00:00
|
|
|
else:
|
2014-08-22 07:21:37 +00:00
|
|
|
airdate = parse_result.air_date.toordinal()
|
2015-07-13 09:39:20 +00:00
|
|
|
my_db = db.DBConnection()
|
|
|
|
sql_results = my_db.select('SELECT season, episode FROM tv_episodes WHERE showid = ? AND airdate = ?',
|
|
|
|
[show_obj.indexerid, airdate])
|
2014-07-22 06:00:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if 1 != len(sql_results):
|
|
|
|
logger.log(u'Tried to look up the date for the episode ' + title + ' but the database didn\'t'
|
|
|
|
+ u' give proper results, skipping it', logger.WARNING)
|
|
|
|
add_cache_entry = True
|
2014-07-22 06:00:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if not add_cache_entry:
|
|
|
|
actual_season = int(sql_results[0]['season'])
|
|
|
|
actual_episodes = [int(sql_results[0]['episode'])]
|
2014-07-22 06:00:58 +00:00
|
|
|
|
|
|
|
# add parsed result to cache for usage later on
|
2015-07-13 09:39:20 +00:00
|
|
|
if add_cache_entry:
|
|
|
|
logger.log(u'Adding item from search to cache: ' + title, logger.DEBUG)
|
|
|
|
ci = self.cache.add_cache_entry(title, url, parse_result=parse_result)
|
|
|
|
if None is not ci:
|
2014-07-22 06:00:58 +00:00
|
|
|
cl.append(ci)
|
|
|
|
continue
|
2014-07-21 05:07:53 +00:00
|
|
|
|
|
|
|
# make sure we want the episode
|
2015-07-13 09:39:20 +00:00
|
|
|
want_ep = True
|
2014-07-21 05:07:53 +00:00
|
|
|
for epNo in actual_episodes:
|
2015-07-13 09:39:20 +00:00
|
|
|
if not show_obj.wantEpisode(actual_season, epNo, quality, manual_search):
|
|
|
|
want_ep = False
|
2014-07-21 05:07:53 +00:00
|
|
|
break
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if not want_ep:
|
|
|
|
logger.log(u'Ignoring result %s because we don\'t want an episode that is %s'
|
|
|
|
% (title, Quality.qualityStrings[quality]), logger.DEBUG)
|
2014-07-21 05:07:53 +00:00
|
|
|
continue
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
logger.log(u'Found result %s at %s' % (title, url), logger.DEBUG)
|
2014-07-21 05:07:53 +00:00
|
|
|
|
|
|
|
# make a result object
|
2015-07-13 09:39:20 +00:00
|
|
|
ep_obj = []
|
2014-07-21 05:07:53 +00:00
|
|
|
for curEp in actual_episodes:
|
2015-07-13 09:39:20 +00:00
|
|
|
ep_obj.append(show_obj.getEpisode(actual_season, curEp))
|
2014-07-21 05:07:53 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
result = self.get_result(ep_obj, url)
|
|
|
|
if None is result:
|
|
|
|
continue
|
|
|
|
result.show = show_obj
|
2014-07-21 05:07:53 +00:00
|
|
|
result.name = title
|
|
|
|
result.quality = quality
|
|
|
|
result.release_group = release_group
|
|
|
|
result.content = None
|
2014-07-22 04:53:32 +00:00
|
|
|
result.version = version
|
2014-07-21 05:07:53 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if 1 == len(ep_obj):
|
|
|
|
ep_num = ep_obj[0].episode
|
|
|
|
logger.log(u'Single episode result.', logger.DEBUG)
|
|
|
|
elif 1 < len(ep_obj):
|
|
|
|
ep_num = MULTI_EP_RESULT
|
|
|
|
logger.log(u'Separating multi-episode result to check for later - result contains episodes: '
|
|
|
|
+ str(parse_result.episode_numbers), logger.DEBUG)
|
|
|
|
elif 0 == len(ep_obj):
|
|
|
|
ep_num = SEASON_RESULT
|
|
|
|
logger.log(u'Separating full season result to check for later', logger.DEBUG)
|
|
|
|
|
|
|
|
if ep_num not in results:
|
|
|
|
results[ep_num] = [result]
|
2014-07-21 13:29:07 +00:00
|
|
|
else:
|
2015-07-13 09:39:20 +00:00
|
|
|
results[ep_num].append(result)
|
2014-05-03 23:26:12 +00:00
|
|
|
|
2014-07-22 06:00:58 +00:00
|
|
|
# check if we have items to add to cache
|
2015-07-13 09:39:20 +00:00
|
|
|
if 0 < len(cl):
|
|
|
|
my_db = self.cache.get_db()
|
|
|
|
my_db.mass_action(cl)
|
2014-07-22 06:00:58 +00:00
|
|
|
|
2014-05-03 23:26:12 +00:00
|
|
|
return results
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def find_propers(self, search_date=None):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
results = self.cache.listPropers(search_date)
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
return [classes.Proper(x['name'], x['url'], datetime.datetime.fromtimestamp(x['time']), self.show) for x in
|
|
|
|
results]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def seed_ratio(self):
|
|
|
|
"""
|
2014-05-08 22:28:28 +00:00
|
|
|
Provider should override this value if custom seed ratio enabled
|
|
|
|
It should return the value of the provider seed ratio
|
2015-07-13 09:39:20 +00:00
|
|
|
"""
|
2014-05-08 22:28:28 +00:00
|
|
|
return ''
|
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def _log_result(mode='cache', count=0, url='url missing'):
|
|
|
|
"""
|
|
|
|
Simple function to log the result of a search
|
|
|
|
:param count: count of successfully processed items
|
|
|
|
:param url: source url of item(s)
|
|
|
|
"""
|
|
|
|
mode = mode.lower()
|
|
|
|
logger.log(u'%s in response from %s' % (('No %s items' % mode,
|
|
|
|
'%s %s item%s' % (count, mode, maybe_plural(count)))[0 < count], url))
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def check_auth_cookie(self):
|
|
|
|
|
|
|
|
if hasattr(self, 'cookies'):
|
|
|
|
cookies = self.cookies
|
|
|
|
|
|
|
|
if not re.match('^(\w+=\w+[;\s]*)+$', cookies):
|
|
|
|
return False
|
|
|
|
|
|
|
|
cj = requests.utils.add_dict_to_cookiejar(self.session.cookies,
|
|
|
|
dict([x.strip().split('=') for x in cookies.split(';')
|
|
|
|
if x != ''])),
|
|
|
|
for item in cj:
|
|
|
|
if not isinstance(item, requests.cookies.RequestsCookieJar):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _check_cookie(self):
|
|
|
|
|
|
|
|
if self.check_auth_cookie():
|
|
|
|
return True, None
|
|
|
|
|
|
|
|
return False, 'Cookies not correctly formatted key=value pairs e.g. uid=xx;pass=yy)'
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
class NZBProvider(GenericProvider):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
def __init__(self, name, supports_backlog=True, anime_only=False):
|
|
|
|
GenericProvider.__init__(self, name, supports_backlog, anime_only)
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.providerType = GenericProvider.NZB
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def image_name(self):
|
|
|
|
|
|
|
|
return GenericProvider.image_name(self, 'newznab')
|
|
|
|
|
|
|
|
def maybe_apikey(self):
|
|
|
|
|
|
|
|
if hasattr(self, 'needs_auth'):
|
|
|
|
if hasattr(self, 'key') and 0 < len(self.key):
|
|
|
|
return self.key
|
|
|
|
if hasattr(self, 'api_key') and 0 < len(self.api_key):
|
|
|
|
return self.api_key
|
|
|
|
return None
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _check_auth(self):
|
|
|
|
|
|
|
|
has_key = self.maybe_apikey()
|
|
|
|
if has_key:
|
|
|
|
return has_key
|
|
|
|
if None is has_key:
|
|
|
|
raise AuthException('%s for %s is empty in config provider options'
|
|
|
|
% ('API key' + ('', ' and/or Username')[hasattr(self, 'username')], self.name))
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
return GenericProvider._check_auth(self)
|
2015-06-19 23:34:56 +00:00
|
|
|
|
|
|
|
def _find_propers(self, search_date=None):
|
|
|
|
|
|
|
|
cache_results = self.cache.listPropers(search_date)
|
|
|
|
results = [classes.Proper(x['name'], x['url'], datetime.datetime.fromtimestamp(x['time']), self.show) for x in
|
|
|
|
cache_results]
|
|
|
|
|
|
|
|
index = 0
|
2015-07-13 09:39:20 +00:00
|
|
|
alt_search = ('nzbs_org' == self.get_id())
|
2015-06-19 23:34:56 +00:00
|
|
|
term_items_found = False
|
|
|
|
do_search_alt = False
|
|
|
|
|
|
|
|
search_terms = ['.proper.', '.repack.']
|
|
|
|
proper_check = re.compile(r'(?i)\b(proper)|(repack)\b')
|
|
|
|
|
|
|
|
while index < len(search_terms):
|
|
|
|
search_params = {'q': search_terms[index]}
|
|
|
|
if alt_search:
|
|
|
|
|
|
|
|
if do_search_alt:
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
if term_items_found:
|
|
|
|
do_search_alt = True
|
|
|
|
term_items_found = False
|
|
|
|
else:
|
|
|
|
if do_search_alt:
|
|
|
|
search_params['t'] = 'search'
|
|
|
|
|
|
|
|
do_search_alt = (True, False)[do_search_alt]
|
|
|
|
|
|
|
|
else:
|
|
|
|
index += 1
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
for item in self._do_search(search_params, age=4):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
|
|
|
(title, url) = self._get_title_and_url(item)
|
|
|
|
|
|
|
|
if not proper_check.search(title):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if 'published_parsed' in item and item['published_parsed']:
|
|
|
|
result_date = item.published_parsed
|
|
|
|
if result_date:
|
|
|
|
result_date = datetime.datetime(*result_date[0:6])
|
|
|
|
else:
|
|
|
|
logger.log(u'Unable to figure out the date for entry %s, skipping it', title)
|
|
|
|
continue
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if not search_date or search_date < result_date:
|
2015-06-19 23:34:56 +00:00
|
|
|
search_result = classes.Proper(title, url, result_date, self.show)
|
|
|
|
results.append(search_result)
|
|
|
|
term_items_found = True
|
|
|
|
do_search_alt = False
|
|
|
|
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class TorrentProvider(GenericProvider):
|
2015-05-27 00:00:01 +00:00
|
|
|
|
|
|
|
def __init__(self, name, supports_backlog=True, anime_only=False):
|
|
|
|
GenericProvider.__init__(self, name, supports_backlog, anime_only)
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2014-07-25 04:29:03 +00:00
|
|
|
self.providerType = GenericProvider.TORRENT
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
self._seed_ratio = None
|
2015-09-12 17:06:54 +00:00
|
|
|
self.seed_time = None
|
2015-05-27 00:00:01 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def image_name(self):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
return GenericProvider.image_name(self, 'torrent')
|
2015-05-27 00:00:01 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def seed_ratio(self):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
return self._seed_ratio
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def get_quality(self, item, anime=False):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-05-27 00:00:01 +00:00
|
|
|
if isinstance(item, tuple):
|
|
|
|
name = item[0]
|
2015-06-06 15:08:59 +00:00
|
|
|
elif isinstance(item, dict):
|
|
|
|
name, url = self._get_title_and_url(item)
|
2015-05-27 00:00:01 +00:00
|
|
|
else:
|
|
|
|
name = item.title
|
|
|
|
return Quality.sceneQuality(name, anime)
|
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def _reverse_quality(quality):
|
|
|
|
|
|
|
|
return {
|
|
|
|
Quality.SDTV: 'HDTV x264',
|
|
|
|
Quality.SDDVD: 'DVDRIP',
|
|
|
|
Quality.HDTV: '720p HDTV x264',
|
|
|
|
Quality.FULLHDTV: '1080p HDTV x264',
|
|
|
|
Quality.RAWHDTV: '1080i HDTV mpeg2',
|
|
|
|
Quality.HDWEBDL: '720p WEB-DL h264',
|
|
|
|
Quality.FULLHDWEBDL: '1080p WEB-DL h264',
|
|
|
|
Quality.HDBLURAY: '720p Bluray x264',
|
|
|
|
Quality.FULLHDBLURAY: '1080p Bluray x264'
|
|
|
|
}.get(quality, '')
|
|
|
|
|
|
|
|
def _get_season_search_strings(self, ep_obj, detail_only=False, scene=True):
|
|
|
|
|
2015-08-14 23:02:05 +00:00
|
|
|
if ep_obj.show.air_by_date or ep_obj.show.is_sports:
|
2015-06-19 23:34:56 +00:00
|
|
|
ep_detail = str(ep_obj.airdate).split('-')[0]
|
2015-08-14 23:02:05 +00:00
|
|
|
elif ep_obj.show.is_anime:
|
2015-06-19 23:34:56 +00:00
|
|
|
ep_detail = ep_obj.scene_absolute_number
|
|
|
|
else:
|
2015-08-14 23:02:05 +00:00
|
|
|
ep_detail = 'S%02d' % int((ep_obj.season, ep_obj.scene_season)[bool(ep_obj.show.is_scene)])
|
2015-06-19 23:34:56 +00:00
|
|
|
|
2015-08-14 23:02:05 +00:00
|
|
|
detail = ({}, {'Season_only': [ep_detail]})[detail_only and not self.show.is_sports and not self.show.is_anime]
|
2015-06-19 23:34:56 +00:00
|
|
|
return [dict({'Season': self._build_search_strings(ep_detail, scene)}.items() + detail.items())]
|
|
|
|
|
|
|
|
def _get_episode_search_strings(self, ep_obj, add_string='', detail_only=False, scene=True, sep_date=' ', use_or=True):
|
|
|
|
|
|
|
|
if not ep_obj:
|
|
|
|
return []
|
|
|
|
|
2015-08-14 23:02:05 +00:00
|
|
|
if self.show.air_by_date or self.show.is_sports:
|
2015-06-19 23:34:56 +00:00
|
|
|
ep_detail = str(ep_obj.airdate).replace('-', sep_date)
|
2015-08-14 23:02:05 +00:00
|
|
|
if self.show.is_sports:
|
2015-06-19 23:34:56 +00:00
|
|
|
month = ep_obj.airdate.strftime('%b')
|
|
|
|
ep_detail = ([ep_detail] + [month], '%s|%s' % (ep_detail, month))[use_or]
|
2015-08-14 23:02:05 +00:00
|
|
|
elif self.show.is_anime:
|
2015-06-19 23:34:56 +00:00
|
|
|
ep_detail = ep_obj.scene_absolute_number
|
|
|
|
else:
|
2015-08-14 23:02:05 +00:00
|
|
|
season, episode = ((ep_obj.season, ep_obj.episode),
|
|
|
|
(ep_obj.scene_season, ep_obj.scene_episode))[bool(ep_obj.show.is_scene)]
|
|
|
|
ep_dict = {'seasonnumber': season, 'episodenumber': episode}
|
|
|
|
ep_detail = sickbeard.config.naming_ep_type[2] % ep_dict
|
|
|
|
append = (add_string, '')[self.show.is_anime]
|
|
|
|
detail = ({}, {'Episode_only': [ep_detail]})[detail_only and not self.show.is_sports and not self.show.is_anime]
|
2015-06-19 23:34:56 +00:00
|
|
|
return [dict({'Episode': self._build_search_strings(ep_detail, scene, append)}.items() + detail.items())]
|
|
|
|
|
|
|
|
def _build_search_strings(self, ep_detail, process_name=True, append=''):
|
|
|
|
"""
|
|
|
|
Build a list of search strings for querying a provider
|
|
|
|
:param ep_detail: String of episode detail or List of episode details
|
|
|
|
:param process_name: Bool Whether to call sanitizeSceneName() on show name
|
|
|
|
:param append: String to append to search strings
|
|
|
|
:return: List of search string parameters
|
|
|
|
"""
|
|
|
|
if not isinstance(ep_detail, list):
|
|
|
|
ep_detail = [ep_detail]
|
|
|
|
if not isinstance(append, list):
|
|
|
|
append = [append]
|
|
|
|
|
|
|
|
search_params = []
|
|
|
|
crop = re.compile(r'([\.\s])(?:\1)+')
|
|
|
|
for name in set(allPossibleShowNames(self.show)):
|
|
|
|
if process_name:
|
|
|
|
name = helpers.sanitizeSceneName(name)
|
|
|
|
for detail in ep_detail:
|
|
|
|
search_params += [crop.sub(r'\1', '%s %s' % (name, detail) + ('', ' ' + x)[any(x)]) for x in append]
|
|
|
|
return search_params
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def _check_auth(self):
|
2015-06-19 23:34:56 +00:00
|
|
|
|
|
|
|
if hasattr(self, 'username') and hasattr(self, 'password'):
|
|
|
|
if self.username and self.password:
|
|
|
|
return True
|
|
|
|
setting = 'Password or Username'
|
|
|
|
elif hasattr(self, 'username') and hasattr(self, 'passkey'):
|
|
|
|
if self.username and self.passkey:
|
|
|
|
return True
|
|
|
|
setting = 'Passkey or Username'
|
|
|
|
elif hasattr(self, 'api_key'):
|
|
|
|
if self.api_key:
|
|
|
|
return True
|
|
|
|
setting = 'Apikey'
|
2015-07-06 11:14:37 +00:00
|
|
|
elif hasattr(self, 'passkey'):
|
|
|
|
if self.passkey:
|
|
|
|
return True
|
|
|
|
setting = 'Passkey'
|
2015-06-19 23:34:56 +00:00
|
|
|
else:
|
2015-07-13 09:39:20 +00:00
|
|
|
return GenericProvider._check_auth(self)
|
2015-06-19 23:34:56 +00:00
|
|
|
|
|
|
|
raise AuthException('%s for %s is empty in config provider options' % (setting, self.name))
|
|
|
|
|
|
|
|
def _find_propers(self, search_date=datetime.datetime.today(), search_terms=None):
|
2015-05-27 00:00:01 +00:00
|
|
|
"""
|
|
|
|
Search for releases of type PROPER
|
|
|
|
:param search_date: Filter search on episodes since this date
|
2015-06-19 23:34:56 +00:00
|
|
|
:param search_terms: String or list of strings that qualify PROPER release types
|
2015-05-27 00:00:01 +00:00
|
|
|
:return: list of Proper objects
|
|
|
|
"""
|
|
|
|
results = []
|
|
|
|
|
|
|
|
my_db = db.DBConnection()
|
|
|
|
sql_results = my_db.select(
|
|
|
|
'SELECT s.show_name, e.showid, e.season, e.episode, e.status, e.airdate FROM tv_episodes AS e' +
|
|
|
|
' INNER JOIN tv_shows AS s ON (e.showid = s.indexer_id)' +
|
|
|
|
' WHERE e.airdate >= ' + str(search_date.toordinal()) +
|
|
|
|
' AND (e.status IN (%s)' % ','.join([str(x) for x in Quality.DOWNLOADED]) +
|
|
|
|
' OR (e.status IN (%s)))' % ','.join([str(x) for x in Quality.SNATCHED])
|
|
|
|
)
|
|
|
|
|
|
|
|
if not sql_results:
|
|
|
|
return results
|
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
clean_term = re.compile(r'(?i)[^a-z\|\.]+')
|
2015-05-27 00:00:01 +00:00
|
|
|
for sqlshow in sql_results:
|
2015-06-19 23:34:56 +00:00
|
|
|
showid, season, episode = [int(sqlshow[item]) for item in ('showid', 'season', 'episode')]
|
2015-05-27 00:00:01 +00:00
|
|
|
|
|
|
|
self.show = helpers.findCertainShow(sickbeard.showList, showid)
|
|
|
|
if not self.show:
|
|
|
|
continue
|
|
|
|
|
|
|
|
cur_ep = self.show.getEpisode(season, episode)
|
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
if None is search_terms:
|
|
|
|
search_terms = ['proper', 'repack']
|
|
|
|
elif not isinstance(search_terms, list):
|
|
|
|
if '' == search_terms:
|
|
|
|
search_terms = 'proper|repack'
|
|
|
|
search_terms = [search_terms]
|
2015-05-27 00:00:01 +00:00
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
for proper_term in search_terms:
|
|
|
|
proper_check = re.compile(r'(?i)(?:%s)' % clean_term.sub('', proper_term))
|
2015-05-27 00:00:01 +00:00
|
|
|
|
2015-06-19 23:34:56 +00:00
|
|
|
search_string = self._get_episode_search_strings(cur_ep, add_string=proper_term)
|
2015-07-13 09:39:20 +00:00
|
|
|
for item in self._do_search(search_string[0]):
|
2015-05-27 00:00:01 +00:00
|
|
|
title, url = self._get_title_and_url(item)
|
2015-06-19 23:34:56 +00:00
|
|
|
if not proper_check.search(title):
|
2015-05-27 00:00:01 +00:00
|
|
|
continue
|
|
|
|
results.append(classes.Proper(title, url, datetime.datetime.today(), self.show))
|
|
|
|
|
|
|
|
return results
|
2015-06-19 23:34:56 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _has_no_results(*html):
|
|
|
|
return re.search(r'(?i)<(?:h\d|strong)[^>]*>(?:'
|
|
|
|
+ 'your\ssearch\sdid\snot\smatch|'
|
|
|
|
+ 'nothing\sfound|'
|
|
|
|
+ 'no\storrents\sfound|'
|
|
|
|
+ '.*?there\sare\sno\sresults|'
|
|
|
|
+ '.*?no\shits\.\sTry\sadding'
|
|
|
|
+ ')', html[0])
|
|
|
|
|
|
|
|
def get_cache_data(self, *args, **kwargs):
|
|
|
|
|
|
|
|
search_params = {'Cache': ['']}
|
2015-07-13 09:39:20 +00:00
|
|
|
return self._do_search(search_params)
|