liuyuqi-dellpc 5 years ago
parent
commit
3cbc04bc57
6 changed files with 276 additions and 8 deletions
  1. 1 0
      .gitignore
  2. 20 8
      comment/comment.py
  3. 0 0
      utils/__init__.py
  4. 150 0
      utils/alive.py
  5. 55 0
      utils/collect.py
  6. 50 0
      utils/get_cookie.py

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 /.idea
 /.idea
+/utils/__pycache__/*.pyc

+ 20 - 8
comment/comment.py

@@ -38,17 +38,21 @@ baseHeader = {
     "Origin": baseUrl,
     "Origin": baseUrl,
     "kpf": "PC_WEB",
     "kpf": "PC_WEB",
     "kpn": "GAME_ZONE",
     "kpn": "GAME_ZONE",
-    'User-Agent': user_agent.getUA()
 }
 }
 
 
-
-def login(parameter_list):
+def login():
     """
     """
-    登录快手
+    登录快手,执行一次即可。
     param :
     param :
     return:
     return:
     """
     """
-    url = ""
+    url = baseUrl + "/rest/wd/live/liveStream/myfollow"
+    res=sessionClient.get(url=url)
+    print(res)
+
+def myfollow(parameter_list):
+
+    url = baseUrl + "/rest/wd/live/liveStream/myfollow"
     headers = ""
     headers = ""
 
 
     # cookies = requests.utils.dict_from_cookiejar(session.cookies)
     # cookies = requests.utils.dict_from_cookiejar(session.cookies)
@@ -60,6 +64,14 @@ def login(parameter_list):
 
 
     print(res)
     print(res)
 
 
+def collect(parameter_list):
+    """
+    param :
+    return:
+    """
+    url = baseHeader + "/rest/wd/live/web/collect"
+
+
 
 
 def comment():
 def comment():
     """
     """
@@ -80,19 +92,19 @@ def comment():
         "did": "web_804ededc5b702fbd39b9292e8fbd401a",
         "did": "web_804ededc5b702fbd39b9292e8fbd401a",
         "client_key": "65890b29",
         "client_key": "65890b29",
         "userId": "1365365171",
         "userId": "1365365171",
-        "userId": "1365365171",
         "kuaishou.live.web_st": "ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAVk6tclD4E4ts3EOu0AkqBSre4QzQIHJF4gGjASPMpwdd-LRKwmNJqM5Me_HpbjGgFdRi-DklvaR_UybDISWL-9AFj5mcGhGcH6QKbW-LaLy7q4tvXwKxd0TkzzXRH26uIlrrUaiFg2e9V7qUJ_Wd63zMjpJW9QlY5A51ls7ygE9AeOixq96fiQEq6bsFb0PjoW40kurt-Mdsu9cmpoW_6gaEm-zwBmcbUA4lm5ejQnh9kVjySIgNVsKf0l7b9hYQPICQLltkWi8863TWCzJndAqxL1jK-koBTAB",
         "kuaishou.live.web_st": "ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAVk6tclD4E4ts3EOu0AkqBSre4QzQIHJF4gGjASPMpwdd-LRKwmNJqM5Me_HpbjGgFdRi-DklvaR_UybDISWL-9AFj5mcGhGcH6QKbW-LaLy7q4tvXwKxd0TkzzXRH26uIlrrUaiFg2e9V7qUJ_Wd63zMjpJW9QlY5A51ls7ygE9AeOixq96fiQEq6bsFb0PjoW40kurt-Mdsu9cmpoW_6gaEm-zwBmcbUA4lm5ejQnh9kVjySIgNVsKf0l7b9hYQPICQLltkWi8863TWCzJndAqxL1jK-koBTAB",
         "kuaishou.live.bfb1s": "477cb0011daca84b36b3a4676857e5a1",
         "kuaishou.live.bfb1s": "477cb0011daca84b36b3a4676857e5a1",
         "kuaishou.live.web_ph": "f14d184cde684f6f7f1aae2f6eca47b8c34d"
         "kuaishou.live.web_ph": "f14d184cde684f6f7f1aae2f6eca47b8c34d"
     }
     }
 
 
-# 下面值是变动的cookie
-# "kuaishou.live.web_ph": "35642148eaef81c55d0c31082222fa91c2c7"
+    # 下面值是变动的cookie
+    # "kuaishou.live.web_ph": "35642148eaef81c55d0c31082222fa91c2c7"
 
 
     res = sessionClient.post(
     res = sessionClient.post(
         url=url, headers=mHeader, data=data, cookies=cookies,
         url=url, headers=mHeader, data=data, cookies=cookies,
         # proxies=proxies
         # proxies=proxies
     )
     )
+    print("finish")
     print(res.text)
     print(res.text)
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":

+ 0 - 0
utils/__init__.py


+ 150 - 0
utils/alive.py

@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@File    :   alive.py
+@Time    :   2019/05/26 12:14:04
+@Author  :   Liuyuqi 
+@Version :   1.0
+@Contact :   liuyuqi.gov@msn.cn
+@License :   (C)Copyright 2019
+@Desc    :   保持心跳
+
+https://live.kuaishou.com/rest/wd/live/liveStream/myfollow
+Request URL: https://live.kuaishou.com/rest/wd/live/liveStream/myfollow
+Request Method: POST
+Status Code: 200 OK
+Remote Address: 127.0.0.1:1080
+Referrer Policy: unsafe-url
+
+Accept: application/json
+Accept-Encoding: gzip, deflate, br
+Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
+Connection: keep-alive
+Content-Length: 0
+Cookie: did=web_4095924dbbb4d12df7a87725a16104b6; didv=1558790317127; Hm_lvt_86a27b7db2c5c0ae37fee4a8a35033ee=1558790317; kuaishou.live.bfb1s=9b8f70844293bed778aade6e0a8f9942; clientid=3; client_key=65890b29; Hm_lpvt_86a27b7db2c5c0ae37fee4a8a35033ee=1558793089; userId=1365365171; userId=1365365171; kuaishou.live.web_st=ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAZFHcXmdSz0ZqgeR8-TTgoi9QQRV_2I1NT-eK4Wr46MXF-NpYe4Fo00puYYJRRdqB4nJLluPiMpO7YGwHtxLbnttpwFVvU0_aelP9U7kgg9s1BVI9U1dGQsZ0_JLfL8137gJKr4CqwJaq-9LBhM5a_ATTdc5Te1meUd1st2HGg-Dy_g18QwLY84WirWBIaKm8R7r6Dd3fFKYm-gdtJYmMukaEk2hY_LIikBot7IUVtJ3ydB6KCIgx-OxXNSqmT0GdbC5rtc3vchNEkamnb0pAQf6txb5g3YoBTAB; kuaishou.live.web_ph=b1dec6f6601bf17d2cb5b72665242eea8524
+Host: live.kuaishou.com
+kpf: PC_WEB
+kpn: GAME_ZONE
+Origin: https://live.kuaishou.com
+Referer: https://live.kuaishou.com/u/fengyeYS
+User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
+
+res:
+
+'''
+
+import queue
+import threading
+import time
+
+import requests
+
+# import pymysql
+import utils.get_cookie as get_cookie
+
+baseUrl = "https://live.kuaishou.com"
+# baseUrl = "http://nginx.ok.yoqi.me:88"
+
+proxies = {"http": "http://120.26.110.59:8080"}
+# sessionClient=requests.session()
+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)
+
+
+def login(userPhone, cookies, userAgent, threadid):
+    """
+    登录快手,执行一次即可。
+    param :
+    return:
+    """
+    url = baseUrl + "/rest/wd/live/liveStream/myfollow"
+    mPartHeader = {
+        "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
+        "User-Agent": userAgent
+    }
+    mHeader = baseHeader.copy()
+    mHeader.update(mPartHeader)
+    proxies = ""
+    res = sessionClients[threadid].get(url=url, headers=mHeader, cookies=cookies,
+                                       # proxies=proxies
+                                       )
+    print(res.text)
+    if eval(res.text)["result"] != "200":
+        return False
+    else:
+        return True
+
+
+def myfollow(userPhone, cookies, userAgent, threadid):
+    '''
+    维持心跳,更新cookie。
+    :param userPhone:
+    :param cookies:
+    :param userAgent:
+    :param threadid:
+    :return:
+    '''
+    url = baseUrl + "/rest/wd/live/liveStream/myfollow"
+    mPartHeader = {
+        "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
+        "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
+    )
+    if eval(res.text)["result"] != "200":
+        return False
+    else:
+        return True
+
+
+def run(userPhone, cookies, userAgent, threadid):
+    '''
+    执行多线程批量登录,维持在线
+    :param userPhone: 用户手机号
+    :param cookies: 用户初始化cookie
+    :param userAgent: 用户agent
+    :param threadid: 线程id
+    :return:
+    '''
+    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))
+    while True:
+        state = myfollow(userPhone, cookies, userAgent, threadid)
+        if state == False:
+            print("thread {id}: User {userPhone}. is alive".format(id=threadid, userPhone=userPhone))
+            break
+        else:
+            print("thread {id}: User {userPhone}. is alive".format(id=threadid, userPhone=userPhone))
+        time.sleep(10)
+
+
+if __name__ == "__main__":
+    start_time = time.time()
+    mUser = get_cookie.getUser()
+    account_alive_count = len(mUser["userPhone"])
+    for i in range(3):
+        sessionClients.append(requests.session())  # 新建一个session
+        threading.Thread(target=run,
+                         args=(mUser["userPhone"][i], mUser["cookies"][i], mUser["userAgent"][i], i)).start()
+
+    print("last time: {} s".format(time.time() - start_time))

+ 55 - 0
utils/collect.py

@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@File    :   collect.py
+@Time    :   2019/05/26 12:29:25
+@Author  :   Liuyuqi 
+@Version :   1.0
+@Contact :   liuyuqi.gov@msn.cn
+@License :   (C)Copyright 2019
+@Desc    :   None
+
+https://live.kuaishou.com/rest/wd/live/web/collect
+
+Request URL: https://live.kuaishou.com/rest/wd/live/web/collect
+Request Method: POST
+Status Code: 200 OK
+Remote Address: 114.118.4.4:443
+Referrer Policy: unsafe-url
+
+Accept: */*
+Accept-Encoding: gzip, deflate, br
+Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
+Connection: keep-alive
+Content-Length: 3162
+Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryqCWW56sc8L7fQ2qJ
+Cookie: did=web_4095924dbbb4d12df7a87725a16104b6; didv=1558790317127; Hm_lvt_86a27b7db2c5c0ae37fee4a8a35033ee=1558790317; kuaishou.live.bfb1s=9b8f70844293bed778aade6e0a8f9942; clientid=3; client_key=65890b29; Hm_lpvt_86a27b7db2c5c0ae37fee4a8a35033ee=1558793089; userId=1365365171; userId=1365365171; kuaishou.live.web_st=ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAeLqVli2ylW_RqILieJA5M7WER8j-O6HgZ7dU5p1NUGVTdFetnPaPbE2w677IiOnBGMKIGlwFS3ZWgls-nm7MgtEeiYOqLJ7BL-fVgjHGevHk2i6DUW5aD7k8tAJyBzPyiSQcX-0oeTGqQMErN4DQ6hdfyuiGNYoLOOBTVbqLtwgmJlElEMtqr51L0skzodfrvpt65U-1FQ9cJpxJNKVG98aEhqtgBlt9k0TlHg5Y4Goo9D9kyIgNCO7Z8nLU6riZ2yR2yxVxRWy5wzGPpUjAE_gKAPjt9soBTAB; kuaishou.live.web_ph=21f148cee5fd1b69b2b06882a149f9954b54
+Host: live.kuaishou.com
+kpf: PC_WEB
+kpn: GAME_ZONE
+Origin: https://live.kuaishou.com
+Referer: https://live.kuaishou.com/u/miaowu66
+User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
+
+
+session_id: spKrN7MpJVGSvrnm
+page_id: eGZ7-VqgdI7llXcl_1558844873536
+refer_page_id: GdWPtfWNHnUBSHj9_1558844721551
+refer_show_id: 
+refer_url: https://live.kuaishou.com/
+page_live_stream_id: 6KxDomQOYyM
+url: https://live.kuaishou.com/u/miaowu66
+screen: 1366*768
+platform: Win32
+log_time: 1558844900450
+type: player
+client_timestamp: 1558844900450
+browser_name: Chrome
+browser_version: 73.0.3683.86
+os_type: Windows
+os_version: 10
+qos_info: {"kbytes_received":3787,"kernel_version":"1.4.4","session_id":"b2f359d6-e9e8-45a9-86f2-349b5154e0f8","tick_index":3,"play_url":"https://tx-adaptive.pull.yximgs.com/gifshow/6KxDomQOYyM.flv?txSecret=32f1e6718680ab01d7858b0d30aa6a0d&txTime=5ceb6749&stat=7Lm4rJjgbtQIPfFcNid0rYCnMX3bvXsAzFKMznfjlCEpA1dEQV1/d2dkWUkL3ovV&highTraffic=1&oidc=alihb","domain":"tx-adaptive.pull.yximgs.com","play_start_time":1558844874064,"tick_start":1558844894067,"tick_duration":6383,"block_count":1,"buffer_time":4168,"pause_duration":0,"play_duration":2215,"played_video_duration":2215,"a_buf_len":2276,"a_buf_slices":1,"v_buf_len":2272,"v_buf_slices":1,"speed_chg_metric":{"0.9x":6383},"block_count_with_buffer":0,"buffer_during_block":0,"decoded_video_frames":120,"dropped_video_frames":2,"adaptive_multi_rate":0,"play_pos":13228,"v_data_rate":8423,"a_data_rate":117,"error_code":0,"error_message":"","v_bandwidth":0,"buffer_queue_count":0,"v_segment_count":73,"a_segment_count":73,"hidden":0,"first_screen_total_duration":-1,"stream_duration":null,"skip_cnt":0,"skip_duration":0,"demuxed_video_duration":3451,"dropped_packet_duration":0,"v_rec_delay":16329,"v_render_delay":18275,"a_render_delay":18233,"stream_id":"6KxDomQOYyM","server_ip":"","retry_cnt":0,"cpu":0}
+
+'''
+
+

+ 50 - 0
utils/get_cookie.py

@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@File    :   get_cookie.py
+@Time    :   2019/05/26 11:53:58
+@Author  :   Liuyuqi 
+@Version :   1.0
+@Contact :   liuyuqi.gov@msn.cn
+@License :   (C)Copyright 2019
+@Desc    :   None
+'''
+
+
+def getCookies():
+    with open("../data/cookie.txt", "r") as f:
+        res = []
+        for line in f.readlines():
+            res.append(extract_cookies(line.split("----")[0]))
+        # print(res[0])
+        # cookies
+        # print(res[0].split("----")[0])
+        # print(res[0].split("----")[1])
+        # print(len(res))
+        return res
+
+
+def getUser():
+    with open("../data/cookie.txt", "r") as f:
+        mCookies = []
+        mUser = []
+        mUA = []
+        for line in f.readlines():
+            mCookies.append(extract_cookies(line.split("----")[0]))
+            mUA.append(line.split("----")[1])
+            mUser.append(str(extract_cookies(line.split("----")[2])).split(":")[4].split("\"")[1])
+        return {
+            "cookies": mCookies, "userPhone": mUser, "userAgent": mUA
+            }
+
+def extract_cookies(cookie):
+    """从浏览器或者request headers中拿到cookie字符串,提取为字典格式的cookies"""
+    cookies = dict([l.split("=", 1) for l in cookie.split("; ")])
+    return cookies
+
+    # if __name__ == "__main__":
+    # cookie = "continue=http://www.bj.10086.cn/service/fee/zdcx/; continuelogout=http://www.bj.10086.cn/service/fee/zdcx/; CmLocation=100|100; CmProvid=bj; WT_FPC=id=2e4f7c373760da4bead2e31489565793714:lv=1489635199762:ss=1489635053131; Webtrends=58.132.171.245.1489565794320746; JSESSIONID=0000UGBr1eur3P3Yp6EalieXCp8GP2T:16vf1jlcr; input_loginName=15210357242; c_loginName=15210357242; SSOTime=2017-03-15 16:17:16; mobileNo1=6251a6b7d69b5b5047495ale16ad8b5a5c149d6f2c0@@b50883ee4753ba0784210ce85435ee29c7oesl24a856f3@@1489565836722"
+    # cookies = extract_cookies(cookie)
+    # print(cookies)
+
+# print(getUser()["userPhone"])