-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# HTTP API | ||
|
||
Stream using the HTTP API built into Deluge Streaming. | ||
|
||
## Requirements | ||
|
||
* Python | ||
* requests package | ||
|
||
## Config | ||
|
||
You need to enable HTTP API in Deluge Streaming config. | ||
|
||
## Example | ||
|
||
```python | ||
from streamtorrent import stream_torrent | ||
|
||
if __name__ == '__main__': | ||
with open('TPB.AFK.2013.1080p.h264-SimonKlose', 'rb') as f: | ||
torrent_data = f.read() | ||
|
||
# Stream 1080p TPB AFK using infohash to avoid posting the torrent | ||
# if it already exist. | ||
url = stream_torrent( | ||
'http://stream:password@127.0.0.1:46123/streaming/stream', | ||
infohash='411a7a164505636ab1a8276395b375a3a30bff32', | ||
torrent_body=torrent_data, | ||
label='tpbafk' | ||
) | ||
print('we can stream %s' % (url, )) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import requests | ||
|
||
class FailedToStreamException(Exception): | ||
pass | ||
|
||
|
||
def stream_torrent(remote_control_url, infohash=None, path=None, wait_for_end_pieces=True, label=None, torrent_body=None): | ||
""" | ||
Add a torrent to deluge, stream it and return a URL to where it can be watched. | ||
All optional parameters are optional but you will need to at least provide an infohash (if the torrent is already added) | ||
or a torrent_body (if you want the torrent added). | ||
remote_control_url - The URL found in Deluge Streaming config | ||
infohash - Torrent infohash, makes it faster if the torrent is already added | ||
path - path inside the torrent you want to stream | ||
wait_for_end_pieces - make sure the first and last piece are downloaded before returning url. | ||
This might be necessary for some players | ||
label - Label to set in deluge | ||
torrent_body - The content of the .torrent file you want to stream | ||
""" | ||
first_part, second_part = remote_control_url.split('@') | ||
username, password = first_part.split('/')[2].split(':') | ||
url = '/'.join(first_part.split('/')[:2]) + '/' + second_part | ||
|
||
params = {} | ||
if infohash: | ||
params['infohash'] = infohash | ||
|
||
if wait_for_end_pieces: | ||
params['wait_for_end_pieces'] = wait_for_end_pieces | ||
|
||
if path: | ||
params['path'] = path | ||
|
||
if label: | ||
params['label'] = label | ||
|
||
if infohash: # try to stream it without posting torrent body first | ||
r = requests.get(url, auth=(username, password), params=params) | ||
if r.status_code != 200: | ||
raise FailedToStreamException('Got non-200 error code from Deluge') | ||
|
||
data = r.json() | ||
if data['status'] == 'success': | ||
return data['url'] | ||
|
||
if torrent_body: | ||
r = requests.post(url, auth=(username, password), params=params, data=torrent_body) | ||
if r.status_code != 200: | ||
raise FailedToStreamException('Got non-200 error code from Deluge') | ||
|
||
data = r.json() | ||
if data['status'] == 'success': | ||
return data['url'] | ||
|
||
raise FailedToStreamException('Streaming was never successful') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters