-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
74 lines (65 loc) · 2.89 KB
/
api.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
from flask import Flask, jsonify, request, Blueprint, render_template
from torrent_manager import Torrent, get_torrents_db, save_torrent, delete_torrent, get_torrent
api_blueprint = Blueprint('api',__name__)
from main import libtorrent_session, config
@api_blueprint.route('/api/v1/torrent', methods=['GET'])
def get_all_torrents():
all_torrent_ids = list(get_torrents_db().keys())
return jsonify({'torrent_ids': all_torrent_ids}), 200
@api_blueprint.route('/api/v1/torrent', methods=['POST'])
def add_torrent():
data = request.get_json()
magnet_link = data.get('magnet_link')
if not magnet_link:
return jsonify({'error': 'Magnet link is required'}), 400
torrent = Torrent(
magnet_link, libtorrent_session, config['download_dir'],
config['default_upload_speed'], config['default_download_speed']
)
torrent_id = str(len(get_torrents_db()) + 1)
save_torrent(torrent_id, torrent)
torrent.play_download() # Start download by default
return jsonify({'message': 'Torrent added successfully', 'torrent_id': torrent_id}), 201
@api_blueprint.route('/api/v1/torrent/<torrent_id>', methods=['DELETE'])
def delete_torrent_route(torrent_id):
torrent = get_torrent(torrent_id, libtorrent_session)
if torrent:
torrent.stop_download()
delete_torrent(torrent_id)
return jsonify({'message': 'Torrent deleted successfully'}), 200
else:
return jsonify({'error': 'Torrent not found'}), 404
@api_blueprint.route('/api/v1/torrent/<torrent_id>', methods=['GET'])
def get_torrent_status(torrent_id):
torrent = get_torrent(torrent_id, libtorrent_session)
if torrent:
status = torrent.get_status()
return jsonify(status), 200
else:
return jsonify({'error': 'Torrent not found'}), 404
@api_blueprint.route('/api/v1/torrent/<torrent_id>/pause', methods=['POST'])
def pause_torrent(torrent_id):
torrent = get_torrent(torrent_id, libtorrent_session)
if torrent:
torrent.pause_download()
save_torrent(torrent_id, torrent)
return jsonify({'message': 'Torrent download paused'}), 200
else:
return jsonify({'error': 'Torrent not found'}), 404
@api_blueprint.route('/api/v1/torrent/<torrent_id>/play', methods=['POST'])
def play_torrent(torrent_id):
torrent = get_torrent(torrent_id, libtorrent_session)
if torrent:
torrent.play_download()
save_torrent(torrent_id, torrent)
return jsonify({'message': 'Torrent download resumed'}), 200
else:
return jsonify({'error': 'Torrent not found'}), 404
@api_blueprint.route('/api/v1/torrent/<torrent_id>/progress', methods=['GET'])
def get_torrent_progress(torrent_id):
torrent = get_torrent(torrent_id, libtorrent_session)
if torrent:
progress = torrent.get_progress()
return jsonify({'progress': progress}), 200
else:
return jsonify({'error': 'Torrent not found'}), 404