Guest User

Untitled

a guest
Apr 8th, 2021
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2. from os import listdir, rename, remove, mkdir, path
  3. from os.path import join, isdir, isfile
  4. import os
  5. import os.path
  6. import shutil
  7.  
  8. BASE_PATH = path.abspath(path.dirname(__file__))
  9. SOURCE_PATH = BASE_PATH + "/new"
  10. CURRENT_PATH = BASE_PATH + "/sorted"
  11.  
  12. # NAME_RE = r"(^\[.+?\])\s*?(.+$)"
  13.  
  14. NAME_RE = r"(^\[.+?\])\s(.+)\s(\(.+?\))\s(\{.+?})(.+$)"
  15. if "sorted" not in listdir(BASE_PATH):
  16.     mkdir(join(BASE_PATH, "sorted"))
  17.  
  18. print("copy or move")
  19. answer = input()
  20. if answer == 'copy':
  21.     print("copying...")
  22. elif answer == 'move':
  23.     print("moving...")
  24. else:
  25.     print("copy or move only, exiting cuz im lazy...")
  26.     exit()
  27.  
  28. current_artists = [f for f in listdir(
  29.     CURRENT_PATH) if isdir(join(CURRENT_PATH, f))]
  30. mangas = dict((f, join(SOURCE_PATH, f)) for f in listdir(SOURCE_PATH) if isfile(join(SOURCE_PATH, f)))
  31. artists_name = set([re.match(NAME_RE, name).group(1).strip("[] ") for name in list(mangas.keys())])
  32.  
  33. for artist_name in artists_name:
  34.     if artist_name not in current_artists:
  35.         mkdir(join(CURRENT_PATH, artist_name))
  36.  
  37.  
  38. for name, manga_path in mangas.items():
  39.     artist_name = re.match(NAME_RE, name).group(1).strip("[] ")
  40.     artist_ = re.match(NAME_RE, name).group(1).strip()
  41.     manga_name = re.match(NAME_RE, name).group(2).strip()
  42.     comic_ = re.match(NAME_RE, name).group(3).strip()
  43.     tags_ = re.match(NAME_RE, name).group(4).strip()
  44.     exe_type = re.match(NAME_RE, name).group(5).strip()
  45.  
  46.     artist_path = join(CURRENT_PATH, artist_name)
  47.     current_mangas = [f for f in listdir(
  48.         artist_path) if isdir(join(artist_path, f))]
  49.     if manga_name in current_mangas:
  50.         continue
  51.     filename = artist_ + " " + manga_name + " " + comic_ + exe_type
  52.     # filename = "[" + artist_name + "] " + manga_name
  53.     # print(artist_path)
  54.     # print(filename)
  55.     if answer == 'copy':
  56.         shutil.copy(manga_path, join(artist_path, filename))
  57.     elif answer == 'move':
  58.         shutil.move(manga_path, join(artist_path, filename))
  59.  
RAW Paste Data