1
0
Fork 0
CodingContest-FHJoanneum-Wi.../contest.py

18 lines
2.3 KiB
Python

#!/usr/bin/python3
# Description: Loads all images for a searchterm (=commandline argument) from flickr to an existing directory specified in contest.cfg
# History:
# 2013-10-05: v0.1 by Manuel Kamper (Read dirname from cfg-file, read searchstring from commandline, download images from url-list, generate HTML files for slideshow)
# 2013-10-05: v0.2 by Manuel Kamper (Dirname inline where needed = win 1 line :), generate meta-csv file)
# 2013-10-05: v0.3 by Manuel Kamper (HTML slideshow loop (previous=from first to last page, next=from last to first page))
# 2013-10-06: v0.4 by Manuel Kamper (Added filesize in meta-csv, get Image-URLs in list)
# 2013-12-31: v0.5 by Manuel Kamper (took index-number from item instead of own counted i - saved 2 lines!)
# 2014-01-01: v0.6 by Manuel Kamper (remove unwanted non-search-related images from list)
# 2014-01-01: v0.7 by Manuel Kamper (replace spaces in search-term with %20)
# 2014-01-06: v0.8 by Manuel Kamper (enumerate in for-loop saves one line! but removed slideshow-loop :( )
# 2014-01-06: v0.9 by Manuel Kamper (put the three steps in for-loop to save one line!)
# 2014-01-07: v0.10 by Manuel Kamper (saved one line because of the o - thx to franz!)
import os, re, sys, time, urllib.request, urllib.error, urllib.parse
for id, url in enumerate([x for x in re.findall('img .*?src="(.*?)"',urllib.request.urlopen('http://www.flickr.com/search/?q='+sys.argv[1].replace(" ","%20")).read().decode(sys.stdout.encoding)) if (re.split("/",x)[-1] != "spaceball.gif") and ((re.split("\.",x)[-1] == "gif") or (re.split("\.",x)[-1] == "jpg"))]):
for o in [open(open("contest.cfg").read()+"/"+time.strftime("%Y%m%d-")+str((id+1))+"-"+re.split("/",url)[-1],"wb").write(urllib.request.urlopen(url).read()), open(open("contest.cfg").read()+"/meta.csv","a+").write(url+";"+time.strftime("%d.%m.%Y %H:%M:%S")+";"+str(os.stat(open("contest.cfg").read()+"/"+time.strftime("%Y%m%d-")+str((id+1))+"-"+re.split("/",url)[-1]).st_size)+";\n"), open(open("contest.cfg").read()+"/page"+str(id+1)+".htm","w").write('<html><head><title>Diashow</title><meta http-equiv="refresh" content="5; URL=page'+str(id+2)+'.htm"></head><body><a href="page'+str(id)+'.htm">previous</a><img src="'+time.strftime("%Y%m%d-")+str((id+1))+"-"+re.split("/",url)[-1]+'" alt="Downloaded image no. '+str(id+1)+'"><a href="page'+str(id+2)+'.htm">next</a></body></html>')]:o