1+ import argparse
2+
13import requests
24
35class FailedToStreamException (Exception ):
@@ -44,6 +46,8 @@ def stream_torrent(remote_control_url, infohash=None, path=None, wait_for_end_pi
4446 data = r .json ()
4547 if data ['status' ] == 'success' :
4648 return data ['url' ]
49+ else :
50+ raise FailedToStreamException ('Request failed: %r' % (data , ))
4751
4852 if torrent_body :
4953 r = requests .post (url , auth = (username , password ), params = params , data = torrent_body )
@@ -53,5 +57,40 @@ def stream_torrent(remote_control_url, infohash=None, path=None, wait_for_end_pi
5357 data = r .json ()
5458 if data ['status' ] == 'success' :
5559 return data ['url' ]
60+ else :
61+ raise FailedToStreamException ('Request failed: %r' % (data , ))
5662
5763 raise FailedToStreamException ('Streaming was never successful' )
64+
65+
66+ if __name__ == '__main__' :
67+ parser = argparse .ArgumentParser (description = "Stream some torrents" )
68+ parser .add_argument ('url' , help = "Full API Url including auth info" )
69+ parser .add_argument ('--infohash' , nargs = '?' , help = "Infohash of torrent to stream" )
70+ parser .add_argument ('--path' , nargs = '?' , help = "Path to file within the torrent to stream" )
71+ parser .add_argument ('--label' , nargs = '?' , help = "Label to add the torrent with" )
72+ parser .add_argument ('--torrent' , nargs = '?' , help = "Path to the torrent to stream" , type = argparse .FileType (mode = 'rb' ))
73+ parser .add_argument ('--skip_wait_for_end_pieces' , help = "Wait until client downloaded the first and last piece of the torrent" , action = 'store_false' )
74+
75+
76+ args = parser .parse_args ()
77+
78+ kwargs = {
79+ 'remote_control_url' : args .url ,
80+ 'wait_for_end_pieces' : args .skip_wait_for_end_pieces
81+ }
82+
83+ if args .infohash :
84+ kwargs ['infohash' ] = args .infohash
85+
86+ if args .path :
87+ kwargs ['path' ] = args .path
88+
89+ if args .label :
90+ kwargs ['label' ] = args .label
91+
92+ if args .torrent :
93+ kwargs ['torrent_body' ] = args .torrent .read ()
94+
95+ result = stream_torrent (** kwargs )
96+ print ('URL %s' % (result , ))
0 commit comments