organize.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. import shutil
  3. from datetime import datetime
  4. import time
  5. try:
  6. import pil
  7. import logger
  8. except ImportError:
  9. from . import pil
  10. from . import logger
  11. def organize_videos():
  12. try:
  13. # Make a variable equal to the names of the files in the current directory.
  14. download_path_files = os.listdir(pil.dl_path)
  15. print(download_path_files)
  16. # Count the amount of files moved and not moved because they already exist etc.
  17. not_moved = 0
  18. has_moved = 0
  19. # The downloaded livestream(s) are in MP4 format.
  20. video_format = ['mp4']
  21. # Find the MP4 files and save them in a variable called 'filenames'.
  22. filenames = [filename for filename in download_path_files
  23. if filename.split('.')[-1] in video_format]
  24. if len(filenames) == 0:
  25. print("No MP4 files in the current directory.")
  26. return
  27. for filename in filenames:
  28. # Split the filenames into parts.
  29. filename_parts = filename.split('_')
  30. # Get the date from the filename.
  31. date = datetime.strptime(filename_parts[0], '%Y%m%d').strftime('%d-%m-%Y')
  32. # Get the username from the filename.
  33. username = '_'.join(filename_parts[1:-3])
  34. # Get the time from the unix timestamp.
  35. time_from_unix_timestamp = time.strftime('%I.%M%p', time.localtime(int(filename_parts[-2])))
  36. # # Remove the leading zero from single-digit hours.
  37. if float(time_from_unix_timestamp[0:2]) < 10:
  38. time_from_unix_timestamp = time_from_unix_timestamp[1:]
  39. # Get the last part of the filename ("live.mp4" or "replay.mp4").
  40. live_or_replay = filename_parts[-1]
  41. # The path of each original filename is as follows:
  42. old_filename_path = os.path.join(pil.dl_path, filename)
  43. # We want to change the format of each filename to:
  44. new_filename_format = date + " " + username + " [" + time_from_unix_timestamp + "] " + live_or_replay
  45. # The path of each new filename is as follows:
  46. new_filename_path = os.path.join(pil.dl_path, new_filename_format)
  47. # Change the filenames.
  48. os.rename(old_filename_path, new_filename_path)
  49. # Now that the files have been renamed, we need to rescan the files in the directory.
  50. download_path_files = os.listdir(pil.dl_path)
  51. new_filenames = [filename for filename in download_path_files if filename.split('.')[-1] in video_format]
  52. # We want a dictionary where the filenames are the keys
  53. # and the usernames are the values.
  54. filenames_to_usernames = {}
  55. # Populate the dictionary with a loop.
  56. for filename in new_filenames:
  57. # Split the filenames into parts so we get just the usernames:
  58. filename_parts = filename.split()
  59. # This is how to get the usernames from the split filenames:
  60. username = filename_parts[1]
  61. # Filename = key and username = value:
  62. filenames_to_usernames[filename] = username
  63. # We only want one folder for each username, so convert the list into a set to remove duplicates.
  64. usernames = set(filenames_to_usernames.values())
  65. # Make a folder for each username.
  66. for username in usernames:
  67. username_path = os.path.join(pil.dl_path, username)
  68. if not os.path.isdir(username_path):
  69. os.mkdir(username_path)
  70. # Move the videos into the folders
  71. for filename, username in filenames_to_usernames.items():
  72. filename_base = os.path.basename(filename)
  73. source_path = os.path.join(pil.dl_path, filename)
  74. destination_path = os.path.join(pil.dl_path, username, filename_base)
  75. if not os.path.isfile(destination_path):
  76. try:
  77. shutil.move(source_path, destination_path)
  78. logger.info("Moved '{:s}' successfully.".format(filename_base))
  79. has_moved += 1
  80. except OSError as oe:
  81. logger.warn("Could not move {:s}: {:s}".format(filename_base, str(oe)))
  82. not_moved += 1
  83. else:
  84. logger.binfo("Did not move '{:s}' because it already exists.".format(filename_base))
  85. not_moved += 1
  86. logger.separator()
  87. logger.info("{} {} moved.".format(has_moved, "file was" if has_moved == 1 else "files were"))
  88. if not_moved:
  89. logger.binfo("{} {} not moved.".format(not_moved, "file was" if not_moved == 1 else "files were"))
  90. logger.separator()
  91. except Exception as e:
  92. logger.error("Could not organize files: {:s}".format(str(e)))