Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import torrent_parser as tp
- import os
- import argparse
- def run(torrent_file, saved_location):
- data = tp.parse_torrent_file(torrent_file)
- dir_name = data['info']['name']
- os.chdir(os.path.join(saved_location, dir_name))
- files_torrent = set([os.path.join(*d['path'])
- for d in data['info']['files']])
- files_actual = set()
- for root, directories, filenames in os.walk('.'):
- for filename in filenames:
- files_actual.add(os.path.join(root, filename)[2:])
- files_missing = set()
- for fname in files_torrent:
- if fname not in files_actual:
- files_missing.add(fname)
- files_useless = set()
- for fname in files_actual:
- if fname not in files_torrent:
- files_useless.add(fname)
- return files_useless, files_missing
- def main():
- parser = argparse.ArgumentParser(
- description='remove useless files in the torrent download directory')
- parser.add_argument('-t', type=str, dest='torrent_file',
- help='path to .torrent file', required=True)
- parser.add_argument('-d', type=str, dest='saved_location',
- help='path to saved_location', required=True)
- parser.add_argument('-r', dest='remove_useless_files',
- help='whether delete files', action='store_true')
- args = parser.parse_args()
- torrent_file = args.torrent_file
- saved_location = args.saved_location
- remove_useless_files = args.remove_useless_files
- files_useless, files_missing = run(torrent_file, saved_location)
- files_uselessQ, files_missingQ = len(
- files_useless) > 0, len(files_missing) > 0
- if not files_missingQ and not files_uselessQ:
- print('you have a perfect copy.')
- exit(0)
- if files_uselessQ:
- print('useless:', files_useless)
- if files_missingQ:
- print('missing:', files_missing)
- if remove_useless_files:
- if files_missingQ:
- print(
- 'torrent file not complete or directory specification error, aborting for safety')
- exit(-1)
- for i in files_useless:
- os.remove(i)
- print('removed.')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement