Skip to content

Instantly share code, notes, and snippets.

@mjpieters
Created May 18, 2012 12:31
Show Gist options
  • Save mjpieters/2725038 to your computer and use it in GitHub Desktop.
Save mjpieters/2725038 to your computer and use it in GitHub Desktop.
List .torrent files that have pathlengths over a given threshold (defaults to 180 characters).
import bencode
import os
def toolong(torrent, max_, host=None):
decoded = bencode.bdecode(torrent)
if host is not None:
for url in decoded.get('announce-list', [decoded['announce']]):
if host in url: break
else: return False
name = decoded['info']['name'] + '/'
if 'files' not in decoded['info']:
return len(name[:-1]) > max_
for file in decoded['info']['files']:
if len(name + '/'.join(file['path'])) > max_:
return True
def main():
from optparse import OptionParser
usage = "usage: %prog [options] directory"
parser = OptionParser(usage=usage)
parser.add_option('-t', '--tracker', dest='host',
help='Only test torrents with this tracker hostname', default=None)
parser.add_option('-m', '--max', dest='max_', help='Maximum file length',
type='int', default=180, metavar='MAX')
options, args = parser.parse_args()
for (dirpath, dirnames, filenames) in os.walk(args[0]):
for filename in filenames:
if not filename.endswith('.torrent'): continue
torrent = open(os.path.join(dirpath, filename), 'rb').read()
if toolong(torrent, options.max_, options.host):
print filename
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment