pandalearning.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import time
  2. from sys import argv
  3. import random
  4. from pdlearn import version
  5. from pdlearn import user
  6. from pdlearn import dingding
  7. from pdlearn import mydriver
  8. from pdlearn import score
  9. from pdlearn import threads
  10. from pdlearn import get_links
  11. def user_flag(dd_status, uname):
  12. if dd_status:
  13. cookies = dingding.dd_login_status(uname, has_dd=True)
  14. else:
  15. if (input("是否保存钉钉帐户密码,保存后可后免登陆学习(Y/N) ")) not in ["y", "Y"]:
  16. driver_login = mydriver.Mydriver(nohead=False)
  17. cookies = driver_login.login()
  18. else:
  19. cookies = dingding.dd_login_status(uname)
  20. a_log = user.get_a_log(uname)
  21. v_log = user.get_v_log(uname)
  22. return cookies, a_log, v_log
  23. def get_argv():
  24. nohead = True
  25. lock = False
  26. stime = False
  27. if len(argv) > 2:
  28. if argv[2] == "hidden":
  29. nohead = True
  30. elif argv[2] == "show":
  31. nohead = False
  32. if len(argv) > 3:
  33. if argv[3] == "single":
  34. lock = True
  35. elif argv[3] == "multithread":
  36. lock = False
  37. if len(argv) > 4:
  38. if argv[4].isdigit():
  39. stime = argv[4]
  40. return nohead, lock, stime
  41. def show_score(cookies):
  42. total, each = score.get_score(cookies)
  43. print("当前学习总积分:" + str(total))
  44. print("阅读文章:{}/6,观看视频:{}/6,登陆:{}/1,文章时长:{}/6,视频时长:{}/6".format(*each))
  45. return total, each
  46. def article(cookies, a_log, each):
  47. if each[0] < 6 or each[3] < 8:
  48. driver_article = mydriver.Mydriver(nohead=nohead)
  49. driver_article.get_url("https://www.xuexi.cn/notFound.html")
  50. driver_article.set_cookies(cookies)
  51. links = get_links.get_article_links()
  52. try_count = 0
  53. while True:
  54. if each[0] < 6 and try_count < 10:
  55. a_num = 6 - each[0]
  56. for i in range(a_log, a_log + a_num):
  57. driver_article.get_url(links[i])
  58. time.sleep(random.randint(5, 15))
  59. for j in range(120):
  60. if random.random() > 0.5:
  61. driver_article.go_js('window.scrollTo(0, document.body.scrollHeight/120*{})'.format(j))
  62. print("\r文章学习中,文章剩余{}篇,本篇剩余时间{}秒".format(a_log + a_num - i, 120 - j), end="")
  63. time.sleep(1)
  64. driver_article.go_js('window.scrollTo(0, document.body.scrollHeight)')
  65. total, each = show_score(cookies)
  66. if each[0] >= 6:
  67. print("检测到文章数量分数已满,退出学习")
  68. break
  69. a_log += a_num
  70. else:
  71. with open("./user/{}/a_log".format(uname), "w", encoding="utf8") as fp:
  72. fp.write(str(a_log))
  73. break
  74. try_count = 0
  75. while True:
  76. if each[3] < 6 and try_count < 10:
  77. num_time = 60
  78. driver_article.get_url(links[a_log-1])
  79. time.sleep(random.randint(5, 15))
  80. remaining = (6 - each[3]) * 4 * num_time
  81. for i in range(remaining):
  82. if random.random() > 0.5:
  83. driver_article.go_js(
  84. 'window.scrollTo(0, document.body.scrollHeight/{}*{})'.format(remaining, i))
  85. print("\r文章时长学习中,文章总时长剩余{}秒".format(remaining - i), end="")
  86. time.sleep(1)
  87. if i % (120) == 0 and i != remaining:
  88. total, each = show_score(cookies)
  89. if each[3] >= 6:
  90. print("检测到文章时长分数已满,退出学习")
  91. break
  92. driver_article.go_js('window.scrollTo(0, document.body.scrollHeight)')
  93. total, each = show_score(cookies)
  94. else:
  95. break
  96. if try_count < 10:
  97. print("文章学习完成")
  98. else:
  99. print("文章学习出现异常,请检查用户名下a_log文件记录数")
  100. driver_article.quit()
  101. else:
  102. print("文章之前学完了")
  103. def video(cookies, v_log, each):
  104. if each[1] < 6 or each[4] < 10:
  105. driver_video = mydriver.Mydriver(nohead=nohead)
  106. driver_video.get_url("https://www.xuexi.cn/notFound.html")
  107. driver_video.set_cookies(cookies)
  108. links = get_links.get_video_links()
  109. try_count = 0
  110. while True:
  111. if each[1] < 6 and try_count < 10:
  112. v_num = 6 - each[1]
  113. for i in range(v_log, v_log + v_num):
  114. driver_video.get_url(links[i])
  115. time.sleep(random.randint(5, 15))
  116. for j in range(180):
  117. if random.random() > 0.5:
  118. driver_video.go_js('window.scrollTo(0, document.body.scrollHeight/180*{})'.format(j))
  119. print("\r视频学习中,视频剩余{}个,本次剩余时间{}秒".format(v_log + v_num - i, 180 - j), end="")
  120. time.sleep(1)
  121. driver_video.go_js('window.scrollTo(0, document.body.scrollHeight)')
  122. total, each = show_score(cookies)
  123. if each[1] >= 6:
  124. print("检测到视频数量分数已满,退出学习")
  125. break
  126. v_log += v_num
  127. else:
  128. with open("./user/{}/v_log".format(uname), "w", encoding="utf8") as fp:
  129. fp.write(str(v_log))
  130. break
  131. try_count = 0
  132. while True:
  133. if each[4] < 6 and try_count < 10:
  134. num_time = 60
  135. driver_video.get_url(links[v_log-1])
  136. time.sleep(random.randint(5, 15))
  137. remaining = (6 - each[4]) * 3 * num_time
  138. for i in range(remaining):
  139. if random.random() > 0.5:
  140. driver_video.go_js(
  141. 'window.scrollTo(0, document.body.scrollHeight/{}*{})'.format(remaining, i))
  142. print("\r视频学习中,视频总时长剩余{}秒".format(remaining - i), end="")
  143. time.sleep(1)
  144. if i % (180) == 0 and i != remaining:
  145. total, each = show_score(cookies)
  146. if each[4] >= 6:
  147. print("检测到视频时长分数已满,退出学习")
  148. break
  149. driver_video.go_js('window.scrollTo(0, document.body.scrollHeight)')
  150. total, each = show_score(cookies)
  151. else:
  152. break
  153. if try_count < 10:
  154. print("视频学习完成")
  155. else:
  156. print("视频学习出现异常,请检查用户名下v_log文件记录数")
  157. driver_video.quit()
  158. else:
  159. print("视频之前学完了")
  160. if __name__ == '__main__':
  161. # 0 读取版本信息
  162. start_time = time.time()
  163. info_shread = threads.MyThread("获取更新信息...", version.up_info)
  164. info_shread.start()
  165. # 1 创建用户标记,区分多个用户历史纪录
  166. dd_status, uname = user.get_user()
  167. cookies, a_log, v_log = user_flag(dd_status, uname)
  168. total, each = show_score(cookies)
  169. nohead, lock, stime = get_argv()
  170. article_thread = threads.MyThread("文章学习", article, cookies, a_log, each, lock=lock)
  171. video_thread = threads.MyThread("视频学习", video, cookies, v_log, each, lock=lock)
  172. article_thread.start()
  173. video_thread.start()
  174. article_thread.join()
  175. video_thread.join()
  176. print("总计用时" + str(int(time.time() - start_time) / 60) + "分钟")
  177. user.shutdown(stime)