mirror of
https://github.com/instaloader/instaloader.git
synced 2024-09-11 16:22:24 +02:00
Refactor download() function
This commit is contained in:
parent
9aa2b91d52
commit
220584c483
167
instagram.py
167
instagram.py
@ -196,76 +196,96 @@ def get_session(user, passwd, empty_session_only=False, session=None):
|
|||||||
else:
|
else:
|
||||||
raise LoginException('Login error! Connection error!')
|
raise LoginException('Login error! Connection error!')
|
||||||
|
|
||||||
def download(name, username = None, password = None, sessionfile = None, \
|
def download(name, session=None, profile_pic_only=False, download_videos=True,
|
||||||
profile_pic_only = False, download_videos = True, fast_update = False, \
|
fast_update=False, sleep_min_max=[0.25,2], quiet=False):
|
||||||
sleep_min_max=[0.25,2], quiet=False):
|
"""Download one profile"""
|
||||||
# pylint:disable=too-many-arguments,too-many-locals,too-many-nested-blocks,too-many-branches
|
# pylint:disable=too-many-arguments
|
||||||
# pylint:disable=too-many-statements
|
# Get profile main page json
|
||||||
# We are aware that this function has many arguments, many local variables, many nested blocks
|
|
||||||
# and many branches. But we don't care.
|
|
||||||
session = load_object(sessionfile, quiet=quiet)
|
|
||||||
data = get_json(name, session=session)
|
data = get_json(name, session=session)
|
||||||
if len(data["entry_data"]) == 0 or "ProfilePage" not in data["entry_data"]:
|
if len(data["entry_data"]) == 0 or "ProfilePage" not in data["entry_data"]:
|
||||||
raise ProfileNotExistsException("user %s does not exist" % name)
|
raise ProfileNotExistsException("user %s does not exist" % name)
|
||||||
else:
|
# Download profile picture
|
||||||
download_profilepic(name, data["entry_data"]["ProfilePage"][0]["user"]["profile_pic_url"],
|
download_profilepic(name, data["entry_data"]["ProfilePage"][0]["user"]["profile_pic_url"],
|
||||||
quiet=quiet)
|
quiet=quiet)
|
||||||
time.sleep(abs(sleep_min_max[1]-sleep_min_max[0])*random.random()+abs(sleep_min_max[0]))
|
time.sleep(abs(sleep_min_max[1]-sleep_min_max[0])*random.random()+abs(sleep_min_max[0]))
|
||||||
if not profile_pic_only and data["entry_data"]["ProfilePage"][0]["user"]["is_private"]:
|
if profile_pic_only:
|
||||||
if not test_login(username, session):
|
return
|
||||||
if username is None or password is None:
|
# Catch some errors
|
||||||
if quiet:
|
if data["entry_data"]["ProfilePage"][0]["user"]["is_private"]:
|
||||||
raise LoginRequiredException("user %s requires login" % name)
|
if not session:
|
||||||
while True:
|
raise LoginRequiredException("user %s requires login" % name)
|
||||||
if username is None:
|
if not data["entry_data"]["ProfilePage"][0]["user"]["followed_by_viewer"]:
|
||||||
username = input('Enter your Instagram username to login: ')
|
raise PrivateProfileNotFollowedException("user %s: private but not followed" % name)
|
||||||
if password is None:
|
if ("nodes" not in data["entry_data"]["ProfilePage"][0]["user"]["media"] or
|
||||||
password = getpass.getpass(
|
len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0) \
|
||||||
prompt='Enter your corresponding Instagram password: ')
|
and not profile_pic_only:
|
||||||
try:
|
raise ProfileHasNoPicsException("user %s: no pics found" % name)
|
||||||
session = get_session(username, password, session=session)
|
# Iterate over pictures and download them
|
||||||
except LoginException as err:
|
|
||||||
print("%s" % err, file=sys.stderr)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
username = None
|
|
||||||
password = None
|
|
||||||
else:
|
|
||||||
session = get_session(username, password, session=session)
|
|
||||||
data = get_json(name, session=session)
|
|
||||||
if not data["entry_data"]["ProfilePage"][0]["user"]["followed_by_viewer"]:
|
|
||||||
raise PrivateProfileNotFollowedException("user %s: private but not followed" % name)
|
|
||||||
if ("nodes" not in data["entry_data"]["ProfilePage"][0]["user"]["media"] \
|
|
||||||
or len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0) \
|
|
||||||
and not profile_pic_only:
|
|
||||||
raise ProfileHasNoPicsException("user %s: no pics found" % name)
|
|
||||||
totalcount = data["entry_data"]["ProfilePage"][0]["user"]["media"]["count"]
|
totalcount = data["entry_data"]["ProfilePage"][0]["user"]["media"]["count"]
|
||||||
if not profile_pic_only:
|
count = 1
|
||||||
count = 1
|
while get_last_id(data) is not None:
|
||||||
while get_last_id(data) is not None:
|
for node in data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]:
|
||||||
for node in data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]:
|
log("[%3i/%3i] " % (count, totalcount), end="", flush=True, quiet=quiet)
|
||||||
log("[%3i/%3i] " % (count, totalcount), end="", flush=True, quiet=quiet)
|
count = count + 1
|
||||||
count = count + 1
|
downloaded = download_pic(name, node["display_src"], node["date"], quiet=quiet)
|
||||||
downloaded = download_pic(name, node["display_src"], node["date"], quiet=quiet)
|
time.sleep(abs(sleep_min_max[1]-sleep_min_max[0])*random.random() + \
|
||||||
time.sleep(abs(sleep_min_max[1]-sleep_min_max[0])*random.random() + \
|
abs(sleep_min_max[0]))
|
||||||
abs(sleep_min_max[0]))
|
if "caption" in node:
|
||||||
if "caption" in node:
|
save_caption(name, node["date"], node["caption"], quiet=quiet)
|
||||||
save_caption(name, node["date"], node["caption"], quiet=quiet)
|
if node["is_video"] and download_videos:
|
||||||
if node["is_video"] and download_videos:
|
video_data = get_json('p/' + node["code"], session=session)
|
||||||
video_data = get_json('p/' + node["code"], session=session)
|
download_pic(name, \
|
||||||
download_pic(name, \
|
video_data['entry_data']['PostPage'][0]['media']['video_url'], \
|
||||||
video_data['entry_data']['PostPage'][0]['media']['video_url'], \
|
node["date"], 'mp4', quiet=quiet)
|
||||||
node["date"], 'mp4', quiet=quiet)
|
log(quiet=quiet)
|
||||||
log(quiet=quiet)
|
if fast_update and not downloaded:
|
||||||
if fast_update and not downloaded:
|
return
|
||||||
if test_login(username, session):
|
data = get_json(name, get_last_id(data), session)
|
||||||
save_object(session, sessionfile, quiet=quiet)
|
time.sleep(abs(sleep_min_max[1]-sleep_min_max[0])*random.random()+abs(sleep_min_max[0]))
|
||||||
return username
|
|
||||||
data = get_json(name, get_last_id(data), session)
|
def get_logged_in_session(username, password=None, quiet=False):
|
||||||
time.sleep(abs(sleep_min_max[1]-sleep_min_max[0])*random.random()+abs(sleep_min_max[0]))
|
"""Logs in and returns session"""
|
||||||
if test_login(username, session):
|
if password is not None:
|
||||||
|
return get_session(username, password)
|
||||||
|
if quiet:
|
||||||
|
raise LoginRequiredException("Quiet mode requires given password or valid "
|
||||||
|
"session file.")
|
||||||
|
while password is None:
|
||||||
|
password = getpass.getpass(prompt='Enter your Instagram password: ')
|
||||||
|
try:
|
||||||
|
return get_session(username, password)
|
||||||
|
except LoginException as err:
|
||||||
|
print(err, file=sys.stderr)
|
||||||
|
password = None
|
||||||
|
|
||||||
|
def download_profiles(targets, username=None, password=None, sessionfile=None,
|
||||||
|
profile_pic_only=False, download_videos=True, fast_update=False,
|
||||||
|
sleep_min_max=[0.25,2], quiet=False):
|
||||||
|
"""Download set of profiles and handle sessions"""
|
||||||
|
# pylint:disable=too-many-arguments
|
||||||
|
# Login, if desired
|
||||||
|
session = None
|
||||||
|
if username is not None:
|
||||||
|
session = load_object(sessionfile, quiet=quiet)
|
||||||
|
if not test_login(username, session):
|
||||||
|
session = get_logged_in_session(username, password, quiet)
|
||||||
|
log("Logged in as %s." % username, quiet=quiet)
|
||||||
|
# Iterate through targets list and download them
|
||||||
|
failedtargets = []
|
||||||
|
for target in targets:
|
||||||
|
try:
|
||||||
|
download(target, session, profile_pic_only, download_videos,
|
||||||
|
fast_update, sleep_min_max, quiet)
|
||||||
|
except (ProfileNotExistsException, ProfileHasNoPicsException,
|
||||||
|
PrivateProfileNotFollowedException, LoginRequiredException) as err:
|
||||||
|
failedtargets.append(target)
|
||||||
|
print(err, file=sys.stderr)
|
||||||
|
if len(targets) > 1 and len(failedtargets) > 0:
|
||||||
|
print("Errors occured (see above) while downloading profiles: %s" %
|
||||||
|
", ".join(failedtargets), file=sys.stderr)
|
||||||
|
# Save session if it is useful
|
||||||
|
if username is not None:
|
||||||
save_object(session, sessionfile, quiet=quiet)
|
save_object(session, sessionfile, quiet=quiet)
|
||||||
return username
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = ArgumentParser(description='Simple downloader to fetch all Instagram pics and '\
|
parser = ArgumentParser(description='Simple downloader to fetch all Instagram pics and '\
|
||||||
@ -289,20 +309,9 @@ def main():
|
|||||||
help='Disable user interaction, i.e. do not print messages (except errors) and fail ' \
|
help='Disable user interaction, i.e. do not print messages (except errors) and fail ' \
|
||||||
'if login credentials are needed but not given.')
|
'if login credentials are needed but not given.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
username = args.login
|
download_profiles(args.targets, args.login, args.password, args.sessionfile,
|
||||||
failedtargets = []
|
args.profile_pic_only, not args.skip_videos, args.fast_update,
|
||||||
for target in args.targets:
|
[0,0] if args.no_sleep else [0.25,2], args.quiet)
|
||||||
try:
|
|
||||||
username = download(target, username, args.password, args.sessionfile,
|
|
||||||
args.profile_pic_only, not args.skip_videos, args.fast_update,
|
|
||||||
[0,0] if args.no_sleep else [0.25,2], args.quiet)
|
|
||||||
except (ProfileNotExistsException, ProfileHasNoPicsException,
|
|
||||||
PrivateProfileNotFollowedException, LoginRequiredException) as err:
|
|
||||||
failedtargets.append(target)
|
|
||||||
print("%s" % err, file=sys.stderr)
|
|
||||||
if len(args.targets) > 1 and len(failedtargets) > 0:
|
|
||||||
print("Errors occured (see above) while downloading profiles: %s" %
|
|
||||||
", ".join(failedtargets), file=sys.stderr)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user