This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sowsapi.py
127 lines (109 loc) · 3.51 KB
/
sowsapi.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python2.7
import json
import time
import requests
__site_url__ = "https://bemaniso.ws/"
__torrent_url__ = "https://bemaniso.ws:34443/"
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3)'\
'AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79'\
'Safari/535.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9'\
',*/*;q=0.8',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'}
class LoginException(Exception):
pass
class RequestException(Exception):
pass
class SowsAPI:
def __init__(self, username=None, password=None, totp=None):
self.session = requests.Session()
self.session.headers.update(headers)
self.browser = None
self.username = username
self.password = password
self.authkey = None
self.passkey = None
self.userid = None
self.tracker = __torrent_url__ + "/"
self.last_request = time.time()
self.rate_limit = 2.0 # seconds between requests
self.site = "SOWS"
self._login()
def _login(self):
'''Logs in user and gets authkey from server'''
loginpage = __site_url__ + '/login.php'
data = {'username': self.username,
'password': self.password}
r = self.session.post(loginpage, data=data)
if r.status_code != 200:
raise LoginException
accountinfo = self.request('index')
self.authkey = accountinfo['authkey']
self.passkey = accountinfo['passkey']
self.userid = accountinfo['id']
def limit(self):
while time.time() - self.last_request < self.rate_limit:
time.sleep(0.1)
def logout(self):
self.session.get("%s/logout.php?auth=%s" % (__site_url__, self.authkey))
def request(self, action, **kwargs):
'''Makes an AJAX request at a given action page'''
self.limit()
ajaxpage = __site_url__ + '/ajax.php'
params = {'action': action}
if self.authkey:
params['auth'] = self.authkey
params.update(kwargs)
r = self.session.get(ajaxpage, params=params, allow_redirects=False)
self.last_request = time.time()
try:
parsed = json.loads(r.content.decode())
print(parsed)
if parsed['status'] != 'success':
raise RequestException
return parsed['response']
except ValueError as e:
raise RequestException(e)
def request_html(self, action, **kwargs):
self.limit()
ajaxpage = __site_url__ + '/' + action
if self.authkey:
kwargs['auth'] = self.authkey
r = self.session.get(ajaxpage, params=kwargs, allow_redirects=False)
self.last_request = time.time()
return r.content
def get_torrent(self, torrent_id):
'''Downloads the torrent at torrent_id using the authkey and passkey'''
self.limit()
torrentpage = __site_url__ + '/torrents.php'
params = {'action': 'download', 'id': torrent_id}
if self.authkey:
params['authkey'] = self.authkey
params['torrent_pass'] = self.passkey
r = self.session.get(torrentpage, params=params, allow_redirects=False)
self.last_request = time.time() + 2.0
if r.status_code == 200 and 'application/x-bittorrent' in r.headers['content-type']:
return r.content
return None
def get_torrent_info(self, id=None, hash=None):
if not (hash is None):
return self.request('torrent', hash=hash)
else:
return self.request('torrent', id=id)
def HTMLtoBBCODE(self, text):
self.limit()
params = {
"action": "parse_html"
}
data = [
("html", text)
]
url = __site_url__ + '/upload.php'
r = self.session.post(url, data=data, params=params)
self.last_request = time.time()
return r.content