def sort_year(self): ret = MongoDBUtil.query({}, "fundList") fund_list = [] for item in ret: fund_list.append([item['name'], item['number']]) print("Get fund list:", len(fund_list)) fund_year = [] empty_count = 0 for fund in fund_list: data = MongoDBUtil.query({'number': fund[1]}, 'fundDetail') try: first_year = data[0]['earn'][1] first_year = eval(str(first_year)) print(fund[0], data[0]['earn'][1], tuple(first_year.keys())[0]) fund_year.append({ 'name': fund[0], 'number': fund[1], 'year': tuple(first_year.keys())[0] }) except Exception as e: empty_count = empty_count + 1 print("Empty ", fund[0], e) print("Empty count:", empty_count, len(fund_list)) fund_year.sort(key=lambda k: (k.get('year', 0))) print(fund_year) MongoDBUtil.update({"name": "sort_year"}, { 'name': "sort_year", 'data': fund_year }, "fundAnalyze")
def update_fund_worth(fund_list): for fund_number in fund_list: try: dates, values = Spider.get_fund_worth(str(fund_number)) MongoDBUtil.update({"number": fund_number}, {"number": fund_number, "dates": dates, "values": values}, "fundWorth") except Exception as e: print(e)
def add_worth_fund(): number = str(request.form["number"]) print("[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "]:", "fund number ", number) ret = MongoDBUtil.query({"number": str(number)}, "fundWorth") try: print(ret[0]) dates, values = ret[0]["dates"], ret[0]["values"] except Exception as e: print("No data") dates, values = Spider.get_fund_worth(str(number)) MongoDBUtil.update({"number": number}, {"number": number, "dates": dates, "values": values}, "fundWorth") return jsonify({"dates": dates, "values": values})
def repair_data(self, fund_list): count = 0 amount = len(fund_list) for fund in fund_list: count = count + 1 try: number = fund[1] situation_data = "empty" earn_data = Spider.get_history_earn(number) MongoDBUtil.update({"number": number}, { 'number': number, 'situation': situation_data, 'earn': earn_data }, "fundDetail") print("Repair %s:" % fund[0], count, amount) except Exception as e: print("Failed %s:" % fund[0], count, amount)
def get_fund_list(): url = "http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?t=1&lx=1&letter=&gsid=&text=&sort=zdf,desc" \ "&page=1,9999&feature=|&dt=1534241003763&atfc=&onlySale=0" res = requests.get(url, stream=True).text fund_list = re.split("datas:", res)[1] fund_list = re.split(",count:", fund_list)[0] fund_list = json.loads(fund_list) data = [] for fund in fund_list: data.append({"name": fund[1], "number": fund[0]}) print(data) for fund in data: MongoDBUtil.update({"number": fund["number"]}, fund, "fundList") return fund_list