5
from flexget.entry import Entry
6
from flexget.plugin import register_plugin, internet
7
from flexget.utils.soup import get_soup
8
from flexget.utils.tools import urlopener
10
log = logging.getLogger("newzleech")
13
class UrlRewriteNewzleech(object):
15
UrlRewriter or search by using newzleech.com
16
TODO: implement basic url rewriting
21
def search(self, query, comparator, config=None):
22
# TODO: Implement comparator matching
23
url = u'http://newzleech.com/?%s' % str(urllib.urlencode({'q': query.encode('latin1'),
24
'm': 'search', 'group': '', 'min': 'min',
25
'max': 'max', 'age': '', 'minage': '', 'adv': ''}))
26
#log.debug('Search url: %s' % url)
29
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
30
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
31
'Accept-Language': 'en-us,en;q=0.5',
32
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
34
'Connection': 'keep-alive',
37
req = urllib2.Request(url, headers=txheaders)
38
page = urlopener(req, log)
44
for item in soup.findAll('table', attrs={'class': 'contentt'}):
45
subject_tag = item.find('td', attrs={'class': 'subject'}).next
46
subject = ''.join(subject_tag.findAll(text=True))
47
complete = item.find('td', attrs={'class': 'complete'}).contents[0]
48
size = item.find('td', attrs={'class': 'size'}).contents[0]
49
nzb_url = 'http://newzleech.com/' + item.find('td', attrs={'class': 'get'}).next.get('href')
51
#TODO: confidence match
52
# generate regexp from entry title and see if it matches subject
54
wildcardize = [' ', '-']
55
for wild in wildcardize:
56
regexp = regexp.replace(wild, '.')
57
regexp = '.*' + regexp + '.*'
58
#log.debug('Title regexp: %s' % regexp)
60
if re.match(regexp, subject):
61
log.debug('%s matches to regexp' % subject)
62
if complete != u'100':
63
log.debug('Match is incomplete %s from newzleech, skipping ..' % query)
65
log.info('Found \'%s\'' % query)
67
def parse_size(value):
69
num = float(value[:-3])
71
log.error('Failed to parse_size %s' % value)
73
# convert into megabytes
80
nzb = Entry(title=subject, url=nzb_url, content_size=parse_size(size))
82
nzb['size'] = parse_size(size)
87
log.debug('Unable to find %s' % query)
91
nzbs.sort(reverse=True, key=lambda x: x.get('content_size', 0))
95
register_plugin(UrlRewriteNewzleech, 'newzleech', groups=['search'])