Guest User

Add season titles

a guest
Aug 23rd, 2023
2,705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | Software | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog
  4. import shutil
  5. from datetime import datetime
  6.  
  7. def list_files_and_organize_by_year(folder_path):
  8. print("Listing files by date modified and organizing by year...")
  9.  
  10. # Get a list of files in the folder
  11. files = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, filename))]
  12.  
  13. # Create a dictionary to track the season numbers for each year
  14. year_counts = {}
  15.  
  16. # Traverse the folder tree and list files by date modified
  17. for root, _, files in os.walk(folder_path):
  18. for file_name in files:
  19. file_path = os.path.join(root, file_name)
  20. year = get_modification_year(file_path)
  21. if year:
  22. # Create a folder for the year if it doesn't exist
  23. year_folder = os.path.join(folder_path, str(year))
  24. os.makedirs(year_folder, exist_ok=True)
  25.  
  26. # Move the file to the corresponding year folder
  27. shutil.move(file_path, os.path.join(year_folder, file_name))
  28. print(f"Moved: {file_name} to {year}")
  29.  
  30. print("Files listed by date modified and organized by year.")
  31. organize_folders_as_seasons(folder_path)
  32.  
  33. def organize_folders_as_seasons(folder_path):
  34. print("Organizing folders as seasons...")
  35.  
  36. # Get a list of folders in the parent folder
  37. subfolders = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))]
  38.  
  39. # Sort the folders by their names (years)
  40. subfolders.sort()
  41.  
  42. # Rename the folders as "Season 1," "Season 2," and so on
  43. for idx, subfolder in enumerate(subfolders, start=1):
  44. season_name = f"Season {idx}"
  45. season_path = os.path.join(folder_path, subfolder)
  46. os.rename(season_path, os.path.join(folder_path, season_name))
  47. print(f"Renamed folder: {subfolder} to {season_name}")
  48.  
  49. print("Folders organized as seasons.")
  50. rename_files_in_seasons(folder_path)
  51.  
  52. def rename_files_in_seasons(folder_path):
  53. print("Renaming files in each season folder...")
  54.  
  55. # Get a list of folders (seasons)
  56. seasons = [f for f in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, f))]
  57.  
  58. # Sort the seasons by their names (Season 1, Season 2, ...)
  59. seasons.sort()
  60.  
  61. # Loop through each season folder
  62. for season in seasons:
  63. season_folder = os.path.join(folder_path, season)
  64. files = [os.path.join(season_folder, filename) for filename in os.listdir(season_folder) if os.path.isfile(os.path.join(season_folder, filename))]
  65.  
  66. # Sort files by date modified in ascending order
  67. files.sort(key=lambda x: os.path.getmtime(x))
  68.  
  69. # Rename files in the format sXXeYY
  70. for idx, file_path in enumerate(files, start=1):
  71. season_episode = f"s{season.split(' ')[-1].zfill(2)}e{idx:02d}"
  72. _, file_extension = os.path.splitext(file_path)
  73. new_file_name = f"{season_episode}{file_extension}"
  74. os.rename(file_path, os.path.join(season_folder, new_file_name))
  75. print(f"Renamed: {file_path} to {new_file_name}")
  76.  
  77. print("Files renamed in each season folder.")
  78.  
  79. def get_modification_year(file_path):
  80. try:
  81. # Get the file's modification timestamp
  82. modification_timestamp = os.path.getmtime(file_path)
  83. # Convert the timestamp to a datetime object
  84. modification_date = datetime.fromtimestamp(modification_timestamp)
  85. # Extract the year from the datetime object
  86. return modification_date.year
  87. except Exception as e:
  88. print(f"Error getting modification year for {file_path}: {str(e)}")
  89. return None
  90.  
  91. def main():
  92. root = tk.Tk()
  93. root.withdraw() # Hide the main tkinter window
  94.  
  95. folder_path = filedialog.askdirectory(title="Select Folder")
  96. if folder_path:
  97. list_files_and_organize_by_year(folder_path)
  98.  
  99. # Add an input prompt to wait for user input before closing the console window
  100. input("Press Enter to exit...")
  101.  
  102. if __name__ == "__main__":
  103. main()
  104.  
Tags: youtube Plex
Advertisement
Add Comment
Please, Sign In to add comment