2015-08-30 03:28:43 +02:00
|
|
|
import urllib.parse as parse
|
|
|
|
import html.parser as parser
|
|
|
|
|
2015-08-30 04:00:42 +02:00
|
|
|
|
2015-08-30 03:28:43 +02:00
|
|
|
class BayParser(parser.HTMLParser):
|
|
|
|
title = ''
|
|
|
|
q = ''
|
|
|
|
state = 'looking'
|
|
|
|
results = []
|
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
if tag == 'title':
|
|
|
|
self.state = 'title'
|
|
|
|
if tag == 'magnet' and self.state == 'matched':
|
|
|
|
self.state = 'magnet'
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
if self.state == 'title':
|
|
|
|
if data.lower().find(self.q) != -1:
|
|
|
|
self.title = data
|
|
|
|
self.state = 'matched'
|
|
|
|
else:
|
|
|
|
self.state = 'looking'
|
|
|
|
if self.state == 'magnet':
|
|
|
|
self.results.append([
|
|
|
|
'magnet:?xt=urn:btih:' +
|
|
|
|
parse.quote(data) +
|
|
|
|
'&dn=' +
|
|
|
|
parse.quote(self.title), '?', '?'])
|
|
|
|
self.state = 'looking'
|
|
|
|
|
|
|
|
|
|
|
|
def search(db, terms):
|
2016-07-02 23:05:18 +02:00
|
|
|
f = open(db)
|
|
|
|
xml = f.readlines()
|
|
|
|
f.close()
|
2015-10-09 05:33:13 +02:00
|
|
|
parser = BayParser()
|
|
|
|
parser.q = (' '.join(terms)).lower()
|
2015-08-30 03:28:43 +02:00
|
|
|
parser.feed(''.join(xml))
|
2015-08-30 04:00:42 +02:00
|
|
|
return parser.results
|