Refactor download() function
This commit is contained in:
parent
9aa2b91d52
commit
220584c483
109
instagram.py
109
instagram.py
@ -196,51 +196,32 @@ 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"]:
|
||||||
|
if not session:
|
||||||
raise LoginRequiredException("user %s requires login" % name)
|
raise LoginRequiredException("user %s requires login" % name)
|
||||||
while True:
|
|
||||||
if username is None:
|
|
||||||
username = input('Enter your Instagram username to login: ')
|
|
||||||
if password is None:
|
|
||||||
password = getpass.getpass(
|
|
||||||
prompt='Enter your corresponding Instagram password: ')
|
|
||||||
try:
|
|
||||||
session = get_session(username, password, session=session)
|
|
||||||
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"]:
|
if not data["entry_data"]["ProfilePage"][0]["user"]["followed_by_viewer"]:
|
||||||
raise PrivateProfileNotFollowedException("user %s: private but not followed" % name)
|
raise PrivateProfileNotFollowedException("user %s: private but not followed" % name)
|
||||||
if ("nodes" not in data["entry_data"]["ProfilePage"][0]["user"]["media"] \
|
if ("nodes" not in data["entry_data"]["ProfilePage"][0]["user"]["media"] or
|
||||||
or len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0) \
|
len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0) \
|
||||||
and not profile_pic_only:
|
and not profile_pic_only:
|
||||||
raise ProfileHasNoPicsException("user %s: no pics found" % name)
|
raise ProfileHasNoPicsException("user %s: no pics found" % name)
|
||||||
|
# Iterate over pictures and download them
|
||||||
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"]:
|
||||||
@ -258,14 +239,53 @@ def download(name, username = None, password = None, sessionfile = None, \
|
|||||||
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:
|
||||||
if test_login(username, session):
|
return
|
||||||
save_object(session, sessionfile, quiet=quiet)
|
|
||||||
return username
|
|
||||||
data = get_json(name, get_last_id(data), session)
|
data = get_json(name, get_last_id(data), session)
|
||||||
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 test_login(username, session):
|
|
||||||
|
def get_logged_in_session(username, password=None, quiet=False):
|
||||||
|
"""Logs in and returns 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 = []
|
|
||||||
for target in args.targets:
|
|
||||||
try:
|
|
||||||
username = download(target, username, args.password, args.sessionfile,
|
|
||||||
args.profile_pic_only, not args.skip_videos, args.fast_update,
|
args.profile_pic_only, not args.skip_videos, args.fast_update,
|
||||||
[0,0] if args.no_sleep else [0.25,2], args.quiet)
|
[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