1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-11-04 09:22:29 +01:00

Have --quiet argument

With --quiet activated, user interaction is disabled, i.e. no messages
are printed (except errors) and if login credentials are needed but not
given, instaloader fails.
This commit is contained in:
Alexander Graf 2016-06-26 10:39:26 +02:00
parent de2399a6c8
commit 42dc5a6026
2 changed files with 23 additions and 10 deletions

View File

@ -8,4 +8,4 @@ python:
install: install:
- pip install pylint requests - pip install pylint requests
script: script:
- python3 -m pylint -r n -d bad-whitespace,bad-continuation,missing-docstring,anomalous-backslash-in-string,invalid-name,multiple-imports,dangerous-default-value --max-args=8 --max-locals=16 --max-branches=19 --max-nested-blocks=6 instagram - python3 -m pylint -r n -d bad-whitespace,bad-continuation,missing-docstring,anomalous-backslash-in-string,invalid-name,multiple-imports,dangerous-default-value --max-args=8 --max-locals=16 --max-branches=20 --max-nested-blocks=6 instagram

View File

@ -8,6 +8,12 @@ import requests
class DownloaderException(Exception): class DownloaderException(Exception):
pass pass
quiet = False
def log(*msg, sep='', end='\n', flush=False):
if not quiet:
print(*msg, sep=sep, end=end, flush=flush)
def get_json(name, max_id = 0, session=None, SleepMinMax=[1,5]): def get_json(name, max_id = 0, session=None, SleepMinMax=[1,5]):
if session is None: if session is None:
session = get_session(None, None, True) session = get_session(None, None, True)
@ -44,11 +50,11 @@ def download_pic(name, url, date_epoch, outputlabel=None):
outputlabel = epochToString(date_epoch) outputlabel = epochToString(date_epoch)
filename = name.lower() + '/' + epochToString(date_epoch) + '.' + get_fileExtension(url) filename = name.lower() + '/' + epochToString(date_epoch) + '.' + get_fileExtension(url)
if os.path.isfile(filename): if os.path.isfile(filename):
print(outputlabel + ' exists', end=' ', flush=True) log(outputlabel + ' exists', end=' ', flush=True)
return False return False
r = get_session(None, None, True).get(url, stream=True) r = get_session(None, None, True).get(url, stream=True)
if r.status_code == 200: if r.status_code == 200:
print(outputlabel, end=' ', flush=True) log(outputlabel, end=' ', flush=True)
os.makedirs(name.lower(), exist_ok=True) os.makedirs(name.lower(), exist_ok=True)
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
r.raw.decode_content = True r.raw.decode_content = True
@ -64,7 +70,7 @@ def saveCaption(name, date_epoch, caption):
with open(filename, 'r') as f: with open(filename, 'r') as f:
fileCaption = f.read() fileCaption = f.read()
if fileCaption == caption: if fileCaption == caption:
print('txt unchanged', end=' ', flush=True) log('txt unchanged', end=' ', flush=True)
return None return None
else: else:
def get_filename(index): def get_filename(index):
@ -75,8 +81,8 @@ def saveCaption(name, date_epoch, caption):
i = i + 1 i = i + 1
for index in range(i, 0, -1): for index in range(i, 0, -1):
os.rename(get_filename(index-1), get_filename(index)) os.rename(get_filename(index-1), get_filename(index))
print('txt updated', end=' ', flush=True) log('txt updated', end=' ', flush=True)
print('txt', end=' ', flush=True) log('txt', end=' ', flush=True)
os.makedirs(name.lower(), exist_ok=True) os.makedirs(name.lower(), exist_ok=True)
with open(filename, 'w') as text_file: with open(filename, 'w') as text_file:
text_file.write(caption) text_file.write(caption)
@ -88,7 +94,7 @@ def download_profilepic(name, url):
filename = name.lower() + '/' + epochToString(date_object.timestamp()) + \ filename = name.lower() + '/' + epochToString(date_object.timestamp()) + \
'_UTC_profile_pic.' + url[-3:] '_UTC_profile_pic.' + url[-3:]
if os.path.isfile(filename): if os.path.isfile(filename):
print(filename + ' already exists') log(filename + ' already exists')
return None return None
m = re.search('http.*://.*instagram.*[^/]*\.(com|net)/[^/]+/.', url) m = re.search('http.*://.*instagram.*[^/]*\.(com|net)/[^/]+/.', url)
if m is None: if m is None:
@ -98,7 +104,7 @@ def download_profilepic(name, url):
url = url[:index] + 's2048x2048' + ('/' if offset == 0 else str()) + url[index+offset:] url = url[:index] + 's2048x2048' + ('/' if offset == 0 else str()) + url[index+offset:]
r = get_session(None, None, True).get(url, stream=True) r = get_session(None, None, True).get(url, stream=True)
if r.status_code == 200: if r.status_code == 200:
print(filename) log(filename)
os.makedirs(name.lower(), exist_ok=True) os.makedirs(name.lower(), exist_ok=True)
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
r.raw.decode_content = True r.raw.decode_content = True
@ -188,6 +194,9 @@ def download(name, username = None, password = None, sessionfile = None, \
if not ProfilePicOnly and data["entry_data"]["ProfilePage"][0]["user"]["is_private"]: if not ProfilePicOnly and data["entry_data"]["ProfilePage"][0]["user"]["is_private"]:
if not test_login(username, session): if not test_login(username, session):
if username is None or password is None: if username is None or password is None:
if quiet:
raise DownloaderException('Login required, credentials not given, ' \
'operating in quiet mode')
while True: while True:
if username is None: if username is None:
username = input('Enter your Instagram username to login: ') username = input('Enter your Instagram username to login: ')
@ -211,7 +220,7 @@ def download(name, username = None, password = None, sessionfile = None, \
count = 1 count = 1
while not get_last_id(data) is None: while not get_last_id(data) is None:
for node in data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]: for node in data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]:
print("[%3i/%3i] " % (count, totalcount), end="", flush=True) log("[%3i/%3i] " % (count, totalcount), end="", flush=True)
count = count + 1 count = count + 1
downloaded = download_pic(name, node["display_src"], node["date"]) downloaded = download_pic(name, node["display_src"], node["date"])
time.sleep(abs(SleepMinMax[1]-SleepMinMax[0])*random.random()+SleepMinMax[0]) time.sleep(abs(SleepMinMax[1]-SleepMinMax[0])*random.random()+SleepMinMax[0])
@ -222,7 +231,7 @@ def download(name, username = None, password = None, sessionfile = None, \
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') node["date"], 'mp4')
print() log()
if FastUpdate and not downloaded: if FastUpdate and not downloaded:
return return
data = get_json(name, get_last_id(data), session) data = get_json(name, get_last_id(data), session)
@ -248,7 +257,11 @@ if __name__ == "__main__":
help='Abort at encounter of first already-downloaded picture') help='Abort at encounter of first already-downloaded picture')
parser.add_argument('-S', '--no-sleep', action='store_true', parser.add_argument('-S', '--no-sleep', action='store_true',
help='Do not sleep between actual downloads of pictures') help='Do not sleep between actual downloads of pictures')
parser.add_argument('-q', '--quiet', action='store_true',
help='Disable user interaction, i.e. do not print messages (except errors) and fail ' \
'if login credentials are needed but not given.')
args = parser.parse_args() args = parser.parse_args()
quiet = args.quiet
for target in args.targets: for target in args.targets:
download(target, args.login, args.password, args.sessionfile, download(target, args.login, 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,