Procházet zdrojové kódy

Merge pull request #51 from BassThatHertz/master

Organize the downloaded videos.
Cammy před 6 roky
rodič
revize
75ef54b6d9
4 změnil soubory, kde provedl 62 přidání a 3 odebrání
  1. 2 0
      MOREHELP.md
  2. 1 1
      pyinstalive/assembler.py
  3. 48 0
      pyinstalive/organize.py
  4. 11 2
      pyinstalive/startup.py

+ 2 - 0
MOREHELP.md

@@ -29,6 +29,8 @@
 
 - ```-nhb``` or ```--no-heartbeat```  **—** Passing this argument means no livestream heartbeat checks will be conducted, and the logged in user will not show up as a viewer during the livestream. May cause degraded performance.
 
+- ```-o``` or ```--organize```  **—** Passing this argument will create a folder for each user whose livestream(s) you have downloaded. The names of the folders will be their usernames. It will then move the video(s) of each user into their associated folder. 
+
 # Default configuration file
 
 ```ini

+ 1 - 1
pyinstalive/assembler.py

@@ -150,4 +150,4 @@ 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)))

+ 48 - 0
pyinstalive/organize.py

@@ -0,0 +1,48 @@
+import os
+import shutil
+try:
+    import logger
+except ImportError:
+    from . import logger
+
+def organize_videos():
+
+    # Make a variable equal to the names of the files in the current directory.
+    current_dir_files = os.listdir()
+
+    # The downloaded livestream(s) 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:
+        logger.error("No MP4 files in the 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)
+
+    num_videos_moved = len(filenames_to_usernames.keys())
+    logger.info("{} videos moved successfully.".format(num_videos_moved))

+ 11 - 2
pyinstalive/startup.py

@@ -15,6 +15,7 @@ try:
     import assembler
     import dlfuncs
     from constants import Constants
+    import organize
 except ImportError:
     from urllib.parse import urlparse
     from . import pil
@@ -25,13 +26,17 @@ except ImportError:
     from . import assembler
     from . import dlfuncs
     from .constants import Constants
-
+    from . import organize
 
 def validate_inputs(config, args, unknown_args):
     error_arr = []
     try:
         config.read(pil.config_path)
 
+        if args.organize:
+            organize.organize_videos()
+            return False
+
         if args.download:
             pil.dl_user = args.download
             if args.downloadfollowing or args.batchfile:
@@ -272,6 +277,9 @@ def run():
     parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="PyInstaLive will output JSON "
                                                                                      "responses and some misc "
                                                                                      "variables.")
+
+    parser.add_argument('-o', '--organize', action='store_true', help="Create a folder for each user whose livestream(s) you have downloaded. The names of the folders will be their usernames. Then move the video(s) of each user into their associated folder.")
+
     # Workaround to 'disable' argument abbreviations
     parser.add_argument('--usernamx', help=argparse.SUPPRESS, metavar='IGNORE')
     parser.add_argument('--passworx', help=argparse.SUPPRESS, metavar='IGNORE')
@@ -281,6 +289,7 @@ def run():
     parser.add_argument('--downloadfollowinx', help=argparse.SUPPRESS, metavar='IGNORE')
     parser.add_argument('--configpatx', help=argparse.SUPPRESS, metavar='IGNORE')
     parser.add_argument('--confix', help=argparse.SUPPRESS, metavar='IGNORE')
+    parser.add_argument('--organizx', help=argparse.SUPPRESS, metavar='IGNORE')
 
     parser.add_argument('-cx', help=argparse.SUPPRESS, metavar='IGNORE')
     parser.add_argument('-nx', help=argparse.SUPPRESS, metavar='IGNORE')
@@ -311,4 +320,4 @@ def run():
                     logger.error("PyInstaLive must be properly installed when using the -b argument.")
                     logger.separator()
                 else:
-                    dlfuncs.iterate_users(pil.dl_batchusers)
+                    dlfuncs.iterate_users(pil.dl_batchusers)