Skip to content

Commit 6585331

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

File tree

4 files changed

+56
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)