Esempio n. 1
0
func getCategory(songs *[]*Song, genre Genre, page dna.Int) <-chan bool {
	channel := make(chan bool, 1)
	go func() {
		link := "http://nhacso.net/bai-hat-theo-the-loai-" + genre.Id.ToString() + "/joke-link-2-" + page.ToString() + ".html"
		// dna.Log(link)
		result, err := http.Get(link)
		if err == nil {
			data := &result.Data

			// transform string {"2":[0,3,5,7,9,11,13,15,29],"10":[1,2,4,6,8,]}
			// to  map[dna.Int]dna.Int{20:2, 28:2, 4:10, 12:10} Ex: map[29] = 2
			temp := data.FindAllStringSubmatch(`getCategory.+'(\{.+\})'`, -1)
			mapping := map[dna.Int]dna.Int{}
			if len(temp) > 0 && temp[0].Length() > 0 {
				vals := temp[0][1].FindAllString(`"[0-9]+":\[[0-9,]+?\]`, -1)
				if vals.Length() > 0 {
					for _, val := range vals {
						target := val.FindAllStringSubmatch(`"(.+)"`, -1)[0][1].ToInt()
						arr := val.FindAllStringSubmatch(`\[(.+)\]`, -1)[0][1].Split(",").ToIntArray()
						for _, itm := range arr {
							mapping[itm] = target
						}
					}
				}
			}
			// Finding cat id for each song. cats var is 2-dimentional array.
			// Each index of it represents the correspondent song, its value is the categories the song belongs to
			catStrings := data.FindAllString(`Thể loại :.+`, -1)
			cats := []dna.IntArray{}
			for _, val := range catStrings {
				tagids := dna.IntArray{}
				tmp := val.FindAllStringSubmatch(`cate_tag_song_([0-9]+)`, -1)
				if len(tmp) > 0 {
					for _, el := range tmp {
						tagids.Push(el[1].ToInt())
					}
				}
				cats = append(cats, tagids)
			}
			// Log(cats)

			// get songids
			temps := data.FindAllStringSubmatch(`play" id="blocksongtag_([0-9]+)`, -1)
			songids := dna.IntArray{}
			if len(temps) > 0 {
				for _, val := range temps {
					songids.Push(val[1].ToInt())
				}
			}

			tmpsongs := &[]*Song{}
			for idx, songid := range songids {
				song := NewSong()
				song.Id = songid
				category := dna.StringArray{}
				for _, val := range cats[idx] {
					if mapping[val] > 0 && mapping[val] < CatTags.Length() {
						if CatTags[mapping[val]] != "" {
							category.Push(CatTags[mapping[val]])
						}
					} else {
						mess := dna.Sprintf("WRONG INDEX AT CATTAGS: %v %v %v - %v", mapping[val], genre, page, link)
						panic(mess.String())
					}

				}
				category.Push(genre.Name)
				song.Category = transformCats(category.Unique()).Unique()
				*tmpsongs = append(*tmpsongs, song)
			}
			*songs = *tmpsongs

		}
		channel <- true

	}()
	return channel
}