def _login_check(self): url_phoenix = consts.URL_PHOENIX url_login_history = consts.URL_LOGIN_HISTORY % (self.user["token"], fm_utils.timestamp()) url_login_check = consts.URL_LOGIN_CHECK % (self.user["token"], fm_utils.timestamp(), self.username) self._request(url_phoenix) self._request(url_login_history) self._request(url_login_check)
def login(self, username=None, password=None, verify_code=None): if username and password: self.username = username self.password = password if not self.login_data: self._login_check() pub_key, rsa_key = self._get_public_key() key = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key) pwd_rsa = base64.b64encode(rsa.encrypt(self.password, key)) self.login_data = { 'staticpage': 'http://fm.baidu.com/player/v2Jump.html', 'charset': 'UTF-8', 'token': self.user['token'], 'tpl': 'box', 'subpro': None, 'apiver': 'v3', 'tt': fm_utils.timestamp(), 'codestring': None, 'isPhone': None, 'safeflg': '0', 'u': 'http://fm.baidu.com', 'quick_user': '******', 'logintype': 'dialogLogin', 'logLoginType': 'pc_loginDialog', 'idc': None, 'loginmerge': 'true', 'splogin': '******', 'username': self.username, 'password': pwd_rsa, 'verifycode': verify_code, 'mem_pass': '******', 'rsakey': rsa_key, 'crypttype': 12, 'ppui_logintime': 14929, 'callback': 'parent.bd__pcbs__irpbf3' } else: self.login_data['verifycode'] = verify_code result = self._request(consts.URL_LOGIN, 'post', self.login_data) if not result.ok: raise APIError('Logging failed.') content = result.content # 是否需要验证码 if 'err_no=257' in content or 'err_no=6' in content: codestring = re.findall('codeString=(.*?)&', content)[0] logger.info('need captcha, codeString=%s', codestring) self.login_data['codestring'] = codestring captcha_path = self._get_captcha(codestring) return captcha_path self._check_account_exception(content) try: self.user['BDUSS'] = self.session.cookies['BDUSS'] except Exception as e: logger.error("Get BDUSS: %s", str(e.args)) logger.info('user %s Logged in BDUSS: %s' % (self.username, self.user['BDUSS'])) self._save_cookies()
def login(self, username=None, password=None, verify_code=None): if username and password: self.username = username self.password = password if not self.login_data: self._login_check() pub_key, rsa_key = self._get_public_key() key = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key) pwd_rsa = base64.b64encode(rsa.encrypt(self.password, key)) self.login_data = { "staticpage": "http://fm.baidu.com/player/v2Jump.html", "charset": "UTF-8", "token": self.user["token"], "tpl": "box", "subpro": None, "apiver": "v3", "tt": fm_utils.timestamp(), "codestring": None, "isPhone": None, "safeflg": "0", "u": "http://fm.baidu.com", "quick_user": "******", "logintype": "dialogLogin", "logLoginType": "pc_loginDialog", "idc": None, "loginmerge": "true", "splogin": "******", "username": self.username, "password": pwd_rsa, "verifycode": verify_code, "mem_pass": "******", "rsakey": rsa_key, "crypttype": 12, "ppui_logintime": 14929, "callback": "parent.bd__pcbs__irpbf3", } else: self.login_data["verifycode"] = verify_code result = self._request(consts.URL_LOGIN, "post", self.login_data) if not result.ok: raise APIError("Logging failed.") content = result.content # 是否需要验证码 if "err_no=257" in content or "err_no=6" in content: codestring = re.findall("codeString=(.*?)&", content)[0] logger.info("need captcha, codeString=%s", codestring) self.login_data["codestring"] = codestring captcha_path = self._get_captcha(codestring) return captcha_path self._check_account_exception(content) try: self.user["BDUSS"] = self.session.cookies["BDUSS"] except Exception as e: logger.error("Get BDUSS: %s", str(e.args)) logger.info("user %s Logged in BDUSS: %s" % (self.username, self.user["BDUSS"])) self._save_cookies()
def get_fm_user_info(self): """ 获取用户信息 :return: """ url = consts.FM_USER_INFO % fm_utils.timestamp() content = self._request(url) return content.json()
def get_next_play_list(self, channel_id, song_id): tt = str(fm_utils.timestamp()) baidu_uid = self.user['BAIDUID'] params = dict() params['_'] = tt url = 'ch_name=%s&item_id=%s&action_no=%d&userid=%d&baiduid=%s' % (channel_id, song_id, 2, 0, baidu_uid) api = consts.FM_NEXT_PLAY_LIST + url content = self._request(api, extra_params=params) song_ids = [int(play['songid']) for play in content.json()['list']] return song_ids
def get_next_play_list(self, channel_id, song_id): tt = str(fm_utils.timestamp()) baidu_uid = self.user["BAIDUID"] params = dict() params["_"] = tt url = "ch_name=%s&item_id=%s&action_no=%d&userid=%d&baiduid=%s" % (channel_id, song_id, 2, 0, baidu_uid) api = consts.FM_NEXT_PLAY_LIST + url content = self._request(api, extra_params=params) song_ids = [str(play["songid"]) for play in content.json()["list"]] return song_ids
def get_fm_user_counts(self): """ 获取用户听歌统计和hash_code :return: """ hashcode = self.user.get('hashcode', '') params = dict() params['_'] = fm_utils.timestamp() params['tn'] = 'usercounts' params['hashcode'] = hashcode content = self._request(consts.FM_API, extra_params=params) return content.json()
def get_fm_user_counts(self): """ 获取用户听歌统计和hash_code :return: """ hashcode = self.user.get("hashcode", "") params = dict() params["_"] = fm_utils.timestamp() params["tn"] = "usercounts" params["hashcode"] = hashcode content = self._request(consts.FM_API, extra_params=params) return content.json()
def dislike(self, song_id): """ 加入垃圾箱(不喜欢) :rtype : object """ url = consts.FM_DISLIKE % (song_id, fm_utils.timestamp()) content = self._request(url) data = content.json() error_code = data['errorCode'] or 0 if int(error_code) == 22000: return 1 else: return 0
def get_fm_play_list(self, channel_id='public_yuzhong_huayu'): """ 根据频道id获取播放列表 :param channel_id: :return: """ params = dict() params['_'] = fm_utils.timestamp() params['tn'] = 'playlist' params['channel_id'] = channel_id params['hashcode'] = '' content = self._request(consts.FM_API, extra_params=params) song_ids = [str(play['id']) for play in content.json()['list']] return song_ids
def get_fm_play_list(self, channel_id="public_yuzhong_huayu"): """ 根据频道id获取播放列表 :param channel_id: :return: """ params = dict() params["_"] = fm_utils.timestamp() params["tn"] = "playlist" params["channel_id"] = channel_id params["hashcode"] = "" content = self._request(consts.FM_API, extra_params=params) song_ids = [str(play["id"]) for play in content.json()["list"]] return song_ids
def _get_token(self): """ 获取token :return: """ token = '' token_path = os.path.join(consts.HOST_PATH, self.token_file) if os.path.exists(token_path): with open(token_path, 'r') as token_file: token = token_file.readline() if not token: token_url = consts.URL_TOKEN % fm_utils.timestamp() content = self._request(token_url).content.replace('\'', '\"') token = json.loads(content)['data']['token'] with open(token_path, 'w') as f: f.write(token) logger.info('get token: %s' % token) return token
def _get_token(self): """ 获取token :return: """ token = "" token_path = os.path.join(consts.HOST_PATH, self.token_file) if os.path.exists(token_path): with open(token_path, "r") as token_file: token = token_file.readline() if not token: token_url = consts.URL_TOKEN % fm_utils.timestamp() content = self._request(token_url).content.replace("'", '"') token = json.loads(content)["data"]["token"] with open(token_path, "w") as f: f.write(token) logger.info("get token: %s" % token) return token
def get_fm_channel_list(self): """ 获取频道信息 :return: """ if 'hashcode' not in self.user: user_count = self.get_fm_user_counts() hash_code = user_count['hash_code'] self.user['hashcode'] = hash_code else: hash_code = self.user['hashcode'] params = dict() params['_'] = fm_utils.timestamp() params['tn'] = 'channellist' params['hashcode'] = hash_code content = self._request(consts.FM_API, extra_params=params) channels = [(c['channel_name'], c['channel_id']) for c in content.json()['channel_list']] return channels
def get_fm_channel_list(self): """ 获取频道信息 :return: """ if "hashcode" not in self.user: user_count = self.get_fm_user_counts() hash_code = user_count["hash_code"] self.user["hashcode"] = hash_code else: hash_code = self.user["hashcode"] params = dict() params["_"] = fm_utils.timestamp() params["tn"] = "channellist" params["hashcode"] = hash_code content = self._request(consts.FM_API, extra_params=params) channels = [(c["channel_name"], c["channel_id"]) for c in content.json()["channel_list"]] return channels
def _get_public_key(self): url = consts.URL_PUBLIC_KEY % (self.user['token'], fm_utils.timestamp()) content = self._request(url).content data = json.loads(content.replace('\'', '"')) return data['pubkey'], data['key']