Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recheck command and make standalone build #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions btc/btc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
except ImportError:
from ordereddict import OrderedDict

try:
exit = exit
except Exception, e:
exit = sys.exit


def finish():
try:
sys.stdout.close()
Expand Down
54 changes: 54 additions & 0 deletions btc/btc_recheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import argparse
import sys
import time
from .btc import encoder, decoder, error, list_to_dict, dict_to_list, client

_description = 'recheck torrent'

def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--delay', default=0, type=int)
parser.add_argument('-w', '--wait', action="store_true")
args = parser.parse_args()

if sys.stdin.isatty():
parser.error('no input, pipe another btc command output into this command')
torrents = sys.stdin.read()

if len(torrents.strip()) == 0:
exit(1)

try:
torrents = decoder.decode(torrents)
except ValueError:
error('unexpected input: %s' % torrents)

time.sleep(args.delay)

hashes = [t['hash'] for t in torrents]
for h in hashes:
client.recheck_torrent(h)


while args.wait:
d = list_to_dict(client.list_torrents(), 'hash')
all_checked = True
for h in d:
if h not in hashes:
continue

if d[h]['state'] not in ('CHECKED',): # maybe this not needed
all_checked = False
break
if all_checked:
break
time.sleep(1)


if not sys.stdout.isatty():
d = list_to_dict(client.list_torrents(), 'hash')
d = dict((h, d[h]) for h in hashes if h in d)
print(encoder.encode(dict_to_list(d, 'hash')))

if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions btc/btc_show.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def main():
for key in keys:
s = ''
if args.show_keys:
s += '{0}{1}{0}{2}'.format(args.quote, key, args.separator)
s += u'{0}{1}{0}{2}'.format(args.quote, key, args.separator)
try:
print('{0}{1}{2}{1}'.format(s, args.quote, e[key]))
print(u'{0}{1}{2}{1}'.format(s, args.quote, e[key]))
except KeyError:
error('key not found: {}'.format(key))
else:
Expand Down
3 changes: 1 addition & 2 deletions btc/btc_start.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ def main():
break
time.sleep(1)


if not sys.stdout.isatty():
d = list_to_dict(client.list_torrents(), 'hash')
d = dict((h, d[h]) for h in hashes if h in d)
print(encoder.encode(dict_to_list(l)))
print(encoder.encode(dict_to_list(d, 'hash')))

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion btc/btc_stop.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def main():
if not sys.stdout.isatty():
d = list_to_dict(client.list_torrents(), 'hash')
d = dict((h, d[h]) for h in hashes if h in d)
print(encoder.encode(dict_to_list(l)))
print(encoder.encode(dict_to_list(d, 'hash')))

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion btc/btc_wait.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def main():
if not sys.stdout.isatty():
d = list_to_dict(client.list_torrents(), 'hash')
d = dict((h, d[h]) for h in hashes if h in d)
print(encoder.encode(dict_to_list(d)))
print(encoder.encode(dict_to_list(d, 'hash')))

if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions btc/btclient.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def stop_torrent(self, thash):
def start_torrent(self, thash):
self.send_command('action=start&hash=%s' % thash)

def recheck_torrent(self, thash):
self.send_command('action=recheck&hash=%s' % thash)

def torrent_files(self, thash, sids={}):
if isinstance(thash, list):
if len(thash) == 0:
Expand Down
Binary file added dist/btc.exe
Binary file not shown.
Binary file added dist/library.zip/_hashlib.pyd
Binary file not shown.
Binary file added dist/library.zip/_socket.pyd
Binary file not shown.
Binary file added dist/library.zip/_ssl.pyd
Binary file not shown.
Empty file.
158 changes: 158 additions & 0 deletions dist/library.zip/btc/bencode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import sys

try:
unicode
except NameError:
unicode = str

try:
long
except NameError:
long = int

if bytes == str:
def f(s, *args, **kwargs):
return str(s)
bytes = f

class BTFailure(Exception):
pass

def bytes_index(s, pattern, start):
if sys.version_info[0] == 2:
return s.index(pattern, start)

assert len(pattern) == 1
for i, e in enumerate(s[start:]):
if e == ord(pattern):
return i + start
raise ValueError('substring not found')

def ord_(s):
if sys.version_info[0] == 3:
return ord(s)
return s

def chr_(s):
if sys.version_info[0] == 3:
return chr(s)
return s

def decode_int(x, f):
f += 1
newf = bytes_index(x, 'e', f)
n = int(x[f:newf])
if x[f] == ord_('-'):
if x[f + 1] == ord_('0'):
raise ValueError
elif x[f] == ord_('0') and newf != f+1:
raise ValueError
return (n, newf+1)

def decode_string(x, f):
colon = bytes_index(x, ':', f)
n = int(x[f:colon])
if x[f] == ord_('0') and colon != f+1:
raise ValueError
colon += 1
return (x[colon:colon+n], colon+n)

def decode_list(x, f):
r, f = [], f+1
while x[f] != ord_('e'):
v, f = decode_func[chr_(x[f])](x, f)
r.append(v)
return (r, f + 1)

def decode_dict(x, f):
r, f = {}, f+1
while x[f] != ord_('e'):
k, f = decode_string(x, f)
r[k], f = decode_func[chr_(x[f])](x, f)
return (r, f + 1)

decode_func = {}
decode_func['l'] = decode_list
decode_func['d'] = decode_dict
decode_func['i'] = decode_int
decode_func['0'] = decode_string
decode_func['1'] = decode_string
decode_func['2'] = decode_string
decode_func['3'] = decode_string
decode_func['4'] = decode_string
decode_func['5'] = decode_string
decode_func['6'] = decode_string
decode_func['7'] = decode_string
decode_func['8'] = decode_string
decode_func['9'] = decode_string

def bdecode(x):
try:
r, l = decode_func[chr_(x[0])](x, 0)
except (IndexError, KeyError, ValueError):
raise
raise BTFailure("not a valid bencoded string")
if l != len(x):
raise BTFailure("invalid bencoded value (data after valid prefix)")
return r

class Bencached(object):

__slots__ = ['bencoded']

def __init__(self, s):
self.bencoded = s

def encode_bencached(x,r):
r.append(x.bencoded)

def encode_int(x, r):
r.append(b'i')
r.append(bytes(str(x), 'ascii'))
r.append(b'e')

def encode_bool(x, r):
if x:
encode_int(1, r)
else:
encode_int(0, r)

def encode_string(x, r):
r.extend((bytes(str(len(x)), 'ascii'), b':', x))

def encode_list(x, r):
r.append(b'l')
for i in x:
encode_func[type(i)](i, r)
r.append(b'e')

def encode_dict(x,r):
r.append(b'd')
ilist = list(x.items())
ilist.sort()
for k, v in ilist:
r.extend((bytes(str(len(k)), 'ascii'), b':', k))
encode_func[type(v)](v, r)
r.append(b'e')

encode_func = {}
encode_func[Bencached] = encode_bencached
encode_func[int] = encode_int
encode_func[long] = encode_int
encode_func[str] = encode_string
encode_func[bytes] = encode_string
encode_func[unicode] = encode_string
encode_func[list] = encode_list
encode_func[tuple] = encode_list
encode_func[dict] = encode_dict

try:
from types import BooleanType
encode_func[BooleanType] = encode_bool
except ImportError:
pass

def bencode(x):
r = []
encode_func[type(x)](x, r)
return b''.join(r)
Loading