def check_fund_status(taskobj): config = global_obj.get_obj("config") fund_list = config.get("fund_list", []) if len(fund_list) == 0: log.Waring("fund_list is empty") return ls = [] for code in fund_list: old = fund_api.load_fund(code) code = str(code) fund_api.spiker_fund_and_save(code) ret = fund_api.load_fund(code) log.Info("spiker fund done", code) if old and ret: result = compare_fund(old,ret) if result: ls.append(result) else: log.Waring("load fund false", taskobj, type(old), type(ret)) log.Info("spiker fund all", len(ls)) if len(ls) > 0: htmobj = html.CHtml("你的韭菜日报:") for v in ls: line = "基金:%s(%s) 变化"%(v["name"],v["code"]) htmobj.AddLine(line) htmobj.AddTable(v["data"], v["head"]) html_text = htmobj.GetHtml() mailobj = global_obj.get_obj("mail") message = mailobj.HtmlMailMessage() if message.SendMessage("韭菜研报", html_text): log.Info("send jiucai mail done") else: log.Info("基金信息没有变化")
def set_label(self): db_conf = global_obj.get_obj("Config")["db"] ui = self.ui_obj ui.addrEdit.setText(db_conf["addr"]) ui.portEdit.setText(db_conf["port"]) ui.accountEdit.setText(db_conf["user"]) ui.passwordEdit.setText(db_conf["password"])
def init_logger(): config = global_obj.get_obj("config") file = None if "log" in config: file = config["log"] logger = CFileLog(file) global_obj.set_obj("logger", logger)
def init_mail(): config = global_obj.get_obj("config") mail_data = config["mail"] obj = CMailBox(mail_data["user"], mail_data["password"], mail_data["host"]) obj.SetSender(mail_data["user"]) for name in mail_data["to"]: obj.SetReceive(name) global_obj.set_obj("mail", obj)
def init_fund_task(): task_timer = global_obj.get_obj("task_timer") time1 = CTimeTrigger(CTimeTrigger.TDay, "23:40:00") taskobj1 = CTask("check_fund", time1, check_fund_status, run_type = CTask.TForever) task_timer.AddTask(taskobj1) time2 = CTimeTrigger(CTimeTrigger.TDay, "23:50:00") taskobj2 = CTask("update_all", time2, update_all_fund, run_type = CTask.TForever) task_timer.AddTask(taskobj2)
def test(): boby = """ <h3>Hi,all</h3> <p>附件为本次FM_自动化测试报告。</p> <p>请解压zip,并使用Firefox打开index.html查看本次自动化测试报告结果。</p> <p> <br><img src="cid:image1"></br> <br><img src="cid:image2"></br> </p> <p> """ mailobj = global_obj.get_obj("mail") message = mailobj.HtmlMailMessage() if message.SendMessage("这是标题", boby): print("发送成功")
def main(config_file): #读取配置 init_config(config_file) #初始化日志 logger.init_logger() #初始化db init_db() #建立索引 init_db_index() #初始化邮件对象 mail_box.init_mail() #初始化定时任务 task.init_task() #添加任务 myfund.init_fund_task() #开始执行任务 taskobj = global_obj.get_obj("task_timer") taskobj.RunForever()
def log_obj(): return global_obj.get_obj("logger")
def update_all_fund(taskobj): fund_list = fund_api.fund_all() l = len(fund_list) log.Info("update_all_fund", taskobj, l) thread_num = 5 args_list = split_args(thread_num,fund_list) result = thread_api.start_args(thread_spike_fund, args_list) all_ok = 0 all_error = 0 stock_total = {} top_fund = [] for data in result: all_ok+=data["ok"] all_error+=data["error"] tools.combine_dict(stock_total, data["data"]["stock"]) for code, v in data["data"]["yeild"].items(): top_fund.append((code, v)) top_stock = [ (k, v) for k,v in stock_total.items() ] top_stock = sorted(top_stock, key = lambda k:tofloat(k[1]), reverse = True) top_fund = sorted(top_fund, key = lambda d:tofloat(d[1]["now"]), reverse = True) def write_stock_list(): with open("stock_list.txt", "w") as fp: for v in top_stock: fp.write("%s %s\n"%(v[0],v[1])) #write_stock_list() top20stock = top_stock[:40] top20fund = top_fund[:20] tail20fund = list(reversed(top_fund[len(top_fund) - 20:])) def make_fund(fund_list): ls = [] for v in fund_list: code = v[0] data = v[1] d = data["history"] l = [data["name"], code, data["now"], d.get("month1",0), d.get("month3", 0), d.get("month6", 0) ,d.get("year1", 0) ] ls.append(l) return ls htmobj = html.CHtml("韭菜排行:") if len(top20stock) > 0: htmobj.AddLine("基金持仓top20股票") htmobj.AddTable(top20stock, head = ["股票名","基金持有数"]) head = ["基金名","代码","今日收益", "近1月收益", "近3月收益", "近6月收益", "近1年收益"] if len(top20fund) > 0: htmobj.AddLine("收益top20") htmobj.AddTable(make_fund(top20fund), head = head) if len(tail20fund) > 0: htmobj.AddLine("亏损top20") htmobj.AddTable(make_fund(tail20fund), head = head) html_text = htmobj.GetHtml() mailobj = global_obj.get_obj("mail") message = mailobj.HtmlMailMessage() if message.SendMessage("韭菜排行榜", html_text): log.Info("send jiucai mail done")
def init_db(): config = global_obj.get_obj("config") db_data = config["db"] obj = mongo_api.CMongodbManager(DB_NAME, db_data["addr"], db_data["port"], db_data["user"], db_data["password"]) global_obj.set_obj("dbobj", obj)
def init_db_index(): dbobj = global_obj.get_obj("dbobj") for v in COLLECTION.values(): ret = dbobj.CreateIndex(v[0], v[1])
def load_fund(code): dbobj = global_obj.get_obj("dbobj") col = dbobj.Collection(COLLECTION["fund"][0]) ret = col.find_one({"code": code}, {"_id": 0}) return ret
def save_fund(code, data): dbobj = global_obj.get_obj("dbobj") col = dbobj.Collection(COLLECTION["fund"][0]) col.update({"code": code}, data, upsert=True)
def onActionTrigged_action_modiy_config(self): config = global_obj.get_obj("Config") config.show_dialog()