Skip to content

Instantly share code, notes, and snippets.

@zhanghuabin
Created July 22, 2020 03:54
Show Gist options
  • Save zhanghuabin/3b27458cbd8a10eaf2cfcbee3064abf1 to your computer and use it in GitHub Desktop.
Save zhanghuabin/3b27458cbd8a10eaf2cfcbee3064abf1 to your computer and use it in GitHub Desktop.
自动完成课程脚本
#!/usr/bin/env bash
# Prerequisites:
# 1. Install pup
# brew install pup
# 2. Install jq
# brew install jq
# set -v
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
cookie=$BJJNTS_COOKIE
user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
content_type_text_html='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
lang='zh-CN,zh;q=0.9'
function accomplish_lesson() {
course_id=$1
lesson_id=$2
duration=$3
curl -s "https://www.bjjnts.cn/addstudentTaskVer2/$course_id/$lesson_id" \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "Cookie: ${cookie}" \
-H 'Origin: https://www.bjjnts.cn' \
-H "Referer: https://www.bjjnts.cn/lessonStudy/$course_id/$(expr $lesson_id - 1)" \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H "User-Agent: ${user_agent}" \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Accept: */*' \
-H 'Accept-Language: zh-CN,zh;q=0.9' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
--data-raw "learnTime=${duration}" > /dev/null
}
function accomplish_course() {
course_id=$1
first_lesson_id=$2
course_and_first_lesson_url="https://www.bjjnts.cn/lessonStudy/$course_id/$first_lesson_id"
first_lesson_html_page=$(curl -s ${course_and_first_lesson_url} \
-H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' \
-H "User-Agent: ${user_agent}" \
-H "Accept: ${content_type_text_html}" \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Referer: https://www.bjjnts.cn/userCourse' \
-H "Accept-Language: ${lang}" \
-H "Cookie: ${cookie}")
lesson_ids=$(echo $first_lesson_html_page | pup '.course_study_sonmenu a attr{data-lessonid}')
lesson_durations=$(echo $first_lesson_html_page | pup '.course_study_sonmenu a p.course_study_menudate text{}' | sed 's/(\(.*\))/\1/g' | awk -F':' '{ print $1 * 3600 + $2 * 60 + $3 }')
lesson_id_and_durations=$(paste -d' ' <(echo "$lesson_ids") <(echo "$lesson_durations"))
while read lesson_id_and_duration
do
accomplish_lesson $(echo "$course_id $lesson_id_and_duration")
echo "✔ 又TM熬过一堂课 📙:$course_id $lesson_id_and_duration"
done <<< "$lesson_id_and_durations"
}
function accomplish_courses() {
course_and_first_lessons=$(curl -s 'https://www.bjjnts.cn/userCourse' \
-H 'Connection: keep-alive' \
-H 'Cache-Control: max-age=0' \
-H 'Upgrade-Insecure-Requests: 1' \
-H "User-Agent: ${user_agent}" \
-H "Accept: ${content_type_text_html}" \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Referer: https://www.bjjnts.cn/userSetting' \
-H "Accept-Language: ${lang}" \
-H "Cookie: ${cookie}" | pup '.tinywan_try_view json{}' | jq -r '.[] | .["data-id"] + " " + .["data-cid"]')
while read course_and_first_lesson
do
echo "⏳ 正在混一门课 📚:$course_and_first_lesson"
accomplish_course $(echo $course_and_first_lesson)
echo "✔ 终于混完一门课 📚:$course_and_first_lesson"
done <<< "$course_and_first_lessons"
}
echo -e "${GREEN}逃学威龙:v1991.7.18${NC}"
echo -e "欢迎反馈⮕ ${CYAN}zhanghb@cheche365.com${NC}"
if ! pup --version > /dev/null 2>&1;
then
echo -e "✘ 未安装${RED}pup${NC},执行${GREEN}brew install pup${NC}"
exit 1
fi
if ! jq --version > /dev/null 2>&1;
then
echo -e "✘ 未安装${RED}jq${NC},执行${GREEN}brew install jq${NC}"
exit 1
fi
if [ -z "$BJJNTS_COOKIE" ]
then
echo -e "✘ 必须指定名为${RED}BJJNTS_COOKIE${NC}的环境变量,此变量的值取自浏览器登录后的${GREEN}开发者工具${NC}"
exit 1
else
echo '✔ 环境就绪,开始完成所有名下课程'
accomplish_courses
echo '🍺 放学喽~'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment