Skip to content

Commit

Permalink
Signed-off-by: alivon <[email protected]>
Browse files Browse the repository at this point in the history
  • Loading branch information
alivon committed Apr 12, 2019
1 parent 524c0f8 commit e862e79
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 0 deletions.
191 changes: 191 additions & 0 deletions Source Packages/pandalearning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import time
from sys import argv
import random
from pdlearn import version
from pdlearn import user
from pdlearn import dingding
from pdlearn import mydriver
from pdlearn import score
from pdlearn import threads
from pdlearn import get_links


def user_flag(dd_status, uname):
if dd_status:
cookies = dingding.dd_login_status(uname, has_dd=True)
else:
if (input("是否保存钉钉帐户密码,保存后可后免登陆学习(Y/N) ")) not in ["y", "Y"]:
driver_login = mydriver.Mydriver(nohead=False)
cookies = driver_login.login()
else:
cookies = dingding.dd_login_status(uname)
a_log = user.get_a_log(uname)
v_log = user.get_v_log(uname)

return cookies, a_log, v_log


def get_argv():
nohead = True
lock = False
stime = False
if len(argv) > 2:
if argv[2] == "hidden":
nohead = True
elif argv[2] == "show":
nohead = False
if len(argv) > 3:
if argv[3] == "single":
lock = True
elif argv[3] == "multithread":
lock = False
if len(argv) > 4:
if argv[4].isdigit():
stime = argv[4]
return nohead, lock, stime


def show_score(cookies):
total, each = score.get_score(cookies)
print("当前学习总积分:" + str(total))
print("阅读文章:{}/6,观看视频:{}/6,登陆:{}/1,文章时长:{}/6,视频时长:{}/6".format(*each))
return total, each


def article(cookies, a_log, each):
if each[0] < 6 or each[3] < 8:
driver_article = mydriver.Mydriver(nohead=nohead)
driver_article.get_url("https://www.xuexi.cn/notFound.html")
driver_article.set_cookies(cookies)
links = get_links.get_article_links()
try_count = 0
while True:
if each[0] < 6 and try_count < 10:
a_num = 6 - each[0]
for i in range(a_log, a_log + a_num):
driver_article.get_url(links[i])
time.sleep(random.randint(5, 15))
for j in range(120):
if random.random() > 0.5:
driver_article.go_js('window.scrollTo(0, document.body.scrollHeight/120*{})'.format(j))
print("\r文章学习中,文章剩余{}篇,本篇剩余时间{}秒".format(a_log + a_num - i, 120 - j), end="")
time.sleep(1)
driver_article.go_js('window.scrollTo(0, document.body.scrollHeight)')
total, each = show_score(cookies)
if each[0] >= 6:
print("检测到文章数量分数已满,退出学习")
break
a_log += a_num
else:
with open("./user/{}/a_log".format(uname), "w", encoding="utf8") as fp:
fp.write(str(a_log))
break
try_count = 0
while True:
if each[3] < 6 and try_count < 10:
num_time = 60
driver_article.get_url(links[a_log-1])
time.sleep(random.randint(5, 15))
remaining = (6 - each[3]) * 4 * num_time
for i in range(remaining):
if random.random() > 0.5:
driver_article.go_js(
'window.scrollTo(0, document.body.scrollHeight/{}*{})'.format(remaining, i))
print("\r文章时长学习中,文章总时长剩余{}秒".format(remaining - i), end="")
time.sleep(1)
if i % (120) == 0 and i != remaining:
total, each = show_score(cookies)
if each[3] >= 6:
print("检测到文章时长分数已满,退出学习")
break
driver_article.go_js('window.scrollTo(0, document.body.scrollHeight)')
total, each = show_score(cookies)
else:
break
if try_count < 10:
print("文章学习完成")
else:
print("文章学习出现异常,请检查用户名下a_log文件记录数")
driver_article.quit()
else:
print("文章之前学完了")


def video(cookies, v_log, each):
if each[1] < 6 or each[4] < 10:
driver_video = mydriver.Mydriver(nohead=nohead)
driver_video.get_url("https://www.xuexi.cn/notFound.html")
driver_video.set_cookies(cookies)
links = get_links.get_video_links()
try_count = 0
while True:
if each[1] < 6 and try_count < 10:
v_num = 6 - each[1]
for i in range(v_log, v_log + v_num):
driver_video.get_url(links[i])
time.sleep(random.randint(5, 15))
for j in range(180):
if random.random() > 0.5:
driver_video.go_js('window.scrollTo(0, document.body.scrollHeight/180*{})'.format(j))
print("\r视频学习中,视频剩余{}个,本次剩余时间{}秒".format(v_log + v_num - i, 180 - j), end="")
time.sleep(1)
driver_video.go_js('window.scrollTo(0, document.body.scrollHeight)')
total, each = show_score(cookies)
if each[1] >= 6:
print("检测到视频数量分数已满,退出学习")
break
v_log += v_num
else:
with open("./user/{}/v_log".format(uname), "w", encoding="utf8") as fp:
fp.write(str(v_log))
break
try_count = 0
while True:
if each[4] < 6 and try_count < 10:
num_time = 60
driver_video.get_url(links[v_log-1])
time.sleep(random.randint(5, 15))
remaining = (6 - each[4]) * 3 * num_time
for i in range(remaining):
if random.random() > 0.5:
driver_video.go_js(
'window.scrollTo(0, document.body.scrollHeight/{}*{})'.format(remaining, i))
print("\r视频学习中,视频总时长剩余{}秒".format(remaining - i), end="")
time.sleep(1)
if i % (180) == 0 and i != remaining:
total, each = show_score(cookies)
if each[4] >= 6:
print("检测到视频时长分数已满,退出学习")
break
driver_video.go_js('window.scrollTo(0, document.body.scrollHeight)')
total, each = show_score(cookies)
else:
break
if try_count < 10:
print("视频学习完成")
else:
print("视频学习出现异常,请检查用户名下v_log文件记录数")
driver_video.quit()
else:
print("视频之前学完了")


if __name__ == '__main__':
# 0 读取版本信息
start_time = time.time()
info_shread = threads.MyThread("获取更新信息...", version.up_info)
info_shread.start()
# 1 创建用户标记,区分多个用户历史纪录
dd_status, uname = user.get_user()
cookies, a_log, v_log = user_flag(dd_status, uname)
total, each = show_score(cookies)

nohead, lock, stime = get_argv()
article_thread = threads.MyThread("文章学习", article, cookies, a_log, each, lock=lock)
video_thread = threads.MyThread("视频学习", video, cookies, v_log, each, lock=lock)
article_thread.start()
video_thread.start()
article_thread.join()
video_thread.join()
print("总计用时" + str(int(time.time() - start_time) / 60) + "分钟")
user.shutdown(stime)
Empty file.
45 changes: 45 additions & 0 deletions Source Packages/pdlearn/dingding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import re
from pdlearn import mydriver
import sys


def get_dd():
while True:
dname = input('请输入正确的学习强国帐号(钉钉手机号):')
ret = re.match(r"^1[3-9]\d{9}$", dname)
if ret:
pwd = input("请输入学习强国密码:")
break
return dname, pwd


def dd_login_status(uname, has_dd=False):
while True:
if has_dd:
dname, pwd = load_dingding("./user/{}/dingding".format(uname))
print("读取用户信息成功")
else:
dname, pwd = get_dd()
driver_login = mydriver.Mydriver(noimg=False)
login_status = driver_login.dd_login(dname, pwd)
if login_status:
save_dingding("./user/{}/dingding".format(uname), dname, pwd)
cookies = driver_login.get_cookies()
break
return cookies


def save_dingding(user_path, dname, pwd):
with open(user_path, "w", encoding="utf8") as fp:
fp.write(dname + "," + pwd)


def load_dingding(user_path):
with open(user_path, "r", encoding="utf8") as fp:
try:
dname, pwd = fp.read().split(",")
return dname, pwd
except:
print("钉钉记录文件损坏,错误代码3程序退出")
sys.exit(3)

26 changes: 26 additions & 0 deletions Source Packages/pdlearn/get_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import requests
import re

def get_article_links():
article = requests.get(
"https://www.xuexi.cn/c06bf4acc7eef6ef0a560328938b5771/data9a3668c13f6e303932b5e0e100fc248b.js").content.decode(
"utf8")
pattern = r"list\"\:(.+),\"count\"\:"
links = []
list = eval(re.search(pattern, article).group(1))[:20000]
list.reverse()
for i in range(len(list)):
links.append(list[i]["static_page_url"])
return links


def get_video_links():
video = requests.get(
"https://www.xuexi.cn/4426aa87b0b64ac671c96379a3a8bd26/datadb086044562a57b441c24f2af1c8e101.js").content.decode(
"utf8")
pattern = r'https://www.xuexi.cn/[^,"]*html'
link = re.findall(pattern, video, re.I)
link.reverse()
return link


119 changes: 119 additions & 0 deletions Source Packages/pdlearn/mydriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions
from selenium.webdriver.chrome.options import Options
from pdlearn import user_agent
import os


class Mydriver:

def __init__(self, noimg=True, nohead=True):
self.options = Options()
if os.path.exists("./chrome/chrome.exe"): # win
self.options.binary_location = "./chrome/chrome.exe"
elif os.path.exists("/opt/google/chrome/chrome"): # linux
self.options.binary_location = "/opt/google/chrome/chrome"
if noimg:
self.options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
if nohead:
self.options.add_argument('--headless')
self.options.add_argument('--disable-extensions')
self.options.add_argument('--disable-gpu')
self.options.add_argument('--no-sandbox')
self.options.add_argument('--mute-audio') # 关闭声音
self.options.add_argument('--window-size=400,500')
self.options.add_argument('--window-position=800,0')
self.options.add_argument('--log-level=3')

self.options.add_argument('--user-agent={}'.format(user_agent.getheaders()))
self.options.add_experimental_option('excludeSwitches',
['enable-automation'])
self.webdriver = webdriver
if os.path.exists("./chrome/chromedriver.exe"): # win
self.driver = self.webdriver.Chrome(executable_path="./chrome/chromedriver.exe",
chrome_options=self.options)
elif os.path.exists("./chromedriver"): # linux
self.driver = self.webdriver.Chrome(executable_path="./chromedriver",
chrome_options=self.options)
elif os.path.exists("/usr/lib64/chromium-browser/chromedriver"): # linux 包安装chromedriver
self.driver = self.webdriver.Chrome(executable_path="/usr/lib64/chromium-browser/chromedriver",
chrome_options=self.options)
elif os.path.exists("/usr/local/bin/chromedriver"): # linux 包安装chromedriver
self.driver = self.webdriver.Chrome(executable_path="/usr/local/bin/chromedriver",
chrome_options=self.options)
else:
self.driver = self.webdriver.Chrome(chrome_options=self.options)

def login(self):
print("正在打开二维码登陆界面,请稍后")
self.driver.get("https://pc.xuexi.cn/points/login.html")
try:
remover = WebDriverWait(self.driver, 30, 0.2).until(
lambda driver: driver.find_element_by_class_name("redflagbox"))
except exceptions.TimeoutException:
print("网络缓慢,请重试")
else:
self.driver.execute_script('arguments[0].remove()', remover)
try:
remover = WebDriverWait(self.driver, 30, 0.2).until(
lambda driver: driver.find_element_by_class_name("header"))
except exceptions.TimeoutException:
print("当前网络缓慢...")
else:
self.driver.execute_script('arguments[0].remove()', remover)
try:
remover = WebDriverWait(self.driver, 30, 0.2).until(
lambda driver: driver.find_element_by_class_name("footer"))
except exceptions.TimeoutException:
print("当前网络缓慢...")
else:
self.driver.execute_script('arguments[0].remove()', remover)
self.driver.execute_script('window.scrollTo(document.body.scrollWidth/2 - 200 , 0)')
try:
WebDriverWait(self.driver, 270).until(EC.title_is(u"我的学习"))
cookies = self.get_cookies()
return cookies
except:
print("扫描二维码超时")

def dd_login(self, d_name, pwd):
__login_status = False
self.driver.get(
"https://login.dingtalk.com/login/index.htm?"
"goto=https%3A%2F%2Foapi.dingtalk.com%2Fconnect%2Foauth2%2Fsns_authorize"
"%3Fappid%3Ddingoankubyrfkttorhpou%26response_type%3Dcode%26scope%3Dsnsapi"
"_login%26redirect_uri%3Dhttps%3A%2F%2Fpc-api.xuexi.cn%2Fopen%2Fapi%2Fsns%2Fcallback"
)
self.driver.find_elements_by_id("mobilePlaceholder")[0].click()
self.driver.find_element_by_id("mobile").send_keys(d_name)
self.driver.find_elements_by_id("mobilePlaceholder")[1].click()
self.driver.find_element_by_id("pwd").send_keys(pwd)
self.driver.find_element_by_id("loginBtn").click()
try:
print("登陆中...")
WebDriverWait(self.driver, 2, 0.1).until(lambda driver: driver.find_element_by_class_name("modal"))
print(self.driver.find_element_by_class_name("modal").find_elements_by_tag_name("div")[0].text)
self.driver.quit()
__login_status = False
except:
__login_status = True
return __login_status

def get_cookies(self):
cookies = self.driver.get_cookies()
return cookies

def set_cookies(self, cookies):
for cookie in cookies:
self.driver.add_cookie({k: cookie[k] for k in cookie.keys()})

def get_url(self, url):
self.driver.get(url)

def go_js(self, js):
self.driver.execute_script(js)

def quit(self):
self.driver.quit()
Loading

0 comments on commit e862e79

Please sign in to comment.