Skip to content

Commit

Permalink
sanity refactor, bump min py version to 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
fastily committed Dec 10, 2023
1 parent 76f8664 commit 05da981
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ usage: 4cr [-h] [-b board_id] [-i] [-s] [--out output_directory] [urls ...]
positional arguments:
urls the urls to process
optional arguments:
options:
-h, --help show this help message and exit
-b board_id The short id of the board to target. Ignored if the program was not started in interactive mode. Default is hr
-i Causes the archive file to get ignored. Only applicable in interactive mode.
Expand Down
22 changes: 9 additions & 13 deletions four_chan_ripper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
class RippableThread:
"""Represents a thread on 4chan to rip"""

OUT_DIR = Path.home() / "Public"

def __init__(self, board: str, thread: str, is_photoset: bool = False) -> None:
"""Initializer, creates a new RippableThread object
Expand All @@ -41,13 +39,16 @@ def __init__(self, board: str, thread: str, is_photoset: bool = False) -> None:
self._subject = op.get("sub") or op.get("semantic_url", thread)
self._comment = op.get("com")

def download(self) -> bool:
def download(self, out_dir: Path) -> bool:
"""Actually download try downloading the thread represented by this RippableThread.
Args:
out_dir (Path): The output directory to save downloaded files to.
Returns:
bool: `True` if there were no errors
"""
(output_dir := RippableThread.OUT_DIR / ((f"{self.thread} - " if not self.is_photoset else "") + self._subject.replace("/", "_").replace(":", "").replace("&", "&"))).mkdir(parents=True, exist_ok=True)
(output_dir := out_dir / ((f"{self.thread} - " if not self.is_photoset else "") + self._subject.replace("/", "_").replace(":", "").replace("&", "&"))).mkdir(parents=True, exist_ok=True)
log.info("Using folder: '%s'", output_dir)

if self.is_photoset and self._comment and len((txt := BeautifulSoup(self._comment, "lxml").get_text("\n"))) > 50:
Expand All @@ -72,25 +73,20 @@ def _main() -> None:
cli_parser.add_argument('-b', type=str, metavar="board_id", default="hr", help="The short id of the board to target. Ignored if the program was not started in interactive mode. Default is hr")
cli_parser.add_argument('-i', action='store_true', help="Causes the archive file to get ignored. Only applicable in interactive mode.")
cli_parser.add_argument('-s', action='store_true', help="Treat the input urls as a photoset to rip")
cli_parser.add_argument('--out', type=Path, metavar="output_directory", help="The output directory. Default is ~/Public")
cli_parser.add_argument('-o', type=Path, default=Path("."), metavar="output_directory", help="The output directory. Defaults to the current working directory.")
cli_parser.add_argument('urls', type=str, nargs='*', help='the urls to process')

args = cli_parser.parse_args()

log.addHandler(RichHandler(rich_tracebacks=True))
log.setLevel(logging.INFO)

if args.out:
RippableThread.OUT_DIR = args.out

if args.urls:
for url in args.urls:
board, _, no = urlparse(url).path.split("/")[1:4]
RippableThread(board, no, args.s).download()

RippableThread(board, no, args.s).download(args.o)
return

archived = set(archive_file.read_text().splitlines()) if not args.i and (archive_file := RippableThread.OUT_DIR / ".archive.txt").is_file() else set()
archived = set(archive_file.read_text().splitlines()) if not args.i and (archive_file := args.o / ".archive.txt").is_file() else set()

targets = {}
display_pairs = {}
Expand All @@ -109,7 +105,7 @@ def _main() -> None:
for id in photoset_selected:
targets[id].is_photoset = True

if (successful_downloads := [f"{targets[id].board}{id}" for id in selected if targets[id].download()]) and not args.i:
if (successful_downloads := [f"{targets[id].board}{id}" for id in selected if targets[id].download(args.o)]) and not args.i:
with archive_file.open("a") as f:
f.write("\n".join(successful_downloads) + "\n")

Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="4chan-ripper",
version="0.2.0",
version="0.0.1",
author="Fastily",
author_email="fastily@users.noreply.github.com",
description="Rips the contents of threads on 4chan",
Expand All @@ -26,9 +26,9 @@
classifiers=[
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
],
python_requires='>=3.9',
python_requires='>=3.11',
)

0 comments on commit 05da981

Please sign in to comment.