1
0
Fork 0
mirror of synced 2024-05-19 11:42:40 +12:00

Add option to download user comments (#258)

* Add option to download user comments

* Update README
This commit is contained in:
Serene 2021-04-09 23:15:45 +10:00 committed by Ali Parlakci
parent 37ca413424
commit 34c8a9a5d0
5 changed files with 26 additions and 0 deletions

View file

@ -136,6 +136,8 @@ The following options apply only to the `download` command. This command downloa
The following options are for the `archive` command specifically.
- `--all-comments`
- When combined with the `--user` option, this will download all the user's comments
- `-f, --format`
- This specifies the format of the data file saved to disk
- The following formats are available:

View file

@ -70,6 +70,7 @@ def cli_download(context: click.Context, **_):
@cli.command('archive')
@_add_common_options
@click.option('--all-comments', is_flag=True, default=None)
@click.option('-f,', '--format', type=click.Choice(('xml', 'json', 'yaml')), default=None)
@click.pass_context
def cli_archive(context: click.Context, **_):

View file

@ -4,6 +4,7 @@
import json
import logging
import re
from typing import Iterator
import dict2xml
import praw.models
@ -41,6 +42,14 @@ class Archiver(RedditDownloader):
supplied_submissions.append(self.reddit_instance.submission(url=sub_id))
return [supplied_submissions]
def _get_user_data(self) -> list[Iterator]:
results = super(Archiver, self)._get_user_data()
if self.args.user and self.args.all_comments:
sort = self._determine_sort_function()
logger.debug(f'Retrieving comments of user {self.args.user}')
results.append(sort(self.reddit_instance.redditor(self.args.user).comments, limit=self.args.limit))
return results
@staticmethod
def _pull_lever_entry_factory(praw_item: (praw.models.Submission, praw.models.Comment)) -> BaseArchiveEntry:
if isinstance(praw_item, praw.models.Submission):

View file

@ -37,6 +37,7 @@ class Configuration(Namespace):
# Archiver-specific options
self.format = 'json'
self.all_comments = False
def process_click_arguments(self, context: click.Context):
for arg_key in context.params.keys():

View file

@ -202,6 +202,19 @@ def test_cli_archive_subreddit(test_args: list[str], tmp_path: Path):
assert re.search(r'Writing entry .*? to file in .*? format', result.output)
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.skipif(Path('test_config.cfg') is False, reason='A test config file is required for integration tests')
@pytest.mark.parametrize('test_args', (
['--user', 'me', '--authenticate', '--all-comments', '-L', '10'],
))
def test_cli_archive_all_user_comments(test_args: list[str], tmp_path: Path):
runner = CliRunner()
test_args = ['archive', str(tmp_path), '-v', '--config', 'test_config.cfg'] + test_args
result = runner.invoke(cli, test_args)
assert result.exit_code == 0
@pytest.mark.online
@pytest.mark.reddit
@pytest.mark.slow