def getToken(self, username, passwd): """ 获取机器人用户token Args: username: 用户名 passwd: 密码(md5 32位小写) Returns: Raises: """ cookies = {} cookies['device_id'] = 'monitordevice' cookies['device_type'] = '1' cookies['tpl'] = 'baidu_music' _session_client = requests.Session() url = 'https://passport.qianqian.com/v2/api/login?' url = url + 'login_id=' + str(username) + '&password='******'&login_type=1' token = '' try: ret = _session_client.get(url, cookies=cookies, proxies=self.__proxies, timeout=1) cookies_ret = ret.cookies token = cookies_ret['token_'] except BaseException, error: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(error))
def toJson(self, content): """ 转为json对象 Args: content: 转换内容 Returns: Raises: """ s = {} try: s = json.loads(content) except ValueError, error: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(error))
def getJson(self, url): """ 获得json内容 Args: url: url链接 Returns: Raises: """ try: r = requests.get(url, proxies=self.__proxies) return r.json() except BaseException, error: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(error))
def getStatus(self, url): """ 获得http状态码 Args: url: url链接 Returns: Raises: """ try: r = requests.head(url=url, allow_redirects=self.__allow_redirects, proxies=self.__proxies) return r.status_code except BaseException, error: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(error))
def query(self, sql): """ sql请求 Args: sql: sql语句 Returns: Raises: """ try: n = self.cur.execute(sql) return n except MySQLdb.Error as e: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(e.args[0]) + str(e.args[1])) return False
def request(self, url): """ 发送get请求 Args: url: url链接 Returns: Raises: """ try: self.__proxies = {} r = requests.get(url=url, headers=self.__headers, cookies=self.__cookies, timeout=self.__timeout, allow_redirects=self.__allow_redirects, proxies=self.__proxies) return r.content except BaseException, error: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(error))
def __init__(self): """ init函数 Args: Returns: Raises: """ charset = "utf8" try: self.conn = MySQLdb.connect(host=db_host, port=db_port, user=db_user, passwd=db_passwd, db=db_db) self.conn.set_character_set(charset) self.cur = self.conn.cursor() except MySQLdb.Error as e: scheduler_logger.error(os.path.basename(__file__), sys._getframe().f_lineno, str(e.args[0]) + str(e.args[1]))