Regex target matching

This commit is contained in:
Lars Lindqvist 2020-04-18 12:55:28 +02:00 committed by Alexander Graf
parent ab67ca30e5
commit 78023b3c98

View File

@ -3,6 +3,7 @@
import ast import ast
import datetime import datetime
import os import os
import re
import sys import sys
from argparse import ArgumentParser, SUPPRESS from argparse import ArgumentParser, SUPPRESS
from typing import List, Optional from typing import List, Optional
@ -136,19 +137,19 @@ def _main(instaloader: Instaloader, targetlist: List[str],
# strip '/' characters to be more shell-autocompletion-friendly # strip '/' characters to be more shell-autocompletion-friendly
target = target.rstrip('/') target = target.rstrip('/')
with instaloader.context.error_catcher(target): with instaloader.context.error_catcher(target):
if target[0] == '@': if re.match(r"^@\w(?:(?:\w|(?:\.(?!\.))){0,28}(?:\w))?$", target):
instaloader.context.log("Retrieving followees of %s..." % target[1:]) instaloader.context.log("Retrieving followees of %s..." % target[1:])
profile = Profile.from_username(instaloader.context, target[1:]) profile = Profile.from_username(instaloader.context, target[1:])
for followee in profile.get_followees(): for followee in profile.get_followees():
instaloader.save_profile_id(followee) instaloader.save_profile_id(followee)
profiles.add(followee) profiles.add(followee)
elif target[0] == '#': elif re.match(r"^#[A-Za-z0-9]+$", target):
instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update, instaloader.download_hashtag(hashtag=target[1:], max_count=max_count, fast_update=fast_update,
post_filter=post_filter, post_filter=post_filter,
profile_pic=download_profile_pic, posts=download_posts) profile_pic=download_profile_pic, posts=download_posts)
elif target[0] == '-': elif re.match(r"^-[A-Za-z0-9-_]+$", target):
instaloader.download_post(Post.from_shortcode(instaloader.context, target[1:]), target) instaloader.download_post(Post.from_shortcode(instaloader.context, target[1:]), target)
elif target[0] == "%": elif re.match(r"^%[0-9]+$", target):
instaloader.download_location(location=target[1:], max_count=max_count, fast_update=fast_update, instaloader.download_location(location=target[1:], max_count=max_count, fast_update=fast_update,
post_filter=post_filter) post_filter=post_filter)
elif target == ":feed": elif target == ":feed":
@ -159,7 +160,7 @@ def _main(instaloader: Instaloader, targetlist: List[str],
elif target == ":saved": elif target == ":saved":
instaloader.download_saved_posts(fast_update=fast_update, max_count=max_count, instaloader.download_saved_posts(fast_update=fast_update, max_count=max_count,
post_filter=post_filter) post_filter=post_filter)
else: elif re.match(r"^\w(?:(?:\w|(?:\.(?!\.))){0,28}(?:\w))?$", target):
try: try:
profile = instaloader.check_profile_id(target) profile = instaloader.check_profile_id(target)
if instaloader.context.is_logged_in and profile.has_blocked_viewer: if instaloader.context.is_logged_in and profile.has_blocked_viewer:
@ -185,6 +186,8 @@ def _main(instaloader: Instaloader, targetlist: List[str],
.format(target, err)) .format(target, err))
else: else:
raise raise
else:
raise ProfileNotExistsException('Invalid username {}'.format(target))
if len(profiles) > 1: if len(profiles) > 1:
instaloader.context.log("Downloading {} profiles: {}".format(len(profiles), instaloader.context.log("Downloading {} profiles: {}".format(len(profiles),
' '.join([p.username for p in profiles]))) ' '.join([p.username for p in profiles])))