Skip to content

Instantly share code, notes, and snippets.

@jyxjjj
Last active August 9, 2021 04:08
Show Gist options
  • Save jyxjjj/35afd58da47eec58ed7f96380c610c73 to your computer and use it in GitHub Desktop.
Save jyxjjj/35afd58da47eec58ed7f96380c610c73 to your computer and use it in GitHub Desktop.
Tmooc Downloader - To generate TMOOC videos' download links and ffmpeg commands.
#-*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import re
import time
import os, sys
from urllib import parse
dir = os.path.abspath(os.path.dirname(os.getcwd()))
def ParseCookiestr(cookie_str):
cookielist = []
for item in cookie_str.split('; '):
cookie={}
itemname = item.split('=')[0]
itemvalue = item.split('=')[1]
cookie['name']= itemname
cookie['value']= parse.unquote(itemvalue)
cookielist.append(cookie)
return cookielist
ua = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36"
options = webdriver.ChromeOptions()
options.add_argument('User-Agent=' + ua)
browser = webdriver.Chrome(executable_path="chromedriver.exe", options=options)
browser.implicitly_wait(8)
browser.get('http://tts.tmooc.cn/video/showVideo?menuId=693540&version=JSDVN201905')
# 注意 一定要将访问此域名的cookie放在同级目录的cookie.txt中,再启动。
# 注意 一定要将访问此域名的cookie放在同级目录的cookie.txt中,再启动。
with open('cookie.txt', encoding='utf-8') as cooke:
cookies = cooke.read()
cookielist = ParseCookiestr(cookies)
for i in cookielist:
cookie = {}
cookie['name'] = i['name']
cookie['value'] = i['value']
browser.delete_cookie(i['name'])
browser.add_cookie(cookie)
browser.get('http://tts.tmooc.cn/video/showVideo?menuId=690873&version=JSDVN201905')
time.sleep(3)
chain = ActionChains(browser)
for page in range(1,13):
elem = browser.find_element_by_xpath('/html/body/div[2]/div/div[3]/div[2]/div/div/div[1]/div[2]/div/p[%s]/a'%page)
chain.move_to_element(elem).perform()
elem.click()
time.sleep(3)
url = browser.find_element_by_xpath('/html/body/div[2]/div/div[1]/div/div/div/div/video/source').get_attribute('src')
print('ffmpeg -multiple_requests 1 -reconnect_streamed 1 -i "' + url +'" -vcodec h264 W:\\tmooc\\S4\\C1\\D1\\' + '%s'%page + '.mp4')
print('\r\n')
browser.quit()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment