Skip to content

Instantly share code, notes, and snippets.

@goncalomb
Created April 23, 2018 21:52
Show Gist options
  • Save goncalomb/2d2987525719afae0fa45626f250264b to your computer and use it in GitHub Desktop.
Save goncalomb/2d2987525719afae0fa45626f250264b to your computer and use it in GitHub Desktop.
Extract and index the SkyTorrents dump.
#!/usr/bin/env python3
"""
--------------------------------------------------------------------------------
Copyright (c) 2018 Gonçalo Baltazar <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--------------------------------------------------------------------------------
Extract and index the SkyTorrents dump (WIP).
Python 3 and 7z are required.
Usage:
./skyt.py dump -f skytorrents.in_dump.7z > skytorrents_dump.txt
--------------------------------------------------------------------------------
"""
import argparse, subprocess, hashlib, sys, io
def read_torrent(reader):
hash_inst = None
info_hash = None
def peek_byte():
return reader.peek(1)[:1]
def read_bytes(s):
buf = reader.read(s)
if hash_inst:
hash_inst.update(buf)
if s == 0:
raise Exception("bencode: unexpected end of stream")
return buf
def read_integer_raw():
sign = 1
int_b = b''
o = ord(peek_byte())
if o == 45:
sign = -1
read_bytes(1)
o = ord(peek_byte())
while o >= 48 and o <= 57:
int_b += read_bytes(1)
o = ord(peek_byte())
return sign*int(int_b.decode('ascii'))
def read_integer():
if read_bytes(1) != b'i':
raise Exception("bencode: invalid integer")
i = read_integer_raw()
if read_bytes(1) != b'e':
raise Exception("bencode: invalid integer")
return i
def read_byte_string():
size = read_integer_raw()
if read_bytes(1) != b':':
raise Exception("bencode: invalid byte string")
str_b = b''
while size:
buf = read_bytes(size)
size -= len(buf)
str_b += buf
return str_b
def read_dictionary(depth):
nonlocal hash_inst
nonlocal info_hash
if read_bytes(1) != b'd':
raise Exception("bencode: invalid dictionary")
dic = {}
while peek_byte() != b'e':
key = read_byte_string()
if depth == 0 and key == b'info':
hash_inst = hashlib.sha1()
dic[key] = read_next(depth + 1)
if depth == 0 and key == b'info':
info_hash = hash_inst.digest()
hash_inst = None
read_bytes(1)
return dic
def read_list(depth):
lis = []
if read_bytes(1) != b'l':
raise Exception("bencode: invalid list")
while peek_byte() != b'e':
lis.append(read_next(depth + 1))
read_bytes(1)
return lis
def read_next(depth):
c = peek_byte()
if c == b'd':
return read_dictionary(depth)
elif c == b'l':
return read_list(depth)
elif c == b'i':
return read_integer()
return read_byte_string()
dic = read_dictionary(0)
return (info_hash, dic)
def command_dump(args):
with subprocess.Popen(['7z', 'e', '-so', args.f], stdout=subprocess.PIPE) as proc:
reader = io.BufferedReader(proc.stdout)
i = 0
while True:
torr = read_torrent(reader)
sys.stdout.buffer.write(torr[0].hex().encode('ascii'))
sys.stdout.buffer.write(b' ')
sys.stdout.buffer.write(torr[1][b'info'][b'name'])
sys.stdout.buffer.write(b'\n')
i += 1
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(description='Extract and index the SkyTorrents dump.')
subparsers = parser.add_subparsers(title='commands', dest='command')
subparsers.required = True
parser_dump = subparsers.add_parser('dump', description='dump torrent hash and names')
parser_dump.add_argument('-f', metavar='file', help='skytorrents.in_dump.7z file')
args = parser.parse_args()
locals()['command_' + args.command](args)
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment