Improve photo handling. Should now handle URLs a lot better. Also added a way to add a parent directory for all photos.
This commit is contained in:
parent
258a192b6a
commit
d7376256f5
1 changed files with 7 additions and 13 deletions
|
@ -8,12 +8,9 @@ def verboselog(string, verbose = False, output = sys.stderr):
|
||||||
if verbose:
|
if verbose:
|
||||||
log(string, False, output)
|
log(string, False, output)
|
||||||
|
|
||||||
def fileDownloader(url, sender = False):
|
def fileDownloader(url, sender = False, directory = False):
|
||||||
if sender:
|
os.makedirs("%s/%s" % (directory or ".", sender or "."), exist_ok=True)
|
||||||
os.makedirs(sender, exist_ok=True)
|
filename = "%s/%s/%s" % (directory or ".", sender or ".", url.split('/')[-1])
|
||||||
filename = "%s/%s" % (sender, url.split('/')[-1])
|
|
||||||
else:
|
|
||||||
filename = url.split('/')[-1]
|
|
||||||
remote = requests.get(url, stream=True)
|
remote = requests.get(url, stream=True)
|
||||||
with open(filename, 'wb') as outfile:
|
with open(filename, 'wb') as outfile:
|
||||||
for chunk in remote.iter_content(chunk_size=1024):
|
for chunk in remote.iter_content(chunk_size=1024):
|
||||||
|
@ -25,11 +22,11 @@ def urlparse(url):
|
||||||
if "/img/usr/" in url:
|
if "/img/usr/" in url:
|
||||||
return url
|
return url
|
||||||
if "/auswertung/pix/popup.php/" in url:
|
if "/auswertung/pix/popup.php/" in url:
|
||||||
return "http://www.planetromeo.com/img/usr/%s" % url[52:82]
|
return "http://www.planetromeo.com/img/usr/%s" % url.split("/")[-1].split("?")[0]
|
||||||
else:
|
else:
|
||||||
raise ValueError("%s is not a valid URL" % url)
|
raise ValueError("%s is not a valid URL" % url)
|
||||||
|
|
||||||
def processURL(url, sender = False, geturls = False, verbose = False, shutup = False):
|
def processURL(url, sender = False, directory = "photos", geturls = False, verbose = False, shutup = False):
|
||||||
verboselog("Processing URL %s..." % url, verbose)
|
verboselog("Processing URL %s..." % url, verbose)
|
||||||
try:
|
try:
|
||||||
purl = urlparse(url)
|
purl = urlparse(url)
|
||||||
|
@ -40,10 +37,7 @@ def processURL(url, sender = False, geturls = False, verbose = False, shutup = F
|
||||||
log("Warning: You may have copied the image URL rather than the link URL from the message window. Use the link URL instead!", shutup)
|
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:
|
if geturls:
|
||||||
print(purl)
|
print(purl)
|
||||||
elif sender:
|
|
||||||
verboselog("Downloading file to directory %s..." % sender, verbose)
|
|
||||||
fileDownloader(purl, sender)
|
|
||||||
else:
|
else:
|
||||||
verboselog("Downloading file...", verbose)
|
verboselog("Downloading file to directory %s..." % sender, verbose)
|
||||||
fileDownloader(purl)
|
fileDownloader(purl, sender, directory)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue