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

Replace argparse with TypedArgumentParser in archive-chan #8

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Flask
requests
SuperJson
Toolz
typed-argument-parser
80 changes: 15 additions & 65 deletions src/archive_chan/params.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,20 @@
from argparse import ArgumentParser
from tap import Tap
from pathlib import Path

class Args(Tap):
thread: str # Link to the 4chan thread or the name of the board.
archived: bool = False # Download threads from the /board/archive/ as well.
archived_only: bool = False # Download threads from the /board/archive/ INSTEAD.
preserve_media: bool = False # Save images and video files locally.
path: Path = Path("./threads/") # Path to folder where the threads should be saved.
posts: int = None # Number of posts to download.
retries: int = 1 # Retry -r times if a download fails.
skip_renders: bool = False # Do not render thread HTMLs after downloading them.
text_only: bool = False # Download only HTMLs or JSONs.
use_db: bool = False # Stores threads into a database, this is experimental.
verbose: bool = False # Verbose logging to stdout.

def get_args():
"""Get user input from the command-line and parse it."""
parser = ArgumentParser(description="Archives 4chan threads")
parser.add_argument(
"thread", help="Link to the 4chan thread or the name of the board."
)
parser.add_argument(
"-a",
"--archived",
action="store_true",
help="Download threads from the /board/archive/ as well.",
)
parser.add_argument(
"-ao",
"--archived_only",
action="store_true",
help="Download threads from the /board/archive/ INSTEAD.",
)
parser.add_argument(
"-p",
"--preserve_media",
action="store_true",
help="Save images and video files locally.",
)
parser.add_argument(
"--path",
default="./threads/",
help="Path to folder where the threads should be saved.",
type=Path,
)
parser.add_argument(
"--posts",
default=None,
help="Number of posts to download",
type=int,
)
parser.add_argument(
"-r",
"--retries",
default=1,
help="Retry -r times if a download fails.",
type=int,
)
parser.add_argument(
"--skip_renders",
action="store_true",
help="Do not render thread HTMLs after downloading them.",
)
parser.add_argument(
"--text_only",
action="store_true",
help="Download only HTMLs or JSONs.",
)
parser.add_argument(
"--use_db",
action="store_true",
help="Stores threads into a database, this is experimental.",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Verbose logging to stdout.",
)
args = parser.parse_args()
return args
args = Args().parse_args()
return args
17 changes: 17 additions & 0 deletions tests/test_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from unittest.mock import patch
from src.archive_chan.params import Args

class TestParams(unittest.TestCase):

def test_args(self):
test_args = [
'--name', 'test_name'
]

with patch('sys.argv', test_args):
args = Args().parse_args()
self.assertEqual(args.name, 'test_name')

if __name__ == '__main__':
unittest.main()