liuyuqi-dellpc 5 years ago
parent
commit
4d7a6fa432
3 changed files with 220 additions and 29 deletions
  1. 6 8
      comment/comment.py
  2. 213 19
      utils/alive.py
  3. 1 2
      utils/get_cookie.py

+ 6 - 8
comment/comment.py

@@ -26,8 +26,8 @@ import user_agent
 # os.chdir(src)
 # sys.path.append(src)
 
-baseUrl = "https://live.kuaishou.com"
-# baseUrl = "http://nginx.ok.yoqi.me:88"
+# baseUrl = "https://live.kuaishou.com"
+baseUrl = "http://nginx.ok.yoqi.me:88"
 
 proxies = {"http": "http://120.26.110.59:8080"}
 sessionClient = requests.session()
@@ -71,8 +71,6 @@ def collect(parameter_list):
     """
     url = baseHeader + "/rest/wd/live/web/collect"
 
-
-
 def comment():
     """
     给主播评论
@@ -81,7 +79,7 @@ def comment():
     """
     url = baseUrl+"/comment"
     mPartHeader = {
-        "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
+        "Referer": "https://live.kuaishou.com/u/3x7zqyj9au3wzaw",
     }
     mHeader = baseHeader.copy()
     mHeader.update(mPartHeader)
@@ -89,12 +87,12 @@ def comment():
         "utf8")
     cookies = {
         "clientid": "3",
-        "did": "web_804ededc5b702fbd39b9292e8fbd401a",
+        "did": "web_4095924dbbb4d12df7a87725a16104b6",
         "client_key": "65890b29",
         "userId": "1365365171",
-        "kuaishou.live.web_st": "ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAVk6tclD4E4ts3EOu0AkqBSre4QzQIHJF4gGjASPMpwdd-LRKwmNJqM5Me_HpbjGgFdRi-DklvaR_UybDISWL-9AFj5mcGhGcH6QKbW-LaLy7q4tvXwKxd0TkzzXRH26uIlrrUaiFg2e9V7qUJ_Wd63zMjpJW9QlY5A51ls7ygE9AeOixq96fiQEq6bsFb0PjoW40kurt-Mdsu9cmpoW_6gaEm-zwBmcbUA4lm5ejQnh9kVjySIgNVsKf0l7b9hYQPICQLltkWi8863TWCzJndAqxL1jK-koBTAB",
+        "kuaishou.live.web_st": "ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAUuoAahJmqz3gcRCt5L_K5-otg-5bj5Rg_WChLSt2KDdpifZzHPHXqoxiOeswJoSyGLTrKAnS3VP-lnD9ryAWhPUnEWXfEBL80S9HDu3DUwCv10QFoFmpaRtV7RQs1oCEk8yr0vJH_UaKbG9OOToVcg8cPMpSuVkweBvf14jZJsjL7Ak0dHZukgrBfQ_uglsbdl2xIRYVYNitxg3yZtq-HkaEhq2DZZ7DUHSmXUzdRyAf3O3USIgQt6RuXtFQd4lTRGIeHeD4-JehbLHLMHVo6PoztW7wuUoBTAB",
         "kuaishou.live.bfb1s": "477cb0011daca84b36b3a4676857e5a1",
-        "kuaishou.live.web_ph": "f14d184cde684f6f7f1aae2f6eca47b8c34d"
+        "kuaishou.live.web_ph": "19c53e5f66220620785d73d07a347e8e76b3"
     }
 
     # 下面值是变动的cookie

+ 213 - 19
utils/alive.py

@@ -33,6 +33,7 @@ res:
 
 '''
 
+import json
 import queue
 import threading
 import time
@@ -43,7 +44,12 @@ import requests
 import utils.get_cookie as get_cookie
 
 baseUrl = "https://live.kuaishou.com"
+baseTokenUrl = "https://id.kuaishou.com"
+# 房间号
+# https://live.kuaishou.com/u/lol111112
+room = baseUrl + "/u/Q1998227"
 # baseUrl = "http://nginx.ok.yoqi.me:88"
+# baseTokenUrl="http://nginx.ok.yoqi.me:88"
 
 proxies = {"http": "http://120.26.110.59:8080"}
 # sessionClient=requests.session()
@@ -51,10 +57,7 @@ sessionClients = []
 
 baseHeader = {
     "Accept": "application/json",
-    "Content-Type": "application/json;charset=UTF-8",
     "Origin": baseUrl,
-    "kpf": "PC_WEB",
-    "kpn": "GAME_ZONE",
 }
 account_alive_count = 0
 detail_url_queue = queue.Queue(maxsize=1000)
@@ -66,22 +69,128 @@ def login(userPhone, cookies, userAgent, threadid):
     param :
     return:
     """
-    url = baseUrl + "/rest/wd/live/liveStream/myfollow"
+    url = baseTokenUrl + "/pass/kuaishou/login/passToken"
     mPartHeader = {
-        "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
+        "Content-Type": "application/x-www-form-urlencoded",
+        "Referer": room,
         "User-Agent": userAgent
     }
     mHeader = baseHeader.copy()
     mHeader.update(mPartHeader)
-    proxies = ""
-    res = sessionClients[threadid].get(url=url, headers=mHeader, cookies=cookies,
-                                       # proxies=proxies
-                                       )
+    proxies = []
+    res = sessionClients[threadid].post(url=url, headers=mHeader, cookies=cookies, data="sid=kuaishou.live.web"
+                                        # proxies=proxies
+                                        )
     print(res.text)
-    if eval(res.text)["result"] != "200":
+    resText = json.loads(res.text)
+    if resText["result"] != 1:
+        print("thread {id}: User {userPhone}.passToken failure".format(id=threadid, userPhone=userPhone))
         return False
     else:
-        return True
+        url2 = baseUrl + "/user/login"
+        mPartHeader = {
+            "Content-Type": "application/json;charset=UTF-8",
+            "Referer": room,
+            "kpn": "GAME_ZONE",
+            "kpf": "PC_WEB",
+            "User-Agent": userAgent
+        }
+        mHeader2 = baseHeader.copy()
+        mHeader2.update(mPartHeader)
+        cookie_dict = {
+            "kuaishou.live.web.at": resText["kuaishou.live.web.at"],
+            "kuaishou.live.web_st": resText["kuaishou.live.web_st"],
+        }
+        cookies.update(cookie_dict)
+        sessionClients[threadid].cookies.update(cookie_dict)
+        mData = {"authToken": resText["kuaishou.live.web.at"], "sid": "kuaishou.live.web"}
+        mData = json.dumps(mData).replace("", "")
+        res2 = sessionClients[threadid].post(url=url2, headers=mHeader2, data=mData, cookies=cookies
+                                             # proxies=proxies
+                                             )
+        # urllib.request.
+        print(res2.text)
+        if res2.text == "'Bad Request'":
+            print("thread {id}: User {userPhone}.login failure".format(id=threadid, userPhone=userPhone))
+            return False
+        if json.loads(res2.text)["result"] != 1:
+            print("thread {id}: User {userPhone}.login failure".format(id=threadid, userPhone=userPhone))
+            return False
+        else:
+            print("thread {id}: User {userPhone}.login success".format(id=threadid, userPhone=userPhone))
+            return True
+
+
+def goToRoom(userPhone, cookies, userAgent, threadid):
+    '''
+    进入直播间
+    :param userPhone:
+    :param cookies:
+    :param userAgent:
+    :param threadid:
+    :return:
+    '''
+    url = room
+    mPartHeader = {
+        "Content-Type": "application/json;charset=UTF-8",
+        "Referer": room,
+        "User-Agent": userAgent
+    }
+    mHeader = baseHeader.copy()
+    mHeader.update(mPartHeader)
+    # cookies = requests.utils.dict_from_cookiejar(session.cookies)
+    # print(session)
+    res = sessionClients[threadid].get(
+        url=url, headers=mHeader,
+        # proxies=proxies
+    )
+    print("进入直播间结果:" + res.text[0:200])
+
+
+def firstcollect(userPhone, cookies, userAgent, threadid):
+    url = baseUrl + ""
+    '''
+    session_id: yLZP77qeoyWVjfJV
+    page_id: 1ReS9GIUNs-VyxWJ_1558897018534
+    refer_page_id: eM4eCCCOFHxvnT70_1558896991974
+    refer_show_id: 
+    refer_url: https://live.kuaishou.com/u/Q1998227
+    page_live_stream_id: 9PLaXTwT6vs
+    url: https://live.kuaishou.com/u/Q1998227
+    screen: 1366*768
+    platform: Win32
+    log_time: 1558897029472
+    type: error
+    errorDetail: Error: WebSocket timeout
+    '''
+    pass
+
+
+def watchingFeed(userPhone, cookies, userAgent, threadid):
+    url = baseUrl + "/wd/live/watchingFeed"
+    data = {"liveStreamId": "9PLaXTwT6vs"}
+    sessionClients[threadid].post(url=url, data=data)
+    pass
+
+def livefeed(userPhone, cookies, userAgent, threadid):
+    '''
+
+    :param userPhone:
+    :param cookies:
+    :param userAgent:
+    :param threadid:
+    :return:
+    '''
+    url = baseUrl + "/rest/wd/live/feed/v2"
+    data = '{"liveStreamId":"9PLaXTwT6vs","pageId":"1ReS9GIUNs-VyxWJ_1558897018534","isHeartbeat":true}'
+    data2='{"liveStreamId":"9PLaXTwT6vs","commentCursor":"syR2en5SdlY","giftCursor":"tQO_AlQBrO4","pageId":"1ReS9GIUNs-VyxWJ_1558897018534","isHeartbeat":false}'
+    sessionClients[threadid].post(url=url, data=data)
+    pass
+
+def wssLogin():
+    url = "wss://live-ws-pg-group3.kuaishou.com/websocket"
+
+    pass
 
 
 def myfollow(userPhone, cookies, userAgent, threadid):
@@ -95,7 +204,8 @@ def myfollow(userPhone, cookies, userAgent, threadid):
     '''
     url = baseUrl + "/rest/wd/live/liveStream/myfollow"
     mPartHeader = {
-        "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
+        "Content-Type": "application/json;charset=UTF-8",
+        "Referer": room,
         "User-Agent": userAgent
     }
     mHeader = baseHeader.copy()
@@ -106,7 +216,84 @@ def myfollow(userPhone, cookies, userAgent, threadid):
         url=url, headers=mHeader, cookies=cookies,
         # proxies=proxies
     )
-    if eval(res.text)["result"] != "200":
+    print(res.text)
+
+    if (not json.loads(res.text).get("result")) or json.loads(res.text)["result"] != 1:
+        return False
+    else:
+        return True
+
+
+def collect(userPhone, cookies, userAgent, threadid):
+    {
+        "kbytes_received": 5865,
+        "kernel_version": "1.4.4",
+        "session_id": "326fa539-4620-4892-a38f-42e0163c92d1",
+        "tick_index": 18,
+        "play_url": "https://tx-adaptive.pull.yximgs.com/gifshow/9PLaXTwT6vs.flv?txSecret=3d78c0935d21b9fc7a2cc869280bc1fc&txTime=5cec3114&stat=K5DJraklhpYTr46EqykzzDPJ3eereyUOENGTqe69syLjKCw6RVyHE3Gg0nSpfL/6&oidc=txhb",
+        "domain": "tx-adaptive.pull.yximgs.com",
+        "play_start_time": 1558896531540,
+        "tick_start": 1558896701559,
+        "tick_duration": 10002,
+        "block_count": 0,
+        "buffer_time": 0,
+        "pause_duration": 0,
+        "play_duration": 10002,
+        "played_video_duration": 10002,
+        "a_buf_len": 9880,
+        "a_buf_slices": 1,
+        "v_buf_len": 9903,
+        "v_buf_slices": 1,
+        "speed_chg_metric": {
+            "0.9x": 259,
+            "1.1x": 7214
+        },
+        "block_count_with_buffer": 0,
+        "buffer_during_block": 0,
+        "decoded_video_frames": 95,
+        "dropped_video_frames": 4,
+        "adaptive_multi_rate": 0,
+        "play_pos": 158379,
+        "v_data_rate": 7981,
+        "a_data_rate": 114,
+        "error_code": 0,
+        "error_message": "",
+        "v_bandwidth": 0,
+        "buffer_queue_count": 0,
+        "v_segment_count": 102,
+        "a_segment_count": 102,
+        "hidden": 0,
+        "first_screen_total_duration": -1,
+        "stream_duration": "null",
+        "skip_cnt": 0,
+        "skip_duration": 0,
+        "demuxed_video_duration": 5916,
+        "dropped_packet_duration": 0,
+        "v_rec_delay": 9984,
+        "v_render_delay": 22865,
+        "a_render_delay": 22872,
+        "stream_id": "9PLaXTwT6vs",
+        "server_ip": "",
+        "retry_cnt": 0,
+        "cpu": 0
+    }
+    url = baseUrl + "/rest/wd/live/liveStream/myfollow"
+    mPartHeader = {
+        "Content-Type": "application/json;charset=UTF-8",
+        "Referer": room,
+        "User-Agent": userAgent
+    }
+    mHeader = baseHeader.copy()
+    mHeader.update(mPartHeader)
+    # cookies = requests.utils.dict_from_cookiejar(session.cookies)
+    # print(session)
+    res = sessionClients[threadid].get(
+        url=url, headers=mHeader, cookies=cookies,
+        # proxies=proxies
+    )
+    print(res.text)
+
+    if (not json.loads(res.text).get("result")) or json.loads(res.text)["result"] != 1:
         return False
     else:
         return True
@@ -123,11 +310,8 @@ def run(userPhone, cookies, userAgent, threadid):
     '''
     state = False  # 登录状态
     state = login(userPhone, cookies, userAgent, threadid)
-    if state == False:
-        print("thread {id}: User {userPhone}.login failure".format(id=threadid, userPhone=userPhone))
-        return
-    else:
-        print("thread {id}: User {userPhone}.login success".format(id=threadid, userPhone=userPhone))
+    # 进入直播间
+    goToRoom(userPhone, cookies, userAgent, threadid)
     while True:
         state = myfollow(userPhone, cookies, userAgent, threadid)
         if state == False:
@@ -138,11 +322,21 @@ def run(userPhone, cookies, userAgent, threadid):
         time.sleep(10)
 
 
+def logout():
+    '''
+    登出,如果需要暂停,则登出所有账号
+    :return:
+    '''
+    pass
+
+
 if __name__ == "__main__":
     start_time = time.time()
     mUser = get_cookie.getUser()
     account_alive_count = len(mUser["userPhone"])
-    for i in range(3):
+    # print(account_alive_count)
+    # exit()
+    for i in range(account_alive_count):
         sessionClients.append(requests.session())  # 新建一个session
         threading.Thread(target=run,
                          args=(mUser["userPhone"][i], mUser["cookies"][i], mUser["userAgent"][i], i)).start()

+ 1 - 2
utils/get_cookie.py

@@ -10,7 +10,6 @@
 @Desc    :   None
 '''
 
-
 def getCookies():
     with open("../data/cookie.txt", "r") as f:
         res = []
@@ -25,7 +24,7 @@ def getCookies():
 
 
 def getUser():
-    with open("../data/cookie.txt", "r") as f:
+    with open("../data/cookie.txt", "r",encoding="utf8",errors="ignore") as f:
         mCookies = []
         mUser = []
         mUA = []