Add option to download user comments (#258)
* Add option to download user comments * Update README
This commit is contained in:
@@ -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.
|
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`
|
- `-f, --format`
|
||||||
- This specifies the format of the data file saved to disk
|
- This specifies the format of the data file saved to disk
|
||||||
- The following formats are available:
|
- The following formats are available:
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ def cli_download(context: click.Context, **_):
|
|||||||
|
|
||||||
@cli.command('archive')
|
@cli.command('archive')
|
||||||
@_add_common_options
|
@_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.option('-f,', '--format', type=click.Choice(('xml', 'json', 'yaml')), default=None)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def cli_archive(context: click.Context, **_):
|
def cli_archive(context: click.Context, **_):
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
from typing import Iterator
|
||||||
|
|
||||||
import dict2xml
|
import dict2xml
|
||||||
import praw.models
|
import praw.models
|
||||||
@@ -41,6 +42,14 @@ class Archiver(RedditDownloader):
|
|||||||
supplied_submissions.append(self.reddit_instance.submission(url=sub_id))
|
supplied_submissions.append(self.reddit_instance.submission(url=sub_id))
|
||||||
return [supplied_submissions]
|
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
|
@staticmethod
|
||||||
def _pull_lever_entry_factory(praw_item: (praw.models.Submission, praw.models.Comment)) -> BaseArchiveEntry:
|
def _pull_lever_entry_factory(praw_item: (praw.models.Submission, praw.models.Comment)) -> BaseArchiveEntry:
|
||||||
if isinstance(praw_item, praw.models.Submission):
|
if isinstance(praw_item, praw.models.Submission):
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ class Configuration(Namespace):
|
|||||||
|
|
||||||
# Archiver-specific options
|
# Archiver-specific options
|
||||||
self.format = 'json'
|
self.format = 'json'
|
||||||
|
self.all_comments = False
|
||||||
|
|
||||||
def process_click_arguments(self, context: click.Context):
|
def process_click_arguments(self, context: click.Context):
|
||||||
for arg_key in context.params.keys():
|
for arg_key in context.params.keys():
|
||||||
|
|||||||
@@ -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)
|
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.online
|
||||||
@pytest.mark.reddit
|
@pytest.mark.reddit
|
||||||
@pytest.mark.slow
|
@pytest.mark.slow
|
||||||
|
|||||||
Reference in New Issue
Block a user