Skip to content

Commit

Permalink
Re-implement organize command
Browse files Browse the repository at this point in the history
  • Loading branch information
dvingerh committed Jun 28, 2022
1 parent 4ae7bcc commit a8c6895
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pyinstalive/organize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import shutil
from datetime import datetime
import time
import re

from . import logger
from . import globals

def organize_files():

try:
files = os.listdir(globals.config.download_path)

not_moved = 0
has_moved = 0

username_regex = r'(?<=\d{8}_)(.*?)(?=_\d)'
date_regex = r'^\d{8}'
timestamp_regex = r'_(\d{10})_'
raw_file_dict = {}
new_file_dict = {}

has_lock = any(".lock" in file for file in files)
if has_lock:
logger.error("The organize command cannot be used while there are lock files present.")
return
for file in files:
if (file.endswith("_live.mp4") or file.endswith("_live.json")) or (os.path.isdir(file) and file.endswith("_live")):
try:
if not os.path.isdir(file):
username = re.search(username_regex, file)[0]
date_ts = datetime.strptime(re.search(date_regex, file)[0], '%Y%m%d').strftime('%Y-%m-%d')
time_ts = time.strftime('%H-%M-%S', time.localtime(int(re.search(timestamp_regex, file)[1])))
file_ext = os.path.splitext(file)[1]
new_file = "{:s} {:s} {:s}{:s}".format(date_ts, time_ts, username, file_ext)
raw_file_dict[file] = username
new_file_dict[file] = new_file
else:
raw_file_dict[file] = username
new_file_dict[file] = file
except TypeError as e:
logger.warn("Could not parse name '{:s}', skipping.".format(file))

for filename, username in raw_file_dict.items():
try:
os.makedirs(os.path.join(globals.config.download_path, username))
except:
pass
source_path = os.path.join(globals.config.download_path, filename)
destination_path = os.path.join(globals.config.download_path, username, new_file_dict.get(filename))
if not os.path.isfile(destination_path):
try:
shutil.move(source_path, destination_path)
logger.info("{:s} was processed successfully.".format(filename))
has_moved += 1
except OSError as oe:
logger.warn("Could not process {:s}: {:s}".format(filename, str(oe)))
not_moved += 1
else:
logger.binfo("Did not process {:s} because it already exists in the destination folder.".format(filename))
not_moved += 1
if has_moved:
logger.separator()
logger.info("{} {} processed.".format(has_moved if has_moved else "No", "item was" if has_moved == 1 else "items were"))
if not_moved:
logger.binfo("{} {} not processed.".format(not_moved, "item was" if not_moved == 1 else "items were"))
except Exception as e:
logger.error("Could not organize files: {:s}".format(str(e)))
4 changes: 4 additions & 0 deletions pyinstalive/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from . import logger
from . import helpers
from . import assembler
from . import organize
from .constants import Constants
from .session import Session
from .download import Download
Expand Down Expand Up @@ -124,6 +125,7 @@ def __init__(self, prog):
parser.add_argument('-df', '--download-following', dest='download_following', action="store_true", required=False, help="Check for available livestreams from people you're following.")
parser.add_argument('-i', '--info', dest='info', action='store_true', help="Shows information about PyInstaLive.")
parser.add_argument('-cl', '--clean', dest='clean', action='store_true', help="Clean the current download path of all temporary files.")
parser.add_argument('-o', '--organize', dest='organize', action='store_true', help="Organize downloaded files and folders into separate directories with more readable names.")
parser.add_argument('-cp', '--config-path', dest='config_path', metavar='', type=str, required=False, help="Override the default configuration file path.")
parser.add_argument('-dp', '--download-path', dest='download_path', metavar='', type=str, required=False, help="Override the default download path.")
parser.add_argument('-dc', '--download-comments', dest='download_comments', action="store_true", required=False, help="Download livestream comments. Overrides the configuration file setting.")
Expand Down Expand Up @@ -174,5 +176,7 @@ def __init__(self, prog):
helpers.clean_download_dir()
elif globals.args.info:
helpers.show_info()
elif globals.args.organize:
organize.organize_files()
logger.separator()

0 comments on commit a8c6895

Please sign in to comment.