1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-10 10:04:21 +01:00
pirate-get/pirate/local.py
2016-07-02 14:05:18 -07:00

41 lines
1.0 KiB
Python

import urllib.parse as parse
import html.parser as parser
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):
f = open(db)
xml = f.readlines()
f.close()
parser = BayParser()
parser.q = (' '.join(terms)).lower()
parser.feed(''.join(xml))
return parser.results