Selaa lähdekoodia

Merge remote-tracking branch 'origin/master'

Cammy 5 vuotta sitten
vanhempi
commit
96a0ba4227
11 muutettua tiedostoa jossa 174 lisäystä ja 101 poistoa
  1. 3 3
      .github/issue_template.md
  2. 2 0
      .gitignore
  3. 3 0
      MOREHELP.md
  4. 7 6
      README.md
  5. 2 2
      pyinstalive/comments.py
  6. 2 1
      pyinstalive/constants.py
  7. 22 14
      pyinstalive/dlfuncs.py
  8. 115 73
      pyinstalive/organize.py
  9. 2 0
      pyinstalive/pil.py
  10. 15 1
      pyinstalive/startup.py
  11. 1 1
      setup.py

+ 3 - 3
.github/issue_template.md

@@ -2,7 +2,7 @@
 #### Check the boxes below by filling `[ ]` with an `x` so it looks like `[x]`.
 #### Use the Preview button to ensure the template is filled in correctly.
 ##
-- [ ] I am using the latest version of PyInstaLive: 3.1.2.
+- [ ] I am using the latest version of PyInstaLive: 3.1.4.
 - [ ] I have installed either Python 2.7.x or 3.5+: `YOUR VERSION HERE`
 - [ ] I have read the [README](https://github.com/notcammy/pyinstalive/blob/master/README.md).
 - [ ] I have read the [FAQ](https://github.com/notcammy/pyinstalive/blob/master/FAQ.md).
@@ -27,11 +27,11 @@ Run ```pyinstalive --info``` and paste its output below. Don't forget to omit yo
 ```bash
 $ pyinstalive --info
 ---------------------------------------------------------------------------
-[I] PYINSTALIVE (SCRIPT V3.1.2 - PYTHON V3.6.3) - 01-02-2019 07:00:17 PM
+[I] PYINSTALIVE (SCRIPT V3.1.4 - PYTHON V3.6.3) - 01-02-2019 07:00:17 PM
 ---------------------------------------------------------------------------
 [I] To see all the available arguments, use the -h argument.
 
-[I] PyInstaLive version:        3.1.2
+[I] PyInstaLive version:        3.1.4
 [I] Python version:             3.6.3
 [I] FFmpeg framework:           Available
 [I] Cookie files:               2 (johndoe.json matches config user)

+ 2 - 0
.gitignore

@@ -63,3 +63,5 @@ pildev/
 
 *.txt

 venv/
+
+\.vscode/\.ropeproject/

+ 3 - 0
MOREHELP.md

@@ -31,6 +31,8 @@
 
 - ```-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. 
 
+- ```-sm``` or ```--skip-merge```  **—** PyInstaLive will not merge any download livestream files when this argument is used.
+
 # Default configuration file
 
 ```ini
@@ -51,6 +53,7 @@ clear_temp_files = False
 do_heartbeat = True
 proxy = 
 verbose = False
+skip_merge = False
 ```
 
 ```username```  **—**  Instagram username to login with.

+ 7 - 6
README.md

@@ -1,5 +1,5 @@
 # PyInstaLive
-![Version 3.1.2](https://img.shields.io/badge/Version-3.1.2-orange.svg)
+![Version 3.1.4](https://img.shields.io/badge/Version-3.1.4-orange.svg)
 ![Python 2.7, 3.5](https://img.shields.io/badge/Python-2.7%2C%203.5%2B-3776ab.svg)
 
 This Python script enables you to download any ongoing Instagram livestreams as well as any available replays. It is based on [another script](https://github.com/taengstagram/instagram-livestream-downloader) that has now been discontinued.
@@ -50,21 +50,21 @@ Make sure all tools are accessible via command line (added to your PATH if on Wi
 
 ## Installation
 
-*Tip — To install PyInstaLive with the latest commit changes, remove the version tag from the install command (e.g. **@3.1.2**).*
+*Tip — To install PyInstaLive with the latest commit changes, remove the version tag from the install command (e.g. **@3.1.4**).*
 
 #### Installing
 
 Run the following command in your command line (might need to be run as administrator on Windows):
 ```bash
-pip install git+https://github.com/notcammy/PyInstaLive.git@3.1.2 --process-dependency-links
+pip install git+https://github.com/notcammy/PyInstaLive.git@3.1.4 --process-dependency-links
 ```
 
 #### Updating
 
-To update PyInstaLive to the latest version (currently **3.1.2**) run the following command:
+To update PyInstaLive to the latest version (currently **3.1.4**) run the following command:
 
 ```bash
-pip install git+https://github.com/notcammy/PyInstaLive.git@3.1.2 --process-dependency-links --upgrade
+pip install git+https://github.com/notcammy/PyInstaLive.git@3.1.4 --process-dependency-links --upgrade
 ```
 
 #### Specific versions
@@ -104,6 +104,7 @@ clear_temp_files = False
 do_heartbeat = True
 proxy = 
 verbose = False
+skip_merge = False
 ```
 
 #### Example
@@ -122,7 +123,7 @@ Below is an example of PyInstaLive's output after downloading a livestream:
 > pyinstalive -d "janedoe"
 
 ---------------------------------------------------------------------------
-[I] PYINSTALIVE (SCRIPT V3.1.2 - PYTHON V3.6.3) - 01-02-2019 06:56:29 PM
+[I] PYINSTALIVE (SCRIPT V3.1.4 - PYTHON V3.6.3) - 01-02-2019 06:56:29 PM
 ---------------------------------------------------------------------------
 [I] Successfully logged into account: johndoe
 ---------------------------------------------------------------------------

+ 2 - 2
pyinstalive/comments.py

@@ -123,7 +123,7 @@ class CommentsDownloader(object):
         python_version = sys.version.split(' ')[0]
         comments_timeline = {}
         wide_build = sys.maxunicode > 65536
-        for i, c in enumerate(comments):
+        for c in comments:
             if 'offset' in c:
                 for k in c.get('comment').keys():
                     c[k] = c.get('comment', {}).get(k)
@@ -138,7 +138,7 @@ class CommentsDownloader(object):
             total_comments = 0
             timestamps = sorted(comments_timeline.keys())
             subs = []
-            for i, tc in enumerate(timestamps):
+            for tc in timestamps:
                 t = comments_timeline[tc]
                 clip_start = int(tc) - int(download_start_time) + int(comments_delay)
                 if clip_start < 0:

+ 2 - 1
pyinstalive/constants.py

@@ -2,7 +2,7 @@ import sys
 
 
 class Constants:
-    SCRIPT_VER = "3.1.2"
+    SCRIPT_VER = "3.1.4"
     PYTHON_VER = sys.version.split(' ')[0]
     CONFIG_TEMPLATE = """
 [pyinstalive]
@@ -22,4 +22,5 @@ clear_temp_files = False
 do_heartbeat = False
 proxy =
 verbose = False
+skip_merge = False
     """

+ 22 - 14
pyinstalive/dlfuncs.py

@@ -51,7 +51,7 @@ def get_stream_duration(duration_type):
         if stream_started_secs:
             stream_duration_str += ' and %d seconds' % stream_started_secs
         return stream_duration_str
-    except Exception as e:
+    except Exception:
         return "Not available"
 
 
@@ -110,6 +110,9 @@ def get_broadcasts_info():
             return True
         else:
             return False
+    except ClientThrottledError:
+        logger.error('Could not check because you are making too many requests at this time.')
+        return False
     except Exception as e:
         logger.error('Could not finish checking: {:s}'.format(str(e)))
         if "timed out" in str(e):
@@ -119,10 +122,7 @@ def get_broadcasts_info():
                          'again.')
         return False
     except KeyboardInterrupt:
-        logger.binfo('Aborted checking for livestreams and replays, exiting.'.format(pil.dl_user))
-        return False
-    except ClientThrottledError as cte:
-        logger.error('Could not check because you are making too many requests at this time.')
+        logger.binfo('Aborted checking for livestreams and replays, exiting.')
         return False
 
 
@@ -148,10 +148,13 @@ def merge_segments():
         if pil.comment_thread_worker and pil.comment_thread_worker.is_alive():
             logger.info("Waiting for comment downloader to finish.")
             pil.comment_thread_worker.join()
-        logger.info('Merging downloaded files into video.')
         try:
-            pil.broadcast_downloader.stitch(live_mp4_file, cleartempfiles=pil.clear_temp_files)
-            logger.info('Successfully merged downloaded files into video.')
+            if not pil.skip_merge:
+                logger.info('Merging downloaded files into video.')
+                pil.broadcast_downloader.stitch(live_mp4_file, cleartempfiles=pil.clear_temp_files)
+                logger.info('Successfully merged downloaded files into video.')
+            else:
+                logger.binfo("Merging of downloaded files has been disabled.")
             if pil.clear_temp_files:
                 helpers.remove_temp_folder()
             helpers.remove_lock()
@@ -296,13 +299,18 @@ def download_replays():
         for replay_index, replay_obj in enumerate(pil.replays_obj):
             exists = False
             pil.livestream_obj = replay_obj
-            if Constants.PYTHON_VER[0][0] == '2':
-                directories = (os.walk(pil.dl_path).next()[1])
-            else:
-                directories = (os.walk(pil.dl_path).__next__()[1])
+            dl_path_files = os.listdir(pil.dl_path)
+
+            if pil.verbose:
+                logger.separator()
+                logger.plain("Listing contents of the folder '{}':".format(pil.dl_path))
+                for dl_path_file in dl_path_files:
+                    logger.plain(dl_path_file)
+                logger.separator()
+                logger.separator()
 
-            for directory in directories:
-                if (str(replay_obj.get('id')) in directory) and ("_live_" not in directory):
+            for dl_path_file in dl_path_files:
+                if (str(replay_obj.get('id')) in dl_path_file) and ("_live_" not in dl_path_file) and (dl_path_file.endswith(".mp4")):
                     logger.binfo("Already downloaded a replay with ID '{:s}'.".format(str(replay_obj.get('id'))))
                     exists = True
             if not exists:

+ 115 - 73
pyinstalive/organize.py

@@ -1,74 +1,116 @@
-import os
-import shutil
-try:
-    import pil
-    import logger
-except ImportError:
-    from . import pil
-    from . import logger
-
-def organize_videos():
-
-    try:
-        # Make a variable equal to the names of the files in the current directory.
-        current_dir_files = os.listdir(pil.dl_path)
-
-        # Count the amount of files moved and not moved because they already exist etc.
-        not_moved = 0
-        has_moved = 0
-
-        # The downloaded livestream(s) are in MP4 format.
-        video_format = ['mp4']
-
-        # Find the MP4 files and save them in a variable called 'filenames'.
-        filenames = [os.path.join(pil.dl_path, filename) for filename in current_dir_files
-            if filename.split('.')[-1] in video_format]
-
-        if len(filenames) == 0:
-            logger.binfo("No files were found to organize.")
-            logger.separator()
-            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:
-            username_path = os.path.join(pil.dl_path, username)
-            if not os.path.isdir(username_path):
-                os.mkdir(username_path)
-
-        # Move the videos into the folders
-        for filename, username in filenames_to_usernames.items():
-            filename_base = os.path.basename(filename)
-            destination_path = os.path.join(pil.dl_path, username, filename_base)
-            if not os.path.isfile(destination_path):
-                try:
-                    shutil.move(filename, destination_path)
-                    logger.info("Moved '{:s}' successfully.".format(filename_base))
-                    has_moved += 1
-                except OSError as oe:
-                    logger.warn("Could not move {:s}: {:s}".format(filename_base, str(oe)))
-                    not_moved += 1
-            else:
-                logger.binfo("Did not move '{:s}' because it already exists.".format(filename_base))
-                not_moved += 1
-
-        logger.separator()
-        logger.info("{} {} moved.".format(has_moved, "file was" if has_moved == 1 else "files were"))
-        if not_moved:
-            logger.binfo("{} {} not moved.".format(not_moved, "file was" if not_moved == 1 else "files were"))
-        logger.separator()
-    except Exception as e:
+import os
+import shutil
+from datetime import datetime
+import time
+try:
+    import pil
+    import logger
+except ImportError:
+    from . import pil
+    from . import logger
+
+def organize_videos():
+
+    try:
+        # Make a variable equal to the names of the files in the current directory.
+        download_path_files = os.listdir(pil.dl_path)
+
+        # Count the amount of files moved and not moved because they already exist etc.
+        not_moved = 0
+        has_moved = 0
+
+        # 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 download_path_files
+            if filename.split('.')[-1] in video_format]
+        
+        if len(filenames) == 0:
+            logger.binfo("No files were found to organize.")
+            logger.separator()
+            return
+
+        for filename in filenames:
+            # Split the filenames into parts.
+            filename_parts = filename.split('_')
+
+            # Get the date from the filename.
+            date = datetime.strptime(filename_parts[0], '%Y%m%d').strftime('%d-%m-%Y')
+
+            # Get the username from the filename. 
+            username = '_'.join(filename_parts[1:-3])
+
+            # Get the time from the unix timestamp.
+            time_from_unix_timestamp = time.strftime('%I.%M%p', time.localtime(int(filename_parts[-2])))
+
+            # # Remove the leading zero from single-digit hours.
+            if float(time_from_unix_timestamp[0:2]) < 10:
+                time_from_unix_timestamp = time_from_unix_timestamp[1:]
+
+            # Get the last part of the filename ("live.mp4" or "replay.mp4").
+            live_or_replay = filename_parts[-1]
+            
+            # The path of each original filename is as follows:
+            old_filename_path = os.path.join(pil.dl_path, filename)
+
+            # We want to change the format of each filename to:
+            new_filename_format = date + " " + username + " [" + time_from_unix_timestamp + "] " + live_or_replay
+
+            # The path of each new filename is as follows:
+            new_filename_path = os.path.join(pil.dl_path, new_filename_format)
+
+            # Change the filenames.
+            os.rename(old_filename_path, new_filename_path)
+
+        # Now that the files have been renamed, we need to rescan the files in the directory.
+        download_path_files = os.listdir(pil.dl_path)
+
+        new_filenames = [filename for filename in download_path_files if filename.split('.')[-1] in video_format]
+        
+        # We want a dictionary where the filenames are the keys
+        # and the usernames are the values.
+        filenames_to_usernames = {}
+
+        # Populate the dictionary with a loop.
+        for filename in new_filenames:
+            # Split the filenames into parts so we get just the usernames:
+            filename_parts = filename.split()
+            # This is how to get the usernames from the split filenames:
+            username = filename_parts[1]
+            # Filename = key and username = value:
+            filenames_to_usernames[filename] = username
+    
+        # We only want one folder for each username, so convert the list into a set to remove duplicates.
+        usernames = set(filenames_to_usernames.values())
+
+        # Make a folder for each username.
+        for username in usernames:
+            username_path = os.path.join(pil.dl_path, username)
+            if not os.path.isdir(username_path):
+                os.mkdir(username_path)
+
+        # Move the videos into the folders
+        for filename, username in filenames_to_usernames.items():
+            filename_base = os.path.basename(filename)
+            source_path = os.path.join(pil.dl_path, filename)
+            destination_path = os.path.join(pil.dl_path, username, filename_base)
+            if not os.path.isfile(destination_path):
+                try:
+                    shutil.move(source_path, destination_path)
+                    logger.info("Moved '{:s}' successfully.".format(filename_base))
+                    has_moved += 1
+                except OSError as oe:
+                    logger.warn("Could not move {:s}: {:s}".format(filename_base, str(oe)))
+                    not_moved += 1
+            else:
+                logger.binfo("Did not move '{:s}' because it already exists.".format(filename_base))
+                not_moved += 1
+
+        logger.separator()
+        logger.info("{} {} moved.".format(has_moved, "file was" if has_moved == 1 else "files were"))
+        if not_moved:
+            logger.binfo("{} {} not moved.".format(not_moved, "file was" if not_moved == 1 else "files were"))
+        logger.separator()
+    except Exception as e:
         logger.error("Could not organize files: {:s}".format(str(e)))

+ 2 - 0
pyinstalive/pil.py

@@ -45,6 +45,7 @@ def initialize():
     global do_heartbeat
     global proxy
     global verbose
+    global skip_merge
     global config_login_overridden
     ig_api = None
     ig_user = ""
@@ -79,4 +80,5 @@ def initialize():
     do_heartbeat = False
     proxy = None
     verbose = False
+    skip_merge = False
     config_login_overridden = False

+ 15 - 1
pyinstalive/startup.py

@@ -84,6 +84,7 @@ def validate_inputs(config, args, unknown_args):
         pil.run_at_finish = config.get('pyinstalive', 'run_at_finish')
         pil.ffmpeg_path = config.get('pyinstalive', 'ffmpeg_path')
         pil.verbose = config.get('pyinstalive', 'verbose')
+        pil.skip_merge = config.get('pyinstalive', 'skip_merge')
         pil.args = args
         pil.config = config
         pil.proxy = config.get('pyinstalive', 'proxy')
@@ -114,6 +115,14 @@ def validate_inputs(config, args, unknown_args):
         else:
             pil.verbose = False
 
+        if helpers.bool_str_parse(config.get('pyinstalive', 'skip_merge')) == "Invalid":
+            pil.skip_merge = False
+            error_arr.append(['skip_merge', 'False'])
+        elif helpers.bool_str_parse(config.get('pyinstalive', 'skip_merge')):
+            pil.skip_merge = True
+        else:
+            pil.skip_merge = False
+
         if helpers.bool_str_parse(config.get('pyinstalive', 'use_locks')) == "Invalid":
             pil.use_locks = False
             error_arr.append(['use_locks', 'False'])
@@ -170,6 +179,11 @@ def validate_inputs(config, args, unknown_args):
         if args.noreplays:
             pil.dl_replays = False
 
+        if args.verbose:
+            pil.verbose = True
+        if args.skip_merge:
+            pil.skip_merge = True
+
         if not pil.dl_lives and not pil.dl_replays:
             logger.error("You have disabled both livestream and replay downloading.")
             logger.error("Please enable at least one of them and try again.")
@@ -276,7 +290,7 @@ 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('-sm', '--skip-merge', dest='skip_merge', action='store_true', help="PyInstaLive will not merge the downloaded livestream files.")
     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

+ 1 - 1
setup.py

@@ -2,7 +2,7 @@ from setuptools import setup
 
 __author__ = 'notcammy'
 __email__ = 'neus2benen@gmail.com'
-__version__ = '3.1.2'
+__version__ = '3.1.4'
 
 _api_version = '1.5.7'
 _api_extensions_version = '0.3.8'