def query_for_int(self, query, args=None, default=0): result = self.execute_fetchone(query, args) if not result: return default if self.dict_cursor: return get_int(result.popitem()[1], default) else: return get_int(result[0], default)
def gen_lotto_issue(): lotto_list = db_find_code_lotto({}) for lotto_info in lotto_list: db_gen_issue_table(lotto_info.get("lotto_id")) factory_list = db_find_lotto_factory({}) for params in factory_list: lotto_info = db_find_code_lotto({"lotto_id": params.get("lotto_id")}) if lotto_info: lotto_info = lotto_info[0] if get_int(lotto_info.get("status")) != 1: continue else: continue issue_obj = IssueFactory() issue_obj.init(params) d = date.today() for x in xrange(3): issue_list = issue_obj.gen_issue(d) if not issue_list: break d += timedelta(days=1) db_insert_issue_many(lotto_info.get("lotto_id"), issue_list)
def get(self): lotto_id = '' count = 10 ret = {"code": 0, "msg": "", "data": []} if self.request.arguments.has_key('lotto_id'): lotto_id = get_int(self.get_argument('lotto_id', '')) if self.request.arguments.has_key('count'): count = get_int(self.get_argument('count', '')) if not check_lotto(lotto_id): ret["code"] = 100 ret["msg"] = "找不到该彩票" else: ret["data"] = db_find_result(lotto_id, count) self.write(ret)
def init(self, params): self.lotto_id = get_int(params.get('lotto_id', '')) self.status = get_int(params.get('status', '')) self.count = get_int(params.get('count', '')) self.issue_interval = get_int(params.get('issue_interval', '')) self.iss_bit = get_int(params.get('iss_bit', '')) self.block_sec = get_int(params.get('block_sec', '')) self.start_time = get_string(params.get('start_time', '')) self.end_time = get_string(params.get('end_time', '')) self.issue_type = get_int(params.get('issue_type', '')) self.offset = get_int(params.get('offset', '')) self.extra_info = get_string(params.get('extra_info', ''))
def handle_response(response): if response.error: # print response.code warn(get_string(response.error)) pass else: try: result = loads(response.body) code = get_int(result.get("code"), -1) msg = get_string(result.get("msg")) if code == 0: info("==>%s", msg) else: warn("=>%s", msg) except Exception as e: import traceback print traceback.format_exc() print e
def run(self): interval = 5 if self.lotto_status != 1 or self.url_status != 1: return try: html = yield self.get_html() data_list = self.parser_data(html) data_list = filter_lotto_data(self.lotto_id, data_list) msg = "{0}=>".format(get_string(self.lotto_name)) if data_list: self.refresh_last_issue() self.save_data(data_list) crawler_last_issue = max(data_list, key=lambda x: x[0])[0] if self.last_issue.issue >= crawler_last_issue: msg += "已有最新期号:{}".format(self.last_issue.issue) else: msg += "更新最新:{}".format(crawler_last_issue) else: msg += "无数据,最后{}期".format(self.last_issue.issue) now = datetime.now() self.refresh_last_issue() if self.last_issue.issue: self.refresh_next_issue() left_second = get_int(self.next_issue.get_left_second(now)) if left_second > 0: interval = left_second msg += ";下一期{}期将在{}开奖".format(self.next_issue.issue, self.next_issue.result_time) msg += ";系统%s秒后采集" % interval info(msg) except Exception as e: print format_exc() print e finally: IOLoop.instance().add_timeout( timedelta( milliseconds=interval * 1000), partial( self.run))
def query_for_int(sql, args=None, default=0): with db_conn_guard() as conn: result = conn.execute_fetchone(sql, args) if not (result and isinstance(result, tuple) and len(result)): result = [default] return get_int(result[0], default)
def db_get_count_by_sql(self, sql, *args, **kwargs): result = self.db_get_by_sql(sql, *args, **kwargs) return get_int(result.get('count', 0)) if result else 0
def db_find_result(lotto_id, count): lotto_id = get_int(lotto_id) count = get_int(count) query_sql = "SELECT issue, draw_number FROM lotto_result_{} WHERE status=1 ORDER BY issue DESC LIMIT %s".format( lotto_id) return query_for_list(query_sql, count)
def set_lotto_id(self, lotto_id): self.lotto_id = get_int(lotto_id)
def set_lotto_status(self, status): self.lotto_status = get_int(status)
def check_lotto(lotto_id): return get_int(lotto_id) in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
def gen_issue(self, day_date): """ :param day_date: date datetime :return: list """ data = [] if self.issue_type == self.IssueType1: head_issue = get_date_string(day_date) index = get_datetime(head_issue + self.start_time) start_time = get_datetime(head_issue + self.end_time) - timedelta(days=1) for i in range(1, self.count + 1): issue_interval = timedelta(seconds=self.issue_interval) index += issue_interval end_time = index block_sec = timedelta(seconds=self.block_sec) lock_time = index - block_sec issue_format = "%s%0{0}d".format(self.iss_bit) issue = issue_format % (head_issue, i) data.append([ self.lotto_id, issue, get_time_string(start_time), get_time_string(lock_time), get_time_string(end_time), get_time_string(end_time)[:8], ]) start_time = end_time return data elif self.issue_type == self.IssueType2: head_issue = get_date_string(day_date) index = get_datetime(head_issue + self.start_time) start_time = get_datetime(head_issue + self.end_time) for i in range(1, self.count + 1): issue_interval = timedelta(seconds=self.issue_interval) index += issue_interval end_time = index block_sec = timedelta(seconds=self.block_sec) lock_time = index - block_sec issue_format = "%s%0{0}d".format(self.iss_bit) issue = issue_format % (head_issue, i) data.append([ self.lotto_id, issue, get_time_string(start_time), get_time_string(lock_time), get_time_string(end_time), get_time_string(end_time)[:8], ]) start_time = end_time return data elif self.issue_type == self.IssueType3: return data elif self.issue_type == self.IssueType4: extra_info = self.extra_info.split("|") if len(extra_info) != 2: return data point_issue = get_int(extra_info[0]) point_issue_time = extra_info[1] pre_time = get_datetime(point_issue_time) pre_issue = point_issue diff_day = (datetime.strptime(str(day_date), '%Y-%m-%d') - pre_time).days + 1 head_issue = get_date_string(day_date) index = get_datetime(head_issue + self.start_time) start_time = get_datetime(head_issue + self.end_time) - timedelta(days=1) for i in range(1, self.count + 1): issue_interval = timedelta(seconds=self.issue_interval) index += issue_interval end_time = index block_sec = timedelta(seconds=self.block_sec) lock_time = index - block_sec issue_format = "%d" issue = issue_format % (pre_issue + self.count * (diff_day - 1) + i) data.append([ self.lotto_id, issue, get_time_string(start_time), get_time_string(lock_time), get_time_string(end_time), get_time_string(end_time)[:8], ]) start_time = end_time return data elif self.issue_type == self.IssueType5: if CP.spring_first <= day_date < CP.spring_last: return data head_issue = get_date_string(day_date) start_time = None if CP.spring_last == day_date: t = get_date_string(CP.spring_first - timedelta(days=1)) start_time = get_datetime(t + self.end_time) else: t = get_date_string(day_date - timedelta(days=1)) start_time = get_datetime(t + self.end_time) lock_time = get_datetime(head_issue + self.end_time) - timedelta(minutes=10) end_time = get_datetime(head_issue + self.end_time) if CP.spring_last > day_date: issue = '%04d%03d' % ( day_date.year, (day_date - date(day_date.year, 1, 1)).days + 1) else: issue = '%04d%03d' % ( day_date.year, (day_date - date(day_date.year, 1, 1)).days - (CP.spring_last - CP.spring_first).days + 1) data.append([ self.lotto_id, issue, get_time_string(start_time), get_time_string(lock_time), get_time_string(end_time), get_time_string(end_time)[:8], ]) return data elif self.issue_type == self.IssueType6: if CP.spring_first <= day_date < CP.spring_last: return data d = datetime.combine(day_date, time(0, 0)) start_time = get_time_string(d) for index in range(1, 121): if index < 24: d += timedelta(minutes=5) elif 24 == index: d = datetime.combine(day_date, time(10, 0)) elif index < 97: d += timedelta(minutes=10) else: d += timedelta(minutes=5) end_time = get_time_string(d) if index < 24 or index >= 97: lock_time = get_time_string(d - timedelta(seconds=20)) else: lock_time = get_time_string(d - timedelta(seconds=25)) issue = get_date_string(day_date) + '%03d' % index data.append([ self.lotto_id, issue, start_time, lock_time, end_time, end_time[:8] ]) start_time = end_time return data elif self.issue_type == self.IssueType7: if CP.spring_first <= day_date < CP.spring_last: return data d = datetime.combine(day_date, time(0, 0)) - timedelta(minutes=8) start_time = get_time_string(d) for index in range(1, 98): if 14 == index: d = datetime.combine(day_date, time(10, 2)) else: d += timedelta(minutes=10) end_time = get_time_string(d) lock_time = get_time_string(d - timedelta(seconds=90)) issue = get_date_string(day_date) + '%03d' % index data.append([ self.lotto_id, issue, start_time, lock_time, end_time, end_time[:8] ]) start_time = end_time return data