Do not abort when nonfatal errors occur
When a error like "user %s does not exist" occurs when downloading multiple profiles, it now just prints a warning and continues instead of aborting. This fixes #2.
This commit is contained in:
parent
a5ebf4d54c
commit
fde8396cb8
24
instagram.py
24
instagram.py
@ -8,6 +8,12 @@ import requests
|
|||||||
class DownloaderException(Exception):
|
class DownloaderException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class ProfileNotExistsException(DownloaderException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ProfileHasNoPicsException(DownloaderException):
|
||||||
|
pass
|
||||||
|
|
||||||
def log(*msg, sep='', end='\n', flush=False, quiet=False):
|
def log(*msg, sep='', end='\n', flush=False, quiet=False):
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print(*msg, sep=sep, end=end, flush=flush)
|
print(*msg, sep=sep, end=end, flush=flush)
|
||||||
@ -189,7 +195,7 @@ def download(name, username = None, password = None, sessionfile = None, \
|
|||||||
session = load_object(sessionfile)
|
session = load_object(sessionfile)
|
||||||
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 DownloaderException("user %s does not exist" % name)
|
raise ProfileNotExistsException("user %s does not exist" % name)
|
||||||
else:
|
else:
|
||||||
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)
|
||||||
@ -219,7 +225,7 @@ def download(name, username = None, password = None, sessionfile = None, \
|
|||||||
if ("nodes" not in data["entry_data"]["ProfilePage"][0]["user"]["media"] \
|
if ("nodes" not in data["entry_data"]["ProfilePage"][0]["user"]["media"] \
|
||||||
or len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0) \
|
or len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0) \
|
||||||
and not profile_pic_only:
|
and not profile_pic_only:
|
||||||
raise DownloaderException("no pics found")
|
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:
|
if not profile_pic_only:
|
||||||
count = 1
|
count = 1
|
||||||
@ -269,10 +275,18 @@ def main():
|
|||||||
'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
|
username = args.login
|
||||||
|
failedtargets = []
|
||||||
for target in args.targets:
|
for target in args.targets:
|
||||||
username = download(target, username, args.password, args.sessionfile,
|
try:
|
||||||
args.profile_pic_only, not args.skip_videos, args.fast_update,
|
username = download(target, username, args.password, args.sessionfile,
|
||||||
[0,0] if args.no_sleep else [0.25,2], args.quiet)
|
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) as err:
|
||||||
|
failedtargets.append(target)
|
||||||
|
print("%s\n" % err, file=sys.stderr)
|
||||||
|
if len(args.targets) > 1 and len(failedtargets) > 0:
|
||||||
|
print("Errors occured (see above) while downloading profiles: %s\n" %
|
||||||
|
", ".join(failedtargets), file=sys.stderr)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user