def import_table(db_obj, model): """Does exactly as the name suggests""" tbl_name = model.__tablename__ file_name = tbl_name + '.pickle' logger.info("Importing %s", tbl_name) try: now = time() with open(join(BACKUP_DIR, file_name)) as fp: rows = load(fp) logger.info("No. of rows: %s", len(rows)) if not rows: logger.info("Nothing to import. Skipping table %s.", tbl_name) return logger.info("Inserting the rows of %s in bulk", tbl_name) try: objs = [model(**row) for row in rows] db_obj.bulk_insert_or_update(objs) except DBAPIError: db_obj.db_session.rollback() logger.error("Error while inserting rows in bulk," "now inserting sequentially") insert_sequential(rows, model) logger.info( 'Completed in %0.3s seconds', (time() - now)) except IOError as e: logger.error( "Importing %s failed. Probably a new table. Confirm message: %s", tbl_name, e.message)
def submit(self, firstTry=True): """ Submit cached tracks, return True if OK """ if (self.session[SESSION_ID] is None and not self.handshake()) or len(self.cache) == 0: return False try: hardFailure = False cachedTracks = self.getFromCache(MAX_SUBMISSION) data = 's=%s&%s' % (self.session[SESSION_ID], '&'.join(cachedTracks)) reply = urlopen(self.session[SUBMISSION_URL], data).read().strip().split('\n') if reply[0] == 'OK': self.removeFromCache(len(cachedTracks)) return True elif reply[0] == 'BADSESSION' and firstTry: self.session[:] = [None, None, None] return self.submit(False) else: hardFailure = True except: hardFailure = True logger.error('[%s] Unable to perform submission\n\n%s' % (MOD_NAME, traceback.format_exc())) if hardFailure: if self.nbHardFailures < 2: self.nbHardFailures += 1 else: self.handshake() else: self.nbHardFailures = 0 return False
def nowPlayingNotification(self, track, firstTry = True): """ The Now-Playing notification is a lightweight mechanism for notifying the Audioscrobbler server that a track has started playing """ if (self.session[SESSION_ID] is None and not self.handshake()) or not track.hasArtist() or not track.hasTitle(): return params = ( ( 's', self.session[SESSION_ID] ), ( 'a', tools.percentEncode(track.getSafeArtist()) ), ( 't', tools.percentEncode(track.getSafeTitle()) ), ( 'b', tools.percentEncode(track.getSafeAlbum()) ), ( 'l', track.getSafeLength() ), ( 'n', track.getSafeNumber() ), ( 'm', track.getSafeMBTrackId() ) ) try: data = '&'.join(['%s=%s' % (key, val) for (key, val) in params]) reply = urlopen(self.session[NOW_PLAYING_URL], data).read().strip().split('\n') if reply[0] == 'BADSESSION' and firstTry: self.session[:] = [None, None, None] self.nowPlayingNotification(track, False) except: logger.error('[%s] Unable to perform now-playing notification\n\n%s' % (MOD_NAME, traceback.format_exc()))
def get_val(col, json_key, values): """Map the DB column to key in request JSON and get its value. Do a dump if column is of JSON type. Finds key in nested dictionaries.""" try: values = values[json_key] except KeyError: key_chain = json_key.split('.') for _key in key_chain: values = values[_key] if strict and hasattr(col, 'read_only') and values: logger.info('Skipping field %s as it is read-only', col) return None if hasattr(col, 'is_json'): return json.dumps({json_key: values}) if not values: return if isinstance(col.type, DateTime): return datetime.strptime(values, DATETIME_FORMAT) if isinstance(col.type, Date): try: values = datetime.strptime(values, DATE_FORMAT) except (TypeError, ValueError): try: values = datetime.strptime(values, DATETIME_FORMAT) except (TypeError, ValueError): logger.error("Unable to convert data to timestamp") logger.error(traceback.format_exc()) return None return values
def PushApk2Devices(self): skip_pushapk2devices = self.get_skip_pushapk2devices() skip_pushapk2devices = True if skip_pushapk2devices == "1" else False if skip_pushapk2devices: return "Skip" device = self.get_mdevice() skip_check_of_install = self.get_skip_check_of_install() skip_check_of_install = True if skip_check_of_install == "1" else False # 启动一个线程,执行AppInstall函数 try: installThread = threading.Thread(target=self.AppInstall, args=()) installThread.start() if not skip_check_of_install: # 如果配置上skip_check_of_install为True,则再开一个线程,执行安装权限点击操作 logger.info( "设备{},skip_check_of_install为{},开始进行安装点击权限操作".format( device, skip_check_of_install)) inputThread = threading.Thread(target=self.InputEvent, args=()) inputThread.start() inputThread.join() else: logger.info("设备{},skip_check_of_install为{},不进行安装点击权限操作".format( device, skip_check_of_install)) installThread.join() # 从queue里获取线程函数的返回值 result = q.get() if result == "Install Success": return "Success" else: return "Fail" except Exception as e: logger.error(e) pass
def loadLibrary(self, tree, name): """ Load the given library """ rows = [] path = os.path.join(ROOT_PATH, name) prevChar = '' # Make sure the version number is the good one if not os.path.exists(os.path.join(path, 'VERSION_%u' % VERSION)): logger.error('[%s] Version number does not match, loading of library "%s" aborted' % (MOD_NAME, name)) error = _('This library is deprecated, please refresh it.') tree.replaceContent([(consts.icoError, error, TYPE_NONE, None, None)]) return # Create the rows, with alphabetical header if needed for artist in pickleLoad(os.path.join(path, 'artists')): if len(artist[ART_NAME]) != 0: currChar = unicode(artist[ART_NAME], errors='replace')[0] else: currChar = prevChar if prevChar != currChar and not (prevChar.isdigit() and currChar.isdigit()): prevChar = currChar if currChar.isdigit(): rows.append((None, None, '<b>0 - 9</b>', TYPE_HEADER, None, None)) else: rows.append((None, None, '<b>%s</b>' % currChar, TYPE_HEADER, None, None)) rows.append((consts.icoDir, None, cgi.escape(artist[ART_NAME]), TYPE_ARTIST, os.path.join(path, artist[ART_INDEX]), None)) # Insert all rows, and then add a fake child to each artist tree.replaceContent(rows) for node in tree.iterChildren(None): if tree.getItem(node, ROW_TYPE) == TYPE_ARTIST: tree.appendRow((None, None, '', TYPE_NONE, '', None), node)
def select_item_in_cart(self, orderItem: OrderItem): """ 在购物车选中候选商品 :param orderItem: 候选商品 :return: """ logger.info( f'{time.ctime()} > 选择商品:{orderItem.pid}【{orderItem.pname}】x {orderItem.count}' ) url = "https://wq.jd.com/deal/mshopcart/checkcmdy" # 选择item提交 payload = {"commlist": f"{orderItem.pid},,{orderItem.count},,1,,0"} headers = { 'User-Agent': self.user_agent, "Origin": "https://p.m.jd.com", "Referer": "https://p.m.jd.com/" } res = self.sess.post(url, data=payload, headers=headers) if res.status_code == requests.codes.OK: try: if res.json().get("errId", None) == "0": logger.info("选择成功") return True except Exception as e: logger.error(e) logger.error(traceback.format_exc()) logger.error(res.json().get("errMsg")) logger.error("选择失败") logger.error(res.content.decode("utf8")) return False
def export_table(db_obj, model): """Dump rows of a table as pickle file. Args: db_obj: instance of DatabaseAPI model: instance of DB table """ tbl_name = model.__tablename__ logger.info("Exporting %s", tbl_name) file_name = tbl_name + '.pickle' # If a column is missing from database, likely as a result of a new column # just added, skip that column. try: rows = query_model_during_export(db_obj, model) except ProgrammingError as e: # New Table. logger.error( 'Export failed for %s. Probably new table. Confirm message: %s', tbl_name, e.message) return result = [] with open(join(BACKUP_DIR, file_name), "w") as fp: for row in rows: row_dict = row._asdict() result.append(row_dict) dump(result, fp)
def cancel_select_all_in_cart(self): """取消勾选购物车中的所有商品 :return: 取消勾选结果 True/False """ logger.info(f'{time.ctime()} > 取消选择全部商品') url = "https://wq.jd.com/deal/mshopcart/uncheckcmdy" # 选择item提交 payload = {"commlist": "", "all": 1} headers = { 'User-Agent': self.user_agent, "Origin": "https://p.m.jd.com", "Referer": "https://p.m.jd.com/" } res = self.sess.post(url, data=payload, headers=headers) if res.status_code == requests.codes.OK: try: if res.json().get("errId", None) == "0": logger.info("取消全部选择成功") return True logger.error(res.json().get("errMsg")) except Exception as e: logger.error(e) logger.error(traceback.format_exc()) logger.error("失败") logger.error(res.content.decode("utf8")) return False
def onModLoaded(self): """ Initialize the module """ self.track = None # Current track self.status = '' # The currently used status self.paused = False # True if the current track is paused self.clients = [] # Clients currently active self.cfgWindow = None # Configuration window # Detect active clients try: import dbus session = dbus.SessionBus() activeServices = session.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus').ListNames() for activeClient in [client for client in CLIENTS if client[IM_DBUS_SERVICE_NAME] in activeServices]: obj = session.get_object(activeClient[IM_DBUS_SERVICE_NAME], activeClient[IM_DBUS_OBJECT_NAME]) interface = dbus.Interface(obj, activeClient[IM_DBUS_INTERFACE_NAME]) activeClient[IM_INSTANCE] = activeClient[IM_CLASS](interface) activeClient[IM_ACCOUNTS] = activeClient[IM_INSTANCE].listAccounts() logger.info('[%s] Found %s instance' % (MOD_NAME, activeClient[IM_NAME])) self.clients.append(activeClient) except: logger.error('[%s] Error while initializing\n\n%s' % (MOD_NAME, traceback.format_exc()))
def toReport(self): logger.info("开始读取unittest测试核心目录") dir_path = unittest_path logger.info("开始使用discover到测试") try: discover = unittest.defaultTestLoader.discover(dir_path, pattern="*.py") except Exception as e: logger.error("加载unittest测试类路径失败,请查看问题!原因: s%", e) # 报告命名加上时间格式化 logger.info("设置报告名称格式") time = datetime.now() now = time.strftime('%Y-%m-%d %H_%M_%S') # 报告绝对路径 reportname = report_path + now reportpath= reportname + 'result.html' # 打开文件,写入测试结果 # print(reportpath) logger.info("执行测试用例开始。。。。。。。。。。。。。") try: with open(reportpath, 'wb') as f: runner = HTMLTestRunner(stream=f, verbosity=2, title='接口ddt测试报告', description='用例执行详细信息') runner.run(discover) f.close() # result = BeautifulReport(discover) # result.report(description='用例执行详细信息', filename=reportname+"result") except Exception as e: logger.error("执行测试用例执行出错,请查看问题!原因: s%", e) logger.info("执行测试用例结束。。。。。。。。。。。。。")
def cddbRequest(self, discInfo): """ Return disc information from online CDDB, None if request fails """ import CDDB, socket # Make sure to not be blocked by the request socket.setdefaulttimeout(consts.socketTimeout) try: (status, info) = CDDB.query(discInfo) if status == 200: disc = info # Success elif status == 210: disc = info[0] # Exact multiple matches elif status == 211: disc = info[0] # Inexact multiple matches else: raise Exception, "Unknown disc (phase 1 returned %u)" % status (status, info) = CDDB.read(disc["category"], disc["disc_id"]) if status == 210: return info else: raise Exception, "Unknown disc (phase 2 returned %u)" % status except: logger.error("[%s] CDDB request failed\n\n%s" % (MOD_INFO[modules.MODINFO_NAME], traceback.format_exc())) return None
def getExcel(self): logger.info("遍历用例目录读取用例excel开始。。。。。。。。。。。。。") file_name_list = os.walk(test_case_path, topdown=True) dict = {} for root, dirs, file_name in file_name_list: try: for name in file_name: print(os.path.join(root, name)) logger.info("读取用例excel中sheet开始。。。。。。。。。。。。。") workbook = load_workbook(os.path.join(root, name)) sheet = workbook['Sheet1'] lists = [] rows_sheet = sheet.iter_rows() for item in rows_sheet: if item[0].value == "api": continue list = [] for col in item: # logger.info("遍历每一列加入到一行测试数据。。。。。。。。。。。。。") list.append(col.value) lists.append(list) dict[name] = lists except Exception as e: logger.error("历用例目录读取用例excel执行出错,请查看问题!原因: s%", e) # print(dict) return dict
def listAccounts(self): """ Return a list of existing accounts """ try: return [account for account in self.dbusInterface.list_accounts()] except: logger.error('[%s] Unable to list Gajim accounts\n\n%s' % (MOD_NAME, traceback.format_exc())) return []
def nowPlayingNotification(self, track, firstTry=True): """ The Now-Playing notification is a lightweight mechanism for notifying the Audioscrobbler server that a track has started playing """ import urllib2 if (self.session[SESSION_ID] is None and not self.handshake()) or not track.hasArtist() or not track.hasTitle(): return params = ( ("s", self.session[SESSION_ID]), ("a", tools.percentEncode(track.getSafeArtist())), ("t", tools.percentEncode(track.getSafeTitle())), ("b", tools.percentEncode(track.getSafeAlbum())), ("l", track.getSafeLength()), ("n", track.getSafeNumber()), ("m", track.getSafeMBTrackId()), ) try: data = "&".join(["%s=%s" % (key, val) for (key, val) in params]) reply = urllib2.urlopen(self.session[NOW_PLAYING_URL], data).read().strip().split("\n") if reply[0] == "BADSESSION" and firstTry: self.session[:] = [None, None, None] self.nowPlayingNotification(track, False) except: logger.error("[%s] Unable to perform now-playing notification\n\n%s" % (MOD_NAME, traceback.format_exc()))
def new(cls, self): try: g.my_session.add(self) except: logger.error('g.my_session add error, rollback') g.my_session.rollback() else: g.my_session.commit()
def delete(cls, self): try: g.my_session.delete(self) except: logger.error('g.my_session delete error, rollback') g.my_session.rollback() else: g.my_session.commit()
def post(self, url, data=None, headers=None, **kargs): logger.info("执行post请求开始。。。。。。。。。。。。。") try: response = requests.post(url=url, data=data, headers=headers) except Exception as e: logger.error("执行post请求出错,请查看问题!原因: s%", e) return response
def get(self, url, params=None, headers=None, **kargs): logger.info("执行get请求开始。。。。。。。。。。。。") try: response = requests.get(url=url, params=params, headers=headers) except Exception as e: logger.error("执行get请求出错,请查看问题!原因: s%", e) return response
def make_req(message): response = requests.post(main_url, message.encode()) if response != 200: logger.error( 'POST request to {0} with message = {1} returned {2}.'.format( main_url, message, response)) exit(1)
def setStatusMsg(self, account, msg): """ Change the status message of the given account """ try: currentStatus = self.dbusInterface.get_status(account) if currentStatus in ('online', 'chat') or prefs.get(__name__, 'update-when-away', DEFAULT_UPDATE_WHEN_AWAY): self.dbusInterface.change_status(currentStatus, msg, account) except: logger.error('[%s] Unable to set Gajim status\n\n%s' % (MOD_NAME, traceback.format_exc()))
def setStatusMsg(self, account, msg): """ Change the status message of the given account """ try: currentStatus, currentMsg = self.dbusInterface.GetPresence('') if currentStatus == 'available' or prefs.get(__name__, 'update-when-away', DEFAULT_UPDATE_WHEN_AWAY): self.dbusInterface.SetPresence(currentStatus, msg) except: logger.error('[%s] Unable to set Gossip status\n\n%s' % (MOD_NAME, traceback.format_exc()))
def _getTrackFromFile(file): """ Return a Track object, based on the tags of the given file The 'file' parameter must be a real file (not a playlist or a directory) """ try: return mFormats[splitext(file.lower())[1]].getTrack(file) except: logger.error('Unable to extract information from %s\n\n%s' % (file, traceback.format_exc())) return FileTrack(file)
def onModLoaded(self): """ The module has been loaded """ self.icon = gtk.STOCK_DIALOG_INFO self.notif = None self.cfgWin = None self.hasNext = False self.initialized = False if self.gtkExecute(self.initPynotify): self.initialized = True else: logger.error('[%s] Initialization failed' % MOD_INFO[modules.MODINFO_NAME])
def implement_order(self, orderItems: list): """ 下单 :param orderItems: :return: """ if self.confirmOrderItems(orderItems): if self.submitOrder(): logger.info("提交订单成功,结束单次任务!") return True logger.error("提交订单失败,结束单次执行任务!") return False
def _parse_product(products): for product in products: mainSku = product.get("mainSku") if int(product.get("mainSku").get("outOfStock")) == 0: continue else: sku_id = mainSku.get("id") sku_name = mainSku.get("name") logger.error(f"订单中{sku_id}:{sku_name}无货!") return False return True
def setStatusMsg(self, account, msg): """ Change the status message of the given account """ try: current = self.dbusInterface.GaimSavedstatusGetCurrent() statusType = self.dbusInterface.GaimSavedstatusGetType(current) statusId = self.dbusInterface.GaimPrimitiveGetIdFromType(statusType) if statusId == 'available' or prefs.get(__name__, 'update-when-away', DEFAULT_UPDATE_WHEN_AWAY): saved = self.dbusInterface.GaimSavedstatusNew('', statusType) self.dbusInterface.GaimSavedstatusSetMessage(saved, msg) self.dbusInterface.GaimSavedstatusActivate(saved) except: logger.error('[%s] Unable to set Gaim status\n\n%s' % (MOD_NAME, traceback.format_exc()))
def updatesql(self, sql): conn = pymysql.connect(**self.conn) cursor = conn.cursor() cursor.execute(sql) conn.commit() try: cursor.execute(sql) # logger.info(sql) conn.commit() except Exception as e: conn.rollback() logger.error(e.message)
def __createNotification(self, title, body, icon): """ Create the Notification object """ if not Notify.init(consts.appNameShort): logger.error('[%s] Initialization of python-notify failed' % MOD_INFO[modules.MODINFO_NAME]) self.notif = Notify.Notification.new(title, body, icon) self.notif.set_urgency(Notify.Urgency.LOW) self.notif.set_timeout( prefs.get(__name__, 'timeout', PREFS_DEFAULT_TIMEOUT) * 1000) if prefs.get(__name__, 'skip-track', PREFS_DEFAULT_SKIP_TRACK): self.notif.add_action('stop', _('Skip track'), self.onSkipTrack)
def __createNotification(self, title, body, icon): """ Create the Notification object """ import pynotify if not pynotify.init(consts.appNameShort): logger.error('[%s] Initialization of pynotify failed' % MOD_INFO[modules.MODINFO_NAME]) self.notif = pynotify.Notification(title, body, icon) self.notif.set_urgency(pynotify.URGENCY_LOW) self.notif.set_timeout(prefs.get(__name__, 'timeout', PREFS_DEFAULT_TIMEOUT) * 1000) if prefs.get(__name__, 'skip-track', PREFS_DEFAULT_SKIP_TRACK): self.notif.add_action('stop', _('Skip track'), self.onSkipTrack)
def loadDisc(self): """ Read disc information and create the explorer tree accordingly """ import DiscID try: discInfo = DiscID.disc_id(DiscID.open(prefs.get(__name__, 'device', PREFS_DFT_DEVICE))) except Exception, err: if err[0] == 123: self.tree.replaceContent([(consts.icoCdrom, _('No disc found'), None)]) modules.postMsg(consts.MSG_CMD_EXPLORER_RENAME, {'modName': MOD_L10N, 'expName': self.expName, 'newExpName': MOD_L10N}) self.expName = MOD_L10N else: logger.error('[%s] Unable to read device\n\n%s' % (MOD_INFO[modules.MODINFO_NAME], traceback.format_exc())) return
def cancelOrder(self, orderId): """ 取消订单 :param orderId: :return: """ url = "https://wq.jd.com/ordercancel/cancelorder" payload = { "orderid": f"{orderId}", "cancelReason": "1100", } headers = { 'User-Agent': self.user_agent, "Referer": "https://wqs.jd.com/" } res = self.sess.get(url, params=payload, headers=headers) if res.status_code == requests.codes.OK: try: res_json = demjson.decode(res.text) if str(res_json.get("retcode", None)) == "0": order_id = res_json.get("data").get("orderid") logger.info(f"取消订单成功!订单号:{order_id}") return True logger.error(res_json.get("errMsg")) except Exception as e: logger.error(e) logger.error(traceback.format_exc()) time.sleep(4) logger.error("提交Order失败") return False
def _core_method(self, func, *args, **kwargs): """Performs common operations""" self.content = { "response": { "status": "ok" }, "service": { "version": APP_VERSION } } try: if self.request.uri == '/auth': # request.body contains password; don't log it logger.info('%s %s', self.request.method, self.request.uri) else: logger.info('%s %s %s', self.request.method, self.request.uri, self.request.body) self._json_to_dict() self._authenticate_user() self._authorize_user() self._check_simultaneous_edit() resp = func(self, *args, **kwargs) # self._update_username(resp) if resp: self.content.get("response").update(resp) except AuthError as e: logger.info(e.message) self.set_status(HTTP_CODE['unauthorized']) self.content.get("response").update({ "status": "error", "error": e.message }) except HTTPError as e: logger.error(e.message, exc_info=True) self.set_status(e.status_code) self.content.get("response").update({ "status": "error", "error": str(e.message) }) except Exception as e: logger.error(e.message, exc_info=True) self.set_status(HTTP_CODE['server_error']) self.content.get("response").update({ "status": "error", "error": e.__class__.__name__ + ": " + str(e) }) else: pass
def _validate_cookies(self): """验证cookies是否有效(是否登陆) :return: cookies是否有效 True/False """ url = 'https://wq.jd.com/user_redpoint/QueryUserRedPoint?type=4&sceneid=2' try: resp = self.sess.get(url=url, allow_redirects=False) resp_json = json.loads(resp.text) if resp_json.get("msg", None) == "success": return True except Exception as e: logger.error(e) self.sess = requests.session() return False
def setStatusMsg(self, account, msg): """ Change the status message of the given account """ try: current = self.dbusInterface.PurpleSavedstatusGetCurrent() # This used to be needed, but seems to have been fixed in Pidgin # statusType = dbus.UInt32(self.dbusInterface.PurpleSavedstatusGetType(current)) statusType = self.dbusInterface.PurpleSavedstatusGetType(current) statusId = self.dbusInterface.PurplePrimitiveGetIdFromType(statusType) if statusId == 'available' or prefs.get(__name__, 'update-when-away', DEFAULT_UPDATE_WHEN_AWAY): saved = self.dbusInterface.PurpleSavedstatusNew('', statusType) self.dbusInterface.PurpleSavedstatusSetMessage(saved, msg) self.dbusInterface.PurpleSavedstatusActivate(saved) except: logger.error('[%s] Unable to set Pidgin status\n\n%s' % (MOD_NAME, traceback.format_exc()))
def submitOrder(self): """ 提交订单 :return: """ url = "https://wq.jd.com/deal/msubmit/confirm" data = { "paytype": 0, "paychannel": 1, "action": 1, "reg": 1, "type": 0, # TODO: "token2": self.submitOrderExtraData.get("token2"), "dpid": "", "skulist": self.submitOrderExtraData.get("skulist"), "scan_orig": "", "gpolicy": "", "platprice": 0, "pick": "", "savepayship": 0, "sceneval": 2, "setdefcoupon": 0, "mix": 0, "r": random.random(), "traceid": self.submitOrderExtraData.get("traceid"), } headers = { 'User-Agent': self.user_agent, "Origin": "https://p.m.jd.com", "Referer": "https://p.m.jd.com/" } res = self.sess.get(url, params=data, headers=headers) if res.status_code == requests.codes.OK: try: res_json = demjson.decode(res.text) if res_json.get("errId", None) == "0": order_id = res_json.get("dealId") price = int(res_json.get("totalPrice")) / 100 logger.info("提交Order成功") logger.info( f"下单成功, 订单号:{order_id}, 请前往京东官方商城付款. 需付款: {price}") send_email('下单成功', f'订单号:{order_id}, 请前往京东官方商城付款. 需付款: f{price}') return True logger.error(res_json.__str__()) except Exception as e: logger.error(e) logger.error(traceback.format_exc()) finally: # 订单执行成功与否都要等待一段时间 京东风控 # logger.info("京东风控,等待4s下单") # time.sleep(4) pass logger.error("提交Order失败") return False
def loadArtists(self, tree, name): """ Load the given library """ libPath = os.path.join(ROOT_PATH, name) # Make sure the version number is the good one if not os.path.exists(os.path.join(libPath, 'VERSION_%u' % VERSION)): logger.error('[%s] Version number does not match, loading of library "%s" aborted' % (MOD_INFO[modules.MODINFO_NAME], name)) error = _('This library is deprecated, please refresh it.') tree.replaceContent([(icons.errorMenuIcon(), None, error, TYPE_NONE, None, None)]) return rows = [] icon = icons.dirMenuIcon() prevChar = '' allArtists = pickleLoad(os.path.join(libPath, 'artists')) self.allGenres = pickleLoad(os.path.join(libPath, 'genres')) # Filter artists by genre if needed if self.currGenre is not None: allArtists = [artist for artist in allArtists if artist[ART_NAME] in self.allGenres[self.currGenre]] rows.append((icons.infoMenuIcon(), None, '<b>%s</b>' % self.currGenre.capitalize(), TYPE_GENRE_BANNER, None, None)) else: rows.append((icons.infoMenuIcon(), None, '<b>%s</b>' % _('All genres'), TYPE_GENRE_BANNER, None, None)) # Filter artists by favorites if needed if self.showOnlyFavs: allArtists = [artist for artist in allArtists if self.isArtistInFavorites(artist[ART_NAME])] rows.append((icons.starMenuIcon(), None, '<b>%s</b>' % _('My Favorites'), TYPE_FAVORITES_BANNER, None, None)) # Create the rows for artist in allArtists: if len(artist[ART_NAME]) != 0: currChar = unicode(artist[ART_NAME], errors='replace')[0].lower() else: currChar = prevChar if prevChar != currChar and not (prevChar.isdigit() and currChar.isdigit()): prevChar = currChar if currChar.isdigit(): rows.append((None, None, '<b>0 - 9</b>', TYPE_HEADER, None, None)) else: rows.append((None, None, '<b>%s</b>' % currChar.upper(), TYPE_HEADER, None, None)) rows.append((icon, None, htmlEscape(artist[ART_NAME]), TYPE_ARTIST, os.path.join(libPath, artist[ART_INDEX]), artist[ART_NAME])) # Insert all rows, and then add a fake child to each artist tree.replaceContent(rows) for node in tree.iterChildren(None): if tree.getItem(node, ROW_TYPE) == TYPE_ARTIST: tree.appendRow(FAKE_CHILD, node)
def detail(id): Web_View.incre_views() note = Weekly.find_by_id(id) if note is not None: note.incre_views() else: logger.error('get_obj_by_id is None') # 根据用户Id,查找作者 author = User.find_by_id(note.user_id).user_name # 根据文章id,查找其所有评论 comments = Comments.get_obj_by_filter(note_id=id) return render_template('weekly/detail.html', note=note, comments=comments, author=author)
def get_val(json_key, col): """Get model value. Do a load if a JSON field. Else raise error.""" val = getattr(obj, col.name) if not val: return val if isinstance(col.type, DateTime): val = val.strftime(DATETIME_FORMAT) if isinstance(col.type, Date): val = val.strftime(DATE_FORMAT) if hasattr(col, 'is_json'): try: val = json.loads(val).get(json_key) except ValueError as e: logger.error("Invalid JSON found in column %s", col) raise e except KeyError: raise return val
def _update_username(resp): """Replace user id with user display name in the response dict""" if not resp: return key = resp.keys()[0] if not isinstance(resp[key], dict) or\ not resp[key].get('last_modified_by'): return user_id = resp[key].get('last_modified_by') try: with DatabaseAPI() as db_obj: user = db_obj.get_obj(User, id=user_id) if not user: return name = user.display_name if user.display_name else user.mail resp[key]['last_modified_by'] = name except Exception as e: logger.error(e.message, exc_info=True)
def loadDisc(self): """ Read disc information and create the explorer tree accordingly """ import DiscID try: discInfo = DiscID.disc_id(DiscID.open(prefs.get(__name__, "device", PREFS_DFT_DEVICE))) except Exception, err: if err[0] == 123: self.tree.replaceContent([(icons.cdromMenuIcon(), None, _("No disc found"), None)]) modules.postMsg( consts.MSG_CMD_EXPLORER_RENAME, {"modName": MOD_L10N, "expName": self.expName, "newExpName": MOD_L10N}, ) self.expName = MOD_L10N else: logger.error( "[%s] Unable to read device\n\n%s" % (MOD_INFO[modules.MODINFO_NAME], traceback.format_exc()) ) return
def generateFullSizeCover(self, inFile, outFile, format): """ Resize inFile if needed, and write it to outFile (outFile and inFile may be equal) """ try: # Open the image cover = Image.open(inFile) # Resize in the best way we can if cover.size[0] < FULL_SIZE_COVER_WIDTH: newWidth = cover.size[0] else: newWidth = FULL_SIZE_COVER_WIDTH if cover.size[1] < FULL_SIZE_COVER_HEIGHT: newHeight = cover.size[1] else: newHeight = FULL_SIZE_COVER_HEIGHT cover = cover.resize((newWidth, newHeight), Image.ANTIALIAS) # We're done cover.save(outFile, format) except: logger.error('[%s] An error occurred while generating a showable full size cover\n\n%s' % (MOD_NAME, traceback.format_exc()))
def _generateCover(self, inFile, outFile, format, max_width, max_height): from PIL import Image try: # Open the image cover = Image.open(inFile) width = cover.size[0] height = cover.size[1] newWidth, newHeight = tools.resize(width, height, max_width, max_height) cover = cover.resize((newWidth, newHeight), Image.ANTIALIAS) cover.save(outFile, format) except Exception: # This message will probably be displayed for the thumbnail and the big cover. logger.error( '[%s] An error occurred while generating the cover for "%s"\n\n%s' % (MOD_NAME, inFile, traceback.format_exc())) # Remove corrupted file. tools.remove(outFile)
def main(): # 忽略输出中的warning warnings.filterwarnings('ignore') # 读取相关配置 # 判断url是否填写 url = AutoWeb.get_url() if url == "": logger.error('配置文件url字段为空') raise # 读取是否跳过发送邮件 skip_email_flag = AutoWeb.get_skip_email() skip_email = True if skip_email_flag == '1' else False report_path = os.path.join(os.getcwd(), "Report") # 没有Report目录时自动创建 if not os.path.exists(report_path): os.mkdir(report_path) if os.path.exists(report_path): logger.info('{}创建成功'.format(report_path)) else: logger.error('{}创建失败'.format(report_path)) else: logger.info('{}已存在,跳过创建'.format(report_path)) logger.info("测试开始") try: start = time.localtime() web = AutoWeb() # 进入功能进程模块 RunTestCase(web, start) logger.info("测试结束") except AirtestSeleniumException: logger.error("AirtestSelenium发生错误\n" + traceback.format_exc()) except Exception: logger.error("发生未知错误\n" + traceback.format_exc()) # 判断是否发送邮件 if not skip_email: try: Email().send_email() except Exception: logger.error(traceback.format_exc()) else: logger.info('跳过发送邮件')
def get_cart_detail(self): """获取购物车商品详情 通过网页h5 获取购物车 :return: 购物车商品信息 dict """ """获取购物车商品详情 :return: 购物车商品信息 dict """ logger.info(f"获取{self.username}购物车详情") url = "https://wq.jd.com/deal/mshopcart/checkcmdy" # 选择item提交 payload = {"commlist": "1,,1,1,1,,0"} headers = { 'User-Agent': self.user_agent, "Origin": "https://p.m.jd.com", "Referer": "https://p.m.jd.com/" } res = self.sess.post(url, data=payload, headers=headers) if res.json().get("errId", None) == "0": cart = res.json().get("cart") cart_detail = self._parse_cart(cart) # logger.info(cart_detail) return cart_detail logger.error(res.json().get("errMsg")) logger.error("失败") logger.error(res.content.decode("utf8")) return None
def login(self): if self.is_login: logger.info('登录成功') return # 打开chrome 浏览器 self.browser = webdriver.Chrome(chromeDriverPath) self.__openBrowser() # 等待登录按钮加载 logger.info(f"请尽快登录") WebDriverWait(self.browser, 300).until( EC.presence_of_element_located((By.ID, "msShortcutMenu"))) self.browser.find_element_by_id("msShortcutMenu").click() try: WebDriverWait(self.browser, 300).until( EC.presence_of_element_located( (By.CLASS_NAME, "my_header_name"))) # 加载昵称 userName = self.browser.find_element_by_class_name( "my_header_name") logger.info(userName.text) self.is_login = True logger.info("登录成功") cookie_list = self.browser.get_cookies() for cookie in cookie_list: self.sess.cookies.update({cookie["name"]: cookie["value"]}) self._save_cookies() return True # except Exception as e: # 出错退出 import traceback logger.error(e) logger.error(traceback.format_exc()) return False finally: self.__closeBrowser()
def change_item_count_in_cart(self, orderItem: OrderItem): """修改购物车商品的数量 修改购物车中商品数量后,该商品将会被自动勾选上。 :param orderItem:商品数据对象 :return: 商品数量修改结果 True/False """ url = "https://wq.jd.com/deal/mshopcart/modifycmdynum" # 选择item提交 payload = { "commlist": f"{orderItem.pid},,{orderItem.count},{orderItem.pid},11,{orderItem.selectPromotion},0" } headers = { 'User-Agent': self.user_agent, "Origin": "https://p.m.jd.com", "Referer": "https://p.m.jd.com/" } res = self.sess.get(url, params=payload, headers=headers) if res.status_code == requests.codes.OK: try: if res.json().get("errId", None) == "0": logger.info("修改商品 %s 数量为 %s 成功", orderItem.pid, orderItem.count) return True logger.error(res.json().get("errMsg")) except Exception as e: logger.error(e) logger.error(traceback.format_exc()) return False
def onNewTrack(self, track): """ A new track has started """ status = track.format(prefs.get(__name__, 'status-msg', DEFAULT_STATUS_MSG)) if status == self.lastStatus: return self.gtkExecute(self.getAuthInfo) if self.passwd is None: return authToken = base64.b64encode(self.login + ':' + self.passwd) self.passwd = None self.lastStatus = status request = urllib2.Request('http://twitter.com/statuses/update.xml') request.headers['Authorization'] = 'Basic ' + authToken request.data = urllib.urlencode({'status': status}) try: urllib2.urlopen(request) except: logger.error('[%s] Unable to set Twitter status\n\n%s' % (MOD_NAME, traceback.format_exc()))
def __getFromInternet(self, artist, album): """ Try to download the cover from the Internet If successful, add it to the cache and return the path to it Otherwise, return None """ # Make sure to not be blocked by the request socket.setdefaulttimeout(consts.socketTimeout) # Request information to Last.fm # Beware of UTF-8 characters: we need to percent-encode all characters try: url = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s' % (AS_API_KEY, tools.percentEncode(artist), tools.percentEncode(album)) request = urllib2.Request(url, headers = {'User-Agent': USER_AGENT}) stream = urllib2.urlopen(request) data = stream.read() except urllib2.HTTPError, err: if err.code == 400: logger.error('[%s] No known cover for %s / %s' % (MOD_NAME, artist, album)) else: logger.error('[%s] Information request failed\n\n%s' % (MOD_NAME, traceback.format_exc())) return None
def generateThumbnail(self, inFile, outFile, format): """ Generate a thumbnail from inFile (e.g., resize it) and write it to outFile (outFile and inFile may be equal) """ try: # Open the image cover = Image.open(inFile).convert('RGBA') # Resize in the best way we can if cover.size[0] < THUMBNAIL_WIDTH: newWidth = cover.size[0] offsetX = (THUMBNAIL_WIDTH - cover.size[0]) / 2 else: newWidth = THUMBNAIL_WIDTH offsetX = 0 if cover.size[1] < THUMBNAIL_HEIGHT: newHeight = cover.size[1] offsetY = (THUMBNAIL_HEIGHT - cover.size[1]) / 2 else: newHeight = THUMBNAIL_HEIGHT offsetY = 0 cover = cover.resize((newWidth, newHeight), Image.ANTIALIAS) # Paste the resized cover into our model model = Image.open(THUMBNAIL_MODEL).convert('RGBA') model.paste(cover, (THUMBNAIL_OFFSETX + offsetX, THUMBNAIL_OFFSETY + offsetY), cover) cover = model # Add the glow effect glow = Image.open(THUMBNAIL_GLOW).convert('RGBA') cover.paste(glow, (0, 0), glow) # We're done cover.save(outFile, format) except: logger.error('[%s] An error occurred while generating a thumbnail\n\n%s' % (MOD_NAME, traceback.format_exc()))
def handshake(self): """ Authenticate the user to the submission servers, return True if OK """ now = int(time()) self.session[:] = [None, None, None] # Postpone or cancel this handshake? if self.isBanned or (now - self.lastHandshake) < self.handshakeDelay: return False # Asking for login information must be done in the GTK main loop, because a dialog box might be displayed if needed self.gtkExecute(self.getAuthInfo) if self.passwd is None: return False # Compute the authentication token md5Pwd = hashlib.md5() md5Token = hashlib.md5() md5Pwd.update(self.passwd) md5Token.update('%s%u' % (md5Pwd.hexdigest(), now)) # Try to forget authentication info ASAP token = md5Token.hexdigest() self.passwd = None request = 'http://%s/?hs=true&p=%s&c=%s&v=%s&u=%s&t=%d&a=%s' % (AS_SERVER, PROTO_VER, CLI_ID, CLI_VER, self.login, now, token) self.lastHandshake = now try: hardFailure = False reply = urlopen(request).read().strip().split('\n') if reply[0] == 'OK': self.session[:] = reply[1:] self.handshakeDelay = 0 self.nbHardFailures = 0 logger.info('[%s] Logged into Audioscrobbler server' % MOD_NAME) elif reply[0] == 'BANNED': logger.error('[%s] This version of %s has been banned from the server' % (MOD_NAME, consts.appName)) self.isBanned = True elif reply[0] == 'BADAUTH': logger.error('[%s] Bad authentication information' % MOD_NAME) return self.handshake() elif reply[0] == 'BADTIME': logger.error('[%s] Server reported that the current system time is not correct, please correct it' % MOD_NAME) self.isBanned = True else: hardFailure = True logger.error('[%s] Hard failure during handshake' % MOD_NAME) except: hardFailure = True logger.error('[%s] Unable to perform handshake\n\n%s' % (MOD_NAME, traceback.format_exc())) if hardFailure: if self.handshakeDelay == 0: self.handshakeDelay = 1*60 # Start at 1mn elif self.handshakeDelay >= 64*60: self.handshakeDelay = 120*60 # Max 120mn else: self.handshakeDelay *= 2 # Double the delay self.login = None return self.session[SESSION_ID] is not None
# Request information to Last.fm # Beware of UTF-8 characters: we need to percent-encode all characters try: url = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s' % (AS_API_KEY, tools.percentEncode(artist), tools.percentEncode(album)) request = urllib2.Request(url, headers = {'User-Agent': USER_AGENT}) stream = urllib2.urlopen(request) data = stream.read() except urllib2.HTTPError, err: if err.code == 400: logger.error('[%s] No known cover for %s / %s' % (MOD_NAME, artist, album)) else: logger.error('[%s] Information request failed\n\n%s' % (MOD_NAME, traceback.format_exc())) return None except: logger.error('[%s] Information request failed\n\n%s' % (MOD_NAME, traceback.format_exc())) return None # Extract the URL to the cover image malformed = True startIdx = data.find(AS_TAG_START) endIdx = data.find(AS_TAG_END, startIdx) if startIdx != -1 and endIdx != -1: coverURL = data[startIdx+len(AS_TAG_START):endIdx] coverFormat = os.path.splitext(coverURL)[1].lower() if coverURL.startswith('http://') and coverFormat in ACCEPTED_FILE_FORMATS: malformed = False if malformed: logger.error('[%s] Received malformed data\n\n%s' % (MOD_NAME, data)) return None