From 0037541289250ff4f8c160dbb310634f4abb7d40 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 6 Aug 2016 15:00:05 +0200 Subject: [PATCH] Put downloader logic into phototools --- downloader.py | 67 ++++++------------------------------------ phototools/__init__.py | 49 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 phototools/__init__.py diff --git a/downloader.py b/downloader.py index cd74d21..7c38171 100755 --- a/downloader.py +++ b/downloader.py @@ -1,66 +1,17 @@ #!/usr/bin/env python3 -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") +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) - filename = "%s/%s" % (sender, url.split('/')[-1]) - else: - filename = url.split('/')[-1] - remote = requests.get(url, stream=True) - with open(filename, 'wb') as outfile: - for chunk in remote.iter_content(chunk_size=1024): - if chunk: - outfile.write(chunk) - outfile.flush() - -def urlparse(url): - if "/img/usr/" in url: - return url - if "/auswertung/pix/popup.php/" in url: - return "http://www.planetromeo.com/img/usr/%s" % url[52:82] - else: - raise ValueError("%s is not a valid URL" % url) - -def processURL(url, sender = False, geturls = False, verbose = False, shutup = False): - verboselog("Processing URL %s..." % url, verbose) - try: - purl = urlparse(url) - except ValueError as e: - log("Notice: %s. Skipping." % e, shutup) - else: - if purl == urlparse(url): - 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 sender: - verboselog("Downloading file to directory %s..." % sender, verbose) - fileDownloader(purl, sender) - else: - 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) + phototools.processURL(url, args.sender, args.get_urls, args.verbose, args.shut_up) diff --git a/phototools/__init__.py b/phototools/__init__.py new file mode 100644 index 0000000..869f028 --- /dev/null +++ b/phototools/__init__.py @@ -0,0 +1,49 @@ +import argparse, os, requests, sys + +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) + filename = "%s/%s" % (sender, url.split('/')[-1]) + else: + filename = url.split('/')[-1] + remote = requests.get(url, stream=True) + with open(filename, 'wb') as outfile: + for chunk in remote.iter_content(chunk_size=1024): + if chunk: + outfile.write(chunk) + outfile.flush() + +def urlparse(url): + if "/img/usr/" in url: + return url + if "/auswertung/pix/popup.php/" in url: + return "http://www.planetromeo.com/img/usr/%s" % url[52:82] + else: + raise ValueError("%s is not a valid URL" % url) + +def processURL(url, sender = False, geturls = False, verbose = False, shutup = False): + verboselog("Processing URL %s..." % url, verbose) + try: + purl = urlparse(url) + except ValueError as e: + log("Notice: %s. Skipping." % e, shutup) + else: + if purl == urlparse(url): + 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 sender: + verboselog("Downloading file to directory %s..." % sender, verbose) + fileDownloader(purl, sender) + else: + verboselog("Downloading file...", verbose) + fileDownloader(purl) +