[go: up one dir, main page]

Skip to content

Commit

Permalink
Prefix CLI-related methods with an underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherArchivist committed Jan 12, 2022
1 parent 93e6274 commit deb2659
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions snscrape/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def parse_args():
classes.extend(cls.__subclasses__())
for scraper, cls in sorted(scrapers.items()):
subparser = subparsers.add_parser(cls.name, help = '', formatter_class = argparse.ArgumentDefaultsHelpFormatter)
cls.cli_setup_parser(subparser)
cls._cli_setup_parser(subparser)
subparser.set_defaults(cls = cls)

args = parser.parse_args()
Expand Down Expand Up @@ -293,7 +293,7 @@ def main():
setup_logging()
args = parse_args()
configure_logging(args.verbosity, args.dumpLocals)
scraper = args.cls.cli_from_args(args)
scraper = args.cls._cli_from_args(args)

i = 0
with _dump_locals_on_exception():
Expand Down
6 changes: 3 additions & 3 deletions snscrape/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ def _post(self, *args, **kwargs):
return self._request('POST', *args, **kwargs)

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
pass

@classmethod
def cli_from_args(cls, args):
def _cli_from_args(cls, args):
return cls._construct(args)

@classmethod
def cli_construct(cls, argparseArgs, *args, **kwargs):
def _cli_construct(cls, argparseArgs, *args, **kwargs):
return cls(*args, **kwargs, retries = argparseArgs.retries)


Expand Down
12 changes: 6 additions & 6 deletions snscrape/modules/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ def get_items(self):
yield from self._soup_to_items(soup, self._baseUrl, 'user')

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('username', type = snscrape.base.nonempty_string('username'), help = 'A Facebook username or user ID')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.username)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.username)


class FacebookUserScraper(_FacebookUserAndCommunityScraper):
Expand Down Expand Up @@ -356,9 +356,9 @@ def get_items(self):
yield from self._soup_to_items(soup, baseUrl, 'group')

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('group', type = snscrape.base.nonempty_string('group'), help = 'A group name or ID')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.group)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.group)
18 changes: 9 additions & 9 deletions snscrape/modules/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ def __init__(self, username, **kwargs):
self._variablesFormat = '{{"id":"{pageID}","first":50,"after":"{endCursor}"}}'

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('username', type = snscrape.base.nonempty_string('username'), help = 'An Instagram username (no leading @)')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.username)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.username)

def _get_entity(self):
r = self._initial_page()
Expand Down Expand Up @@ -211,12 +211,12 @@ def __init__(self, hashtag, **kwargs):
self._variablesFormat = '{{"tag_name":"{pageID}","first":50,"after":"{endCursor}"}}'

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('hashtag', type = snscrape.base.nonempty_string('hashtag'), help = 'An Instagram hashtag (no leading #)')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.hashtag)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.hashtag)


class InstagramLocationScraper(_InstagramCommonScraper):
Expand All @@ -233,9 +233,9 @@ def __init__(self, locationId, **kwargs):
self._variablesFormat = '{{"id":"{pageID}","first":50,"after":"{endCursor}"}}'

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('locationid', help = 'An Instagram location ID', type = int)

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.locationid)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.locationid)
14 changes: 7 additions & 7 deletions snscrape/modules/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,20 @@ def get_items(self):
url = urllib.parse.urljoin(r.url, nextA['href'])

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('account', type = snscrape.base.nonempty_string('account'), help = 'A Mastodon account. This can be either a URL to the profile page or a string of the form @account@instance.example.org')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.account)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.account)


class MastodonTootScraperMode(enum.Enum):
SINGLE = 'single'
THREAD = 'thread'

@classmethod
def cli_from_args(cls, args):
def _cli_from_args(cls, args):
if args.thread:
return cls.THREAD
return cls.SINGLE
Expand Down Expand Up @@ -331,10 +331,10 @@ def get_items(self):
yield from self._entries_to_items(soup.find('div', class_ = 'activity-stream').find_all('div', class_ = 'entry'), r.url)

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('--thread', action = 'store_true', help = 'Collect thread around the toot referenced by the URL')
subparser.add_argument('url', type = snscrape.base.nonempty_string('url'), help = 'A URL for a toot')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.url, MastodonTootScraperMode.cli_from_args(args))
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.url, MastodonTootScraperMode._cli_from_args(args))
6 changes: 3 additions & 3 deletions snscrape/modules/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def get_items(self):
yield from self._iter_api_submissions_and_comments({type(self)._apiField: self._name})

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('--no-submissions', dest = 'noSubmissions', action = 'store_true', default = False, help = 'Don\'t list submissions')
subparser.add_argument('--no-comments', dest = 'noComments', action = 'store_true', default = False, help = 'Don\'t list comments')
subparser.add_argument('--before', metavar = 'TIMESTAMP', type = int, help = 'Fetch results before a Unix timestamp')
Expand All @@ -213,9 +213,9 @@ def cli_setup_parser(cls, subparser):
subparser.add_argument(name, type = snscrape.base.nonempty_string(name))

@classmethod
def cli_from_args(cls, args):
def _cli_from_args(cls, args):
name = cls.name.split('-', 1)[1]
return cls.cli_construct(args, getattr(args, name), submissions = not args.noSubmissions, comments = not args.noComments, before = args.before, after = args.after)
return cls._cli_construct(args, getattr(args, name), submissions = not args.noSubmissions, comments = not args.noComments, before = args.before, after = args.after)


class RedditUserScraper(_RedditPushshiftScraper):
Expand Down
6 changes: 3 additions & 3 deletions snscrape/modules/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ def parse_num(s):
return Channel(**kwargs)

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('channel', type = snscrape.base.nonempty_string('channel'), help = 'A channel name')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.channel)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.channel)
36 changes: 18 additions & 18 deletions snscrape/modules/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,9 @@ def _user_label_to_user_label(self, label):
return UserLabel(**labelKwargs)

@classmethod
def cli_construct(cls, argparseArgs, *args, **kwargs):
def _cli_construct(cls, argparseArgs, *args, **kwargs):
kwargs['guestTokenManager'] = _CLIGuestTokenManager()
return super().cli_construct(argparseArgs, *args, **kwargs)
return super()._cli_construct(argparseArgs, *args, **kwargs)


class TwitterSearchScraper(_TwitterAPIScraper):
Expand Down Expand Up @@ -681,14 +681,14 @@ def get_items(self):
yield from self._instructions_to_tweets(obj)

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('--cursor', metavar = 'CURSOR')
subparser.add_argument('--top', action = 'store_true', default = False, help = 'Enable fetching top tweets instead of live/chronological')
subparser.add_argument('query', type = snscrape.base.nonempty_string('query'), help = 'A Twitter search string')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.query, cursor = args.cursor, top = args.top)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.query, cursor = args.cursor, top = args.top)


class TwitterUserScraper(TwitterSearchScraper):
Expand Down Expand Up @@ -757,7 +757,7 @@ def is_valid_username(s):
return (1 <= len(s) <= 15 and s.strip(string.ascii_letters + string.digits + '_') == '') or (s and s.strip(string.digits) == '')

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
def username(s):
if cls.is_valid_username(s):
return s
Expand All @@ -767,8 +767,8 @@ def username(s):
subparser.add_argument('username', type = username, help = 'A Twitter username (without @)')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.username, args.isUserId)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.username, args.isUserId)


class TwitterProfileScraper(TwitterUserScraper):
Expand Down Expand Up @@ -822,12 +822,12 @@ def __init__(self, hashtag, **kwargs):
self._hashtag = hashtag

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('hashtag', type = snscrape.base.nonempty_string('hashtag'), help = 'A Twitter hashtag (without #)')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.hashtag)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.hashtag)


class TwitterTweetScraperMode(enum.Enum):
Expand All @@ -836,7 +836,7 @@ class TwitterTweetScraperMode(enum.Enum):
RECURSE = 'recurse'

@classmethod
def from_args(cls, args):
def _cli_from_args(cls, args):
if args.scroll:
return cls.SCROLL
if args.recurse:
Expand Down Expand Up @@ -903,15 +903,15 @@ def get_items(self):
queue.append(tweet.id)

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
group = subparser.add_mutually_exclusive_group(required = False)
group.add_argument('--scroll', action = 'store_true', default = False, help = 'Enable scrolling in both directions')
group.add_argument('--recurse', '--recursive', action = 'store_true', default = False, help = 'Enable recursion through all tweets encountered (warning: slow, potentially memory-intensive!)')
subparser.add_argument('tweetId', type = int, help = 'A tweet ID')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.tweetId, TwitterTweetScraperMode.from_args(args))
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.tweetId, TwitterTweetScraperMode._cli_from_args(args))


class TwitterListPostsScraper(TwitterSearchScraper):
Expand All @@ -922,12 +922,12 @@ def __init__(self, listName, **kwargs):
self._listName = listName

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('list', type = snscrape.base.nonempty_string('list'), help = 'A Twitter list ID or a string of the form "username/listname" (replace spaces with dashes)')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.list)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.list)


class TwitterTrendsScraper(_TwitterAPIScraper):
Expand Down
6 changes: 3 additions & 3 deletions snscrape/modules/vkontakte.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ def parse_num(s):
return User(**kwargs)

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('username', type = snscrape.base.nonempty_string('username'), help = 'A VK username')

@classmethod
def cli_from_args(cls, args):
return cls.cli_construct(args, args.username)
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.username)
6 changes: 3 additions & 3 deletions snscrape/modules/weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ def _get_entity(self):
return self._user_info_to_entity(o['data']['userInfo'])

@classmethod
def cli_setup_parser(cls, subparser):
def _cli_setup_parser(cls, subparser):
subparser.add_argument('user', type = snscrape.base.nonempty_string('user'), help = 'A user name or ID')

@classmethod
def cli_from_args(cls, args):
def _cli_from_args(cls, args):
if len(args.user) == 10 and args.user.strip('0123456789') == '':
uid = args.user
name = None
else:
uid = None
name = args.user
return cls.cli_construct(args, name = name, uid = uid)
return cls._cli_construct(args, name = name, uid = uid)

0 comments on commit deb2659

Please sign in to comment.