Advertisement
yclee126

Read scdl output and remove unlisted files

May 30th, 2022
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | None | 0 0
  1. # Read from scdl output file and remove unlisted files in current folder
  2. # also resolve unicode \u and \U escapes
  3. # cmd command: scdl commands 2> output.txt
  4. # scdl: https://github.com/flyingrub/scdl
  5.  
  6. from glob import glob
  7. import os
  8. from pathlib import Path
  9.  
  10. # resolve \ escapes. currently only unicode \u and \U escapes are implemented
  11. def resolve_escapes(text):
  12.     new_text = ""
  13.     escape_mode = ''
  14.     escape_remaining = 0
  15.     escape_contents = ''
  16.  
  17.     for c in text:
  18.  
  19.         # check if in escape mode
  20.         if escape_mode != '':
  21.            
  22.             # write contents first
  23.             escape_contents += c
  24.  
  25.             ### which escape is this?
  26.             if escape_mode == '?':
  27.                
  28.                 escape_mode = escape_contents[1]
  29.  
  30.                 # unicode escape (4 nibbles)
  31.                 if escape_mode == 'u':
  32.                     escape_remaining = 4
  33.                
  34.                 # unicode escape (8 nibbles)
  35.                 elif escape_mode == 'U':
  36.                     escape_remaining = 8
  37.                
  38.                 # unknown escape
  39.                 else:
  40.                     escape_remaining = 1
  41.  
  42.             ### decrease counter
  43.             else:
  44.                 escape_remaining -= 1
  45.  
  46.                 # end of escape character
  47.                 if escape_remaining == 0:
  48.                    
  49.                     resolved_escape_contents = ''
  50.  
  51.                     # unicode
  52.                     if escape_mode == 'u' or escape_mode == 'U':
  53.                         resolved_escape_contents = chr(int(escape_contents[2:], 16)) # convert from hex unicode value to string literal
  54.                    
  55.                     # unknown
  56.                     else:
  57.                         resolved_escape_contents = escape_contents
  58.                    
  59.                     new_text += resolved_escape_contents
  60.                     escape_contents = ''
  61.                     escape_mode = ''
  62.  
  63.         # check if escape character
  64.         elif c == '\\':
  65.             escape_mode = '?'
  66.             escape_contents += c
  67.        
  68.         # not in escape mode or escape character, add the char to the string
  69.         else:
  70.             new_text += c
  71.    
  72.     return new_text
  73.  
  74. # remove invalid windows file name characters
  75. # https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
  76. def remove_invalid_chars(text):
  77.     new_text = ""
  78.     for c in text:
  79.         if c not in "<>:\"/\\|?*":
  80.             new_text += c
  81.  
  82.     return new_text
  83.  
  84. files = glob("*.mp3")
  85.  
  86. log_file = open("output.txt", 'r')
  87.  
  88. file_found = False
  89. download_count = 0
  90. found_count = 0
  91.  
  92. for line in log_file:
  93.     if "Downloading" in line:
  94.         title = line.split("Downloading")[1].strip()
  95.         title = resolve_escapes(title)
  96.         title = remove_invalid_chars(title)
  97.         download_count += 1
  98.         file_found = False
  99.        
  100.         # find in files and remove
  101.         for file in files:
  102.             if title in file:
  103.                 found_count += 1
  104.                 file_found = True
  105.                 files.remove(file)
  106.                 break
  107.        
  108.         # print result
  109.         if file_found:
  110.             print("--- " + title)
  111.         else:
  112.             print("!!! " + title)
  113.  
  114. print()
  115. print("Total ")
  116. print("Total download texts: " + str(download_count))
  117. print("Files found: " + str(found_count))
  118. print("Files remaining: " + str(len(files)))
  119. print()
  120.  
  121. # delete files on command
  122. user = input('Delete remaining files? (Y/n): ')
  123. if user == '' or user.lower() == 'y':
  124.     for file in files:
  125.         os.remove(file)
  126.  
  127. print('Files deleted.')
  128. exit()
  129.  
  130. # Alternative code: move files into folder
  131. # doesn't work - headaches with os independent paths
  132.  
  133. # find the smallest non duplicate folder name
  134. duplicate_count = 0
  135. moved_folder = "moved"
  136. new_folder_name = moved_folder
  137.  
  138. while True:
  139.     if duplicate_count != 0:
  140.         new_path = f'/{new_folder_name} ({duplicate_count})'
  141.     if not os.path.exists(new_path):
  142.         break
  143.  
  144.     duplicate_count += 1
  145.  
  146. # move files
  147. moved_path = Path(new_path).resolve()
  148. for file in files:
  149.     file_path = Path(os.path.abspath(file)).resolve()
  150.     os.rename(str(file_path), str(moved_path.joinpath(file_path)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement