Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit with sys #152

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ def get_test_db():
return curr


def test_successful_download(runner, capsys): # Add capsys as a parameter
def test_channel_download(runner, capsys): # Add capsys as a parameter
reset_testing_env()
runner.invoke(cli, [
results = runner.invoke(cli, [
'download',
'-j',
'5',
'https://www.youtube.com/@JCS'
])

assert results.exit_code == 0

curr = get_test_db()

# jcs channel id
Expand All @@ -54,21 +57,23 @@ def test_successful_download(runner, capsys): # Add capsys as a parameter
subtitle_count = res.fetchone()[0]

assert video_count == 17, f"Expected 17 videos, but got {video_count}"
assert subtitle_count == 21153, f"Expected 21153 subtitles, but got {subtitle_count}"
assert subtitle_count >= 21153, f"Expected 21153 subtitles, but got {subtitle_count}"


def test_playlist_download(runner, capsys):
reset_testing_env()

print("downloading playlist")
runner.invoke(cli, [
results = runner.invoke(cli, [
'download',
'--playlist',
'-j',
'5',
'https://www.youtube.com/playlist?list=PL5q_lef6zVkaTY_cT1k7qFNF2TidHCe-1'
])

assert results.exit_code == 0

curr = get_test_db()
# ycombinator
channel_id = 'UCxIJaCMEptJjxmmQgGFsnCg'
Expand All @@ -92,7 +97,7 @@ def test_playlist_download(runner, capsys):
subtitle_count = res.fetchone()[0]

assert video_count == 21, f"Expected 21 videos, but got {video_count}"
assert subtitle_count == 20970, f"Expected 20970 subtitles, but got {subtitle_count}"
assert subtitle_count >= 20970, f"Expected 20970 subtitles, but got {subtitle_count}"


if __name__ == "__main__":
Expand Down
9 changes: 7 additions & 2 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_export_search(runner, capsys):
'-e'
])

assert result.exit_code == 0

# list of files in the current directory
output_files = os.listdir()
Expand All @@ -62,14 +63,16 @@ def test_export_search(runner, capsys):
def test_export_vsearch(runner, capsys):
reset_testing_env()

runner.invoke(cli, [
result = runner.invoke(cli, [
'vsearch',
'-c',
'3',
'icmb gambit',
'-e'
])

assert result.exit_code == 0

output_files = os.listdir()

exported_file = None
Expand All @@ -89,12 +92,14 @@ def test_export_vsearch(runner, capsys):
def test_export_search_all(runner, capsys):
reset_testing_env()

runner.invoke(cli, [
result = runner.invoke(cli, [
'search',
'guilt',
'-e',
])

assert result.exit_code == 0

output_files = os.listdir()

exported_file = None
Expand Down
34 changes: 9 additions & 25 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,33 @@
import shutil
from click.testing import CliRunner
from yt_fts.yt_fts import download, cli
from testing_utils import fetch_and_unzip_test_db, get_test_db

CONFIG_DIR = os.path.expanduser('~/.config/yt-fts')


@pytest.fixture(scope="session")
def runner():
return CliRunner()


def reset_testing_env():
if os.path.exists(CONFIG_DIR):
if os.environ.get('YT_FTS_TEST_RESET', 'true').lower() == 'true':
shutil.rmtree(CONFIG_DIR)
fetch_and_unzip_test_db()
else:
print('running tests with existing db')

def get_test_db():
conn = sqlite3.connect(f"{CONFIG_DIR}/subtitles.db")
curr = conn.cursor()
return curr

@pytest.fixture(scope="session", autouse=True)
@pytest.mark.order(1)
def setup_environment(runner):
reset_testing_env()
runner.invoke(cli, [
'download',
'-j',
'5',
'https://www.youtube.com/@JCS'
])
runner.invoke(cli, [
'download',
'--playlist',
'-j',
'5',
'https://www.youtube.com/playlist?list=PL5q_lef6zVkaTY_cT1k7qFNF2TidHCe-1'
])

@pytest.mark.order(2)
def test_global_search(runner, capsys):
result = runner.invoke(cli, [
'search',
'guilt'
])

assert result.exit_code == 0

print(result.output)
captured = capsys.readouterr()
output = captured.out
Expand All @@ -57,7 +40,6 @@ def test_global_search(runner, capsys):
assert "Found 16 matches in 9 videos from 2 channels" in output


@pytest.mark.order(3)
def test_channel_search(runner, capsys):
result = runner.invoke(cli, [
'search',
Expand All @@ -66,6 +48,8 @@ def test_channel_search(runner, capsys):
'criminal'
])

assert result.exit_code == 0

print(result.output)
captured = capsys.readouterr()
output = captured.out
Expand All @@ -76,4 +60,4 @@ def test_channel_search(runner, capsys):


if __name__ == "__main__":
pytest.main([__file__, "--order-scope=module"])
pytest.main([__file__])
9 changes: 2 additions & 7 deletions tests/test_vsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ def reset_testing_env():
print('running tests with existing db')



def get_test_db():
conn = sqlite3.connect(f"{CONFIG_DIR}/subtitles.db")
curr = conn.cursor()
return curr


def test_vsearch(runner, capsys):
reset_testing_env()

Expand All @@ -40,6 +33,8 @@ def test_vsearch(runner, capsys):
'icbm gambit',
])

assert result.exit_code == 0

print(result.output)
captured = capsys.readouterr()
output = captured.out
Expand Down
3 changes: 2 additions & 1 deletion yt_fts/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sqlite3
import sys

from sqlite_utils import Database
from rich.console import Console
Expand Down Expand Up @@ -336,5 +337,5 @@ def get_channel_id_from_input(channel_input): # yt_fts, export, search, vector_s
return name_res
else:
show_message("channel_not_found")
exit()
sys.exit(1)

10 changes: 5 additions & 5 deletions yt_fts/download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yt_dlp
import tempfile
import sys
import re
import os
import sqlite3
Expand Down Expand Up @@ -230,13 +231,13 @@ def validate_channel_url(channel_url): # yt_fts
console.print("")
console.print(f"[bold red]Error:[/bold red] Invalid channel domain: [blue]{domain}[/blue]")
console.print("")
exit()
sys.exit(1)

if len(path) < 2:
console.print("")
console.print(f"[bold red]Error:[/bold red] Invalid channel url: [blue]{channel_url}[/blue]")
console.print("")
exit()
sys.exit(1)

if path[1].startswith("@"):
return f"https://www.youtube.com/{path[1]}/videos"
Expand All @@ -252,7 +253,8 @@ def validate_channel_url(channel_url): # yt_fts
console.print(" - https://www.youtube.com/[yellow]@channelname[/yellow]")
console.print(" - https://www.youtube.com/channel/[yellow]channelId[/yellow]")
console.print("")
exit()
# TODO: Make this function return something and handle error there
sys.exit(1)


def download_channel(channel_id, channel_name, language, number_of_jobs, s): # yt_fts
Expand Down Expand Up @@ -297,10 +299,8 @@ def download_playlist(playlist_url, s, language=None, number_of_jobs=None): # yt
if not check_if_channel_exists(channel_id):
add_channel_info(channel_id, channel_name, channel_url)


video_ids = list(set(video["video_id"] for video in playlist_data))


with tempfile.TemporaryDirectory() as tmp_dir:
console.print(f"[green][bold]Downloading [red]{len(playlist_data)}[/red] vtt files[/bold][/green]\n")
download_vtts(number_of_jobs, video_ids, language, tmp_dir)
Expand Down
3 changes: 2 additions & 1 deletion yt_fts/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from pprint import pprint
from .db_utils import *
from .utils import *
Expand Down Expand Up @@ -31,7 +32,7 @@ def fts_search(text, scope, channel_id=None, video_id=None, limit=None):
console.print(f" - EX: \"[bold]{example_or}[/bold]\"")
else:
console.print(f" - EX: \"[bold]foo OR [bold]bar[/bold]\"")
exit()
sys.exit(1)

return res

Expand Down
9 changes: 6 additions & 3 deletions yt_fts/update.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import tempfile, os
import tempfile
import os
import sys

from .download import get_videos_list, download_vtts, vtt_to_db
from .db_utils import add_channel_info, get_num_vids, get_vid_ids_by_channel_id


def update_channel(channel_id, channel_name, language, number_of_jobs, s):
"""
Downloads all the videos from a channel to a tmp directory
Expand All @@ -17,7 +20,7 @@ def update_channel(channel_id, channel_name, language, number_of_jobs, s):

if num_public_vids == num_local_vids:
print("No new videos to download")
exit()
sys.exit(0)

local_vid_ids = get_vid_ids_by_channel_id(channel_id)
local_vid_ids = [i[0] for i in local_vid_ids]
Expand All @@ -34,7 +37,7 @@ def update_channel(channel_id, channel_name, language, number_of_jobs, s):
if len(vtt_to_parse) == 0:
print("No new videos saved")
print(f"{len(fresh_videos)} videos on \"{channel_name}\" do not have subtitles")
exit()
sys.exit(0)

vtt_to_db(tmp_dir)

Expand Down
Loading
Loading