def load_and_split_all_records(dataset_dir: Path, dataset_names, type_inference=False): global exception_record_list all_data = [] all_labels = [] for name in dataset_names: d = dataset_dir / name print(f'\nProcessing dataset: {d}') for type_dir in filter(Path.is_dir, d.iterdir()): print(f'\nPocessing type dir: {type_dir}') for record_dir in filter(Path.is_dir, type_dir.iterdir()): data, label = load_and_split_data(record_dir, inference=type_inference) if data is None and label is None: Log.e(f'Record has exception, check it out: {record_dir}') exception_record_list.append(record_dir) elif data is not None and label is None: Log.w(f'Record has no label, check it out: {record_dir}') else: print( f'Record data segment shape: {data.shape}, label shape: {label.shape}' ) if data.shape[1] != 6 and data.shape[2] != 6: Log.e(f'Record data shape not correct: {record_dir}') all_data.append(data) all_labels.append(label) all_data = np.concatenate(tuple(all_data), axis=0) all_labels = np.concatenate(tuple(all_labels), axis=0) return all_data, all_labels
def send_email(msg_type, **extra_var): """ 邮件通知 :param msg_type: email content type id. in EMAIL_MESSAGE dict. :param extra_var: format content var dict. :return: """ email_conf = Config.email_config if not Config.email_notice_enable: Log.v("未开启邮箱通知") else: data = EMAIL_MESSAGES[msg_type] try: sender = email_conf.from_email receiver = email_conf.notice_email_list subject = data["subject"] username = email_conf.username password = email_conf.password host = email_conf.email_gateway port = email_conf.email_port s = data["content"].format(**extra_var) msg = MIMEText(s, 'html', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = sender msg['To'] = ','.join(receiver) print(host, port) smtp = smtplib.SMTP(host) smtp.connect(host, port=port) smtp.login(username, password) smtp.send_message(msg) smtp.quit() Log.v("邮件发送成功") except Exception as e: Log.e("发送失败, 邮件配置有误, 请检查配置")
def run(self): if not self.login(): return p_status = self.query_passengers() if not p_status: return if not Config.auto_code_enable: Log.v("未开启自动打码功能, 不检测用户登录状态") Log.v("正在查询车次余票信息") count = 0 while True: if self.check_maintain(): Log.v("12306系统每天 23:00 - 6:00 之间 维护中, 程序暂时停止运行") maintain_time = self.delta_maintain_time() Log.v("{0}小时 {1}分钟 {2}秒之后重新启动".format( maintain_time.seconds // 3600, (maintain_time.seconds // 60) % 60, maintain_time.seconds % 3600 % 60)) time.sleep(self.delta_maintain_time().total_seconds()) if Config.auto_code_enable: status, msg = self.online_checker() Log.v(msg) if not status: Log.e("心跳登录失败,继续重试中,建议手动检查原因再尝试重启") self.online_checker_now() dates = DispatcherTool.query_travel_dates for query_date in dates: Log.v("查询第 {0} 次".format(count)) n = datetime.datetime.now() data = DispatcherTool.run(query_date) count += 1 self.submit_order(data) DispatcherTool.output_delta_time(n) if self.order_id: break if self.order_id: break Log.v("抢票成功,{notice}".format(notice="你已开启邮箱配置,稍后会收到邮件通知" if Config. email_notice_enable else "如需邮件通知请先配置")) Log.v("车票信息:") for order_ticket in self.order_tickets: print(order_ticket) # 抢票成功发邮件信息 send_email( 2, **{ "order_no": self.order_id, "ticket_info": "</br>".join([v.to_html() for v in self.order_tickets]) })
def generator_image(self): while True: data = self.getcaptcha() try: img = Image.open(BytesIO(data)) img.close() break except OSError: Log.e("获取验证码图片失败, 重试获取") continue return data
def run(self): while self.retry_login_time: l = NormalLogin() result, msg = l.login() if not result: Log.e("登录失败, 重试中") self.retry_login_time-=1 else: self.login_status = True break if not self.login_status: Log.e("登录失败, 可重启路由或等待一段时间重试")
def send_requests(session, urlmapping_obj, params=None, data=None, **kwargs): session.headers.update(urlmapping_obj.headers) if urlmapping_obj.method.lower() == 'post': session.headers.update({ "Content-Type": r'application/x-www-form-urlencoded; charset=UTF-8' }) else: session.headers.pop("Content-Type", None) try: Log.d("请求 url {url}".format(url=urlmapping_obj.url)) try: response = session.request( method=urlmapping_obj.method, url=urlmapping_obj.url, params=params, data=data, timeout=10, # allow_redirects=False, **kwargs) except requests.RequestException as e: Log.w(e) Log.w("请求{0}异常 ".format(urlmapping_obj.url)) raise ResponseError if params: Log.d("{url} Get 参数 {data}".format(url=urlmapping_obj.url, data=params)) if data: Log.d("{url} Post 参数 {data}".format(url=urlmapping_obj.url, data=data)) Log.d("返回response url {url}".format(url=response.url)) if response.status_code == requests.codes.ok: if 'xhtml+xml' in response.headers['Content-Type']: data = response.text root = ET.fromstring(data) result = {v.tag: v.text for v in root.getchildren()} return result if 'json' in response.headers['Content-Type']: result = response.json() Log.d("{url} 返回值 {result}".format(url=urlmapping_obj.url, result=result)) return result # other type return response.text else: Log.w(response.url) Log.w(response.status_code) Log.w("返回状态码有问题") except Exception as e: Log.e(e) return None
def filter_by_config(self): status = self.query() if not status: Log.e("未能获取乘客信息, 请重试") PassengerData.raw_data = self.passengers PassengerData.get_final_data() data = PassengerData.find_people_by_names(Config.basic_config.ticket_people_list) if len(data) == 0: Log.e("乘客信息未在账号中找到, 请检查") return False if len(data) != len(Config.basic_config.ticket_people_list): Log.w("乘客信息配置中包含错误, 已经滤除错误乘客") self.config_passengers = data return True
def verifyhandle_ruokuai(self): c = RClient() data = c.rk_create(self.generator_image()) if "Result" in data: trans = self.trans_captcha_results(','.join( [str(int(v) - 1) for v in data["Result"]])) self.results = trans return self.check(trans) else: if "Error" in data and data["Error"]: Log.e( "打码平台错误: {0}, 请登录打码平台查看-http://www.ruokuai.com/client/index?6726" .format(data["Error"])) return False, "若快打码平台错误"
def login(self): count = 0 while self.retry_login_time > count: login_instance = NormalLogin() Log.v("正在为您登录") status, msg = login_instance.login() if not status: count += 1 Log.e(msg) Log.v("登录失败, 重试{0}次".format(count)) continue else: Log.v("登录成功") break if self.retry_login_time <= count: Log.v("重试次数已经超过设置") return False return True
def load_sensor_data_and_labels_by_name(record_dir, type_name='accel', inference=False): print(f'\nProcessing record dir: {record_dir}') if type_name == 'accel': suffix = ACC_SUFFIX elif type_name == 'gyro': suffix = GYRO_SUFFIX elif type_name == 'magnet': suffix = MAGNET_SUFFIX else: raise ValueError(f'Unsupport type name: {type_name}') # Load data and split ts out data_file = record_dir / f'{record_dir.name}-{suffix}' if not data_file.exists(): Log.e(f'Record has no {type_name} data, do nothing') return None, None, None data_all = pd.read_csv(data_file) data_all = data_all[:-1] # Remove the last line, maybe one broken line data_ts, data = split_ts_and_data(data_all) # Fitler outliner print(f'Origing raw data shape: {data.shape}') exception_point_process(data.T[0], inplace=True) exception_point_process(data.T[1], inplace=True) exception_point_process(data.T[2], inplace=True) # Low pass data_lp = signal.filtfilt(FILTER_B, FILTER_A, data, axis=0) data = np.hstack((data, data_lp)) # To get the data labels label_file = record_dir / f'{record_dir.name}-{LABEL_SUFFIX}' if label_file.exists(): labels_ts = load_label_result(label_file) labels = label_convert_ts2index(labels_ts, data_ts) else: if inference: Log.i('Label file not exists, Using activity type name Infer.') activity_type_name = get_activity_type_name_by_file_name( record_dir.name) is_daily = activity_type_name in LABEL_DAILY is_other_sports = activity_type_name in LABEL_OTHER_SPORTS if is_daily or is_other_sports: if is_daily: activity_type_name_as = 'DailyActivity' else: activity_type_name_as = 'OtherSports' activity_type = LABEL_ITEMS_INDEX_DICT.get( activity_type_name_as) print( f'Treat {activity_type_name} as {activity_type_name_as}: {activity_type}' ) labels = [(activity_type, 0, data.shape[0])] else: labels = None else: Log.w( 'Label file not exists, Inference by activity type name disabled. Skipped!' ) labels = None return data_ts, data, labels