Guest User

Untitled

a guest
Oct 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. """
  2. A small script used to organise downloaded anime episodes.
  3.  
  4. Moves .mkv files from folderA to folderB such that folderB contains
  5. sub-folders where each sub-folder is the name of the anime series.
  6. """
  7.  
  8. import os
  9. import shutil
  10. import anitopy
  11.  
  12.  
  13. def main():
  14. """
  15. Main method to run the script.
  16.  
  17. return: Integer, the total number of episodes moved.
  18. """
  19. print("{:<55}" .format("EPISODES MOVED"))
  20. print("{:-<59}".format(""))
  21.  
  22. numMoved = 0
  23. downloaded = [f for f in os.listdir(TARGET) if f.endswith(".mkv")]
  24. for episode in downloaded:
  25.  
  26. # Parsing data using Anitopy library
  27. data = anitopy.parse(episode)
  28. title = data["anime_title"]
  29. num = data["episode_number"]
  30.  
  31. # Creates an anime subfolder if it doesn't exist already
  32. animeDir = os.path.join(DESTINATION, title)
  33. if not os.path.exists(animeDir):
  34. os.makedirs(animeDir)
  35.  
  36. # Moves anime episodes into the correct subfolder
  37. subFolders = os.listdir(DESTINATION)
  38. for sf in subFolders:
  39. if title.find(sf) != -1:
  40. move(episode, sf)
  41. numMoved += 1
  42. print("{:<55} {}" .format(title, num))
  43.  
  44. print("\nTOTAL MOVED: {}".format(numMoved))
  45.  
  46.  
  47. def move(episode, animeDir):
  48. """
  49. Moves the episode file into the correct directory.
  50.  
  51. :param episode: String, the name of the episode to move.
  52. :param animeDir: String, the name of the directory to move into.
  53. :return:
  54. """
  55. tPath = os.path.join(TARGET, episode)
  56. dPath = os.path.join(DESTINATION, animeDir, episode)
  57. shutil.move(tPath, dPath)
  58.  
  59.  
  60. if __name__ == "__main__":
  61. # CHANGE THESE PATHS AS NEEDED
  62. TARGET = "C:\\Users\\OzAli\\Desktop"
  63. DESTINATION = "D:\\Anime"
  64.  
  65. print()
  66. main()
  67. print("TARGET PATH: {}".format(TARGET))
  68. print("DESTINATION PATH: {}".format(DESTINATION))
Add Comment
Please, Sign In to add comment