def rk_create(self, im_string, timeout=60): """ im: 图片字节 im_type: 题目类型 """ params = { 'typeid': 6113, 'timeout': timeout, } params.update(self.base_params) files = {'image': ('a.jpg', im_string)} while True: try: r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers) # Log.v("使用若快进行验证码识别") data = r.json() break except requests.RequestException: Log.w("提交若快打码请求出现问题, 正在重试中...") continue Log.d(data) return data
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_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 send_captcha_requests(session, urlmapping_obj, params=None, data=None, **kwargs): """ xml data example: <HashMap> <result_message>验证码校验失败,信息为空</result_message> <result_code>8</result_code> </HashMap> format result data. """ session.headers.update(urlmapping_obj.headers) response = session.request(method=urlmapping_obj.method, url=urlmapping_obj.url, params=params, data=data, timeout=10, # allow_redirects=False, **kwargs) Log.d(urlmapping_obj.url) if response.status_code == requests.codes.ok: if 'xhtml+xml' in response.headers['Content-Type']: data = response.text root = ET.fromstring(data) message = root.find('result_message').text code = root.find('result_code').text return {"result_message": message, "result_code": code} elif 'json' in response.headers['Content-Type']: return response.json() else: Log.w(response.url) Log.w(response.status_code) raise ResponseError else: Log.w(response.url) Log.w(response.status_code) raise ResponseCodeError
def raw_data(self): while True: try: r = requests.get(STATION_URL) if r.status_code == requests.codes.ok: raw_data = city_re.findall(r.text) if raw_data: # remove empty line data. data = map(lambda x: x.strip(), raw_data[0].split("@")) Log.v("获取车站信息成功") return list(filter(lambda x: bool(x), data)) else: Log.w("无法读取车站信息,重试中") continue except requests.RequestException: Log.w("获取车站信息失败,重试获取中") continue
def _get_submit_token(self): html = send_requests(LOGIN_SESSION, self.URLS['getExtraInfo']) Log.v("获取token中....") result = re.findall(r"var globalRepeatSubmitToken = '(.*)'", html) ticket_passenger_info = re.findall(r'var ticketInfoForPassengerForm=(.*);', html) if result: self.token = result[0] if ticket_passenger_info: try: self.ticket_passenger_info = json.loads(ticket_passenger_info[0].replace("'", "\"")) except TypeError: Log.w("获取submit info失败") return False if self.token and self.ticket_passenger_info: Log.v("成功获取token与以及车次信息") return True else: return False
def get_captcha_image(session, urlmapping_obj, params=None, data=None, **kwargs): """ xml data example: <HashMap> <result_message>生成验证码成功</result_message> <result_code>0</result_code> <image>imagedata<image> </HashMap> format result data. """ session.headers.update(urlmapping_obj.headers) 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 response.status_code == requests.codes.ok: if 'xhtml+xml' in response.headers['Content-Type']: data = response.text root = ET.fromstring(data) message = root.find('result_message').text code = root.find('result_code').text image = root.find('image').text return {"result_message": message, "code": code, 'image': image} elif 'json' in response.headers['Content-Type']: return response.json() else: Log.w(response.url) Log.w(response.status_code) raise ResponseCodeError else: Log.w(response.url) Log.w(response.status_code) raise ResponseCodeError
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