organize.py 4.4 KB

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