codekasten/WKNcharts/getWKNcharts.py

107 lines
2.8 KiB
Python
Raw Normal View History

2006-06-07 11:03:58 +02:00
#!/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]
2006-06-07 11:03:58 +02:00
def readwkndictfromfile(self, filename):
'''reads lines like "wkn name\n" from a file and fills a dictionary'''
2006-06-14 01:13:52 +02:00
try:
wknfile = open(filename,"r")
except IOError, e:
print e
2006-06-07 11:03:58 +02:00
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()
2006-06-15 18:12:27 +02:00
#print len(tmp2)
for i in tmp2:
tmp = i.split()
if tmp[0] and tmp[1]: self.wkn_dict[tmp[0]] = tmp[1]
2006-06-07 11:03:58 +02:00
def getchart(self, wkn, time, width=400, height=240):
2006-06-07 11:03:58 +02:00
'''fetches the images via http'''
host = "http://gfx.finanztreff.de/charts/cc_gatrixx.gfx?"
params = urllib.urlencode({
'string':wkn,
'b':width,
'h':height,
2006-06-07 11:03:58 +02:00
'out':"png",
'zeit':time,
2006-06-07 11:03:58 +02:00
'typ':0,
'boerse':1,
'land':276,
'seite':"kurse",
'herkunft':123
})
2006-06-15 18:12:27 +02:00
#print "ich hole jetzt \"%s\" %i:" % (self.wkn_dict[wkn], time)
#print host + params
2006-06-14 01:13:52 +02:00
###self.gui.add_log("%s %s" % (host, params))
2006-06-15 18:12:27 +02:00
2006-06-07 11:03:58 +02:00
try:
f = urllib.urlopen(host + params)
2006-06-15 18:12:27 +02:00
png = open(self.dstdir+"/"+self.wkn_dict[wkn]+str(time)+".png", "w")
2006-06-07 11:03:58 +02:00
png.write(f.read())
png.close()
2006-06-10 14:18:28 +02:00
import time
time.sleep(2)
2006-06-07 11:03:58 +02:00
except IOError, e:
2006-06-09 14:05:30 +02:00
print e
def set_times(self, times):
self.times = times
2006-06-07 11:03:58 +02:00
2006-06-15 18:12:27 +02:00
def set_dstdir(self, dstdir):
self.dstdir = dstdir
2006-06-14 01:13:52 +02:00
def getallchartsnogui(self) :
2006-06-15 18:12:27 +02:00
'''TODO: this is obsolete, merge it with 'getallcharts' '''
2006-06-14 01:13:52 +02:00
for wkn in self.wkn_dict.keys():
for time in self.times:
self.getchart(wkn, time)
print "Download fertig"
return
2006-06-10 14:18:28 +02:00
def getallcharts(self, gui) :
self.gui = gui
2006-06-15 18:12:27 +02:00
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)
2006-06-07 11:03:58 +02:00
for wkn in self.wkn_dict.keys():
2006-06-15 18:12:27 +02:00
self.gui.add_log(" hole gerade: %s (%s) %s" % (self.wkn_dict[wkn], wkn, str(self.times)),2)
for time in self.times:
self.getchart(wkn, time)
2006-06-15 18:12:27 +02:00
gui.add_log("Download Ende",2)
return
2006-06-07 11:03:58 +02:00
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])
2006-06-14 01:13:52 +02:00
wkn.getallchartsnogui()
2006-06-07 11:03:58 +02:00
#wkn.addwkn(wkn, name)
#wkn.delwkn(wkn)