Exemplo n.º 1
0
func (d *DHUStruct) getUserName(client *http.Client) (name string) {
	var err error
	var res *http.Response
	for i := 0; i < 3; i++ {
		d.mutexClient.RLock()
		res, err = client.Get(DHUHostUrl + DHUSelfInfoUrl)
		d.mutexClient.RUnlock()
		if err == nil {
			break
		}
	}
	if err != nil {
		return ""
	}
	dec := mahonia.NewDecoder("GB18030")
	doc, _ := goquery.NewDocumentFromResponse(res)
	doc.Find("tr").EachWithBreak(func(i int, s *goquery.Selection) bool {
		info := s.Find("td").Eq(0).Text()
		a := dec.ConvertString(info)
		if a == "姓名" {
			Newname := s.Find("td").Eq(1).Text()
			name = dec.ConvertString(Newname)
			return false
		}
		return true
	})
	return
}
func GetAllLessons(res *http.Response) []CourseList {
	doc, err := goquery.NewDocumentFromResponse(res)
	if err != nil {
		fmt.Println(err)
		return nil
	} else {
		newlist := []CourseList{}
		dec := mahonia.NewDecoder("GB18030")
		doc.Find("tr").Each(func(i int, s *goquery.Selection) {
			selectid := s.Find("td").Eq(0).Text()
			_, err := strconv.Atoi(selectid)
			if err == nil {
				_, teachername, _ := dec.Translate([]byte(s.Find("td").Eq(6).Text()), true)
				newinfo := []CourseInfo{}
				s.Find("td").Eq(7).Find("tr").Each(func(i int, s *goquery.Selection) {
					_, weektime, _ := dec.Translate([]byte(s.Find("td").Eq(0).Text()), true)
					_, daytime, _ := dec.Translate([]byte(s.Find("td").Eq(1).Text()), true)
					info := CourseInfo{string(weektime), string(daytime)}
					newinfo = append(newinfo, info)
				})
				courselist := CourseList{selectid, string(teachername), newinfo}
				newlist = append(newlist, courselist)
			} else {
				return
			}
		})
		return newlist
	}
}
func GetAllCourse(res *http.Response) map[string]string {
	doc, err := goquery.NewDocumentFromResponse(res)
	if err != nil {
		fmt.Println(err)
		return nil
	} else {
		CourseList := map[string]string{}
		dec := mahonia.NewDecoder("GB18030")
		doc.Find("tr").Each(func(i int, s *goquery.Selection) {
			courseid := s.Find("td").Eq(1).Text()
			_, err := strconv.Atoi(courseid)
			if err == nil {
				coursename := s.Find("td").Eq(0).Text()
				_, data, _ := dec.Translate([]byte(coursename), true)
				CourseList[courseid] = string(data)
			} else {
				return
			}
		})
		return CourseList
	}
}
func GetTeachSchema(client *http.Client, gradelist, majorlist []string) {
	courseTypeList := map[string]string{"政治法律": "2", "自然科学": "3", "语言文字": "3", "other": "1"}
	dec := mahonia.NewDecoder("GB18030")
	session, err := mgo.Dial("localhost:27017")
	if err != nil {
		panic(err)
	}
	defer session.Close()
	courseTable := session.DB("DHU").C("CourseTable")
	courseindexDB := session.DB("DHU").C("CourseIndex")
	for _, grade := range gradelist {
		for _, major := range majorlist {
			var ok bool
			var titletype string
			courseindexs := CourseIndex{grade[2:4] + major[2:], []CourseTypeList{}}
			Other := CourseTypeList{"1", make([]string, 1)}
			PolicyAndLaw := CourseTypeList{"2", make([]string, 1)}
			NatureAndLanguage := CourseTypeList{"3", make([]string, 1)}
			res, err := client.PostForm(DHUHostUrl+DHUTeachSchemaUrl, url_value(map[string]string{"gradeYear": grade, "majorId": major}))
			if err != nil {
				fmt.Println(err)
				return
			}
			doc, _ := goquery.NewDocumentFromResponse(res)
			doc.Find("tr").EachWithBreak(func(i int, s *goquery.Selection) bool {
				_, titleForBreak, _ := dec.Translate([]byte(s.Find("td").Eq(0).Text()), true)
				if string(titleForBreak) == BreakTitle {
					return false
				} else {
					element := s.Find("td").Eq(1).Text()
					_, err := strconv.Atoi(element)
					if err != nil {
						_, title, _ := dec.Translate([]byte(element), true)
						titletype, ok = courseTypeList[string(title)]
						if !ok {
							titletype = courseTypeList["other"]
						}
					} else {
						// fmt.Println(element)
						err := courseTable.Find(bson.M{"courseid": element}).One(nil)
						if err == nil {
							// for index := 9;index < 17; index ++ {
							//     if s.Find("td").Eq(index).Text() != " "{
							//         if index % 2 == 0{
							//             return true
							//         }else{
							//             break
							//         }
							//     }else{
							//         continue
							//     }
							// }
							switch titletype {
							case "1":
								Other.CourseList = append(Other.CourseList, element)
							case "2":
								PolicyAndLaw.CourseList = append(PolicyAndLaw.CourseList, element)
							case "3":
								NatureAndLanguage.CourseList = append(NatureAndLanguage.CourseList, element)
							default:
								fmt.Println("Something Wrong In Switch")
							}
						} else {
							return true
						}
					}
				}
				return true
			})
			courseindexs.CoureseTypeList = append(courseindexs.CoureseTypeList, Other)
			courseindexs.CoureseTypeList = append(courseindexs.CoureseTypeList, PolicyAndLaw)
			courseindexs.CoureseTypeList = append(courseindexs.CoureseTypeList, NatureAndLanguage)
			courseindexDB.Insert(courseindexs)
			// fmt.Println(grade)
			// fmt.Println(major)
			// fmt.Println(Other)
			// fmt.Println(PolicyAndLaw)
			// fmt.Println(NatureAndLanguage)
		}
	}
}