Added --verbose and --shut-up switches

This commit is contained in:
Klaus-Uwe Mitterer 2016-03-27 19:06:25 +02:00
parent 806aa452c3
commit f26e995f58

View file

@ -2,6 +2,25 @@
import argparse, os, requests, sys
def argparser():
parser = argparse.ArgumentParser()
parser.add_argument("url", type=str, nargs="+", help="photo URL as copied from message window")
group = parser.add_mutually_exclusive_group()
group.add_argument("-s", "--sender", type=str, help="name of the sender, puts images into subdirectory")
group.add_argument("-g", "--get-urls", help="get URLs only, don't download", action="store_true")
group2 = parser.add_mutually_exclusive_group()
group2.add_argument("-S", "--shut-up", help="don't output any notices and warnings", action="store_true")
group2.add_argument("-v", "--verbose", help="more verbose output", action="store_true")
return parser.parse_args()
def log(string, shutup = False, output = sys.stderr):
if not shutup:
print(string, file=output)
def verboselog(string, verbose = False, output = sys.stderr):
if verbose:
log(string, False, output)
def fileDownloader(url, sender = False):
if sender:
os.makedirs(sender, exist_ok=True)
@ -23,25 +42,25 @@ def urlparse(url):
else:
raise ValueError("%s is not a valid URL.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("url", type=str, nargs="+", help="photo URL as copied from message window")
group = parser.add_mutually_exclusive_group()
group.add_argument("-s", "--sender", type=str, help="name of the sender, puts images into subdirectory")
group.add_argument("-g", "--get-urls", help="get URLs only, don't download", action="store_true")
args = parser.parse_args()
for url in args.url:
def processURL(url, sender = False, geturls = False, verbose = False, shutup = False):
verboselog("Processing URL %s..." % url, verbose)
try:
purl = urlparse(url)
except ValueError:
print("%s is not a valid URL. Skipping." % url, file=sys.stderr)
log("Notice: %s is not a valid URL. Skipping." % url, shutup)
else:
if purl == urlparse(url):
print("Notice: You may have copied the image URL rather than the link URL from the message window. Use the link URL instead!")
if args.get_urls:
log("Warning: You may have copied the image URL rather than the link URL from the message window. Use the link URL instead!", shutup)
if geturls:
print(purl)
elif args.sender:
fileDownloader(purl, args.sender)
elif sender:
verboselog("Downloading file to directory %s..." % sender, verbose)
fileDownloader(purl, sender)
else:
fileDownloader(purl)
verboselog("Downloading file...", verbose)
fileDownloader(purl)
if __name__ == "__main__":
args = argparser()
for url in args.url:
processURL(url, args.sender, args.get_urls, args.verbose, args.shut_up)