def phone_login(): user = read_ini('account.ini', 'account') phone = user.get('phone') password = user.get('password') # print(phone, password) text = { 'phone': phone, 'password': hashlib.md5(password.encode()).hexdigest(), 'rememberLogin': '******' } data = encrypted_request(text) # 设置session session = requests.session() set_session(session) response = session.post(phone_login_url, headers=headers, data=data) response.encoding = 'utf-8' # 获取cookie和csrf并设置 cookies = dict(response.cookies) csrf = cookies.get('__csrf') set_csrf(csrf) return response
def __get_down_trial_songs(have_down_song_ids, only_trial_song_ids, song_ids): req = { "ids": [], "br": 999000, # 码率,默认设置了 999000 即最大码率,如果要 320k 则可设置为 320000,其他类推 "csrf_token": get_csrf() } split_arrays = ListUtil.list_split(song_ids, 500) for sa in split_arrays: req["ids"].clear() req["ids"].extend(sa) data = encrypted_request(req) response = get_session().post(playsheet_detail_url + get_csrf(), data=data, headers=headers) response.encoding = "UTF-8" r = response.json() details = r.get('data') for d in details: if d.get("code") == 404: # 404表示歌曲已经下架 have_down_song_ids.append(d.get("id")) if d.get("code") == -110: # -110表示歌曲只能试听 only_trial_song_ids.append(d.get("id")) print("休息休息5s...") time.sleep(5)
def __collect_song_to_self_by_keyword(response): keyword = input("输入要查询的关键字: ").strip() req_text = { 's': keyword, 'type': '1000', 'offset': 10, 'total': 'true', 'limit': 20 } data = encrypted_request(req_text) try: response = requests.post(search_songs_url, data=data, headers=headers) response.encoding = 'utf-8' r = response.json() print("休息休息5s...") time.sleep(5) if r.get('code') == 200: return else: print(response.text) except Exception as reason: print('__collect_song_to_self_by_keyword方法捕获异常:', reason) raise Exception
def __get_list(playlist_sheets): req_text = { "id": '', "offset": 1000, "total": True, "limit": 1000, "n": 1000, "csrf_token": csrf } song_ids = [] try: for playlist_sheet in playlist_sheets: req_text['id'] = playlist_sheet data = encrypted_request(req_text) response = get_session().post(playlist_detail_url + get_csrf(), data=data, headers=headers) response.encoding = 'UTF-8' r = response.json() track_ids = r.get('playlist').get('trackIds') song_ids.extend(track_id.get('id') for track_id in track_ids) print("休息休息5s...") time.sleep(5) return song_ids except Exception as reason: print('__get_list方法捕获异常:', reason) raise Exception
def __get_playlist(response): user_id = response['account']['id'] offset = 0 song_list = set() while True: # 请求参数 text = { "offset": offset, "uid": user_id, "limit": 100, "csrf_token": get_csrf() } data = encrypted_request(text) try: response = get_session().post(playlist_url + get_csrf(), data=data, headers=headers) response.encoding = 'utf-8' r = response.json() playlist = r.get("playlist") for s in playlist: # 在显示的歌单中会包含收藏的歌单,去除掉,一般前面全是自己的歌单,后面的则是收藏歌单 if s['userId'] == user_id: song_sheet = SongSheet(s['id'], s['name'], s['privacy']) song_list.add(song_sheet) if s['privacy'] == private_playlist_code: private_playlist_sheets.append(song_sheet) private_playlist_sheet_ids.append(s['id']) if s['privacy'] == public_playlist_code: public_playlist_sheets.append(song_sheet) public_playlist_sheet_ids.append(s['id']) else: break if r.get("more"): offset += 100 else: break print("休息休息5s...") time.sleep(5) except Exception as reason: print(reason) for song_sheet in song_list: print(song_sheet) print('')
def __get_follows(user_id, offset): url = 'https://music.163.com/weapi/user/getfollows/{0}?csrf_token={1}'.format( user_id, csrf) headers['Referer'] = 'http://music.163.com/user/follows?id={0}'.format( user_id) req = {'offset': offset, 'limit': limit, 'csrf_token': csrf} response = get_session().post(url, data=encrypted_request(req), headers=headers) response.encoding = 'utf-8' return response.json()
def __logout(response): req_text = {"csrf_token": csrf} data = encrypted_request(req_text) response = get_session().post(logout_url, data=data, headers=headers) response.encoding = 'UTF-8' r = response.json() if r.get('code') == 200: exit(0) else: exit(-1)
def phone_login(): user = __read_ini('account.ini', 'account') phone = user.get('phone') password = user.get('password') # print(phone, password) text = { 'phone': phone, 'password': hashlib.md5(password.encode()).hexdigest(), 'rememberLogin': '******', 'checkToken': CHECK_TOKEN } data = encrypted_request(text) try: # 设置session session = requests.session() set_session(session) proxies = get_unsafe_proxy_ip() session.trust_env = False response = session.post(phone_login_url, headers=headers, data=data, verify=False, proxies=proxies, timeout=10000) response.encoding = 'utf-8' # 获取cookie和csrf并设置 cookies = dict(response.cookies) csrf = cookies.get('__csrf') if csrf is None: print(response.text) # res = json.loads(response.text) exit(-1) set_csrf(csrf) print("休息休息5s...") time.sleep(5) except Exception as reason: print(reason) exit(1) return response
def __get_songs_detail(song_ids): if len(song_ids) == 0: return dict(songs=[]) req_text = {'ids': song_ids} data = encrypted_request(req_text) try: response = get_session().post(song_detail_url, data=data, headers=headers) response.encoding = 'UTF-8' r = response.json() print("休息休息5s...") time.sleep(5) return r except Exception as reason: print('__get_songs_detail方法捕获异常:', reason) raise Exception
def __remove_songs(song_sheet_id, song_ids): req_text = {'pid': song_sheet_id, 'trackIds': song_ids, 'op': 'del'} data = encrypted_request(req_text) try: response = get_session().post(add_and_remove_song_url_from_sheet, data=data, headers=headers) response.encoding = 'utf-8' r = response.json() print("休息休息5s...") time.sleep(5) if r.get('code') == 200: return else: print() except Exception as reason: print('__remove_songs方法捕获异常:', reason) raise Exception