def encode_uri(uri, verbose=True): expt = None tokens = uri.strip('/').split('/') if len(tokens) > encode_uri_size: tokens[encode_uri_size - 1] = '/'.join(tokens[encode_uri_size - 1:]) tokens = tokens[:encode_uri_size] expt = Exception('encode_uri error: parts of uri: %s large than %d' % (uri, encode_uri_size)) if verbose: if expt: util.print_out('encode_uri: %s' % tokens, 1) print(expt) # time.sleep(1) else: util.print_out('encode_uri: %s' % tokens) for i in range(len(tokens)): tokens[i] = util.str_to_float(tokens[i]) if len(tokens) <= encode_uri_size: tokens += [0.0] * (encode_uri_size - len(tokens)) return tokens
def encode_host(host, verbose=True): i = host.find(':') if i != -1: host = host[:i] tokens = host.split('.') if util.str_is_ip(host): tokens[0] = float(tokens[0]) tokens[1] = float(tokens[1]) tokens[2] = float(tokens[2]) tokens[3] = float(tokens[3]) tokens += [0.0] * encode_host_size if verbose: print('encode_host: %s' % tokens) return tokens tokens = tokens[::-1] if len(tokens[0]) > 2: tokens.insert(0, '') if verbose: print('encode_host: %s' % ([0.0] * 4 + tokens)) for i in range(len(tokens)): tokens[i] = util.str_to_float(tokens[i]) if len(tokens) <= encode_host_size: tokens += [0.0] * (encode_host_size - len(tokens)) else: raise Exception('encode_host error: parts of host: %s large than %d' % (host, encode_host_size)) return [0.0] * 4 + tokens
def run_search(condition=None): search_words = request.args.get('search') #web_index is the index of the website to begin or continue searching web_index = 0 if request.args.get('web_index') == None else int( request.args.get('web_index')) start_time = time.time() continue_searching = True results = [] message = "" #time the result to make sure it makes heroku's 30 second timeout #stop searching if we have 10 or more results, run out of time, or finish searching relevant websites #check if web_index is less than 20 to ensure the while loop ends while continue_searching and web_index < 10 and time.time( ) - start_time < 20 and len(results) < 10: continue_searching, new_message, new_results = backend.search_a_website( search_words, condition, web_index) results.extend(new_results) message = message + new_message web_index += 1 #if we ran out of time and got few results, continue the the search where we left off in a redirect if continue_searching == True and len(results) < 4 and web_index < 20: return redirect("/results/%s/?web_index=%s&search=%s" % (condition, web_index, search_words)) results = util.sort_by_price(results) median = util.price_prettify(util.median_price(results)) for item in results: item.price = util.price_prettify(util.str_to_float(item.price)) return render_template('result_page.html', search_words=search_words, result=results, median=median, message=message, condition=condition)
def wp(wp_no, qty, table_awards, table_main): result, feedback = -(qty["W"] + qty["P"]), 0 w_awards = table_awards[1][1] p_awards = table_awards[2][1] w_flag, p_flag = False, False # win for i, pair in enumerate(w_awards): if wp_no == int(pair[0]): w_flag = True result += util.str_to_float(pair[1]) * qty["W"] / 10 # position for i, pair in enumerate(p_awards): if wp_no == int(pair[0]): p_flag = True result += util.str_to_float(pair[1]) * qty["P"] / 10 if not w_flag and not p_flag: feedback += qty["W"] / 10 + qty["P"] / 10 return result, feedback, int(w_flag), int(p_flag)
def _download_one_day(self, day): """輸入一個date或datetime物件,下載物件指定的日期的資訊。 """ if self.isHoliday(day): return {'_id': self.date_to_datetime(day), 'TradingDay': False} ts = self.timestamp() self.js = js = proxy.get( self.url.format(day.year, day.month, day.day, ts)).json() item = None for k in js.keys(): if k.startswith('fields') and js[k][0] == '證券代號': item = js['data' + k.replace('fields', '')] if not item: return {'_id': self.date_to_datetime(day), 'TradingDay': False} data = dict() data['_id'] = self.date_to_datetime(day) data['TradingDay'] = True for row in item: if not row[0].isdigit(): continue item_name = row[0] data[item_name + '_成交股數'] = str_to_int(row[2]) data[item_name + '_成交筆數'] = str_to_int(row[3]) data[item_name + '_成交金額'] = str_to_int(row[4]) if data[item_name + '_成交筆數'] == 0: data[item_name + '_平均每筆金額'] = 0.0 data[item_name + '_平均每筆股數'] = 0.0 else: n_trans = float(data[item_name + '_成交筆數']) data[item_name + '_平均每筆金額'] = data[item_name + '_成交金額'] / n_trans data[item_name + '_平均每筆股數'] = data[item_name + '_成交股數'] / n_trans data[item_name + '_開盤價'] = str_to_float(row[5]) data[item_name + '_最高價'] = str_to_float(row[6]) data[item_name + '_最低價'] = str_to_float(row[7]) data[item_name + '_收盤價'] = str_to_float(row[8]) return data
def big(wp_no, big_no, qty, table_awards, table_main): result, feedback = -(qty["Q"] + qty["PQ"]), 0 bet = set([str(wp_no), str(big_no)]) q_awards = table_awards[3][1] pq_awards = table_awards[4][1] q_flag, pq_flag = False, False # queue for i, pair in enumerate(q_awards): q = set(pair[0].split(',')) if bet == q: q_flag = True result += util.str_to_float(pair[1]) * qty["Q"] / 10 # position queue for i, pair in enumerate(pq_awards): pq = set(pair[0].split(',')) if bet == pq: pq_flag = True result += util.str_to_float(pair[1]) * qty["PQ"] / 10 if not q_flag and not pq_flag: feedback += qty["Q"] / 10 + qty["PQ"] / 10 return result, feedback, int(q_flag), int(pq_flag)
def _download_one_day(self, day): """輸入一個date或datetime物件,下載物件指定的日期的資訊。 """ date_key = '{:04d}{:02d}'.format(day.year, day.month) if date_key in self.cache: js = self.cache[date_key] else: print('cache miss', date_key) ts = self.timestamp() self.cache[date_key] = js = proxy.get( self.url.format(day.year, day.month, 1, ts)).json() if 'data' not in js: return {'_id': self.date_to_datetime(day), 'TradingDay': False} found = [ item for item in js['data'] if item[0] == '{:3d}/{:02d}/{:02d}'.format( day.year - 1911, day.month, day.day) ] if not found: return {'_id': self.date_to_datetime(day), 'TradingDay': False} item = found[0] data = dict() data['_id'] = self.date_to_datetime(day) data['TradingDay'] = True data['成交股數'] = str_to_int(item[1]) data['成交金額'] = str_to_int(item[2]) data['成交筆數'] = str_to_int(item[3]) data['發行量加權股價指數'] = str_to_float(item[4]) data['漲跌點數'] = str_to_float(item[5]) if data['成交筆數'] == 0: data['平均每筆金額'] = 0.0 data['平均每筆股數'] = 0.0 else: n_trans = float(data['成交筆數']) data['平均每筆金額'] = data['成交金額'] / n_trans data['平均每筆股數'] = data['成交股數'] / n_trans return data
def run_search(condition=None): search_words = request.args.get('search') #web_index is the index of the website to begin or continue searching # web_index=0 if request.args.get('web_index')==None else int(request.args.get('web_index')) # start_time=time.time() # continue_searching=True message = "" results = [] #time the result to make sure it makes heroku's 30 second timeout #stop searching if we have 10 or more results, run out of time, or finish searching relevant websites #check if web_index is less than 20 to ensure the while loop ends # while continue_searching and web_index<10 and time.time()-start_time< 20 and len(results)<10: # continue_searching, new_message, new_results= backend.search_a_website(search_words,condition, web_index) # results.extend(new_results) # message=message+new_message # web_index+=1 outs = backend.searchAllWebsites(search_words, condition) for r in outs: continue_searching, new_message, new_results = r[0] results.extend(new_results) message = message + new_message #if we ran out of time and got few results, continue the the search where we left off in a redirect # if continue_searching==True and len(results)<4 and web_index<20: # return redirect("/results/%s/?web_index=%s&search=%s" %(condition, web_index, search_words)) results = util.sort_by_price(results) global transfer_results transfer_results = results median = util.price_prettify(util.median_price(results)) for item in results: item.set_price(util.price_prettify(util.str_to_float( item.get_price()))) try: response = render_template('result_page.html', search_words=search_words, result=results, median=median, message=message, condition=condition) return response except Exception as e: print("Error was: ", e) return "here1"
def make_table(race_no, race_info, table_results, table_awards, table_racecard, bet_info): table = table_results # ----------------- # combine race info # ----------------- for i, row in enumerate(table): if i == 0: table[i].insert(0, "賽道") table[i].insert(0, "場地") table[i].insert(0, "分數範圍") table[i].insert(0, "長度") table[i].insert(0, "班次") table[i].insert(0, "場次") table[i].insert(0, "日期") else: tags = race_info["tag"].split(" - ") y, m, d = util.convert_date(bet_info["date"]) # 賽道 combined = "" if bet_info["place"] == "ST": combined += "田" else: combined += "谷" track = race_info["track"][5:] if track[0] == "草": combined += "草" combined += track.split("\"")[1] else: combined += "泥" table[i].insert(0, combined) # 場地 condition = race_info["cond"][7:] if condition == "好地": table[i].insert(0, "好") elif condition == "好地至快地": table[i].insert(0, "好至快") elif condition == "好地至黏地": table[i].insert(0, "好至黏") elif condition == "濕慢地": table[i].insert(0, "濕慢") else: table[i].insert(0, condition) # 分數範圍 if len(tags) > 2: table[i].insert(0, tags[2]) else: table[i].insert(0, '') # 長度 table[i].insert(0, tags[1]) # 班次 if tags[0][0] == "第": if len(tags[0]) == 3: table[i].insert(0, tags[0][1:2]) else: table[i].insert(0, tags[0][1:2] + "*") elif tags[0][0] == "國": table[i].insert(0, tags[0][2:]) elif tags[0][0] == "條": table[i].insert(0, "*") else: table[i].insert(0, tags[0]) # 場次 table[i].insert(0, race_no) # 日期 table[i].insert(0, "{}/{}/{}".format(y, m, d)) # ---------------- # combine hot info # ---------------- sort_arr = [] for row in table: if util.is_float(row[-1]): sort_arr.append(float(row[-1])) list.sort(sort_arr) hot_flag = False for i, row in enumerate(table): if i == 0: table[i].append("熱門") else: if util.is_float(row[-1]): if math.isclose(float(row[-1]), sort_arr[0], rel_tol=1e-9) and not hot_flag: # 1st hot table[i].append("1st Hot") hot_flag = True elif math.isclose(float(row[-1]), sort_arr[1], rel_tol=1e-9): # 2nd hot table[i].append("2nd Hot") else: table[i].append("-") else: table[i].append("-") # ---------------- # combine bet info # ---------------- have_bet = False thead = table[0] for i, bet in enumerate(bet_info["bet"]): if bet["id"] == race_no: have_bet = True for j, row in enumerate(table): # append bet for this row horse_number = row[thead.index("馬號")] if j == 0: table[j].append("投注") elif not util.is_int(horse_number): table[j].append("-") elif int(horse_number) == bet["WP"]: table[j].append("W P") # see which WP according to number of Bigs if len(bet["Big"]) > 1: for k in range(len(bet["Big"])): table[j][-1] += " Big{}(PQ)".format(k + 1) elif len(bet["Big"]) != 0: table[j][-1] += " Big(PQ)" elif int(horse_number) in bet["Big"]: # see which Big it is if len(bet["Big"]) != 1: for k in range(len(bet["Big"])): if int(horse_number) == bet["Big"][k]: table[j].append("Big{}(PQ)".format(k + 1)) else: table[j].append("Big(PQ)") else: table[j].append("-") if not have_bet: for j, row in enumerate(table): if j == 0: table[j].append("投注") else: table[j].append("-") # --------------------- # combine racecard info # --------------------- thead = table[0] col_horse_no = thead.index("馬號") for i, row in enumerate(table): if i == 0: table[i].append("皇牌") table[i].append("配備") table[i].append("馬齡") table[i].append("評分") table[i].append("評分+/-") else: # print(table_racecard[horse_number]) if row[1] != '' and util.is_int(row[col_horse_no]): horse_number = int(row[col_horse_no]) table[i].append(table_racecard[horse_number][-6]) # 優先參賽次序 table[i].append(table_racecard[horse_number][-5]) # 配備 else: table[i].append('-') table[i].append('-') horse_id = table[i][9].split('(')[1][:-1] table[i].append(get_age(link_horseinfo, horse_id)) # 馬齡 if row[1] != '' and util.is_int(row[col_horse_no]): horse_number = int(row[col_horse_no]) table[i].append(table_racecard[horse_number][10]) # 優先參賽次序 table[i].append(table_racecard[horse_number][11]) # 配備 else: table[i].append('-') table[i].append('-') # ------------------- # combine place & ddy # ------------------- for i, row in enumerate(table): if i == 0: table[i].append("地點") table[i].append("度地儀") else: if bet_info["place"] == "ST": table[i].append("沙田") else: table[i].append("跑馬地") table[i].append(bet_info["ddy"]) # ------------ # combine odds # ------------ for i, row in enumerate(table): if i == 0: table[i].append("P賠率") table[i].append("P賠率2") table[i].append("P賠率3") table[i].append("Queue賠率") table[i].append("PQ賠率") table[i].append("PQ賠率2") table[i].append("PQ賠率3") else: p_awards = table_awards[2][1] q_awards = table_awards[3][1] pq_awards = table_awards[4][1] # P1/2/3 for j in range(len(p_awards)): if p_awards[j][0] == table[i][col_horse_no]: table[i].append(util.str_to_float(p_awards[j][1])) else: table[i].append('') # Queue for j in range(len(q_awards)): horse_number = q_awards[j][0].split(',') if horse_number[0] == table[i][col_horse_no] or horse_number[ 1] == table[i][col_horse_no]: table[i].append(util.str_to_float(q_awards[j][1])) else: table[i].append('') # Pos-Queue for j in range(len(pq_awards)): horse_number = pq_awards[j][0].split(',') if horse_number[0] == table[i][col_horse_no] or horse_number[ 1] == table[i][col_horse_no]: table[i].append(util.str_to_float(pq_awards[j][1])) else: table[i].append('') return table
def make_table(race_no, bet_info, race_info, table_awards, table_main): # retrieve awards for easy usage w_awards = table_awards[1][1] p_awards = table_awards[2][1] q_awards = table_awards[3][1] pq_awards = table_awards[4][1] # -------------------------------- # add general info (first 3 lines) # -------------------------------- table = [] table.append(["({}){}".format(race_no, race_info["tag"])]) # win odds, queue odds, !1st hot performance table.append( [util.str_to_float(w_awards[0][1]), util.str_to_float(q_awards[0][1])]) # pos odds, !2nd hot performance for i, pair in enumerate(p_awards): if i == 0: continue table.append([util.str_to_float(p_awards[i][1])]) table[2].append("") # add hot performance H_W, H_Q, H_P, H_L = 0, 0, 0, 0 h_W, h_Q, h_P, h_L = 0, 0, 0, 0 L_W, L_Q, L_P, L_L = 0, 0, 0, 0 thead = table_main[0] for i, row in enumerate(table_main): if i == 0: continue hot = row[thead.index("熱門")] dist = row[thead.index("頭馬距離")] if hot == "1st Hot": if i == 1: H_W += 1 table[1].append("H W + {}".format( table_main[2][thead.index("頭馬距離")])) elif i == 2: H_Q += 1 table[1].append("H Q - {}".format(dist)) elif i == 3: H_P += 1 table[1].append("H P - {}".format(dist)) else: H_L += 1 table[1].append("H {} - {}".format(i, dist)) elif hot == "2nd Hot": if i == 1: h_W += 1 table[2].append("h W + {}".format( table_main[2][thead.index("頭馬距離")])) elif i == 2: h_Q += 1 table[2].append("h Q - {}".format(dist)) elif i == 3: h_P += 1 table[2].append("h P - {}".format(dist)) else: h_L += 1 table[2].append("h {} - {}".format(i, dist)) # ----------------------- # add bet & win/loss info # ----------------------- # init cnt variables total_W, total_P, total_Q, total_PQ = 0, 0, 0, 0 total_result, total_feedback = 0, 0 for bet in bet_info["bet"]: if bet["id"] == race_no: # WP wp_name = "" for i, row in enumerate(table_main): if i == 0: continue if util.is_int(row[thead.index("馬號")]) and bet["WP"] == int( row[thead.index("馬號")]): wp_name = row[thead.index("馬名")][:-6] dist = row[thead.index("頭馬距離")] # WP: append info row table.append([ "{}号 {}".format(bet["WP"], wp_name), '', "W {} P {}".format(bet_info["qty"]["W"], bet_info["qty"]["P"]) ]) # WP: append result row if i == 1: table.append([ '', '', "W + {}".format(table_main[2][thead.index("頭馬距離")]) ]) elif i <= 3: table.append(['', '', "Q - {}".format(dist)]) else: table.append(['', '', "{} - {}".format(i, dist)]) # WP: append win/loss row result, feedback, W, P = calc.wp(wp_no=bet["WP"], qty=bet_info["qty"], table_awards=table_awards, table_main=table_main) table.append(['', '', "${}".format(result)]) total_W += W total_P += P total_result += result total_feedback += feedback # Each Big for big in bet["Big"]: for i, row in enumerate(table_main): if i == 0: continue if util.is_int(row[thead.index("馬號")]) and big == int( row[thead.index("馬號")]): big_name = row[thead.index("馬名")][:-6] dist = row[thead.index("頭馬距離")] # Big: append info row table.append([ "{}号 {} + {}号 {}".format(bet["WP"], wp_name, big, big_name), '', "Q {} PQ {}".format(bet_info["qty"]["Q"], bet_info["qty"]["PQ"]) ]) # Big: append result row if i == 1: table.append([ '', '', "{}号 {} W + {}".format( big, big_name, table_main[2][thead.index("頭馬距離")]) ]) elif i <= 3: table.append([ '', '', "{}号 {} Q - {}".format(big, big_name, dist) ]) else: table.append([ '', '', "{}号 {} {} - {}".format( big, big_name, i, dist) ]) # Big: append win/loss row result, feedback, Q, PQ = calc.big( wp_no=bet["WP"], big_no=big, qty=bet_info["qty"], table_awards=table_awards, table_main=table_main) table.append(['', '', "${}".format(result)]) total_Q += Q total_PQ += PQ total_result += result total_feedback += feedback result_info = { 'result': total_result, 'feedback': total_feedback, 'W-P-Q-PQ': [total_W, total_P, total_Q, total_PQ], 'H': [H_W, H_Q, H_P, H_L], 'h': [h_W, h_Q, h_P, h_L], 'L': [L_W, L_Q, L_P, L_L], } return table, result_info
def make_table(race_no, race_info, table_results, table_awards, table_racecard, bet_info): table = table_results # ----------------- # combine race info # ----------------- for i, row in enumerate(table): if i == 0: table[i].insert(0, "賽道") table[i].insert(0, "場地") table[i].insert(0, "分數範圍") table[i].insert(0, "長度") table[i].insert(0, "班次") table[i].insert(0, "場次") table[i].insert(0, "日期") else: tags = race_info["tag"].split(" - ") y, m, d = util.convert_date(bet_info["date"]) table[i].insert(0, race_info["track"][5:]) table[i].insert(0, race_info["cond"][7:]) # 分數範圍 sometimes does not exist if len(tags) > 2: table[i].insert(0, tags[2]) else: table[i].insert(0, '') table[i].insert(0, tags[1]) table[i].insert(0, tags[0]) table[i].insert(0, race_no) table[i].insert(0, "{}/{}/{}".format(y, m, d)) # ---------------- # combine hot info # ---------------- sort_arr = [] for row in table: if util.is_float(row[-1]): sort_arr.append(float(row[-1])) list.sort(sort_arr) hot_flag = False for i, row in enumerate(table): if i == 0: table[i].append("熱門") else: if util.is_float(row[-1]): if math.isclose(float(row[-1]), sort_arr[0], rel_tol=1e-9) and not hot_flag: # 1st hot table[i].append("1st Hot") hot_flag = True elif math.isclose(float(row[-1]), sort_arr[1], rel_tol=1e-9): # 2nd hot table[i].append("2nd Hot") else: table[i].append("-") else: table[i].append("-") # ---------------- # combine bet info # ---------------- have_bet = False thead = table[0] for i, bet in enumerate(bet_info["bet"]): if bet["id"] == race_no: have_bet = True for j, row in enumerate(table): # append bet for this row horse_number = row[thead.index("馬號")] if j == 0: table[j].append("投注") elif not util.is_int(horse_number): table[j].append("-") elif int(horse_number) == bet["WP"]: table[j].append("W P") # see which WP according to number of Bigs if len(bet["Big"]) > 1: for k in range(len(bet["Big"])): table[j][-1] += " Big{}(PQ)".format(k+1) elif len(bet["Big"]) != 0: table[j][-1] += " Big(PQ)" elif int(horse_number) in bet["Big"]: # see which Big it is if len(bet["Big"]) != 1: for k in range(len(bet["Big"])): if int(horse_number) == bet["Big"][k]: table[j].append("Big{}(PQ)".format(k+1)) else: table[j].append("Big(PQ)") else: table[j].append("-") if not have_bet: for j, row in enumerate(table): if j == 0: table[j].append("投注") else: table[j].append("-") # --------------------- # combine racecard info # --------------------- thead = table[0] col_horse_no = thead.index("馬號") for i, row in enumerate(table): if i == 0: table[i].append("皇牌") table[i].append("配備") table[i].append("操練") else: if row[1] != '' and util.is_int(row[col_horse_no]): horse_number = int(row[col_horse_no]) table[i].append(table_racecard[horse_number][-6]) # 優先參賽次序 table[i].append(table_racecard[horse_number][-5]) # 配備 else: table[i].append('-') table[i].append('-') # TODO TODO TODO table[i].append('') # 操練 # ------------------- # combine place & ddy # ------------------- for i, row in enumerate(table): if i == 0: table[i].append("地點") table[i].append("度地儀") else: if bet_info["place"] == "ST": table[i].append("沙田") else: table[i].append("跑馬地") table[i].append(bet_info["ddy"]) # ------------ # combine odds # ------------ for i, row in enumerate(table): if i == 0: table[i].append("P賠率") table[i].append("P賠率2") table[i].append("P賠率3") table[i].append("Queue賠率") table[i].append("PQ賠率") table[i].append("PQ賠率2") table[i].append("PQ賠率3") else: p_awards = table_awards[2][1] q_awards = table_awards[3][1] pq_awards = table_awards[4][1] # P1/2/3 for j in range(len(p_awards)): if p_awards[j][0] == table[i][col_horse_no]: table[i].append(util.str_to_float(p_awards[j][1])) else: table[i].append('') # Queue for j in range(len(q_awards)): horse_number = q_awards[j][0].split(',') if horse_number[0] == table[i][col_horse_no] or horse_number[1] == table[i][col_horse_no]: table[i].append(util.str_to_float(q_awards[j][1])) else: table[i].append('') # Pos-Queue for j in range(len(pq_awards)): horse_number = pq_awards[j][0].split(',') if horse_number[0] == table[i][col_horse_no] or horse_number[1] == table[i][col_horse_no]: table[i].append(util.str_to_float(pq_awards[j][1])) else: table[i].append('') return table