alive.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @File : alive.py
  5. @Time : 2019/05/26 12:14:04
  6. @Author : Liuyuqi
  7. @Version : 1.0
  8. @Contact : liuyuqi.gov@msn.cn
  9. @License : (C)Copyright 2019
  10. @Desc : 保持心跳
  11. https://live.kuaishou.com/rest/wd/live/liveStream/myfollow
  12. Request URL: https://live.kuaishou.com/rest/wd/live/liveStream/myfollow
  13. Request Method: POST
  14. Status Code: 200 OK
  15. Remote Address: 127.0.0.1:1080
  16. Referrer Policy: unsafe-url
  17. Accept: application/json
  18. Accept-Encoding: gzip, deflate, br
  19. Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
  20. Connection: keep-alive
  21. Content-Length: 0
  22. 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
  23. Host: live.kuaishou.com
  24. kpf: PC_WEB
  25. kpn: GAME_ZONE
  26. Origin: https://live.kuaishou.com
  27. Referer: https://live.kuaishou.com/u/fengyeYS
  28. 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
  29. res:
  30. '''
  31. import queue
  32. import threading
  33. import time
  34. import requests
  35. # import pymysql
  36. import utils.get_cookie as get_cookie
  37. baseUrl = "https://live.kuaishou.com"
  38. # baseUrl = "http://nginx.ok.yoqi.me:88"
  39. proxies = {"http": "http://120.26.110.59:8080"}
  40. # sessionClient=requests.session()
  41. sessionClients = []
  42. baseHeader = {
  43. "Accept": "application/json",
  44. "Content-Type": "application/json;charset=UTF-8",
  45. "Origin": baseUrl,
  46. "kpf": "PC_WEB",
  47. "kpn": "GAME_ZONE",
  48. }
  49. account_alive_count = 0
  50. detail_url_queue = queue.Queue(maxsize=1000)
  51. def login(userPhone, cookies, userAgent, threadid):
  52. """
  53. 登录快手,执行一次即可。
  54. param :
  55. return:
  56. """
  57. url = baseUrl + "/rest/wd/live/liveStream/myfollow"
  58. mPartHeader = {
  59. "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
  60. "User-Agent": userAgent
  61. }
  62. mHeader = baseHeader.copy()
  63. mHeader.update(mPartHeader)
  64. proxies = ""
  65. res = sessionClients[threadid].get(url=url, headers=mHeader, cookies=cookies,
  66. # proxies=proxies
  67. )
  68. print(res.text)
  69. if eval(res.text)["result"] != "200":
  70. return False
  71. else:
  72. return True
  73. def myfollow(userPhone, cookies, userAgent, threadid):
  74. '''
  75. 维持心跳,更新cookie。
  76. :param userPhone:
  77. :param cookies:
  78. :param userAgent:
  79. :param threadid:
  80. :return:
  81. '''
  82. url = baseUrl + "/rest/wd/live/liveStream/myfollow"
  83. mPartHeader = {
  84. "Referer": "https://live.kuaishou.com/u/3xphut7zrw7zcbq",
  85. "User-Agent": userAgent
  86. }
  87. mHeader = baseHeader.copy()
  88. mHeader.update(mPartHeader)
  89. # cookies = requests.utils.dict_from_cookiejar(session.cookies)
  90. # print(session)
  91. res = sessionClients[threadid].get(
  92. url=url, headers=mHeader, cookies=cookies,
  93. # proxies=proxies
  94. )
  95. if eval(res.text)["result"] != "200":
  96. return False
  97. else:
  98. return True
  99. def run(userPhone, cookies, userAgent, threadid):
  100. '''
  101. 执行多线程批量登录,维持在线
  102. :param userPhone: 用户手机号
  103. :param cookies: 用户初始化cookie
  104. :param userAgent: 用户agent
  105. :param threadid: 线程id
  106. :return:
  107. '''
  108. state = False # 登录状态
  109. state = login(userPhone, cookies, userAgent, threadid)
  110. if state == False:
  111. print("thread {id}: User {userPhone}.login failure".format(id=threadid, userPhone=userPhone))
  112. return
  113. else:
  114. print("thread {id}: User {userPhone}.login success".format(id=threadid, userPhone=userPhone))
  115. while True:
  116. state = myfollow(userPhone, cookies, userAgent, threadid)
  117. if state == False:
  118. print("thread {id}: User {userPhone}. is alive".format(id=threadid, userPhone=userPhone))
  119. break
  120. else:
  121. print("thread {id}: User {userPhone}. is alive".format(id=threadid, userPhone=userPhone))
  122. time.sleep(10)
  123. if __name__ == "__main__":
  124. start_time = time.time()
  125. mUser = get_cookie.getUser()
  126. account_alive_count = len(mUser["userPhone"])
  127. for i in range(3):
  128. sessionClients.append(requests.session()) # 新建一个session
  129. threading.Thread(target=run,
  130. args=(mUser["userPhone"][i], mUser["cookies"][i], mUser["userAgent"][i], i)).start()
  131. print("last time: {} s".format(time.time() - start_time))