codekasten/WKNcharts/getWKNcharts.py
2007-07-24 23:40:35 +00:00

113 lines
3.1 KiB
Python
Executable file

#!/usr/bin/env python
__version__ = "0.2b"
__author__ = "age"
__date__ = "02006-04-25"
import urllib
from optparse import OptionParser
class WKNCharts:
'''fetches images from stocks'''
def __init__(self):
self.wkn_dict = {}
self.times = [300]
def readwkndictfromfile(self, filename):
'''reads lines like "wkn name\n" from a file and fills a dictionary'''
try:
wknfile = open(filename,"r")
except IOError, e:
print e
for line in wknfile:
'remove CRLF and split the line in two parts on spacer'
tmp = line.strip("\n").split(" ")
if tmp[0] and tmp[1]: self.wkn_dict[tmp[0]] = tmp[1]
wknfile.close()
def readwkndictfromstring(self, wknstring):
'''takes the given string into a "wkn name" dictionary'''
tmp2 = wknstring.splitlines()
#print len(tmp2)
for i in tmp2:
tmp = i.split()
if tmp[0] and tmp[1]: self.wkn_dict[tmp[0]] = tmp[1]
def getchart(self, wkn, time, width=400, height=240):
'''fetches the images via http'''
host = "http://gfx.finanztreff.de/charts/cc_gatrixx.gfx?"
params = urllib.urlencode({
'string':wkn,
'b':width,
'h':height,
'out':"png",
'zeit':time,
'typ':0,
'boerse':1,
'land':276,
'seite':"kurse",
'herkunft':123
})
#print "ich hole jetzt \"%s\" %i:" % (self.wkn_dict[wkn], time)
#print host + params
###self.gui.add_log("%s %s" % (host, params))
try:
f = urllib.urlopen(host + params)
png = open(self.dstdir+"/"+self.wkn_dict[wkn]+str(time)+".png", "w")
png.write(f.read())
png.close()
import time
### bei einigen modem verbindungen war eine pause notwendig
#time.sleep(2)
except IOError, e:
print e
def set_times(self, times):
self.times = times
def set_dstdir(self, dstdir):
self.dstdir = dstdir
def getallchartsnogui(self) :
'''TODO: this is obsolete, merge it with 'getallcharts' '''
for wkn in self.wkn_dict.keys():
for time in self.times:
self.getchart(wkn, time)
print "Download fertig"
return
def getallcharts(self, gui) :
self.gui = gui
from os import path
if not path.isdir(self.dstdir):
gui.add_log("Achtung: Zielverzeichnis %s nicht gefunden. Kein Download!" % self.dstdir, 1)
return
gui.add_log("Beginne Download nach: %s" % self.dstdir, 2)
alldownloads = len(self.wkn_dict.keys()) * len(self.times)
downloaded = 0
for wkn in self.wkn_dict.keys():
self.gui.add_log(" hole gerade: %s (%s) %s" % (self.wkn_dict[wkn], wkn, str(self.times)),2)
for time in self.times:
self.gui.set_progress("Speichern von: %s" % self.wkn_dict[wkn], int(downloaded), int(alldownloads))
self.getchart(wkn, time)
downloaded += 1
self.gui.set_progress("Download beendet!", int(downloaded), int(alldownloads))
gui.add_log("Download Ende",2)
return
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="read WKNS from FILE", metavar="FILE")
(options, args) = parser.parse_args()
wkn = WKNCharts()
wkn.readwkndictfromfile(options.filename)
#for i in wkn.wkn_dict.keys():
#print "%s \t %s" % (i, wkn.wkn_dict[i])
wkn.getallchartsnogui()
#wkn.addwkn(wkn, name)
#wkn.delwkn(wkn)