Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import torrent_parser as tp
  2. import os
  3. import argparse
  4.  
  5.  
  6. def run(torrent_file, saved_location):
  7. data = tp.parse_torrent_file(torrent_file)
  8. dir_name = data['info']['name']
  9. os.chdir(os.path.join(saved_location, dir_name))
  10. files_torrent = set([os.path.join(*d['path'])
  11. for d in data['info']['files']])
  12. files_actual = set()
  13. for root, directories, filenames in os.walk('.'):
  14. for filename in filenames:
  15. files_actual.add(os.path.join(root, filename)[2:])
  16.  
  17. files_missing = set()
  18. for fname in files_torrent:
  19. if fname not in files_actual:
  20. files_missing.add(fname)
  21. files_useless = set()
  22. for fname in files_actual:
  23. if fname not in files_torrent:
  24. files_useless.add(fname)
  25.  
  26. return files_useless, files_missing
  27.  
  28.  
  29. def main():
  30. parser = argparse.ArgumentParser(
  31. description='remove useless files in the torrent download directory')
  32. parser.add_argument('-t', type=str, dest='torrent_file',
  33. help='path to .torrent file', required=True)
  34. parser.add_argument('-d', type=str, dest='saved_location',
  35. help='path to saved_location', required=True)
  36. parser.add_argument('-r', dest='remove_useless_files',
  37. help='whether delete files', action='store_true')
  38. args = parser.parse_args()
  39. torrent_file = args.torrent_file
  40. saved_location = args.saved_location
  41. remove_useless_files = args.remove_useless_files
  42. files_useless, files_missing = run(torrent_file, saved_location)
  43. files_uselessQ, files_missingQ = len(
  44. files_useless) > 0, len(files_missing) > 0
  45. if not files_missingQ and not files_uselessQ:
  46. print('you have a perfect copy.')
  47. exit(0)
  48. if files_uselessQ:
  49. print('useless:', files_useless)
  50. if files_missingQ:
  51. print('missing:', files_missing)
  52. if remove_useless_files:
  53. if files_missingQ:
  54. print(
  55. 'torrent file not complete or directory specification error, aborting for safety')
  56. exit(-1)
  57. for i in files_useless:
  58. os.remove(i)
  59. print('removed.')
  60.  
  61.  
  62. if __name__ == '__main__':
  63. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement