def get_context_data(self, *args, **kwargs): context = super(OneStock, self).get_context_data(**kwargs) try: #logger.info(context['categories']) self.sid = self.request.GET.get('s') if self.sid == None or self.sid == '': return context sinaId = stockIDforSina(self.sid) context["dailyurl"]="http://image.sinajs.cn/newchart/daily/n/%s.gif"%(sinaId) context["minuteurl"]="http://image.sinajs.cn/newchart/min/n/%s.gif"%(sinaId) context["weekurl"]="http://image.sinajs.cn/newchart/weekly/n/%s.gif"%(sinaId) context["monthurl"]="http://image.sinajs.cn/newchart/monthly/n/%s.gif"%(sinaId) sdb = dbAPI.stockDB() alldays = sdb.getAllDate(self.sid) if len(alldays) == 0: print "no data" return context showday = alldays[-1][0] pankouDays = [] for row in alldays: print "day:",row pankouDays.append((row[0], '/stock/'+row[0].strftime('%Y%m%d')+"?s="+self.sid)) print "getAllDate",alldays,showday context['pankouDays'] = pankouDays except Exception as e: logger.exception(u'加载基本信息出错[%s]!', e) return context
def startLoop(self, idlist): filelist = {} csvlist = {} self.lasttime = {} self.lastvol = {} onceCount = 0 allIdList = [] onceIdList = [] print "start to get lasttime from db",datetime.datetime.now() stockdb = stockDB() for id in idlist: onceCount+=1 onceIdList.append(id) lasttime = stockdb.getLastDate(id) if lasttime != None: self.lasttime[id] = lasttime if onceCount < self.maxStockOnce: pass else: allIdList.append(onceIdList) onceIdList = [] onceCount = 0 allIdList.append(onceIdList) print "start to loop",datetime.datetime.now() while True: curTime = datetime.datetime.now() res = common.secToMarcketOpen(curTime) - 60 if res > 0: print "sleep secondes:",res stockdb.close() time.sleep(res) stockdb.connect() continue successCount = 0 for onceIds in allIdList: successCount+=self.getOnceData(onceIds, stockdb) time.sleep(0.8) sleepSec = self.intervalSec if successCount == 0: if sleepSec < 60: print "All failed, sleep 1 minutes" sleepSec = 60
def get_context_data(self, **kwargs): context = super(StockDetailView, self).get_context_data(**kwargs) showday = kwargs.get("day",None) if showday == None or not self.sid: return context context['KLurl'] = 'http://taohui.org.cn/stockKL/?s='+self.sid sdb = dbAPI.stockDB() filename = 'pankou_%s_%s.jpg'%(self.sid, showday) imgUrl = '/stockimage/'+filename imgLoc = "/mnt/myblog/stock/"+filename context['pankouVolUrl'] = imgUrl xAxisTitle = 'showday %s'%(showday) yAxisTitle = 'VOL' rows = sdb.getDayData(self.sid, datetime.datetime.strptime(showday, '%Y%m%d').date()) if len(rows) == 0: print len(rows),sid,showday[0:4],showday[4,2],showday[6,2] return context pankouDatas = [] times = [] lastVol = rows[0][dbAPI.DBCOL_EXCH_VOL] for row in rows: times.append(str(row[1])) pline = [row[dbAPI.DBCOL_CUR_PRI],row[dbAPI.DBCOL_EXCH_VOL]-lastVol,\ row[dbAPI.DBCOL_EXCH_AMOUNT],row[dbAPI.DBCOL_TIME],\ row[dbAPI.DBCOL_COMP_BUY_PRI],row[dbAPI.DBCOL_COMP_SELL_PRI] ] for i in range(20): pline.append(row[10+i]) pankouDatas.append(pline) lastVol = row[dbAPI.DBCOL_EXCH_VOL] idManager = getAllIdFromSina.sinaIdManage() idManager.initLocalData() print idManager.allstockmap[self.sid] context['pankouDatas'] = pankouDatas context['dataLen'] = len(pankouDatas) context['highest'] = rows[-1][dbAPI.DBCOL_HIGH_PRI] context['lowest'] = rows[-1][dbAPI.DBCOL_LOW_PRI] context['open'] = rows[-1][dbAPI.DBCOL_OPEN_PRI] context['price'] = rows[-1][dbAPI.DBCOL_CUR_PRI] context['day'] = rows[-1][dbAPI.DBCOL_DATE].strftime('%Y%m') context['volume'] = rows[-1][dbAPI.DBCOL_EXCH_VOL] context['amount'] = rows[-1][dbAPI.DBCOL_EXCH_AMOUNT] if os.path.exists(imgLoc): print imgLoc,'picfile exist!' return context linenames = [] for i in range(5): linenames.append("buy%d"%(i+1)) for i in range(5): linenames.append("sell%d"%(1+i)) voldatas = [] for i in range(5): line1 = [] line2 = [] for row in rows: line1.append(int(row[29-2*i-1])) line2.append("%.2f"%(row[29-2*i])) voldatas.append(line1) curprices = [] vols = [] for row in rows: vols.append(int(row[8])) line = [] for row in rows: line.append("%.2f"%row[3]) curprices.append(line) for i in range(5): line1 = [] line2 = [] for row in rows: line1.append(int(row[10+2*i])) line2.append("%.2f"%(row[10+2*i+1])) voldatas.append(line1) colors=['0x00CED1','0x00C5CD','0x008B8B','0x00688B','0x0000CD',\ '0xEEA2AD','0xEE82EE','0xEE7942','0xEE6A50','0xEE3B3B'] chart = CreateDiagram.SelfDefStockChart() chart.addXYChart("pankouVol", voldatas, times, linenames, \ colors, xAxisTitle,yAxisTitle) c = chart.addXYChart("pankouVol", curprices, times, ['curpri'], \ ['0xff9999'], xAxisTitle,yAxisTitle) #chart.addBar(c, vols, times) chart.makeChart(imgLoc) return context