From 657e76e2d948667115942711ce66a670cd3004f3 Mon Sep 17 00:00:00 2001 From: Lars Lindqvist Date: Sat, 18 Apr 2020 22:18:44 +0200 Subject: [PATCH] Simplify username regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without passing re.ASCII, \w matches non-ascii letter, such as ö, π and я, which to my knowledge isn't allowed in usernames. Now it's explicit which character ranges are expected in each target. --- instaloader/__main__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/instaloader/__main__.py b/instaloader/__main__.py index c149c3a..cc2266b 100644 --- a/instaloader/__main__.py +++ b/instaloader/__main__.py @@ -137,7 +137,7 @@ def _main(instaloader: Instaloader, targetlist: List[str], # strip '/' characters to be more shell-autocompletion-friendly target = target.rstrip('/') with instaloader.context.error_catcher(target): - if re.match(r"^@\w(?:(?:\w|(?:\.(?!\.))){0,28}(?:\w))?$", target): + if re.match(r"^@[A-Za-z0-9._]+$", target): instaloader.context.log("Retrieving followees of %s..." % target[1:]) profile = Profile.from_username(instaloader.context, target[1:]) for followee in profile.get_followees(): @@ -160,7 +160,7 @@ def _main(instaloader: Instaloader, targetlist: List[str], elif target == ":saved": instaloader.download_saved_posts(fast_update=fast_update, max_count=max_count, post_filter=post_filter) - elif re.match(r"^\w(?:(?:\w|(?:\.(?!\.))){0,28}(?:\w))?$", target): + elif re.match(r"^[A-Za-z0-9._]+$", target): try: profile = instaloader.check_profile_id(target) if instaloader.context.is_logged_in and profile.has_blocked_viewer: @@ -187,7 +187,12 @@ def _main(instaloader: Instaloader, targetlist: List[str], else: raise else: - raise ProfileNotExistsException('Invalid username {}'.format(target)) + target_type = { + '#': 'hashtag', + '%': 'location', + '-': 'shortcode', + }.get(target[0], 'username') + raise ProfileNotExistsException('Invalid {} {}'.format(target_type, target)) if len(profiles) > 1: instaloader.context.log("Downloading {} profiles: {}".format(len(profiles), ' '.join([p.username for p in profiles])))