Skip to content

Commit 9646384

Browse files
committed
progress bar for downloader init
1 parent 8b3b5a7 commit 9646384

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

bdfr/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
click.option("-L", "--limit", default=None, type=int),
4040
click.option("-l", "--link", multiple=True, default=None, type=str),
4141
click.option("-m", "--multireddit", multiple=True, default=None, type=str),
42+
click.option("-p", "--progress-bar", is_flag=True, default=None),
4243
click.option(
4344
"-S", "--sort", type=click.Choice(("hot", "top", "new", "controversial", "rising", "relevance")), default=None
4445
),

bdfr/configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self):
3434
self.max_wait_time = None
3535
self.multireddit: list[str] = []
3636
self.no_dupes: bool = False
37+
self.progress_bar: bool = False
3738
self.saved: bool = False
3839
self.search: Optional[str] = None
3940
self.search_existing: bool = False

bdfr/downloader.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from bdfr import exceptions as errors
2020
from bdfr.configuration import Configuration
2121
from bdfr.connector import RedditConnector
22+
from bdfr.progress_bar import Progress
2223
from bdfr.site_downloaders.download_factory import DownloadFactory
2324

2425
logger = logging.getLogger(__name__)
@@ -43,17 +44,22 @@ def __init__(self, args: Configuration, logging_handlers: Iterable[logging.Handl
4344
self.master_hash_list = self.scan_existing_files(self.download_directory)
4445

4546
def download(self):
47+
progress = Progress(self.args.progress_bar, len(self.reddit_lists))
4648
for generator in self.reddit_lists:
49+
progress.subreddit_new(generator)
4750
try:
4851
for submission in generator:
4952
try:
50-
self._download_submission(submission)
53+
success = self._download_submission(submission)
5154
except prawcore.PrawcoreException as e:
5255
logger.error(f"Submission {submission.id} failed to download due to a PRAW exception: {e}")
56+
success = False
57+
progress.post_done(submission, success)
5358
except prawcore.PrawcoreException as e:
5459
logger.error(f"The submission after {submission.id} failed to download due to a PRAW exception: {e}")
5560
logger.debug("Waiting 60 seconds to continue")
5661
sleep(60)
62+
progress.subreddit_done()
5763

5864
def _download_submission(self, submission: praw.models.Submission):
5965
if submission.id in self.excluded_submission_ids:
@@ -152,6 +158,7 @@ def _download_submission(self, submission: praw.models.Submission):
152158
self.master_hash_list[resource_hash] = destination
153159
logger.debug(f"Hash added to master list: {resource_hash}")
154160
logger.info(f"Downloaded submission {submission.id} from {submission.subreddit.display_name}")
161+
return True
155162

156163
@staticmethod
157164
def scan_existing_files(directory: Path) -> dict[str, Path]:

bdfr/progress_bar.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import logging
2+
from typing import Optional
3+
4+
from tqdm import tqdm
5+
6+
logger = logging.getLogger()
7+
8+
class Progress:
9+
def __init__(self, progress_bar: bool, n_subreddits: int):
10+
self.progress_bar = progress_bar
11+
self.bar_outer: Optional[tqdm] = None
12+
self.bar_inner: Optional[tqdm] = None
13+
14+
if self.progress_bar:
15+
logger.setLevel(logging.CRITICAL)
16+
self.bar_outer = tqdm(
17+
total=n_subreddits,
18+
initial=0,
19+
desc="Subreddits",
20+
unit="subreddit",
21+
colour="green")
22+
23+
def subreddit_new(self, generator):
24+
if self.progress_bar:
25+
26+
# generator is a ListingGenerator or a (usually empty) list
27+
try:
28+
desc = generator.url
29+
except:
30+
desc = "Posts"
31+
32+
try:
33+
total = generator.limit
34+
except:
35+
total = 1
36+
37+
self.bar_inner = tqdm(
38+
total=total,
39+
initial=0,
40+
desc=desc,
41+
unit="post",
42+
colour="green",
43+
leave=False)
44+
45+
def subreddit_done(self):
46+
if self.progress_bar:
47+
self.bar_outer.update(1)
48+
self.bar_inner.close()
49+
50+
def post_done(self, submission, success: bool):
51+
if self.progress_bar:
52+
self.bar_inner.update(1)
53+
title_short = submission.title[:60] + (submission.title[60:] and "...")
54+
log_str = f"{submission.score:5d}🔼 {title_short}"
55+
icon = "✅" if success else "❌"
56+
self.bar_outer.write(f"{icon} {log_str}")

0 commit comments

Comments
 (0)