-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtnclient.py
71 lines (54 loc) · 1.84 KB
/
btnclient.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
import urllib.request
from functools import wraps
from flask import Flask, request
from flask_restful import Resource, Api, abort
from flask_cors import CORS
import json
import configparser
import torrent_parser as tp
import bencoding
import hashlib
from deluge_api import Deluge
app = Flask(__name__)
api = Api(app)
CORS(app)
config = configparser.ConfigParser()
config.read('config.ini')
client_config = config["CLIENT"]
deluge = Deluge(client_config["host"], client_config["port"], client_config["pass"])
def require_appkey(view_function):
@wraps(view_function)
def decorated_function(*args, **kwargs):
if request.headers.get('key') and request.headers.get('key') == client_config["api_key"]:
return view_function(*args, **kwargs)
else:
abort(401)
return decorated_function
class DelugeApp(Resource):
@require_appkey
def get(self):
r = deluge.get_torrents()
return r
class AddTorrents(Resource):
@require_appkey
def post(self):
content = json.loads(request.get_json(silent=True))
url = str(content['url']).format(client_config["auth_key"], client_config["torrent_pass"])
name = content['name']
print(name)
store_url = client_config["watch_folder"] + str(name) + ".torrent"
urllib.request.urlretrieve(url, store_url)
data = tp.parse_torrent_file(store_url)
return str(data["info"]["name"])
class RemoveTorrents(Resource):
@require_appkey
def post(self):
content = json.loads(request.get_json(silent=True))
hash = content['hash']
r = deluge.remove_torrents(hash)
return r
api.add_resource(DelugeApp, '/deluge/getTorrents')
api.add_resource(AddTorrents, '/addtorrent')
api.add_resource(RemoveTorrents, '/deltorrent')
if __name__ == '__main__':
app.run(port=5002, host="0.0.0.0")