-
Notifications
You must be signed in to change notification settings - Fork 8
/
smartorrent.py
93 lines (79 loc) · 3.19 KB
/
smartorrent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
#VERSION: 1.1
#AUTHOR: Davy39 <[email protected]>
#CONTRIBUTORS: Simon <[email protected]>
# Copyleft
from __future__ import print_function
try:
# python2
from HTMLParser import HTMLParser
except ImportError:
# python3
from html.parser import HTMLParser
from helpers import download_file, retrieve_url
from novaprinter import prettyPrinter
class smartorrent(object):
url = "http://smartorrent.com"
name = "Smartorrent (french)"
supported_categories = {"all": "0"}
# TODO: Filter general results for specific categories
def download_torrent(self, url):
print(download_file(url))
class SimpleHTMLParser(HTMLParser):
def __init__(self, results):
HTMLParser.__init__(self)
self.td_counter = None
self.next_title = False
self.next_param = None
self.current_item = None
self.results = results
def handle_starttag(self, tag, attr):
method = 'start_' + tag
if hasattr(self, method) and tag in ('a', 'small', 'td'):
getattr(self, method)(attr)
def start_a(self, attr):
params = dict(attr)
url_prefix = smartorrent.url + '/torrents/'
if (not params.get('data-toggle', None)
and params.get('href', '').startswith(url_prefix)):
self.current_item = {}
self.td_counter = 0
self.current_item['desc_link'] = params['href'].strip()
torrent_id = params['href'].split(url_prefix)[1]
self.current_item['link'] = smartorrent.url + '/download/' + torrent_id
def start_small(self, attr):
if self.current_item and self.td_counter == 0:
self.next_title = True
def handle_data(self, data):
if self.next_title:
self.next_title = False
self.current_item['name'] = data.strip()
elif self.td_counter == 1:
if 'size' not in self.current_item:
self.current_item['size'] = data.strip()
elif self.td_counter == 3:
if 'seeds' not in self.current_item:
self.current_item['seeds'] = data.strip()
elif self.td_counter == 4:
if 'leech' not in self.current_item:
self.current_item['leech'] = data.strip()
def start_td(self, attr):
if isinstance(self.td_counter, int):
self.td_counter += 1
if self.td_counter > 5:
self.td_counter = None
if self.current_item:
self.current_item["engine_url"] = smartorrent.url
prettyPrinter(self.current_item)
self.results.append("a")
def search(self, what, cat="all"):
for page in range(1, 35):
results = []
parser = self.SimpleHTMLParser(results)
data = retrieve_url(
self.url + '/search?page={}&search={}'.format(page, what)
)
parser.feed(data)
parser.close()
if len(results) <= 0:
break