sbtools/phototools/__init__.py

43 lines
1.4 KiB
Python

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, directory = False):
os.makedirs("%s/%s" % (directory or ".", sender or "."), exist_ok=True)
filename = "%s/%s/%s" % (directory or ".", sender or ".", 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.split("/")[-1].split("?")[0]
else:
raise ValueError("%s is not a valid URL" % url)
def processURL(url, sender = False, directory = "photos", 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 == 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)
else:
verboselog("Downloading file to directory %s..." % sender, verbose)
fileDownloader(purl, sender, directory)