import requests import time from log.print_log import PrintLog from libs.Xg04 import X_Gorgon from libs.douyin_pb2 import DouYin from libs.douyin_user_pb2 import DouYinUser from libs.proxy import Proxy from libs.en0414 import DouYinApi class DyBarrageInfo: @staticmethod def get_data(room_id): room_id = str(room_id) proxy = Proxy.get() proxies = { "http": "http://" + proxy, "https": "http://" + proxy } douApi = DouYinApi('', proxies) result = douApi.register_device() device_id, iid, udid, openudid, cookie = result['device_id'], result['iid'], result['uuid'], result['openudid'], \ result['cookie'] douApi.init_device_ids(device_id, iid, udid, openudid) url = 'http://webcast3-normal-c-lf.amemv.com/webcast/room/' + room_id + '/_fetch_message_polling/?' query_param = 'os_api=23&' \ 'device_type=HSF-FL00&' \ 'ssmix=a&' \ 'manifest_version_code=7301&' \ 'dpi=480&' \ 'app_name=aweme&' \ 'version_name=7.3.0&' \ 'app_type=normal&' \ 'ac=wifi&' \ 'host_abi=armeabi&' \ 'update_version_code=7302&' \ 'channel=douyin_tengxun_wzl&' \ '_rticket=1629772802255&' \ 'device_platform=android&' \ 'iid=' + iid + '&' \ 'version_code=730&' \ 'device_id=' + device_id + '&' \ 'resolution=1080*2340&' \ 'os_version=6.0.1&' \ 'language=zh&' \ 'device_brand=HUAWEI&' \ 'mcc_mnc=46000&' \ 'ts=' + str(int(time.time())) + '&' \ 'aid=1128' body = 'live_id=1&' \ 'parse_cnt=0&' \ 'recv_cnt=0&' \ 'cursor=0&' \ 'last_rtt=0&' \ 'identity=audience&' \ 'resp_content_type=protobuf&' \ 'user_id=3117857528618044' url = url + query_param x_gorgon = X_Gorgon(query_param, body) headers = { 'Cookie': cookie, 'user-agent': 'okhttp/3.10.0.1', "X-Gorgon": x_gorgon.get('X-Gorgon'), "X-Khronos": x_gorgon.get('X-Khronos'), "Connection": 'close', "X-SS-REQ-TICKET": str(int(time.time() * 1000)), "x-ss-stub": '6AC103439C12D2FED188040DFB6D7799', "Content-Type": 'application/x-www-form-urlencoded', } retry = 0 response_json = {'room_id': room_id, 'data': {}} while True: if retry > 10: break retry += 1 try: response = requests.post(url, data=body, headers=headers, verify=False, proxies=proxies) if(response.status_code == 200) and (response.content is not None) and (response.content != ''): douyin = DouYin() douyin.ParseFromString(response.content) for k, c in list(enumerate(douyin.messages)): if c.method == 'WebcastChatMessage': douyin_user = DouYinUser() douyin_user.ParseFromString(c.payload) user_info = douyin_user.user response_json['data'][k] = { "message_id": c.messageId, "content": douyin_user.content, "uid": user_info.uid, "gender": user_info.gender, "nickname": user_info.nickname, "short_id": user_info.short_id, "publisher_dy_id": user_info.dy_id, "publisher_sec_uid": user_info.sec_uid } else: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '爬取http连接失败!' + str( response.status_code) + '\n' + Proxy.proxy_info + '\n' + room_id + '\n' ) except requests.exceptions.ProxyError as e: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '代理过期!' + str(e) + '\n' + room_id + '\n' + Proxy.proxy_info ) Proxy.del_proxy(proxy) pass except requests.exceptions.ConnectTimeout as e: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' ConnectTimeout!' + str(e) + '\n' + room_id + '\n' + Proxy.proxy_info ) Proxy.del_proxy(proxy) pass except Exception as e: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '请求抛出异常!' + str(e) + '\n' + room_id + '\n' + Proxy.proxy_info ) pass if not bool(response_json['data']): return False return response_json