Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Read from scdl output file and remove unlisted files in current folder
- # also resolve unicode \u and \U escapes
- # cmd command: scdl commands 2> output.txt
- # scdl: https://github.com/flyingrub/scdl
- from glob import glob
- import os
- from pathlib import Path
- # resolve \ escapes. currently only unicode \u and \U escapes are implemented
- def resolve_escapes(text):
- new_text = ""
- escape_mode = ''
- escape_remaining = 0
- escape_contents = ''
- for c in text:
- # check if in escape mode
- if escape_mode != '':
- # write contents first
- escape_contents += c
- ### which escape is this?
- if escape_mode == '?':
- escape_mode = escape_contents[1]
- # unicode escape (4 nibbles)
- if escape_mode == 'u':
- escape_remaining = 4
- # unicode escape (8 nibbles)
- elif escape_mode == 'U':
- escape_remaining = 8
- # unknown escape
- else:
- escape_remaining = 1
- ### decrease counter
- else:
- escape_remaining -= 1
- # end of escape character
- if escape_remaining == 0:
- resolved_escape_contents = ''
- # unicode
- if escape_mode == 'u' or escape_mode == 'U':
- resolved_escape_contents = chr(int(escape_contents[2:], 16)) # convert from hex unicode value to string literal
- # unknown
- else:
- resolved_escape_contents = escape_contents
- new_text += resolved_escape_contents
- escape_contents = ''
- escape_mode = ''
- # check if escape character
- elif c == '\\':
- escape_mode = '?'
- escape_contents += c
- # not in escape mode or escape character, add the char to the string
- else:
- new_text += c
- return new_text
- # remove invalid windows file name characters
- # https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
- def remove_invalid_chars(text):
- new_text = ""
- for c in text:
- if c not in "<>:\"/\\|?*":
- new_text += c
- return new_text
- files = glob("*.mp3")
- log_file = open("output.txt", 'r')
- file_found = False
- download_count = 0
- found_count = 0
- for line in log_file:
- if "Downloading" in line:
- title = line.split("Downloading")[1].strip()
- title = resolve_escapes(title)
- title = remove_invalid_chars(title)
- download_count += 1
- file_found = False
- # find in files and remove
- for file in files:
- if title in file:
- found_count += 1
- file_found = True
- files.remove(file)
- break
- # print result
- if file_found:
- print("--- " + title)
- else:
- print("!!! " + title)
- print()
- print("Total ")
- print("Total download texts: " + str(download_count))
- print("Files found: " + str(found_count))
- print("Files remaining: " + str(len(files)))
- print()
- # delete files on command
- user = input('Delete remaining files? (Y/n): ')
- if user == '' or user.lower() == 'y':
- for file in files:
- os.remove(file)
- print('Files deleted.')
- exit()
- # Alternative code: move files into folder
- # doesn't work - headaches with os independent paths
- # find the smallest non duplicate folder name
- duplicate_count = 0
- moved_folder = "moved"
- new_folder_name = moved_folder
- while True:
- if duplicate_count != 0:
- new_path = f'/{new_folder_name} ({duplicate_count})'
- if not os.path.exists(new_path):
- break
- duplicate_count += 1
- # move files
- moved_path = Path(new_path).resolve()
- for file in files:
- file_path = Path(os.path.abspath(file)).resolve()
- os.rename(str(file_path), str(moved_path.joinpath(file_path)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement