某教育网站课件资源缓存

  因业务需要,购买某教育网站课程学习,参考优化项目部署方法,虽深入了解后结合实际使用环境不大可能用于目标项目部署优化,好歹将课程资源下载下来存入公司资源库,待后续更需要的团队学习。

strBaseUrl="https://edu.xxxx.com"
strCourseID="xxxx"

function pause() {
  read -p "Press enter to contiune..."
}

function main() {
  nPageNumRoot=1
  nPageCountRoot=0
  bStart=false

  while true
  do
    # Get boot menu
    strRootMenuUrl="${strBaseUrl}/center/wejob/user/train-course-stage-ajax?train_id=${strCourseID}&size=100&page=${nPageNumRoot}"
    echo $strRootMenuUrl
    strCommand="curl -skL \"${strRootMenuUrl}\" $Arr51Headers -o tmp/rootmenu.json"
    eval $strCommand
    nPageNumRoot=$(jq ".data.current_page" tmp/rootmenu.json -r)
    nPageCountRoot=$(jq ".data.count_page" tmp/rootmenu.json -r)
    nPartCount=$(jq ".data.data[0].course|length" tmp/rootmenu.json -r)
    for i in $(seq 1 1 $nPartCount)
    do
      strPartName="$(jq ".data.data[0].course[].course_name" tmp/rootmenu.json -r|sed -n "${i}p"|sed "s/.*-//")"
      strPartName="Part${i}-${strPartName}"
      echo "===================${strPartName}==================="

      # Export current part of courseware's path
      export Path51Root="/root/Media/CKA/${strPartName}"
      if [[ ! -d "$Path51Root" ]]; then
        mkdir "$Path51Root"
      fi

      strPartUrl="$(jq ".data.data[0].course[].list_url" tmp/rootmenu.json -r|sed -n "${i}p")"
      strPartUrl="${strBaseUrl}${strPartUrl}"
      strPartUrl="$(echo "$strPartUrl"|sed "s/course-info/&-ajax/;s/train_id=[0-9]\+//")"
      echo "$strPartUrl"

      # Skip downloaded part of courseware
      if [[ "${strPartName:0:5}" == "Part3" ]]; then
        bStart=true
      fi

      if [[ $bStart == true ]]; then
        download_part "$strPartUrl"
      else
        continue
      fi
    done

    if [[ $nPageNumRoot -ge $nPageCountRoot ]]; then
      break
    else
      let nPageNumRoot+=1
    fi
  done

}

function download_part() {
  nPageNum=1
  nPageCount=0
  bStart=true

  while true
  do
    strInfoUrl="$1"
    strMenuUrl="${strInfoUrl}&size=20&page=${nPageNum}"

    echo "==============================================="
    echo "当前处理 第 ${nPageNum} 页内容"
    echo ${strMenuUrl}
    strCommand="curl -skL \"${strMenuUrl}\" $Arr51Headers -o tmp/menu.json"
    #echo $strCommand
    eval $strCommand
    nPageCount=$(jq ".data.count_page" tmp/menu.json)
    nPageNum=$(jq ".data.current_page" tmp/menu.json)

    nChapterCount=$(jq ".data.data|length" tmp/menu.json)
    echo "当前页包含共 ${nChapterCount} 章内容"
    for i in $(seq 1 1 $nChapterCount)
    do
      let nIndex=$i-1
      nCS=$(jq ".data.data[$nIndex].chapter_sort" tmp/menu.json -r)
      if [[ $nCS -lt 10 ]]; then
        nCS="0${nCS}"
      fi
      nCN=$(jq ".data.data[$nIndex].chapter_name" tmp/menu.json -r)
      strChapterName="第${nCS}章_${nCN}"
      echo "当前处理: ${strChapterName}"

      # Skip downloaded courses
      if [[ "${strChapterName:0:4}" == "第13章" ]]; then
        bStart=true
      fi

      # Export new save cache
      export Path51Download="$Path51Root/$strChapterName"

      if [[ $bStart == true ]]; then
        # Create chapter directory
        if [[ ! -d "$Path51Root/${strChapterName}" ]]; then
          mkdir "$Path51Root/${strChapterName}"
        fi

        nLessonNum=$(jq ".data.data[$nIndex].list|length" tmp/menu.json -r)
        echo "当前章节共含 ${nLessonNum} 节课程"
        for j in $(seq 1 1 $nLessonNum)
        do
          strCourseName=$(jq ".data.data[$nIndex].list[].lesson_name" tmp/menu.json -r|sed -n "${j}p")
          strCourseNum=$(jq ".data.data[$nIndex].list[].show_number" tmp/menu.json -r|sed -n "${j}p"|sed "s/.*-//")
          if [[ $strCourseNum -lt 10 ]]; then
            strCourseNum="0${strCourseNum}"
          fi
          strSaveName="${strCourseNum}-${strCourseName}"
          echo "准备下载课程: ${strSaveName}"

          if [[ -f "${Path51Download}/${strSaveName}.mp4" ]]; then
            echo -e "\t当前课程已存在于: ${Path51Download}/${strSaveName}.mp4"
            continue
          fi

          strPlayUrl="$(jq ".data.data[$nIndex].list[].play_url" tmp/menu.json -r|sed -n "${j}p")"
          strPlayUrl="${strBaseUrl}/${strPlayUrl}"
          echo $strPlayUrl
          strCommand="curl -skL \"${strPlayUrl}\" $Arr51Headers -o tmp/course.html"
          #echo $strCommand
          eval $strCommand

          strType=$(sed -n "/username:/{n;s/.* .\([^ ]*\).,.*/\1/;p}" tmp/course.html)
          echo -e "\t类型: ${strType}"
          strLessonId=$(sed -n "/username:/{n;n;s/.* .\([^ ]*\).,.*/\1/;p}" tmp/course.html)
          echo -e "\t课件ID: ${strLessonId}"
          strId=$(sed -n "/username:/{n;n;n;s/.* .\([^ ]*\).,.*/\1/;p}" tmp/course.html)
          echo -e "\t视频ID: ${strId}"
          strSign=$(sed -n "/username:/{n;n;n;n;s/.* .\([^ ]*\).,.*/\1/;p}" tmp/course.html)
          echo -e "\t签名: ${strSign}"
          strAuthUrl="${strBaseUrl}/center/player/play/vod-play-auth?type=${strType}&lesson_id=${strLessonId}&id=${strId}&sign=${strSign}&lesson_type=course&is_try=0"
          echo $strAuthUrl
          #pause

          strCommand="curl -skL \"${strAuthUrl}\" $Arr51Headers -o tmp/course.json"
          #echo $strCommand
          eval $strCommand
          strAuth="$(jq ".data.info.playAuth" tmp/course.json -r)"
          #echo "The auth string is: ${strAuth}"

          m3u8-downloader aliyun -o="$Path51Download" -f "${strSaveName}.mp4" -p "$strAuth"

          #pause
        done
      fi
    done

    if [[ $nPageNum -ge $nPageCount ]]; then
      break
    else
      let nPageNum+=1
    fi
  done
}

main

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值