def get_filtered_urls(self, task, response): doc = self.get_doc(response) urls = set() for link in self.get_links(doc): u = self.get_url_of_link(link, doc, task.url) u = util.url_para_filter(u, self.AUTO_FILTER_URL_PARAS) with self.url_time_dict_lock: if not re.findall(u'日', link.parent().siblings().text() ) and relative_time_pattern.findall( link.parent().siblings().text()): t_str = relative_time_pattern.findall( link.parent().siblings().text())[0] t_stamp = 0 if re.match(ur'\d+小時\d+分鐘', t_str): h_m = re.split(ur'小時|分鐘', t_str) t_stamp = util.get_now() - int(h_m[0]) * 3600 - int( h_m[1]) * 60 elif re.match(ur'\d+小時', t_str): t_stamp = util.get_now() - int( t_str.split(u'小時')[0]) * 3600 elif re.match(ur'\d+分鐘', t_str): t_stamp = util.get_now() - int( t_str.split(u'分鐘')[0]) * 60 self.url_time_dict[u] = t_stamp if u and self.task_filter(doc, u, task.url): urls.add(u)
def Refresh(self): if get_now() < self.end_time: remaining_time = self.end_time - get_now() for btn_idx in self.btn_indexes: self.buttons()[btn_idx].setText( self.old_names[btn_idx] + f"({math.ceil(remaining_time.total_seconds())} 秒后可以点击)") else: self.time.stop() for btn_idx in self.btn_indexes: btn = self.buttons()[btn_idx] btn.setText(self.old_names[btn_idx]) btn.setEnabled(True)
def need_show(self) -> bool: key = self.get_first_run_key() # 判断是否过期 if get_now() > parse_time(self.expire_at): return False # 判断是否满足版本需求 if self.show_only_before_version != "" and not version_less( now_version, self.show_only_before_version): return False # 根据显示类型判断 if self.show_type == NoticeShowType.ONCE: return is_first_run(key) elif self.show_type == NoticeShowType.DAILY: return is_daily_first_run(key) elif self.show_type == NoticeShowType.WEEKLY: return is_weekly_first_run(key) elif self.show_type == NoticeShowType.MONTHLY: return is_monthly_first_run(key) elif self.show_type == NoticeShowType.ALWAYS: return True elif self.show_type == NoticeShowType.DEPRECATED: return False else: return False
def main(settings_path): heater_poll = Poller(settings_path) while True: start = time.perf_counter() # check each loop for when we are in history current = util.get_now() if (heater_poll.last_current is not None and heater_poll.last_current['day'] != current['day']): logger.info('Entered another day in history.') util.write_log( heater_poll.daily_log_path, { 'date': heater_poll.last_current['formatted_date'], 'time_elapsed': util.format_seconds( heater_poll.time_elapsed) }) heater_poll.time_elapsed = 0 call = time.perf_counter() - start heater_poll.time_elapsed += (heater_poll.poll(current) + call) heater_poll.loop_count += heater_poll.time_to_wait heater_poll.last_current = current
def cb(first_run_data: FirstRunDB) -> bool: # 检查是否是首次运行 first_run = True if first_run_data.file_created: # 仅当文件已经存在时,才有可能不是首次运行 if first_run_type == FirstRunType.ONCE: first_run = False elif first_run_type == FirstRunType.DURATION: first_run = first_run_data.get_update_at( ) + duration < get_now() else: duration_func = duration_func_map[first_run_type] first_run = duration_func() != duration_func( first_run_data.get_update_at()) # 如果是,则更新缓存文件 if first_run: first_run_data.set_update_at() logger.debug( f"{first_run_type:7s} {first_run_data.prepare_env_and_get_db_filepath()} first_run={first_run}, data={first_run_data}" ) return first_run
def _get_timestamp_from_relative_time_str(self, t_str): with relative_time_lock: if relative_time_pattern.findall(t_str): hour = 0 if re.findall(ur'\d+(?=小時)', t_str): hour = int(re.findall(ur'\d+(?=小時)', t_str)[0]) minute = int(re.findall(ur'\d+(?=分前)', t_str)[0]) return util.get_now() - hour * 3600 - minute * 60 return 0
def download_file(url: str, download_dir=".cached/downloads", filename="", connect_timeout=10) -> str: download_dir = os.path.realpath(download_dir) filename = filename or os.path.basename(url) start_time = get_now() target_file_path = os.path.join(download_dir, filename) logger.info(f"开始下载 {url} 到 {target_file_path}") response = requests.get(url, stream=True, timeout=connect_timeout) if response.status_code != 200: raise Exception(f"下载失败,状态码 {response.status_code}") make_sure_dir_exists(download_dir) with open(target_file_path, "wb") as f: total_length_optional = response.headers.get("content-length") if total_length_optional is None: # no content length header f.write(response.content) else: dl = 0 total_length = int(total_length_optional) for data in response.iter_content(chunk_size=4096): # filter out keep-alive new lines if not data: continue f.write(data) dl += len(data) show_progress(filename, total_length, dl) end_time = get_now() logger.info(color("bold_yellow") + f"下载完成,耗时 {end_time - start_time}") return target_file_path
def normal_item_solver(self, item, task, response): doc = self.get_doc(response) title = util.get_filtered_title(doc, {'title'}, ur'\s*-\s*(香港文匯報|香港文匯網)') t = doc('span.date').text() t_stamp = util.get_timestamp_from_string(t) current_date = str( time.localtime().tm_year) + ('%02d' % time.localtime().tm_mon) + ( '%02d' % time.localtime().tm_mday) if t_stamp >= int(time.mktime(time.strptime(current_date, "%Y%m%d"))): t_stamp = util.get_now() category = '' if re.findall(cat_pattern, doc('span.current').text().encode('utf-8')): category = re.findall( cat_pattern, doc('span.current').text().encode('utf-8'))[-1] author = '' content = util.get_paragraphs_from_selector(doc, 'div[id=main-content] p') item.raw = doc.text() item.title = title item.t = t item.t_stamp = t_stamp item.fetched_at = task.fetched_at item.category = category item.author = author item.content = content item.url = task.url item.source = 'WenWei' item.task_no = self.BATCH_NUMBER for img in doc('.imgtxt img').items(): if img.attr('src') != '': media_u = img.attr('src') des = '' if img.attr('alt'): des = img.attr('alt') media = self.NewsItem.MediaItem(media_url=media_u, type='image', description=des, created_at=item.fetched_at) item.media_list.append(media) for a in doc('iframe').items(): if a.attr('src') and re.match(r'.*youtube\.com.+', a.attr('src')): media_u = a.attr('src') if re.match(r'//.+', media_u): media_u = 'http:' + media_u media = self.NewsItem.MediaItem(media_url=media_u, type='youtube', description='youtube', created_at=item.fetched_at) item.media_list.append(media)
def send_request(self, task): r = requests.get(task.url, timeout=self.RESPONSE_TIMEOUT_VALUE) with self.charset_lock: if self.charset_found == '': if charset_pattern.findall(r.text): self.charset_found = charset_pattern.findall(r.text)[0] else: self.charset_found = 'big5' r.encoding = self.charset_found task.fetched_at = util.get_now() return r
def __init__(self, args): self.log_cmd = args.log_cmd log_name = get_dirname_from_args(args) log_name += '_{}'.format(get_now()) self.log_path = args.log_path / log_name # os.makedirs(self.log_path, exist_ok=True) self.log_path.mkdir(exist_ok=True) self.tfboard = SummaryWriter(self.log_path) self.url = run_tensorboard(self.log_path) print("Running Tensorboard at {}".format(self.url))
def __init__(self, bid: Order, ask: Order): self.timestamp = get_now() # since epoch self.size = self._finalize(bid, ask) self.price = bid.price self.buyer_id = bid.sender_id self.seller_id = ask.sender_id # The actual bid and ask objects are used when # logging a trade (order.id and order.size are needed) # and when notifying subscribers self.bid = bid self.ask = ask
def update_image(self, wximg, info): ''' 更新图像 @param wximg: 待更新的wx.Image @param info: OSD信息,A list of InfoEntry实例 ''' img_size = (wximg.GetWidth(), wximg.GetHeight()) if self.user_size < (200, 200): # user hasn't change window size self.SetClientSizeWH(img_size[0], img_size[1]) # 按图像大小扩充窗口大小 cli_size = self.GetClientSizeTuple() # 保存窗口大小 dst_size = img_size # 保存目标bmp大小 else: # user has customized window size cli_size = self.GetClientSizeTuple() cli_scale = (float(cli_size[0]/WIDTH_SCALE_BASE), float(cli_size[1]/HEIGHT_SCALE_BASE) ) # 用户自定义大小在宽高方向的比例 if cli_scale[0] > cli_scale[1]: # 窗口是相对较扁的 scale = float(cli_size[1])/float(img_size[1]) # 以高度比例为准 else:# 窗口是相对较高的 scale = float(cli_size[0])/float(img_size[0]) # 以宽度比例为准 dst_size = (img_size[0]*scale, img_size[1]*scale) # 保存目标bmp大小 self.m_bitmap.SetSize(dst_size) # 把m_bitmap控件设置为目标bmp大小 self.m_bitmap.Center() # 控件居中 wxbmp = wx.BitmapFromImage(wximg.Rescale(dst_size[0],dst_size[1])) # 改变wxImage大小,转换成wx.Bitmap memory = wx.MemoryDC() memory.SelectObject(wxbmp) memory.SetFont( util.WXFONT ) if self.m_menuItem_osd.IsChecked(): text_left_top = (util.PADDING, util.PADDING) # 文字左上角位置 # draw info for index, item in enumerate(info): if item.type == util.InfoEntry.TYPE_LABEL:# 正常显示,颜色正常 memory.SetTextForeground( LABEL_TEXT_COLOR ) elif item.type == util.InfoEntry.TYPE_WARNING: # 警报显示,颜色为警报颜色 memory.SetTextForeground( WARNING_TEXT_COLOR ) pos = (text_left_top[0], text_left_top[1]+int(index*util.TEXTSIZE[1]*1.5) ) # 设置该条目的文字位置 memory.DrawText( str(item), pos[0], pos[1]) # draw time memory.SetTextForeground( LABEL_TEXT_COLOR ) pos = (dst_size[0] - util.PADDING - util.TIME_TEXT_WIDTH, text_left_top[1] ) memory.DrawText(util.get_now(), pos[0], pos[1]) self.dc.Blit(0, 0, cli_size[0], cli_size[1], memory, 0, 0)
def __init__(self, settings_path): self.settings_path = settings_path # load settings self.settings = settings_handler.load_settings(settings_path) # arrange paths self.program_path = self.settings['paths']['program'] self.examples_path = self.settings['paths']['examples'] self.daily_log_path = self.settings['paths']['daily_log'] # get intervals self.thermometer_poll = self.settings['poll_intervals']['temperature'] self.time_to_wait = self.settings['poll_intervals']['settings'] # parameters for UDP communication with thermometer self.UDP_IP = self.settings['configs']['UDP_IP'] self.UDP_port = self.settings['configs']['UDP_port'] self.thermometer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.thermometer.settimeout(1) self.thermometer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.temperature = self.settings['temperatures']['room'] # handle day change if (self.settings['log']['last_day_on'] != util.get_now()['formatted_date']): self.time_elapsed = 0 util.write_log( self.daily_log_path, { 'date': self.settings['log']['last_day_on'], 'time_elapsed': util.format_seconds(self.time_elapsed) }) self.time_elapsed = 0 else: time_elapsed_restore = datetime.datetime.strptime( self.settings['log'].get('time_elapsed', '0:00:00'), '%H:%M:%S') self.time_elapsed = round( datetime.timedelta( hours=time_elapsed_restore.hour, minutes=time_elapsed_restore.minute, seconds=time_elapsed_restore.second).total_seconds()) self.program_number = self.settings['mode']['program'] relay_settings = self.settings['relay'] self.heater_switch = Relay(relay_settings, self.settings_path) self.last_current = None self.loop_count = 0 self.count_start = time.perf_counter()
def send_request(self, task): task.fetched_at = util.get_now() if self.using_proxy: with proxy_turn_lock: proxies = {'http': self.ips[self.turn]} self.turn += 1 if self.ips.__len__() <= self.turn: self.turn = 0 r = requests.get(url=task.url, timeout=self.RESPONSE_TIMEOUT_VALUE, headers=headers, proxies=proxies) return r r = requests.get(task.url, timeout=self.RESPONSE_TIMEOUT_VALUE) return r
def get_recent_lesson(courses): schedule = get_today_schedule(courses) hour = get_now().hour up = (18 if hour < 16 else 21) if hour > 12 else 12 for item in schedule: lessonHour = int(course_times[item.startTime].split(":")[0]) print(lessonHour) if lessonHour >= hour and lessonHour < up: return item return None
def __init__(self, side, price, size): self.timestamp = get_now() # since epoch if side != self.BID and side != self.ASK: msg = '\n'.join([ f'Tried to initialize Order instance with illegal order side arg: "{side}".', f'Only "{self.BID}" or "{self.ASK}" allowed.' ]) raise ValueError(msg) self.side = side # "b" (bid) or "a" (ask) self._size = size self.price = price self.id = id(self) self.sender_id = ''
def send_request(self, task): task.fetched_at = util.get_now() r = requests.get(task.url, timeout=self.RESPONSE_TIMEOUT_VALUE) d = pq(r.text) if d('meta'): for meta in d('meta').items(): if charset_pattern.findall(meta.attr('content')): r.encoding = charset_pattern.findall( meta.attr('content'))[0] if r.encoding == 'big5': r.encoding = 'big5hkscs' return r r.encoding = 'big5hkscs' return r
def normal_item_solver(self, item, task, response): doc = self.get_doc(response) item.raw = doc.text() item.title = util.get_filtered_title(doc, {'h1'}) item.t = util.get_time_string_from_selectors(doc, {'div.post_time'}) item.t_stamp = util.get_now() item.fetched_at = task.feteched_at item.category = doc('div.post_cats a:last-child').text() item.author = doc('.single_author a').text() item.content = util.get_paragraphs_from_selector(doc, 'div.single_text p') item.url = task.url item.source = 'DMHK' item.task_no = self.BATCH_NUMBER
def set_disabled_duration(self, seconds: float, btn_indexes: List[int]): self.end_time = get_now() + datetime.timedelta(seconds=seconds) self.btn_indexes = btn_indexes self.old_names = {} for btn_idx in self.btn_indexes: btn = self.buttons()[btn_idx] self.old_names[btn_idx] = btn.text() btn.setEnabled(False) self.time = QTimer(self) self.time.setInterval(100) self.time.timeout.connect(self.Refresh) self.time.start() self.Refresh()
def normal_item_solver(self, item, task, response): response.encoding = 'utf-8' doc = self.get_doc(response) title = doc('h1').text() t = util.get_day_string(offset=self.OFFSET) t_stamp = util.get_now() category = '' author = '' content = util.get_paragraphs_from_selector(doc, '#content p') item.raw = doc.text() item.title = title item.t = t item.t_stamp = t_stamp item.fetched_at = task.fetched_at item.category = category item.author = author item.content = content item.url = task.url item.source = 'Locpg' item.task_no = self.BATCH_NUMBER
def add_notice( self, title, message, sender="风之凌殇", send_at: str = "", show_type=NoticeShowType.ONCE, open_url="", valid_duration: Optional[timedelta] = None, show_only_before_version="", ): send_at = send_at or format_now() valid_duration = valid_duration or timedelta(days=7) if show_type not in valid_notice_show_type: logger.error( f"无效的show_type={show_type},有效值为{valid_notice_show_type}") return for old_notice in self.notices: if old_notice.title == title and old_notice.message == message and old_notice.sender == sender: logger.error( f"发现内容完全一致的公告,请确定是否是误操作,若非误操作请去文本直接修改。\n{old_notice}") return notice = Notice() notice.title = title notice.message = message notice.sender = sender notice.send_at = send_at notice.show_type = show_type notice.open_url = open_url notice.expire_at = format_time(get_now() + valid_duration) notice.show_only_before_version = show_only_before_version self.notices.append(notice) logger.info(f"添加公告:{notice}")
def normal_item_solver(self, item, task, response): doc = self.get_doc(response) title = util.get_filtered_title(doc, {'h2'}) t = util.get_time_string_from_selectors(doc, {'#article_date'}) t_stamp = util.get_now() category = doc('#article_date +div a:last-child').text() author = '' content = util.get_paragraphs_from_selector(doc, '.wordsnap div') if content == '': content = util.get_paragraphs_from_selector(doc, '.wordsnap') item.raw = doc.text() item.title = title item.t = t item.t_stamp = t_stamp item.fetched_at = task.fetched_at item.category = category item.author = author item.content = content item.url = task.url item.source = 'AM730' item.task_no = self.BATCH_NUMBER
def f() -> float: return get_now().timestamp()
async def loop(self): stop = False stop_time = self.settings["intervals"]["stop_time"] program_now = None time_after_sleep = 0 time_elapsed = 0 # start loop logger.debug("Starting loop. Settings:\n{}".format(self.settings)) while not self.exit.is_set(): # initialize states action = False start = time.perf_counter() # update current time and values from settings_file current = util.get_now() prev_program_target_temperature = program_now program_now = self.update_program_target_temperature( prev_program_target_temperature, current, True ) last_settings = { k: v for k, v in self.settings.items() } if "program_target_temperature" not in last_settings.keys(): # to compute differences at the first run # when we don't have this key yet last_settings.update( {"program_target_temperature": None} ) # adds current target temperature from programs # because it's not an information I want to store # in the settings file self.settings.update( {"program_target_temperature": program_now} ) # then computes differences to send only what has changed self._load_settings() diff_settings = util.compute_differences( self.settings, last_settings ) # send stuff to RTDB if any(diff_settings.values()): payload = { k: v for k, v in self.settings.items() if k in diff_settings.keys() & self.send_to_app_keys } # send to firebase RTDB # TODO: add try/except self._send_to_firebase( "data/{}".format(self.device_id), payload ) # self.iottly_sdk.call_agent('send_message', payload) # log if day_changed day_changed = util.check_same_day( self.settings["last_day_on"], current["formatted_date"] ) if day_changed: self.custom_logger.save_daily_entry( self.settings["time_elapsed"], self.settings["last_day_on"] ) # create async tasks logger.debug("Asking temperature to thermometer...") request_temperatures = asyncio.create_task( self.thermometer.request_temperatures() ) if not self.settings["room_temperature"]: try: temperature = await request_temperatures # if no value for room_temperature and read from thermometer # fails, retry endlessly without taking any other action except ( ThermometerLocalException, ThermometerLocalTimeout, ThermometerDirectException ): self.settings["intervals"]["settings"] continue # stop for given time in settings_file when relay_state changes if diff_settings["relay_state"]: stop = current["datetime"] logger.debug("Stop at {}.".format(stop)) # but cancel stop if settings changes mode_keys = { "manual", "auto", "program", "desired_temp" } if any([diff_settings[k] for k in mode_keys]): stop = False # check if stop is expired if stop: stop_expired = util.stop_expired(current, stop, stop_time) # do stuff if there's no stop or if stop is expired if not stop or stop_expired: action_task = asyncio.create_task(_handle_on_and_off( current, self.relay, **{ k: v for k, v in self.settings.items() # unpacks only for params in func signature if k in _handle_on_and_off.__code__.co_varnames } )) # retrieve new_settings from UI and loop and write them to file logger.debug("Just before await of temp") try: # TODO: multiple sensors logic received_temperature = await request_temperatures logger.info( "Received temperature: {}".format(received_temperature) ) if ( received_temperature != self.settings["room_temperature"] ): self.new_settings.update( {"temperatures": {"room": received_temperature}} ) except ( ThermometerLocalException, ThermometerLocalTimeout ) as e: logger.warning( "Could not retrieve temperatures from themometer." ) self.iottly_sdk.send({"error": str(e)}) except ThermometerDirectException as e: self.iottly_sdk.send({"error": str(e)}) logger.debug("Just before await of action") action = await action_task logger.info("Relay state: {}".format(action)) # FINISHED ACTION: sleep then update settings # interval is X we need to sleep for # X - time spent until now # otherwise we will sleep for more than X time_to_sleep = ( self.settings["intervals"]["settings"] - (time.perf_counter() - start) ) time.sleep(max(0, time_to_sleep - time_after_sleep)) after_sleep = time.perf_counter() time_after_sleep = 0 # update elapsed time with heater on if action: self.time_since_start += time.perf_counter() - start logger.debug("time_since_start: {}".format(self.time_since_start)) time_elapsed = 0 time_to_add = int(self.time_since_start) self.time_since_start -= time_to_add # self.time_since_start goes "backwards" # so we need to reset it sometimes if not time_to_add: time_to_add = round(self.time_since_start) self.time_since_start = 0 if time_to_add: time_elapsed = util.increment_time_elapsed( self.settings, time_to_add ) logger.info("time_elapsed: {}".format(time_elapsed)) # update settings if day_changed: self.new_settings.update({ "log": { "time_elapsed": "0:00:00", "last_day_on": current["formatted_date"] } }) if time_elapsed: self.new_settings.update({ "log": { "time_elapsed": time_elapsed } }) if self.new_settings: # write only if there's a difference # (even if this is already managed by SettingsHandler) self.settings_handler.handler(self.new_settings) self.new_settings = {} time_after_sleep = time.perf_counter() - after_sleep # raise UnknownException('Exited main loop.') self.relay.off() self.relay.clean()
def main_work(self, event): worklist = self.worklist a = time.clock() # 5 Hz Tasks if (time.clock()-self.timer.last_time) > 1.0/5: self.timer.last_time=time.clock() if DISPLAY_XBEE_DATA in worklist: self.update_rcv_area() if USING_JOYSTICK in self.worklist: self.do_joy_control() # 发送MID=0x09时自动返回,不需重复发送。 if DISPLAY_UAVINFO in self.worklist and USING_JOYSTICK not in self.worklist: self.send_data_by_frame(MsgPrcs.pack_control(0, self.state_smart_direction)) if DISPLAY_UAVINFO in self.worklist: self.update_GUI_UAVinfo(self.UAVinfo.get()) # MAIN_TASK_FREQ Hz Tasks if USING_JOYSTICK in self.worklist: self.update_joy_status() if DISPLAY_VIDEO in worklist: srcimg = self.camcap.get_frame() wxbmp = util.cvimg_to_wxbmp(srcimg) wximg = wx.ImageFromBitmap(wxbmp) memvideo = wx.MemoryDC() memvideo.SelectObject(wxbmp) if self.m_menuItem_video_osd.IsChecked(): # draw OSD information on bitmap_video memvideo.SetTextForeground( wx.BLUE ) memvideo.SetFont( util.WXFONT ) pos = (srcimg.shape[1] - util.PADDING - util.TIME_TEXT_WIDTH, util.PADDING) memvideo.DrawText(util.get_now(), pos[0], pos[1]) # 设置缩放比例 memvideo.SetUserScale(float(srcimg.shape[1])/float(self.bitmap_video_size[0]), float(srcimg.shape[0])/float(self.bitmap_video_size[1]) ) self.dc_video.Blit(0, 0, self.bitmap_video_size[0], self.bitmap_video_size[1], memvideo, 0, 0) memvideo.SelectObject(wx.NullBitmap) if RECORD_VIDEO in worklist: self.mov_rec.save_frame(wxbmp) if DISPLAY_INDEPENDENT_VIDEO in worklist: self.video_window.update_image_with_info1(wximg, self.UAVinfo.get_information_in_InfoEntries()) # 结束图像传输需要先停止track if DISPLAY_TRACK_VIDEO in worklist: memtrack = wx.MemoryDC() # 显示原始图像 if self.display_track_state == DISPLAY_TRACK_STATE_RAW: #rstimg = self.get_adjusted_image(srcimg) rstimg = srcimg rstbmp = util.cvimg_to_wxbmp(rstimg) # 正在框选状态 elif self.display_track_state == DISPLAY_TRACK_STATE_SELECTION: assert self.frozen_frame is not None, 'Frozen frame is none.' rectimg = self.get_dragging_image(self.frozen_frame,self.drag_info.get_drag_data()) rstbmp = util.cvimg_to_wxbmp(rectimg) # 显示目标追踪结果 elif self.display_track_state == DISPLAY_TRACK_STATE_RESULT: track_mode = self.m_choice_track_mode.GetStringSelection() display_process = self.m_menuItem_track_display_process.IsChecked() if track_mode == 'template': method = METHOD.TEMPLATEMATCH elif track_mode == 'meanshift': method = METHOD.MEANSHIFT elif track_mode == 'gray-meanshift': method = METHOD.GRAYMEANSHIFT else: method = METHOD.OPTICALFLOW matchimg, center, res = self.objmatch.process(method, srcimg) if display_process: rstbmp = util.cvimg_to_wxbmp(res) else: rstbmp = util.cvimg_to_wxbmp(matchimg) # # 模板匹配模式 # if track_mode == 'template': # matchimg, center, res = self.objmatch.do_tpl_match(srcimg) # if display_process: # rstbmp = util.cvimg_to_wxbmp(res) # else: # rstbmp = util.cvimg_to_wxbmp(matchimg) # # 边缘检测-模板匹配模式 # elif track_mode == 'edge-tpl': # matchimg, center, edgeimg = self.objmatch.do_edge_match(srcimg, arg=self.edge_arg) # if display_process: # rstbmp = util.cvimg_to_wxbmp(edgeimg) # else: # rstbmp = util.cvimg_to_wxbmp(matchimg) # # MeanShift匹配模式 # elif track_mode == 'meanshift': # matchimg, center, prj_img = self.objmatch.do_meanshift(srcimg) # if display_process: # rstbmp = util.cvimg_to_wxbmp(prj_img) # else: # rstbmp = util.cvimg_to_wxbmp(matchimg) # # 多目标MeanShift匹配模式 # elif track_mode == 'multi-meanshift': # matchimg, center, prj_img = self.objmatch.do_multi_meanshift(srcimg, arg=self.multimean_arg) # if display_process: # rstbmp = util.cvimg_to_wxbmp(prj_img) # else: # rstbmp = util.cvimg_to_wxbmp(matchimg) # elif track_mode == 'gray-meanshift': # matchimg, center, prj_img = self.objmatch.do_gray_meanshift(srcimg) # if display_process: # rstbmp = util.cvimg_to_wxbmp(prj_img) # else: # rstbmp = util.cvimg_to_wxbmp(matchimg) # elif track_mode == 'optical-flow': # matchimg, center, prj_img = self.objmatch.do_optical_flow(srcimg) # if display_process: # rstbmp = util.cvimg_to_wxbmp(prj_img) # else: # rstbmp = util.cvimg_to_wxbmp(matchimg) # # 混合匹配模式 # elif track_mode == 'mix': # matchimg, center, _ = self.objmatch.do_mix(srcimg, multimean_arg=self.multimean_arg, edgetpl_arg=self.edge_arg) # rstbmp = util.cvimg_to_wxbmp(matchimg) # 更新track bitmap 界面 memtrack.SelectObject(rstbmp) memtrack.SetUserScale(float(srcimg.shape[1])/float(self.bitmap_track_size[0]), float(srcimg.shape[0])/float(self.bitmap_track_size[1])) self.dc_track.Blit(0, 0, self.bitmap_track_size[0], self.bitmap_track_size[1], memtrack, 0, 0) a = time.clock() if TRACK_OBJECT in worklist: self.trackctrl.add_pt(center) self.trackctrl.update_h(self.UAVinfo.get()['height']) self.trackctrl.get_u() rstimg = self.objmatch.draw_circles(matchimg, self.trackctrl.pts[-1], color='GREEN', radius=10) rstbmp = util.cvimg_to_wxbmp(rstimg) memtrack.SelectObject(rstbmp) memtrack.SetUserScale(float(srcimg.shape[1])/float(self.bitmap_track_size[0]), float(srcimg.shape[0])/float(self.bitmap_track_size[1])) self.dc_track.Blit(0, 0, self.bitmap_track_size[0], self.bitmap_track_size[1], memtrack, 0, 0) n = time.clock() # print('[work time]%4.4f [cir time]%4.4f'%((n-a)*1000,(n-self.lasttime)*1000)) self.lasttime = n
def newNotAmsActInfo(dtBeginTime: str, dtEndTime: str, sActivityName: str) -> AmsActInfo: info = AmsActInfo() info.iActivityId = "000000" info.sActivityName = sActivityName info.dtBeginTime = dtBeginTime info.dtEndTime = dtEndTime return info not_know_start_time__ = "2000-01-01 00:00:00" # 不知道时间的统一把时间设定为后年年初-。- not_know_end_time____ = format_time(get_now().replace(year=get_now().year + 2, month=1, day=1, hour=0, second=0, microsecond=0)) _msd, _med = start_and_end_date_of_a_month(get_now()) month_start_day______, month_end_day________ = format_time(_msd), format_time( _med) not_ams_activities = [ newNotAmsActInfo(not_know_start_time__, not_know_end_time____, "道聚城"), newNotAmsActInfo(not_know_start_time__, not_know_end_time____, "黑钻礼包"), newNotAmsActInfo(not_know_start_time__, not_know_end_time____, "腾讯游戏信用礼包"), newNotAmsActInfo(not_know_start_time__, not_know_end_time____, "心悦app"), newNotAmsActInfo("2022-01-20 00:00:00", "2022-02-28 23:59:59", "管家蚊子腿"), newNotAmsActInfo("2021-10-18 00:00:00", "2021-11-18 23:59:59", "qq视频蚊子腿"), newNotAmsActInfo("2021-01-20 00:00:00", "2022-02-20 23:59:59",
def show_current_valid_act_infos(self): acts: List[ActCommonInfo] = [] # others for not_ams_act in not_ams_activities: if is_act_expired(not_ams_act.dtEndTime): continue acts.append(not_ams_act.get_common_info()) # ams for attr_name, act_id in self.__dict__.items(): if not attr_name.startswith("iActivityId_"): continue # 部分电脑上可能会在这一步卡住,因此加一个标志项,允许不启用活动 if exists_flag_file("不查询活动.txt"): continue act = search_act(act_id) if act is None: continue if is_act_expired(act.dtEndTime): continue acts.append(act.get_common_info()) # ide for attr_name, act_id in self.__dict__.items(): if not attr_name.startswith("ide_iActivityId_"): continue # 部分电脑上可能会在这一步卡住,因此加一个标志项,允许不启用活动 if exists_flag_file("不查询活动.txt"): continue act = search_ide_act(act_id) if act is None: continue if is_act_expired(act.get_endtime()): continue acts.append(act.get_common_info(act_id)) acts.sort(key=lambda act: act.dtEndTime) heads = ["序号", "活动名称", "活动ID", "开始时间", "结束时间", "剩余时间"] colSizes = [4, 44, 10, 20, 20, 14] table = "" table += "\n" + tableify(heads, colSizes) for idx, act in enumerate(acts): line_color = "bold_green" if is_act_expired(act.dtEndTime): line_color = "bold_black" print_act_name = padLeftRight(act.sActivityName, colSizes[1], mode="left", need_truncate=True) remaining_times = parse_time(act.dtEndTime) - get_now() remaining_times = f"{remaining_times.days:3d} 天 {remaining_times.seconds // 3600} 小时" table += ("\n" + color(line_color) + tableify( [ idx + 1, print_act_name, act.iActivityId, act.dtBeginTime, act.dtEndTime, remaining_times ], colSizes, need_truncate=False, )) logger.info(table)
def send_request(self, task): r = requests.get(task.url, timeout=self.RESPONSE_TIMEOUT_VALUE) r.encoding = 'utf-8' task.fetched_at = util.get_now() return r
def test_get_now(): assert type(get_now()) is datetime.datetime
def normal_item_solver(self, item, task, response): doc = self.get_doc(response) title = util.get_filtered_title(doc, {'h1:not(.articleDate)'}) if title == '': title = doc('.bigtitlelink').text() if title == '': title = doc('font[size="5"]').text() t = util.get_day_string(offset=self.OFFSET) t_stamp = util.get_day_stamp(self.OFFSET) if t_stamp >= util.get_day_stamp(0): t_stamp = util.get_now() category = '' if cat_pattern.findall(task.url): cat_word = cat_pattern.findall(task.url)[0] category = doc('.' + cat_word).text() if category == '': if re.findall(cat_pattern_2, task.url): cat = re.findall(cat_pattern_2, task.url)[0] if cat in cat_dict: category = cat_dict[cat] author = '' content = util.get_paragraphs_from_selector( doc, '.leadin p') + util.get_paragraphs_from_selector( doc, '#contentCTN-right p,h3') if doc('.summaryPara'): content = util.get_paragraphs_from_selector( doc, '.summaryPara') + util.get_paragraphs_from_selector( doc, '.newsText p') if content == '': content = doc('tr p').text() if content == '': if doc('tr'): for tr in doc('tr').items(): for thd in tr('th,td').items(): content += u'{:<20}'.format(thd.text()) content += u'\n' item.raw = doc.text() item.title = title item.t = t item.t_stamp = t_stamp item.fetched_at = task.fetched_at item.category = category item.author = author item.content = content item.url = task.url item.source = 'OrientalDaily' item.task_no = self.BATCH_NUMBER for img in doc('.photo img').items(): if img.attr('src') != '': media_u = prefix + img.attr('src') des = '' if img.attr('alt'): des = img.attr('alt') media = self.NewsItem.MediaItem(media_url=media_u, type='image', description=des, created_at=item.fetched_at) item.media_list.append(media) for a in doc('iframe').items(): if a.attr('src') and re.match(r'.*youtube\.com.+', a.attr('src')): media_u = a.attr('src') if re.match(r'//.+', media_u): media_u = 'http:' + media_u media = self.NewsItem.MediaItem(media_url=media_u, type='youtube', description='youtube', created_at=item.fetched_at) item.media_list.append(media)
def main_work(self, event): worklist = self.worklist a = time.clock() # MAIN_TASK_FREQ Hz Tasks if USING_JOYSTICK in self.worklist: self.update_joy_status() if DISPLAY_VIDEO in worklist: srcimg = self.camcap.get_frame() if len(self.cambuf)==0: self.cambuf.append(srcimg) self.cambuf.append(srcimg) # self.cambuf.append(srcimg) # print('cambuf length:%d'%len(self.cambuf)) wxbmp = util.cvimg_to_wxbmp(srcimg) wximg = wx.ImageFromBitmap(wxbmp) memvideo = wx.MemoryDC() memvideo.SelectObject(wxbmp) if self.m_menuItem_video_osd.IsChecked(): # draw OSD information on bitmap_video memvideo.SetTextForeground( wx.BLUE ) memvideo.SetFont( util.WXFONT ) pos = (srcimg.shape[1] - util.PADDING - util.TIME_TEXT_WIDTH, util.PADDING) memvideo.DrawText(util.get_now(), pos[0], pos[1]) # 设置缩放比例 memvideo.SetUserScale(float(srcimg.shape[1])/float(self.bitmap_video_size[0]), float(srcimg.shape[0])/float(self.bitmap_video_size[1]) ) self.dc_video.Blit(0, 0, self.bitmap_video_size[0], self.bitmap_video_size[1], memvideo, 0, 0) memvideo.SelectObject(wx.NullBitmap) if RECORD_VIDEO in worklist: self.mov_rec.save_frame(wxbmp) if DISPLAY_INDEPENDENT_VIDEO in worklist: self.video_window.update_image(wximg, self.UAVinfo.get_information_in_InfoEntries()) # 结束图像传输需要先停止track if DISPLAY_TRACK_VIDEO in worklist: memtrack = wx.MemoryDC() #图像滤波 self.cambuf.append(srcimg) self.cambuf.pop(0) a=time.clock() srcimg = img_filter(self.cambuf) # print '%.6f'%((time.clock()-a)*1000) # self.cambuf.append(srcimg) # self.cambuf.pop(0) # 显示原始图像 if self.display_track_state == DISPLAY_TRACK_STATE_RAW: #rstimg = self.get_adjusted_image(srcimg) rstimg = srcimg rstbmp = util.cvimg_to_wxbmp(rstimg) # 正在框选状态 elif self.display_track_state == DISPLAY_TRACK_STATE_SELECTION: assert self.frozen_frame is not None, 'Frozen frame is none.' rectimg = self.get_dragging_image(self.frozen_frame,self.drag_info.get_drag_data()) rstbmp = util.cvimg_to_wxbmp(rectimg) # 显示目标追踪结果 elif self.display_track_state == DISPLAY_TRACK_STATE_RESULT: track_mode = self.m_choice_track_mode.GetStringSelection() display_process = self.m_menuItem_track_display_process.IsChecked() if track_mode == 'template': method = METHOD.TEMPLATEMATCH elif track_mode == 'meanshift': method = METHOD.MEANSHIFT elif track_mode == 'gray-meanshift': method = METHOD.GRAYMEANSHIFT else: method = METHOD.OPTICALFLOW matchimg, center, res = self.objmatch.process(method, srcimg) if display_process: rstbmp = util.cvimg_to_wxbmp(res) tmpimg = res else: rstbmp = util.cvimg_to_wxbmp(matchimg) tmpimg = matchimg if TRACK_OBJECT in worklist: self.trackctrl.add_pt(center) # TODO:MeanShift-OpticalFlow 卡尔曼 # 更新track bitmap 界面 memtrack.SelectObject(rstbmp) memtrack.SetUserScale(float(srcimg.shape[1])/float(self.bitmap_track_size[0]), float(srcimg.shape[0])/float(self.bitmap_track_size[1])) self.dc_track.Blit(0, 0, self.bitmap_track_size[0], self.bitmap_track_size[1], memtrack, 0, 0) a = time.clock() # 5 Hz Tasks if (time.clock()-self.timer.last_time) > (1.0/TASK_LOW_FREQ): self.timer.last_time=time.clock() if DISPLAY_XBEE_DATA in worklist: self.update_rcv_area() if USING_JOYSTICK in self.worklist: self.do_joy_control() # 发送MID=0x09时自动返回,不需重复发送。 elif DISPLAY_UAVINFO in self.worklist: self.send_data_by_frame(MsgPrcs.pack_control(0, self.state_smart_direction)) if DISPLAY_UAVINFO in self.worklist: self.update_GUI_UAVinfo(self.UAVinfo.get(-2)) if TRACK_OBJECT in worklist: now_height = self.UAVinfo.get().height self.trackctrl.update_h(3 if math.isnan(now_height) else now_height) nt = self.UAVinfo.get(-2).uavtime du = self.trackctrl.get_u(nt, self.camera_pt_pitch) rstimg = self.objmatch.draw_circles(tmpimg, self.trackctrl.pts[-1], color='GREEN', radius=10) rstbmp = util.cvimg_to_wxbmp(rstimg) memtrack.SelectObject(rstbmp) memtrack.SetUserScale(float(srcimg.shape[1])/float(self.bitmap_track_size[0]), float(srcimg.shape[0])/float(self.bitmap_track_size[1])) self.dc_track.Blit(0, 0, self.bitmap_track_size[0], self.bitmap_track_size[1], memtrack, 0, 0) self.send_ref(du) n = time.clock() # print('[work time]%4.4f [cir time]%4.4f'%((n-a)*1000,(n-self.lasttime)*1000)) self.lasttime = n
def send_request(self, task): r = requests.get(task.url, headers=headers, timeout=self.RESPONSE_TIMEOUT_VALUE) task.fetched_at = util.get_now() return r