def get_spots(spot_type, timeout=SUBPERIOD, max_tries=SUBPERIODS): """ Returns a list of the spot prices (calls .spot() on modules) """ if spot_type == "gold": spot_source_modules = gold.spot_source_modules elif spot_type == "bitcoin": spot_source_modules = bitcoin.spot_source_modules else: raise NotImplementedError("spot_type=" + str(spot_type) + "not implemented") tries = 0 while True: tries += 1 if tries > max_tries: break try: mw = len(spot_source_modules) with concurrent.futures.ThreadPoolExecutor( max_workers=mw) as executor: futs = executor.map(spot_optional, spot_source_modules, timeout=timeout) return list(futs) except Exception as ex: logger.exception(ex) time.sleep(1)
def main(): """ Main ts entry point. """ logger.warn("ts started.") time.sleep(1) ts_rt() ts_minutely() ts_hourly() ts_daily() while True: try: # schedule crash guard: importlib.reload(schedule) schedule.every(11).seconds.do(ts_rt) schedule.every(21).seconds.do(ts_minutely) schedule.every(701).seconds.do(ts_hourly) schedule.every(1701).seconds.do(ts_daily) while True: try: time.sleep(2) schedule.run_pending() except Exception as ex: logger.exception(ex) time.sleep(2) except Exception as ex: logger.exception(ex) time.sleep(2)
def ts_hourly(): logger.info("<ts_hourly>") now = datetime.datetime.now().replace(minute=0, second=0, microsecond=0) for key in rtdb_keys: try: val = rock.rocks("rtdb", read_only=True).get(key) fname = f"/data/tsdb/hourly/{key}.parq" if os.path.exists(fname): df = pd.read_parquet(fname) nvals = len(df) if type(val) == dict: nowpie = val nowpie["date"] = now else: nowpie = {"date": now, "value": val} df = df.append(nowpie, ignore_index=True) df.date = pd.to_datetime(df.date) df = df.dropna() df.drop_duplicates(subset=["date"], keep="first", inplace=True) if len(df) != nvals: racoon.to_parquet(df, fname) else: logger.info("Create: " + fname) if type(val) == dict: nowpie = {k: [v] for k, v in val.items()} nowpie["date"] = [now] else: nowpie = {"date": [now], "value": [val]} df = pd.DataFrame(nowpie) df.date = pd.to_datetime(df.date) racoon.to_parquet(df, fname) except Exception as ex: logger.info(f"ts_hourly.exception.{key}") logger.exception(ex) logger.info("</ts_hourly>")
def get_hot_search(self): """热搜 """ items = [] resp = None try: with request_session() as s: resp = s.get(HOT_SEARCH_URL) html = resp.text soup = BeautifulSoup(html) ul = soup.select('section.list > ul.list_a > li') if ul: for li in ul: a = li.find('a') if a: url = 'https://s.weibo.com{}'.format(a['href']) # 去除热度 em = a.select_one('span > em') if em: em.replaceWith('') title = a.find('span').text.strip() items.append({'title': title, 'url': url}) except: logger.exception('get hot search failed') return (items, resp)
def stage_files(file_locations, soap_envelope, ignore_errors=True): """Request scene bundles be moved to fastest cache available.""" try: logger.info('Start staging...') url = "https://dds.cr.usgs.gov/HSMServices/HSMServices?wsdl" headers = {'content-type': 'text/xml'} files = ''.join( ['<files>{}</files>'.format(x) for x in file_locations]) soap_envelope = soap_envelope.format(FILES_PLACE_HOLDER=files, WAIT="false") logger.debug('SOAP Envelope: %s', soap_envelope) request_object = urllib2.Request(url, soap_envelope, headers) response = urllib2.urlopen(request_object) html_string = response.read() logger.info('Stage response: %s', html_string) except Exception as e: if not ignore_errors: logger.exception('Error staging files!') raise else: logger.warning('Error staging files: %s. Continue anyway.', e) else: logger.info('Staging succeeded...')
async def __get_proxies_from_sslproxies(self, session): urls = [ 'https://www.sslproxies.org/', 'https://www.us-proxy.org/', 'https://free-proxy-list.net/', 'https://free-proxy-list.net/uk-proxy.html', 'https://free-proxy-list.net/anonymous-proxy.html' ] idx = 0 proxies = self.get_https_proxy() for url in urls: i = 5 while i > 0: await asyncio.sleep(3) try: if len(proxies) <= idx: idx = 0 res = await session.get(url, proxy='', timeout=10) html = HTML(await res.text()) addresses = html.xpath( '//*[@id="raw"]/div/div/div[2]/textarea/text()' )[0].split('\n')[3:] for adr in addresses: await self.put_proxy('http://' + adr, 'sslproxies') break except Exception: i -= 1 if idx + 1 > len(proxies): proxies = self.get_https_proxy() idx += 1 if (idx >= len(proxies)): idx == 0 logger.exception(f"Parse {url} Fail") await asyncio.sleep(1)
def main(): """ Main rsync entry point. """ logger.warn("leprechaun rsync started.") time.sleep(1) ts_rsync() sqlite_rsync() while True: try: # schedule crash guard: importlib.reload(schedule) schedule.every(1301).seconds.do(ts_rsync) schedule.every(300).seconds.do(sqlite_rsync) while True: try: time.sleep(2) schedule.run_pending() except Exception as ex: logger.exception(ex) time.sleep(2) except Exception as ex: logger.exception(ex) time.sleep(2)
def main(): """ Main entry to arrows. """ logger.warn("arrows started.") time.sleep(1) rt_arrow() minute_arrow() day_arrow() while True: try: # schedule crash guard. importlib.reload(schedule) schedule.every(10).seconds.do(rt_arrow) schedule.every(20).seconds.do(minute_arrow) schedule.every(120).seconds.do(day_arrow) while True: try: time.sleep(1) schedule.run_pending() except Exception as ex: logger.exception( ex, "Arrows schedule.run_pending exception handler:") time.sleep(2) except Exception as ex: logger.exception(ex, "Root arrows exception handler:") time.sleep(2)
def install_package(self, pkg_path, pkg_name='', overwrite=False): '''安装应用 ''' from util import InstallPackageFailedError from adb import TimeoutError if not os.path.exists(pkg_path): raise RuntimeError('安装包:%r 不存在' % pkg_path) for _ in range(3): self.adb.install_apk(pkg_path, overwrite) try: boot_time = self.get_system_boot_time() except TimeoutError: logger.exception('get_system_boot_time failed') return True if boot_time >= 60 * 5: return True logger.info('System Boot Time: %s S' % boot_time) time0 = time.time() if not pkg_name: pkg_name = self.adb._get_package_name(pkg_path) install_success = True while time.time() - time0 < 60: # 酷派大神F2和中兴红牛V5上发现重启后安装应用容易失败 dir_root_list = ['/data/data/%s' % pkg_name, '/sdcard/Android/data/%s' % pkg_name] for i in range(len(dir_root_list)): try: self.adb.list_dir(dir_root_list[i]) if len(dir_root_list) > 1: dir_root_list = [dir_root_list[i]] # 只保留正确的位置 break except RuntimeError, e: logger.warn(u'install app error: %r' % e) if i >= len(dir_root_list) - 1: install_success = False break if not install_success: break time.sleep(1) if install_success: return True
def __init__(self, url, method="GET", parameters=None, cookie=None, headers={}): try: # 解析url if type(url) == bytes: self.__url = url.decode("utf-8") if type(url) == str: self.__url = url logger.debug(self.__url) scheme, rest = urllib.parse.splittype(self.__url) # 拆分域名和路径 logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") self.__host_absolutely, self.__path = urllib.parse.splithost(rest) host_list = self.__host_absolutely.split(":") if len(host_list) == 1: self.__host = host_list[0] self.__port = 80 elif len(host_list) == 2: self.__host = host_list[0] self.__port = host_list[1] # 对所传参数进行处理 self.__method = method self.__data = parameters self.__cookie = cookie if parameters != None: self.__parameters_urlencode_deal = urllib.parse.urlencode(parameters) else: self.__parameters_urlencode_deal = "" self.__jdata = simplejson.dumps(parameters, ensure_ascii=False) self.__headers = headers except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def get_hot_search(self): """热搜 { "queryDisplay": "空军宣传片疑似出现轰 20 ", "realQuery": "轰20", "queryDescription": "暗示首飞在即?", "redirectLink": "", "type": 0, "status": 0, "isNew": false } """ items = [] resp = None try: with request_session() as s: resp = s.get(HOT_SEARCH_URL2) soup = BeautifulSoup(resp.text) script = soup.find( 'script', type='text/json', id='js-initialData') if script: obj = json.loads(script.string) items = obj['initialState']['topsearch']['data'] except: logger.exception('get hot search failed') return (items, resp)
def select_gakki(): all_dir = "img" gakki_dir = "gakki_img" not_gakki_dir = "not_gakki_img" no_face_dir = "no_face_detected" fr = FaceRec("know") clock = itertools.count() for file_path in filter_dir(all_dir): # type:pathlib.Path logging.info("recognizing {}: {}".format(next(clock), file_path)) ok_path = g_path(gakki_dir, file_path.name) fail_path = g_path(not_gakki_dir, file_path.name) no_face_path = g_path(no_face_dir, file_path.name) if ok_path.exists() or fail_path.exists() or no_face_path.exists(): # already compared continue continue try: result = fr.recon(file_path) if result is True: ok_path.symlink_to(file_path.absolute()) elif result == 2: no_face_path.symlink_to(file_path.absolute()) else: fail_path.symlink_to(file_path.absolute()) except Exception as e: logger.exception(e) continue
def get_hot_topic(self): """热门话题 """ items = [] resp = None try: with request_session() as s: resp = s.get(HOT_TOPIC_URL) html = resp.text soup = BeautifulSoup(html) ul = soup.select('section.list > ul.list_b > li') if ul: for li in ul: a = li.find('a') if a: url = 'https://s.weibo.com{}'.format(a['href']) title = a.select_one('article > h2').text detail = a.select_one('article > p').text if not detail: detail = '暂无数据' info = a.select_one('article > span').text if not info: info = '暂无数据' items.append({ 'title': title, 'url': url, 'detail': detail, 'info': info }) except: logger.exception('get hot topic failed') return (items, resp)
def __init__(self): try: current_dir = os.getcwd() self.__ini_config_file = BASE_DIR + "/conf/" + "config.ini" self.__conf_config_file = BASE_DIR + "/conf/" + "config.conf" except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def ini_data(self, label, key, configFile=None): try: config = configparser() if configFile is None: configFile = self.__ini_config_file config.read(configFile) return config.get(label, key) except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def _close_activity(self, activity): '''关闭Activity ''' try: result = self.send_command(EnumCommand.CmdCloseActivity, Activity=activity) return result['Result'] except AndroidSpyError, e: logger.exception('close_activity error') if '不存在' in str(e): return True raise e
def day_arrow(): try: logger.info("<day_arrow>") importlib.reload(spot_history) spot_history.day_arrow() spot_model.day_arrow() logger.info("</day_arrow>") except Exception as ex: logger.exception(ex, "Root day_arrow exception handler:") time.sleep(2)
def rt_arrow(): try: logger.info("<rt_arrow>") adb_data = rock.rocks("tsdbrocks", read_only=True).get("tsdb_data") adb_data["adb_timestamp"] = int(time.time()) rock.rocks("adbrocks").put("adb_data", adb_data) logger.info("</rt_arrow>") except Exception as ex: logger.exception(ex, "Root rt_arrow exception handler:") time.sleep(2)
def get_hot_topic(self): items = [] resp = None try: with request_session() as s: resp = s.get(HOT_URL) items.extend(resp.json()) except: logger.exception('get hot topic failed') return (items, resp)
def minute_arrow(): try: logger.info("<minute_arrow>") importlib.reload(spot_history) spot_history.minute_arrow() stats_history.minute_arrow() key_history.minute_arrow() logger.info("</minute_arrow>") except Exception as ex: logger.exception(ex, "Root minute_arrow exception handler:") time.sleep(2)
def md5(cls, strVar): try: # md5加密并小写化 md5_var = hashlib.md5() md5_var.update(strVar.encode("utf-8")) # md5_var.update(strVar) sign = md5_var.hexdigest().lower() return sign except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def runCallbacks(self, data): """ Runs each of the functions declared as callbacks (to run when the target triggers them) passing the data parameter as a single argument. :param data: any arbitrary data to pass to the callbacks. """ for callback in self.callbacks: try: callback(data) except Exception as e: logger.exception(e)
def get_trending_repository(self, since: Since = Since.daily): """热门仓库 {'href': href, 'url': url, 'description': desc, 'language': language, 'stars': stars, 'folks': folks, 'recent_stars': recent_stars} """ items = [] resp = None try: with request_session() as s: params = {'since': since.value} resp = s.get(TRENDING_REPOSITORY_URL, params=params) soup = BeautifulSoup(resp.text, features='html.parser') articles = soup.select('main article.Box-row') for article in articles: href = article.select_one('h1.h3 > a')['href'] url = 'https://github.com' + href logger.info('%s %s', since, href) # 项目描述 description = '无' desc = article.select_one('article > p') if desc: description = desc.text.strip() div = article.select_one('div.f6') spans = div.select('div > span') # 并非每个仓库都有语言标签 language = '无' if len(spans) == 3: language = spans[0].text.strip() # 最近关注数 recent_stars = spans[-1].text.strip() star_folk = div.select('a') stars = star_folk[0].text.strip() folks = star_folk[1].text.strip() item = { 'href': href, 'url': url, 'description': description, 'language': language, 'stars': stars, 'folks': folks, 'recent_stars': recent_stars } logger.debug('repo:%s', item) items.append(item) except: logger.exception('get trending repository failed') return (items, resp)
def get_trending_developer(self): """热门话题 """ items = [] resp = None try: with request_session() as s: resp = s.get(TRENDING_DEVELOPER_URL) soup = BeautifulSoup(resp.text) except: logger.exception('get trending developer failed') return (items, resp)
def runTriggers(self, context): """ Run all triggers with the watcher thread object they are run within passed as context. :param context: The watcher thread object running this batch. """ ret = [] for trigger in self.triggers: try: ret += trigger(context) except Exception as e: logger.exception(e) return ret
def request_with_cookies(self): try: cookiejar = http.CookieJar() cookiejar.set_cookie(self.__cookie) opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar)) if self.__data is None: request = urllib.request.Request(self.__url) else: request = urllib.request.Request(self.__url, self.__data) html = opener.open(request).read() return html except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def schedule(minutes, task, health_check_server): while True: try: tic = time.time() task() duration = time.time() - tic sleep_time = max(60 * minutes - int(duration), 1) logger.info("sleeping %d seconds" % sleep_time) time.sleep(max(sleep_time, 0)) health_check_server.observe_health(True) except (KeyboardInterrupt, SystemExit) as e: raise e except Exception as e: logger.exception(e) health_check_server.observe_health(False)
def hello(self): '''确认Server身份 ''' for _ in range(3): try: result = self.send_command(EnumCommand.CmdHello) except SocketError: time.sleep(1) continue except AndroidSpyError: logger.exception('init error') time.sleep(2) continue # self._enable_debug() return result['Result']
def oyjtDict(amount, orderDate): try: oyjt_data = dict() oyjt_data["orderID"] = "307900" oyjt_data["orderPoint"] = "3.104" oyjt_data["mallName"] = "欧亚卖场" oyjt_data["storeName"] = "欧亚卖场" oyjt_data["productName"] = "辉山小枕240ml" oyjt_data["quantity"] = "16" oyjt_data["unitPice"] = "1.94" oyjt_data["amount"] = amount oyjt_data["orderDate"] = orderDate return oyjt_data except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def conf_data(self, key, configFile=None): try: if configFile is None: configFile = self.__conf_config_file config_file = open(configFile, 'r') result = config_file.readlines() result_deal_list = [] key_value_list = [] for i in result: result_deal_list.append(i.strip('\n')) for j in result_deal_list: key_value_list.append(j.split('=')) result_dict = dict(key_value_list) return result_dict[key] except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def connect(connstr): """Open a new Oracle DB connection. Args: connstr (str): Oracle formatted connection string Returns: Connection: Oracle connection Example: >>> connection('schema/secret@host:port/database') <cx_Oracle.Connection to schema/secret@host:port/database> """ try: return cx_Oracle.connect(connstr) except Exception: logger.exception('Unable to connect to database!') raise
def get_district_code(): try: with open(DC_PATH) as file: data = file.read() district_list = data.split('\n') for node in district_list: # print node if node[10:11] != ' ': state = node[10:].strip() if node[10:11] == ' ' and node[12:13] != ' ': city = node[12:].strip() if node[10:11] == ' ' and node[12:13] == ' ': district = node[14:].strip() code = node[0:6] code_list.append({"state": state, "city": city, "district": district, "code": code}) except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def get_deck_db(from_backup=False): if from_backup: backup_db = _get_from_backup_db() for game_type, content in backup_db.to_json().items(): try: db.game_types.update_one({'type': game_type}, {'$set': {'data': {game_type: content}}}) except Exception as e: logger.exception(e) pass return backup_db raw_db = db.game_types.find() contents = {} for item in raw_db: game_type = item.get('type') contents[game_type] = item.get('data', {}).get(game_type) game_types = transform_deck_db_json(contents) return DeckDb(game_types)
def ytdycDict(cardno, datetime_str_register, salesamt, datetime_str_buy): try: ytdyc_data = dict() ytdyc_data["card_no"] = cardno ytdyc_data["vipcardno"] = "" ytdyc_data["jointdate"] = datetime_str_register ytdyc_data["openid"] = "" ytdyc_data["points_flag"] = "0" ytdyc_data["score_type"] = "1" ytdyc_data["score"] = "13" ytdyc_data["time"] = datetime_str_buy ytdyc_data["salesamt"] = salesamt ytdyc_data["storecode"] = "1FD151" ytdyc_data["storename"] = u"奢侈品眼镜" return ytdyc_data except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def refresh_media_store(self, file_path=''): '''刷新图库,显示新拷贝到手机的图片 ''' from adb import TimeoutError command = '' if not file_path: sdcard_path = self.get_external_sdcard_path() command = 'am broadcast -a android.intent.action.MEDIA_MOUNTED --ez read-only false -d file://%s' % sdcard_path else: if file_path.startswith('/sdcard/'): file_path = self.get_external_sdcard_path() + file_path[7:] command = 'am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://%s' % file_path try: self.adb.run_shell_cmd(command, True) except TimeoutError: logger.exception('refresh_media_store failed') return False # self.adb.run_shell_cmd('am broadcast -a android.intent.action.BOOT_COMPLETED') if file_path and self.sdk_version >= 16: return self._wait_for_media_available(file_path)
def main(): """ Main rt entry point. """ logger.warn("rt started") started = int(time.time()) while True: try: elapsed = int(time.time()) time.sleep(1) if int(time.time()) - started > 600: logger.info("heartbeat") started = int(time.time()) logger.info("<write_rt>") write_rt() logger.info("</write_rt>") elapsed = int(time.time()) - elapsed time.sleep(max(0, PERIOD - elapsed)) except Exception as ex: logger.exception(ex)
def hello(self): '''确认Server身份 ''' for _ in range(3): try: result = self.send_command(EnumCommand.CmdHello) except SocketError: time.sleep(1) continue except AndroidSpyError: logger.exception('init error') time.sleep(2) continue # self._enable_debug() if not 'Result' in result: raise RuntimeError('Server error') items = result['Result'].split(':') if len(items) > 0 and items[-1].isdigit(): if self._process['id'] and int(items[-1]) != self._process['id']: raise RuntimeError('Server pid not match %s' % result['Result']) return result['Result']
def get_hot_question(self): """热门问题 { "type": "hot_list_feed", "style_type": "1", "id": "0_1610377259.41369", "card_id": "Q_438830539", "feed_specific": { "answer_count": 1376 }, "target": { "title_area": { "text": "拼多多回应解约发帖员工,称「员工网上发布极端言论 ,违反员工手册」,公司是否有权利因网络言论开除员工?" }, "excerpt_area": { "text": "事件背景 1 月 11 日上午消息,针对员工王某(花名太虚)在网络平台发布不实视频讯息,自称「《因为看到同事被抬上救护车我被拼多多开除了》」一事,拼多多人力资源部发布的情况说明表示,王某被公司解约,并非因为其在匿名社区发布了「救护车照片」,而是公司事后调查发现王某多次在这一匿名社区发布带有显著恶意的「极端言论」,违反了员工手册中双方约定的行为规范。于是决定与其解约。 据悉 1 月 7 日,王 * 在公司楼下拍摄救护车照片并匿名将不恰当猜测发布至某匿名社区引发社会讨论。 在公司问询于事发拍摄地路过的同事时收到反馈,高度怀疑是王某在事实不清的情况下随意拍摄及匿名发布了有可能会对公司造成伤害的信息。公司 HR 和行政在主动问询王某时,他承认了是其本人进行了拍摄及发布。 拼多多公司根据王某所发帖的某匿名社区公开页面外显 ID(JgD+STsWV2E),公司查询到其既往匿名发帖内容充斥不良「极端言论」,诸如:「想要××死」,「把××的骨灰扬了」等。 公司人力资源部研判该员工上述言论严重违反员工手册,且有可能其极端情绪会对其他同事造成不可知威胁,决定与其解除劳动合同。 在公司与其解除劳动合同之后,王 * 及其周边人继续在网上发布不实言论,包括公司 HR 通过翻看其手机获悉其在某匿名社区发帖,以及公司 HR 威胁毁坏其档案,公司有 300 小时工时的「本分计算器」等,被各网络社区置顶推送,引发了新一轮网民关注。 公司人力资源部表示,上述均为不实讯息且之前已在相关渠道予以澄清。 以下为情况说明原文: 关于员工王 * 多次在某匿名社区发布「极端言论」被公司解约的情况说明 1 月 10 日,原为我司技术开发工程师的王 *(花名太虚)在网络平台发布不实视频讯息,自称「《因为看到同事被抬上救护车我被拼多多开除了》」。 真实情况为,王 * 被公司解约,不是因为其在匿名社区发布了「救护车照片」,而是公司事后调查发现王 * 多次在这一匿名社区发布带有显著恶意的「极端言论」,违反了员工手册中双方约定的行为规范。于是决定与其解约。 1 、事发当天情况: 1 月 7 日,王 * 在公司楼下拍摄救护车照片并匿名将不恰当猜测发布至某匿名社区引发社会讨论。 在公司问询于事发拍摄地路过的同事时收到反馈,高度怀疑是王 * 在事实不清的情况下随意拍摄及匿名发布了有可能会对公司造成伤害的信息。 公司 HR 和行政在主动问询王 * 时,他承认了是其本人进行了拍摄及发布。 根据王 * 所发帖的某匿名社区公开页面外显 ID(JgD+STsWV2E),公司查询到其既往匿名发帖内容充斥不良「极端言论」,诸如:「想要××死」,「把××的骨灰扬了」等。 公司人力资源部研判该员工上述言论严重违反员工手册,且有可能其极端情绪会对其他同事造成不可知威胁,决定与其解除劳动合同。 2 、其他澄清事项: 在公司与其解除劳动合同之后,王 * 及其周边人继续在网上发布不实言论,包括公司 HR 通过翻看其手机获悉其在某匿名社区发帖,以及公司 HR 威胁毁坏其档案,公司有 300 小时工时的「本分计算器」等,被各网络社区置顶推送,引发了新一轮网民关注。 公司特此对上述不实谣言予以澄清。 上海寻梦信息技术有限公司 人力资源部 1 月 11 日" }, "image_area": { "url": "https://pic2.zhimg.com/80/v2-e698cffa8b974ad49c728cf2098232d9_720w.png" }, "metrics_area": { "text": "4975 万热度" }, "label_area": { "type": "trend", "trend": 0, "night_color": "#B7302D", "normal_color": "#F1403C" }, "link": { "url": "https://www.zhihu.com/question/438830539" } }, "attached_info": "Cj8IoNnq1oWDvdMWEAMaCDU5Nzk2MTIzIO7j7v8FMHI4nypAAHIJNDM4ODMwNTM5eACqAQliaWxsYm9hcmTSAQA=" } """ items = [] resp = None try: with request_session() as s: resp = s.get(HOT_QUESTION_URL) obj = resp.json() items = obj['data'] except: logger.exception('get hot question failed') return (items, resp)
def request(self): try: conn = client.HTTPConnection(self.__host, self.__port) if self.__method == "GET": self.path = self.__path + self.__parameters_urlencode_deal conn.request(self.__method, self.__path) if self.__method == "POST": if self.__headers == {"Content-type": "application/json"}: conn.request(self.__method, self.__path, self.__jdata, self.__headers) if self.__headers == {"Content-type": "application/x-www-form-urlencoded"}: conn.request(self.__method, self.__path, self.__data, self.__headers) response = conn.getresponse() result_origin = response.read() try: result = result_origin.decode("gb2312").encode("utf8") except: result = result_origin return result except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def gennerator(): try: global code_list code_list = [] if not code_list: get_district_code() id = code_list[random.randint(0, len(code_list))]['code'] # 地区项 id = id + str(random.randint(1930, 2013)) # 年份项 da = date.today() + timedelta(days=random.randint(1, 366)) # 月份和日期项 id = id + da.strftime('%m%d') id = id + str(random.randint(100, 300)) # ,顺序号简单处理 i = 0 count = 0 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 权重项 check_code = {'0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '5', '9': '3', '10': '2'} # 校验码映射 for i in range(0, len(id)): count = count + int(id[i]) * weight[i] id = id + check_code[str(count % 11)] # 算出校验码 return id except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def call(self, cmd, *args, **kwds): '''调用命令字 ''' cmd = cmd.replace('-', '_') if cmd == 'forward' and args[1] == '--remove': method = getattr(self, 'remove_forward') args = args[2:] else: method = getattr(self, cmd) # print args sync = True if kwds.has_key('sync'): sync = kwds.pop('sync') if kwds.has_key('timeout') and not cmd in ( 'shell', 'install', 'uninstall', 'wait_for_device', 'reboot'): kwds.pop('timeout') if sync: ret = None retry_count = kwds.pop('retry_count') i = 0 socket_error_count = 0 while i < retry_count: try: self._lock.acquire() ret = method(*args, **kwds) break except socket.error, e: logger.exception(u'执行%s %s error' % (cmd, ' '.join(args))) socket_error_count += 1 if socket_error_count <= 10: i -= 1 time.sleep(1) except AdbError, e: err_msg = str(e) if 'device not found' in err_msg: return '', 'error: device not found' elif 'cannot bind to socket' in err_msg: return '', err_msg elif 'cannot remove listener' in err_msg: return '', err_msg elif 'device offline' in err_msg: return '', 'error: device offline' elif 'Bad response' in err_msg or 'Device or resource busy' in err_msg or 'closed' in err_msg: # wetest设备有时候会返回closed错误 # 需要重试 logger.exception('Run %s%s %r' % (cmd, ' '.join(args), e)) else: raise RuntimeError(u'执行%s %s 命令失败:%s' % (cmd, ' '.join(args), e)) time.sleep(1) if i >= retry_count - 1: raise e except RuntimeError, e: logger.exception(u'执行%s%s %r' % (cmd, ' '.join(args), e)) if 'device not found' in str(e): self.wait_for_device(args[0], retry_count=1, timeout=300) self._sock = None return self.call(cmd, *args, **kwds)
def refresh_media_store(self, file_path=''): '''刷新图库,显示新拷贝到手机的图片 ''' from adb import TimeoutError command = '' if not file_path: if hasattr(self, '_last_refresh_all') and time.time() - self._last_refresh_all < 2 * 60: logger.warn('[DeviceDriver] 120S内不允许全盘重复刷新') return sdcard_path = self.get_external_sdcard_path() command = 'am broadcast -a android.intent.action.MEDIA_MOUNTED --ez read-only false -d file://%s' % sdcard_path self._last_refresh_all = time.time() else: if file_path.startswith('/sdcard/'): file_path = self.get_external_sdcard_path() + file_path[7:] command = 'am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://%s' % file_path try: self.adb.run_shell_cmd(command, self.adb.is_rooted()) except TimeoutError: logger.exception('refresh_media_store failed') return False # self.adb.run_shell_cmd('am broadcast -a android.intent.action.BOOT_COMPLETED') if file_path and self.adb.get_sdk_version() >= 16: return self._wait_for_media_available(file_path)
def call(self, cmd, *args, **kwds): '''调用命令字 ''' cmd = cmd.replace('-', '_') method = getattr(self, cmd) # print args sync = True if kwds.has_key('sync'): sync = kwds.pop('sync') if kwds.has_key('timeout') and not cmd in ( 'shell', 'install', 'uninstall', 'wait_for_device', 'reboot'): kwds.pop('timeout') if sync: ret = None retry_count = kwds.pop('retry_count') i = 0 socket_error_count = 0 while i < retry_count: try: ret = method(*args, **kwds) break except socket.error, e: logger.exception(u'执行%s %s error' % (cmd, ' '.join(args))) socket_error_count += 1 if socket_error_count <= 10: i -= 1 time.sleep(1) except AdbError, e: if 'device not found' in str(e): return '', 'error: device not found' elif 'cannot bind to socket' in str(e): return '', str(e) elif 'device offline' in str(e): return '', 'error: device offline' elif 'Bad response' in str( e) or 'Device or resource busy' in str(e): # 需要重试 logger.exception(u'执行%s%s %r' % (cmd, ' '.join(args), e)) else: raise RuntimeError(u'执行%s %s 命令失败:%s' % (cmd, ' '.join(args), e)) time.sleep(1) if i >= retry_count - 1: raise e except RuntimeError, e: logger.exception(u'执行%s%s %r' % (cmd, ' '.join(args), e)) if 'device not found' in str(e): self.wait_for_device(args[0], retry_count=1, timeout=300) self._sock = None return self.call(cmd, *args, **kwds)
def get_host(self): try: return self.__host except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def get_parameters_urlencode_deal(self): try: return self.__parameters_urlencode_deal except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")
def get_path(self): try: return self.__path except Exception as e: logger.error(e) logger.exception(u"捕获到错误如下:")