Ver Fonte

Added organize_videos function for the newly added --organize command.

BassThatHertz há 6 anos atrás
pai
commit
5b2490f7b7
1 ficheiros alterados com 40 adições e 1 exclusões
  1. 40 1
      pyinstalive/assembler.py

+ 40 - 1
pyinstalive/assembler.py

@@ -150,4 +150,43 @@ def assemble(user_called=True, retry_with_zero_m4v=False):
             if user_called:
                 logger.separator()
     except Exception as e:
-        logger.error("An error occurred: {:s}".format(str(e)))
+        logger.error("An error occurred: {:s}".format(str(e)))
+
+def organize_videos():
+    # Find the files in the current directory.
+    current_dir_files = os.listdir()
+
+    # The Instagram videos are in MP4 format.
+    video_format = ['mp4']
+
+    # Find the MP4 files and save them in a variable called 'filenames'.
+    filenames = [filename for filename in current_dir_files if filename.split('.')[-1] in video_format]
+
+    if len(filenames) == 0:
+        print('Detected no valid files in current directory.')
+        return
+
+    # We want a dictionary where the filenames are the keys and the usernames are the values.
+    filenames_to_usernames = {}
+
+    # Populate the dictionary by going through each filename and removing the undesired characters, leaving just the usernames.
+    for filename in filenames:
+        filename_parts = filename.split('_')[1:-3]
+        usernames = '_'.join(filename_parts)
+        filenames_to_usernames[filename] = usernames
+        
+    # The usernames are the values of the filenames_to_usernames dictionary.
+    usernames = set(filenames_to_usernames.values())
+
+    # Make a folder for each username.
+    for username in usernames:
+        if not os.path.isdir(username):
+            os.mkdir(username)
+
+    # Move the videos into the folders
+    for filename, username in filenames_to_usernames.items():
+        shutil.move(filename, username)
+        # Can also do os.rename(filename, '{}/{}'.format(username, filename))
+
+    num_videos_moved = len(filenames_to_usernames.keys())
+    print("{} videos moved successfully.".format(num_videos_moved))