<s> import argparse <EOL> import time <EOL> import hashlib <EOL> import os <EOL> from . import btclient <EOL> from . import utils <EOL> from . bencode import bdecode , bencode <EOL> from . btc import encoder , decoder , client , error <EOL> _description = '<STR_LIT>' <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT:value>' ) <EOL> group = parser . add_mutually_exclusive_group ( required = False ) <EOL> group . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' ) <EOL> group . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> if not args . url and not args . file : <EOL> args . file = os . path . exists ( args . value ) <EOL> args . url = not args . file <EOL> if args . url : <EOL> if args . value . startswith ( '<STR_LIT>' ) : <EOL> if args . value . find ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> client . add_torrent_url ( args . value ) <EOL> return <EOL> else : <EOL> args . value = utils . httpize ( args . value ) <EOL> try : <EOL> torrent = utils . get ( args . value , utf8 = False ) <EOL> except utils . HTTPError : <EOL> error ( '<STR_LIT>' % args . value ) <EOL> client . add_torrent_url ( args . value ) <EOL> elif args . file : <EOL> if not os . path . exists ( args . value ) : <EOL> error ( '<STR_LIT>' % args . value ) <EOL> try : <EOL> f = open ( args . value , '<STR_LIT:rb>' ) <EOL> torrent = f . read ( ) <EOL> f . close ( ) <EOL> except : <EOL> error ( '<STR_LIT>' % args . value ) <EOL> client . add_torrent_file ( args . value ) <EOL> added = None <EOL> try : <EOL> decoded = bdecode ( torrent ) <EOL> encoded = bencode ( decoded [ b'<STR_LIT:info>' ] ) <EOL> except : <EOL> error ( '<STR_LIT>' ) <EOL> h = hashlib . sha1 ( encoded ) . hexdigest ( ) . upper ( ) <EOL> while not added : <EOL> l = client . list_torrents ( ) <EOL> for t in l : <EOL> if t [ '<STR_LIT>' ] == h : <EOL> added = t <EOL> break <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> print ( encoder . encode ( [ added ] ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
