student.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. package student
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/tebeka/selenium"
  9. "github.com/tebeka/selenium/chrome"
  10. "github.com/tidwall/gjson"
  11. "io"
  12. "io/ioutil"
  13. "net/http"
  14. "os"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. func GetAcounts(filename string) ([][]string, error) {
  20. fi, err := os.Open(filename)
  21. if err != nil {
  22. return nil, err
  23. }
  24. defer fi.Close()
  25. ret := [][]string{}
  26. br := bufio.NewReader(fi)
  27. for {
  28. a, _, c := br.ReadLine()
  29. if c == io.EOF {
  30. break
  31. }
  32. array := strings.Split(string(a), ";")
  33. if len(array) != 3 {
  34. continue
  35. }
  36. item := []string{array[0], array[1], array[2]}
  37. ret = append(ret, item)
  38. }
  39. return ret, nil
  40. }
  41. func Displayed(by, elementName string) func(selenium.WebDriver) (bool, error) {
  42. return func(wd selenium.WebDriver) (ok bool, err error) {
  43. var el selenium.WebElement
  44. el, err = wd.FindElement(by, elementName)
  45. if err != nil {
  46. ok = false
  47. err = nil
  48. return
  49. }
  50. ok, err = el.IsDisplayed()
  51. return
  52. }
  53. }
  54. func StartOneStudent(usernameText string, passwordText string, realName string) (*selenium.Service, selenium.WebDriver, error) {
  55. // TODO 更改路径
  56. var (
  57. //driverPath = `C:\Users\lqzlt\AppData\Local\Google\Chrome\Application\chromedriver.exe`
  58. driverPath = `chromedriver.exe`
  59. port = 60002
  60. )
  61. service, err := selenium.NewChromeDriverService(driverPath, port, []selenium.ServiceOption{}...)
  62. if nil != err {
  63. return nil, nil, err
  64. }
  65. defer func() {
  66. //_ = service.Stop()
  67. }()
  68. // Connect to the WebDriver instance running locally.
  69. caps := selenium.Capabilities{"browserName": "chrome"}
  70. //禁止图片加载,加快渲染速度
  71. imagCaps := map[string]interface{}{
  72. //"profile.managed_default_content_settings.images": 2,
  73. //"profile.default_content_setting_values":2,
  74. "same-site-by-default-cookies":2,
  75. "cookies-without-same-site-must-be-secure":2,
  76. }
  77. chromeCaps := chrome.Capabilities{
  78. Prefs: imagCaps,
  79. Path: "",
  80. ExcludeSwitches:[]string{"enable-automation"},
  81. //Extensions:[]string{"D:\\sql2struct"},
  82. Args: []string{
  83. //"--headless",
  84. //"--disable-javascript",
  85. "--no-sandbox",
  86. "--disable-gpu-sandbox",
  87. "--accept-Encoding=gzip, deflate",
  88. "--accept-language=zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6",
  89. "--cache-control=max-age=0",
  90. "--abc=d",
  91. "--load-extention=D:\\sql2struct",
  92. //"--cookie=MoodleSession=66925citrm0o6b68gsfn9quaof;username=2051101450211;UserName=2051101450211",
  93. "--moodleSession=66925citrm0o6b68gsfn9quaof;username=2051101450211",
  94. "--upgrade-insecure-requests=1",
  95. //"--authorization=Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkYyREVCM0ZGMURBMUFEODNFOTA5QjIxMjM4QTZCMjU5RjAyQjNGMDEiLCJ0eXAiOiJKV1QiLCJ4NXQiOiI4dDZ6X3gyaHJZUHBDYklTT0theVdmQXJQd0UifQ.eyJuYmYiOjE2MzYwMTAzNTMsImV4cCI6MTYzNjAxMzk1MywiaXNzIjoiaHR0cDovL3Bhc3Nwb3J0Lm91Y2huLmNuIiwiYXVkIjpbImh0dHA6Ly9wYXNzcG9ydC5vdWNobi5jbi9yZXNvdXJjZXMiLCJvdWNobnN0dWRlbnRzcGFjZWFwaSJdLCJjbGllbnRfaWQiOiJzdHVkZW50c3BhY2UiLCJzdWIiOiJlZjkzZTdlYi0zYjhlLTQ2ZTUtYjA1ZC1mYTJmYjAwYjA2ZmUiLCJhdXRoX3RpbWUiOjE2MzYwMTAyMDMsImlkcCI6ImxvY2FsIiwibmFtZSI6IjIwNTExMDE0NTAyMTEiLCJVc2VySWQiOiJlZjkzZTdlYi0zYjhlLTQ2ZTUtYjA1ZC1mYTJmYjAwYjA2ZmUiLCJVc2VyTmFtZSI6IjIwNTExMDE0NTAyMTEiLCJuaWNrbmFtZSI6IueOi-aVjyIsIkF2YXRhciI6IiIsIlVzZXJUeXBlIjoiMiIsIlByb3ZpbmNlU2Nob29sQ29kZSI6IjUxMSIsIk9yZ2FuaXphdGlvbkNvZGUiOiI1MTE1NDAwIiwiTW9iaWxlQ29uZmlybWVkIjoiRmFsc2UiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwib3VjaG51c2VyIiwib3VjaG5zdHVkZW50c3BhY2VhcGkiLCJvZmZsaW5lX2FjY2VzcyJdLCJhbXIiOlsicHdkIl19.ACwdaY50NnuXiQso4T6dS1PoQc7K2rTBRKWz0VmocC40xL4PlPy3Usf3kAZf-1dNFnh8PTBUWc9s_F6eTp_Rcemu5IBGL-iKs9I-QMeG3u7TnaNbCL4XwUt8vZoOPHaXzor3gN7WEqs01Qj11nTbERZ2ixEMhk2Ov_lGqCS8me67pyvd2HWCP7yUcizfEk4_5FQBZZ12daRpXrBksCC27I8BcpXQLrogWewa1W4x5uhLFeW7BKhBGxYDT4yM-b0RNM2jNTxn-ToqoOgBIsLsnU8e0eOKi6WBuhbDYK8psiCPt1hT_3BumeuJ-P36iuaPLcNv7D2T1WlCMHJe1Fv92Q",
  96. "--connection=keep-alive",
  97. //"--host=chengdu.ouchn.cn",
  98. "--accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
  99. "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
  100. },
  101. }
  102. //以上是设置浏览器参数
  103. caps.AddChrome(chromeCaps)
  104. wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
  105. if err != nil {
  106. return service, nil, errors.New(fmt.Sprintf("connect to the webDriver failed:%v\n", err))
  107. }
  108. //time.Sleep(120*time.Second)
  109. defer func() {
  110. //time.Sleep(60*time.Second)
  111. //fmt.Printf("!! quit window\n")
  112. //_ = wd.Close()
  113. }()
  114. err = wd.Get("http://www.ouchn.cn/#")
  115. if err != nil {
  116. return service, wd, err
  117. }
  118. //fmt.Printf("1111111\n")
  119. err = wd.WaitWithTimeout(Displayed(selenium.ByXPATH, "//button[text()='学生登录']"), 10*time.Second)
  120. if err != nil {
  121. return service, wd, errors.New(fmt.Sprintf("获取学生登录按钮失败:%v\n", err))
  122. }
  123. //fmt.Printf("111112\n")
  124. button, err := wd.FindElement(selenium.ByXPATH, "//button[text()='学生登录']")
  125. if err != nil {
  126. return service, wd, errors.New(fmt.Sprintf("获取学生登录按钮失败:%v\n", err))
  127. }
  128. err = button.Click()
  129. //time.Sleep(1000*time.Second)
  130. //fmt.Printf("11113:%v\n", err)
  131. err = wd.WaitWithTimeout(Displayed(selenium.ByID, "loginbth"), 5*time.Second)
  132. if err != nil {
  133. return service, wd, errors.New(fmt.Sprintf("获取登录页面失败:%v\n", err))
  134. }
  135. //fmt.Printf("11114\n")
  136. username, err := wd.FindElement(selenium.ByID, "username")
  137. if err != nil {
  138. return service, wd,errors.New(fmt.Sprintf("获取用户名按钮:%v\n", err))
  139. }
  140. password, err := wd.FindElement(selenium.ByID, "password")
  141. if err != nil {
  142. return service, wd,errors.New(fmt.Sprintf("获取密码按钮:%v\n", err))
  143. }
  144. submit, err := wd.FindElement(selenium.ByID, "loginbth")
  145. if err != nil {
  146. return service, wd,errors.New(fmt.Sprintf("获取登录按钮:%v\n", err))
  147. }
  148. err = username.SendKeys(usernameText)
  149. if err != nil {
  150. return service, wd,errors.New(fmt.Sprintf("填充用户名:%v\n", err))
  151. }
  152. err = password.SendKeys(passwordText)
  153. if err != nil {
  154. return service, wd,errors.New(fmt.Sprintf("填充密码:%v\n", err))
  155. }
  156. err = submit.Click()
  157. if err != nil {
  158. return service, wd,errors.New(fmt.Sprintf("点击登录:%v\n", err))
  159. }
  160. err = wd.WaitWithTimeout(Displayed(selenium.ByXPATH, "//button[text()='进入学习']"), 5*time.Second)
  161. if err != nil {
  162. return service, wd,errors.New(fmt.Sprintf("进入学生主页失败:%v\n", err))
  163. }
  164. buttons, _ := wd.FindElements(selenium.ByTagName, "button")
  165. currentWindow, err := wd.CurrentWindowHandle()
  166. if err != nil {
  167. return service, wd,errors.New(fmt.Sprintf("获取当前窗口失败:%v\n", err))
  168. }
  169. //fmt.Printf("xxx:%d\n", len(buttons))
  170. for _, button := range buttons {
  171. text, _ := button.Text()
  172. if text != "进入学习" {
  173. continue
  174. }
  175. //fmt.Printf("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n")
  176. button.Click()
  177. //time.Sleep(60*time.Second)
  178. count := 0
  179. handled := ""
  180. for count < 5 {
  181. time.Sleep(1*time.Second)
  182. handles, _ := wd.WindowHandles()
  183. if len(handles) > 1 {
  184. handled = handles[len(handles)-1]
  185. wd.SwitchWindow(handled)
  186. break
  187. }
  188. count++
  189. }
  190. if count > 5 {
  191. continue
  192. }
  193. CourseHandle(wd, realName)
  194. wd.CloseWindow(handled)
  195. err = wd.SwitchWindow(currentWindow)
  196. //fmt.Printf("switch window:%v\n", err)
  197. }
  198. return service, wd, nil
  199. }
  200. func waitUrl(wd selenium.WebDriver, url1 string, url2 string, count int) string {
  201. for count >= 0 {
  202. time.Sleep(100 *time.Millisecond)
  203. url, _ := wd.CurrentURL()
  204. if url != url1 && url != url2 {
  205. return url
  206. }
  207. count--
  208. }
  209. return ""
  210. }
  211. func getSectionIdFromUrl(url string) (string, string, string) {
  212. array := strings.Split(url, "?")
  213. if len(array) != 2 {
  214. return "", "", ""
  215. }
  216. array = strings.Split(array[1], "&")
  217. if len(array) != 3 {
  218. return "", "", ""
  219. }
  220. couseId := ""
  221. sectionId := ""
  222. mid := ""
  223. for _, v := range array {
  224. subArray := strings.Split(v, "=")
  225. if len(subArray) != 2 {
  226. continue
  227. }
  228. if subArray[0] == "id" {
  229. couseId = subArray[1]
  230. continue
  231. }
  232. if subArray[0] == "sectionid" {
  233. sectionId = subArray[1]
  234. continue
  235. }
  236. if subArray[0] == "mid" {
  237. mid = subArray[1]
  238. }
  239. }
  240. return couseId, sectionId, mid
  241. }
  242. type secIdInfo struct {
  243. Id string
  244. Number int
  245. }
  246. func BubbleAsort(values []secIdInfo) {
  247. for i := 0; i < len(values)-1; i++ {
  248. for j := i+1; j < len(values); j++ {
  249. if values[i].Number>values[j].Number{
  250. values[i],values[j] = values[j],values[i]
  251. }
  252. }
  253. }
  254. }
  255. func GetSections(courseId string, sectionId string, cookie string, url string) ([]string, error) {
  256. str := fmt.Sprintf("sectionid=%s&courseid=%s", sectionId, courseId)
  257. //fmt.Printf("get sections:%v,%v\n", str, cookie)
  258. reader := bytes.NewReader([]byte(str))
  259. request, err := http.NewRequest("POST", url+"/sectionInfo.php", reader)
  260. if err != nil {
  261. return nil, err
  262. }
  263. //request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  264. request.Header.Set("Cookie", cookie)
  265. request.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
  266. request.Header.Set("Host", "chengdu.ouchn.cn")
  267. //request.Header.Set("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
  268. request.Header.Set("Referer", "http://chengdu.ouchn.cn/course/view.php?id=3257&sectionid=129167&mid=375459")
  269. request.Header.Set("Origin", "http://chengdu.ouchn.cn")
  270. //request.Header.Set("Accept-Encoding", "gzip, deflate")
  271. request.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6")
  272. request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
  273. request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  274. request.Header.Set("Accept", "*/*")
  275. request.Header.Set("X-Requested-With","XMLHttpRequest")
  276. request.Header.Set("Connection", "keep-alive")
  277. client := http.Client{}
  278. resp, err := client.Do(request)
  279. if err != nil {
  280. return nil, err
  281. }
  282. defer resp.Body.Close()
  283. respBytes, err := ioutil.ReadAll(resp.Body)
  284. if err != nil {
  285. return nil, err
  286. }
  287. //fmt.Printf("origin origin%s\n", respBytes)
  288. m := map[string]interface{}{}
  289. json.Unmarshal(respBytes, &m)
  290. t, ok := m["secinfo"]
  291. if !ok {
  292. return nil, errors.New("can not find secinfo")
  293. }
  294. sec, ok := t.(map[string]interface{})
  295. if !ok {
  296. return nil, errors.New("can not convert secinfo")
  297. }
  298. ret := []string{}
  299. secArray := []secIdInfo{}
  300. tmpArray := []string{}
  301. //fmt.Printf("sec origin:%v\n", sec)
  302. for k, v := range sec {
  303. tt, ok := v.(map[string]interface{})
  304. if !ok {
  305. continue
  306. }
  307. if id, ok := tt["id"]; ok {
  308. if ids, ok2 := id.(string); ok2 && ids != "" {
  309. //ret = append(ret, ids)
  310. digit, _ := strconv.Atoi(k)
  311. if digit > 0 {
  312. secArray = append(secArray, secIdInfo{Id:ids, Number:digit})
  313. } else {
  314. tmpArray = append(tmpArray, ids)
  315. }
  316. }
  317. }
  318. }
  319. BubbleAsort(secArray)
  320. for _, v := range secArray {
  321. ret = append(ret, v.Id)
  322. }
  323. ret = append(ret, tmpArray...)
  324. return ret, nil
  325. }
  326. type McidInfo struct {
  327. Id string
  328. Name string
  329. }
  330. func GetCourseSectionVediosAndQuiz(courseId string, sectionId string, cookie string, url string)([]McidInfo, []McidInfo, error){
  331. //fmt.Printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n")
  332. str := fmt.Sprintf("sectionid=%s&courseid=%s", sectionId, courseId)
  333. reader := bytes.NewReader([]byte(str))
  334. request, err := http.NewRequest("POST", url+"/sectionInfo.php", reader)
  335. if err != nil {
  336. return nil, nil, err
  337. }
  338. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  339. request.Header.Set("Cookie", cookie)
  340. request.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36")
  341. request.Header.Set("Host", "chengdu.ouchn.cn")
  342. client := http.Client{}
  343. resp, err := client.Do(request)
  344. if err != nil {
  345. return nil, nil, err
  346. }
  347. defer resp.Body.Close()
  348. respBytes, err := ioutil.ReadAll(resp.Body)
  349. if err != nil {
  350. return nil, nil, err
  351. }
  352. // fmt.Printf("content:%s,%s\n", cookie, respBytes)
  353. m := map[string]interface{}{}
  354. json.Unmarshal(respBytes, &m)
  355. vedio := []McidInfo{}
  356. quiz := []McidInfo{}
  357. vm := map[string]McidInfo{}
  358. qm := map[string]McidInfo{}
  359. for k, v := range m {
  360. t, ok := v.(map[string]interface{})
  361. if !ok {
  362. continue
  363. }
  364. if isCom, ok := t["is_com"]; !ok {
  365. continue
  366. } else {
  367. x := fmt.Sprintf("%v", isCom)
  368. if strings.Contains(x, "1") || strings.Contains(x, "true") {
  369. continue
  370. }
  371. }
  372. if videourl, ok := t["videourl"]; ok && videourl != ""{
  373. //fmt.Printf("%v,%v\n", t["name"], t["is_com"])
  374. name := fmt.Sprintf("%v", t["name"])
  375. if !strings.Contains(name, "视频") {
  376. //continue
  377. }
  378. if k == "378172" {
  379. //fmt.Printf("sec:%v\n", sectionId)
  380. }
  381. vedio = append(vedio, McidInfo{Id:k, Name:name})
  382. vm[k] = McidInfo{Id:k, Name:name}
  383. continue
  384. }
  385. if ttype, ok := t["type"]; ok && ttype == "quiz" {
  386. name := fmt.Sprintf("%v", t["name"])
  387. quiz = append(quiz, McidInfo{Id:k, Name:name})
  388. qm[k] = McidInfo{Id:k, Name:name}
  389. }
  390. }
  391. array := gjson.GetBytes(respBytes, "sequence").Array()
  392. sec := []string{}
  393. for _, v := range array {
  394. str := v.String()
  395. subArray := strings.Split(str, ",")
  396. for _, sv := range subArray {
  397. sec = append(sec, sv)
  398. }
  399. }
  400. vedio = make([]McidInfo, len(vm))
  401. quiz = make([]McidInfo, len(qm))
  402. i := 0
  403. j := 0
  404. for _, v := range sec {
  405. if info, ok := vm[v]; ok{
  406. vedio[i] = info
  407. i++
  408. continue
  409. }
  410. if info, ok := qm[v]; ok {
  411. quiz[j] = info
  412. j++
  413. }
  414. }
  415. return vedio, quiz, nil
  416. }
  417. func getCookies(wd selenium.WebDriver) (string, error) {
  418. cookies, err := wd.GetCookies()
  419. if err != nil {
  420. return "", errors.New(fmt.Sprintf("获取cookie 失败,%v\n", err))
  421. }
  422. if len(cookies) == 0 {
  423. return "", errors.New(fmt.Sprintf("获取cookie 失败,%v\n", "cookie为空"))
  424. }
  425. ret := ""
  426. for _, v := range cookies {
  427. item := fmt.Sprintf("%s=%s", v.Name, v.Value)
  428. if ret == "" {
  429. ret = item
  430. continue
  431. }
  432. ret = ret + ";"
  433. ret = ret + item
  434. }
  435. return ret, nil
  436. }
  437. func getSectionIds(wd selenium.WebDriver, cookie string) (string, []string, string, error) {
  438. handledUrl := map[string]bool{}
  439. sectionIdM := map[string]bool{}
  440. datas, err := wd.FindElements(selenium.ByXPATH, "//a[@class='aalink' and contains(@href, 'chengdu.ouchn.cn/mod/page')]")
  441. if err != nil {
  442. return "", nil, "", errors.New(fmt.Sprintf("获取课程详细页面失败%v\n", err))
  443. }
  444. urls := make([]string, len(datas))
  445. for i, data := range datas {
  446. url,_ := data.GetAttribute("href")
  447. if url != "" {
  448. urls[i] = url
  449. }
  450. }
  451. //fmt.Printf("urls:%v\n", urls)
  452. for i, url := range urls {
  453. if i >= 5 {
  454. return "", nil, "", errors.New("未找到section")
  455. }
  456. currentUrl, _ := wd.CurrentURL()
  457. if handledUrl[url] {
  458. continue
  459. }
  460. //fmt.Printf("url:%v\n", url)
  461. err = wd.Get(url)
  462. handledUrl[url] = true
  463. time.Sleep(2*time.Second)
  464. if err != nil {
  465. continue
  466. }
  467. newUrl := waitUrl(wd, currentUrl, url, 20)
  468. if !strings.Contains(newUrl, "id=") ||
  469. !strings.Contains(newUrl, "sectionid=") ||
  470. !strings.Contains(newUrl, "mid=") {
  471. continue
  472. }
  473. courseId, sectionId,_ := getSectionIdFromUrl(newUrl)
  474. if courseId == "" || sectionId == "" {
  475. continue
  476. }
  477. if sectionIdM[sectionId] {
  478. continue
  479. }
  480. link, err := wd.FindElement(selenium.ByXPATH, "//link[contains(@href, 'chengdu.ouchn.cn/theme/blueonionre')]")
  481. if err != nil {
  482. continue
  483. }
  484. href, _ := link.GetAttribute("href")
  485. baseUrl := "http://chengdu.ouchn.cn/theme/blueonionre"
  486. if strings.Contains(href, "blueonionres") {
  487. baseUrl = "http://chengdu.ouchn.cn/theme/blueonionres"
  488. }
  489. sections, err := GetSections(courseId, sectionId, cookie, baseUrl)
  490. if err != nil {
  491. return "", nil, "", errors.New(fmt.Sprintf("获取section失败%v\n", sections))
  492. }
  493. if len(sections) > 0 {
  494. //fmt.Printf("xxxxxxx url:%v\n", url)
  495. //time.Sleep(100*time.Second)
  496. return courseId, sections, baseUrl, nil
  497. }
  498. time.Sleep(1*time.Second)
  499. }
  500. return "", nil, "", errors.New("位找到section")
  501. }
  502. func vedioCompleteUrl(cmid string, id string, sectionid string) string {
  503. //http://chengdu.ouchn.cn/theme/blueonionre/modulesCompletion.php?cmid=271461&id=2564&sectionid=92046
  504. return fmt.Sprintf("http://chengdu.ouchn.cn/theme/blueonionre/modulesCompletion.php?cmid=%s&id=%s&sectionid=%s", cmid, id, sectionid)
  505. }
  506. func vedioComplete(wd selenium.WebDriver, cmid string, id string, sectionid string) {
  507. fmt.Printf("vedio compliete:%s\n", vedioCompleteUrl(cmid, id, sectionid))
  508. wd.Get(vedioCompleteUrl(cmid, id, sectionid))
  509. //fmt.Printf("vedioComplete err:%v\n", err)
  510. time.Sleep(10*time.Second)
  511. }
  512. func CourseHandle(wd selenium.WebDriver, username string) error {
  513. cookie, err := getCookies(wd)
  514. if err != nil {
  515. return err
  516. }
  517. title, err := wd.Title()
  518. if title == "" {
  519. return nil
  520. }
  521. if err == nil {
  522. title = strings.TrimPrefix(title, "课程:")
  523. }
  524. if !strings.Contains(title, "习近平新时代中国特色社会主义思想") &&
  525. !strings.Contains(title, "毛泽东思想和中国特色社会主义理论体系概论") &&
  526. !strings.Contains(title, "马克思主义基本原理概论") &&
  527. !strings.Contains(title, "思想道德与法治") &&
  528. !strings.Contains(title, "思想道德修养与法律基础") {
  529. return nil
  530. }
  531. fmt.Printf("***************正在处理%s课程%s\n", username, title)
  532. //return nil
  533. courseId, sections, baseURL, err := getSectionIds(wd, cookie)
  534. if err != nil {
  535. return err
  536. }
  537. fmt.Printf("sections:%v\n", sections)
  538. //time.Sleep(100*time.Second)
  539. vedioM := map[string][]McidInfo{}
  540. quizM := map[string][]McidInfo{}
  541. for _, v := range sections {
  542. time.Sleep(1*time.Second)
  543. vedios, quiz, err := GetCourseSectionVediosAndQuiz(courseId, v, cookie, baseURL)
  544. if err != nil {
  545. //fmt.Printf("GetCourseSectionVediosAndQuiz err:%v\n", err)
  546. continue
  547. }
  548. if len(vedios) > 0 {
  549. vedioM[v] = vedios
  550. }
  551. if len(quiz) > 0 {
  552. quizM[v] = quiz
  553. }
  554. }
  555. //fmt.Printf("vedioM:%v\n", vedioM)
  556. for _, sec := range sections {
  557. mids := vedioM[sec]
  558. //fmt.Printf("vedioM:%v\n", vedioM)
  559. for _, mid := range mids {
  560. fmt.Printf("%s 视频开始处理\n", mid.Name)
  561. url := fmt.Sprintf("http://chengdu.ouchn.cn/mod/url/view.php?id=%s", mid.Id)
  562. //fmt.Printf("手动url:%s\n", url)
  563. current, _ := wd.CurrentURL()
  564. wd.Get(url)
  565. newUrl := waitUrl(wd, current, url, 20)
  566. if newUrl == "" {
  567. continue
  568. }
  569. courseIdTmp, sectionIdTmp, midTmp := getSectionIdFromUrl(newUrl)
  570. if courseIdTmp == "" || sectionIdTmp == "" || midTmp =="" {
  571. continue
  572. }
  573. //fmt.Printf("courseid:%s, secid:%s, mid:%s,%s\n", courseIdTmp, sectionIdTmp, midTmp, url)
  574. time.Sleep(1*time.Second)
  575. vedioComplete(wd, midTmp, courseIdTmp, sectionIdTmp)
  576. //vedioComplete(wd, mid.Id, courseId, sec)
  577. //if strings.Contains(mid.Name, "充分认识新发展阶段就是聚焦高质量发展的阶段") {
  578. //time.Sleep(1000*time.Second)
  579. //}
  580. fmt.Printf("%s 视频处理完成\n", mid.Name)
  581. //time.Sleep(1000*time.Second)
  582. }
  583. }
  584. return nil
  585. }