Skip to content

Instantly share code, notes, and snippets.

@Rast1234
Created March 9, 2014 02:14
Show Gist options
  • Select an option

  • Save Rast1234/9441956 to your computer and use it in GitHub Desktop.

Select an option

Save Rast1234/9441956 to your computer and use it in GitHub Desktop.
Count how much .torrents have md5 hashes for files
import os
import fnmatch
import bencode
from bencode import BTL
from pprint import pprint
def findTorrents(*searchDirs):
for searchDir in searchDirs:
for root, dirnames, filenames in os.walk(searchDir):
for filename in fnmatch.filter(filenames, '*.torrent'):
yield os.path.join(root, filename)
def isHashInInfo(info):
if 'files' in info.keys():
for f in info['files']:
if 'md5sum' in f.keys():
yield True
else:
yield False
else:
if 'md5sum' in info.keys():
yield True
else:
yield False
def main():
torrents = 0
files = 0
hasMD5 = 0
noMD5 = 0
for filename in findTorrents("f:\\downloads", "d:\\downloads"):
torrent = open(filename, "rb")
try:
metainfo = bencode.bdecode(torrent.read())
except BTL.BTFailure:
print "Bad file: {}".format(filename)
continue
for x in isHashInInfo(metainfo['info']):
if x:
hasMD5 += 1
else:
noMD5 += 1
files += 1
torrents += 1
print "Searched in {} torrents / {} files".format(torrents, files)
print "has md5: {} / no md5: {}".format(hasMD5, noMD5)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment