Skip to content

Commit

Permalink
auto detect file and playlist input, -up flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nichind committed Nov 16, 2024
1 parent 028c38b commit 4a1a173
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.vscode
Downloads/
test.py
test.

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
20 changes: 17 additions & 3 deletions pybalt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .cobalt import Cobalt, check_updates
from os import path
from time import time
from importlib.metadata import version


async def _():
Expand Down Expand Up @@ -70,18 +71,31 @@ async def _():
parser.add_argument(
"-v", "-version", help="Display current pybalt version", action="store_true"
)
parser.add_argument("-up", "-update", help="Check for updates", action="store_true")
args = parser.parse_args()
if args.v:
raise NotImplementedError("Not implemented yet")
try:
print(f"pybalt {version('pybalt')}")
except Exception:
print("Failed to get pybalt version. Running from dev?")
return
if args.up:
await check_updates()
return
if args.url_arg:
args.url = args.url_arg
urls = ([args.url] if args.url else []) + (
[line.strip() for line in open(args.list)] if args.list else []
)
if args.url and not path.isdir(args.url) and path.isfile(args.url):
urls = [
line.strip() for line in open(args.url_arg if args.url_arg else args.url)
]
if not urls and not args.playlist:
print(
"No URLs provided",
"Use -url 'https://...' or -list 'path/to/txt' or -playlist 'https://...'",
"Expected media url, path to file with list of URLs or youtube playlist link",
"Example: cobalt 'https://youtube.com/watch?...' -s",
sep="\n",
)
return
Expand Down Expand Up @@ -126,7 +140,7 @@ def main():
f.write("0")
with open(update_check_file) as f:
if int(f.read()) < int(time()) - 60 * 60:
print("Checking for updates...", end="", flush=True)
print("Checking for updates...", flush=True)
run(check_updates())
with open(update_check_file, "w") as f:
f.write(str(int(time())))
Expand Down
10 changes: 6 additions & 4 deletions pybalt/cobalt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Literal
from dotenv import load_dotenv
from re import findall
from importlib.metadata import version
from importlib.metadata import version, PackageNotFoundError


async def check_updates() -> bool:
Expand All @@ -31,6 +31,8 @@ async def check_updates() -> bool:
f"pybalt {last_version} is avaliable (current: {current_version}). Update with pip install pyeasypay -U"
)
return False
except PackageNotFoundError:
print("pybalt is not installed. Running from dev?")
except Exception as e:
print(f"Failed to check for updates: {e}")
return True
Expand Down Expand Up @@ -190,7 +192,7 @@ async def get_instance(self):
raise exceptions.BadInstance()
self.api_instance = json["cobalt"]["url"]
break
except Exception as exc:
except Exception:
good_instances.pop(0)
return self.api_instance

Expand Down Expand Up @@ -465,11 +467,11 @@ def shorten(s: str, additional_len: int = 0) -> str:
downloaded_since_last = 0
last_update = time()
info = f"[{round(total_size / 1024 / 1024, 2)}Mb \u2015 {speed_display}] {progress_chars[progress_index]}"
max_print_length, _ = get_terminal_size()
max_print_length -= 3
print_line = shorten(
result_path, additional_len=len(info)
)
max_print_length, _ = get_terminal_size()
max_print_length -= 3
print(
"\r" + print_line,
" "
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setup(
name="pybalt",
version="2024.11.13",
version="2024.11.14",
author="nichind",
author_email="[email protected]",
description="Download mediafiles from YouTube, Twitter (X), Instagram, Reddit & more. CLI wrapper and python module for @imputnet's cobalt processing instance api.",
Expand Down

0 comments on commit 4a1a173

Please sign in to comment.