func (ae *AppEngine) DownloadSelectedMusic() { mwroot := ae.MainWindow.Root() table := mwroot.ObjectByName("musiclist") go func() { dstpath := dialogboxes.SelectFolderDialog() if dstpath == "" { return } table.Call("goFillSelected") sel := table.String("selected") //we got string with indexes indexes, _ := ae.stringToIntArr(strings.TrimRight(sel, " ")) var dl []downloaders.Downloadable for _, index := range indexes { qmlItem := table.Call("goGet", index).(*qml.Common) fmt.Println(qmlItem) item := MusicItem{Artist: qmlItem.String("artist"), Album: qmlItem.String("album"), Genre: qmlItem.String("genre"), Title: qmlItem.String("title"), LyricsId: qmlItem.Float64("lyricsId"), Url: qmlItem.String("url"), } dlItem := downloaders.DownloadableMusic{Artist: item.Artist, Album: item.Album, Genre: item.Genre, Title: item.Title, LyricsId: item.LyricsId, AccessToken: ae.RequestAccesser.Token, } dlItem.SetSource(item.Url) if item.Album != "" { tmppath := filepath.Join(dstpath, item.Album) err := ae.mkSubDir(tmppath) if err != nil { dialogboxes.ShowErrorDialog(err.Error()) return } dstpath = tmppath } dlItem.AllocateFile(dstpath, fmt.Sprintf("%s - %s", item.Artist, item.Title), "mp3") dl = append(dl, &dlItem) } _, err := downloaders.Initialize(dl, 3) if err != nil { dialogboxes.ShowErrorDialog(err.Error()) } }() }
func (e *Engine) Show() { if !e.initialized { dialogboxes.ShowErrorDialog("Движок плеера не инициализирован") return } e.mainWindow.Show() e.currentPlaying = 0 e.StartPlay() }
func (ae *AppEngine) mkSubDir(path string) error { //check if already exist info, err := os.Stat(path) if err == nil { if !info.IsDir() { return errors.New("Can not create directory, path collision") } } err = os.Mkdir(path, os.ModeDir) if err != nil { dialogboxes.ShowErrorDialog(err.Error()) return err } return nil }
func (e *Engine) LoadLyrics() { curplaying := e.playlist[e.currentPlaying] if curplaying.LyricsId == 0 { return } ra := requestwrapper.RequestAccesser{Token: e.accessToken} parms := map[string]string{"lyrics_id": strconv.FormatFloat(curplaying.LyricsId, 'f', -1, 64)} resp, err := ra.MakeRequest("audio.getLyrics", parms) if err != nil { dialogboxes.ShowErrorDialog(err.Error()) return } lyricsfield := e.mainWindow.Root().ObjectByName("lyrics") lyrics := resp["response"].(map[string]interface{})["text"].(string) lyricsfield.Set("text", lyrics) }
func (ae *AppEngine) loadName() { params := map[string]string{ "user_ids": strconv.Itoa(ae.UserId), "fields": "", } mwroot := ae.MainWindow.Root() namefield := mwroot.ObjectByName("name") resp, err := ae.MakeRequest("users.get", params) if err != nil { dialogboxes.ShowErrorDialog("Не удалось загрузить имя: " + err.Error()) return } resp = resp["response"].([]interface{})[0].(map[string]interface{}) namestr := resp["first_name"].(string) namestr += " " namestr += resp["last_name"].(string) namefield.Set("text", namestr) }
func (ae *AppEngine) loadAvatar() { params := map[string]string{ "user_ids": strconv.Itoa(ae.UserId), "fields": "photo_100", } mwroot := ae.MainWindow.Root() avatar := mwroot.ObjectByName("avatar") resp, err := ae.MakeRequest("users.get", params) if err != nil { dialogboxes.ShowErrorDialog("Не удалось загрузить аватар: " + err.Error()) return } /*in response we get json like this response: [ {<user1data>},{<user2data>},... ] here we extract first item of array and converting to map[string]... to get info about user */ resp = resp["response"].([]interface{})[0].(map[string]interface{}) avatar.Set("source", resp["photo_100"]) }
func (ae *AppEngine) LoadAudios(uid int) { params := map[string]string{ "owner_id": strconv.Itoa(uid), "count": "6000", } mwroot := ae.MainWindow.Root() model := mwroot.ObjectByName("musiclist") resp, err := ae.MakeRequest("audio.get", params) if err != nil { dialogboxes.ShowErrorDialog("Не удалось загрузить аудиозаписи: " + err.Error()) return } model.Call("clear") content := resp["response"].(map[string]interface{}) items := content["items"].([]interface{}) tmplist := []audioplayer.PtrSong{} //get albums resp, err = ae.MakeRequest("audio.getAlbums", map[string]string{"owner_id": strconv.Itoa(uid)}) if err != nil { dialogboxes.ShowErrorDialog("Не удалось загрузить альбомы: " + err.Error()) return } //generate map for albums id->name albummap := map[float64]string{} gotalbums, hasalbums := resp["response"].(map[string]interface{})["items"].([]map[string]interface{}) if hasalbums { for _, v := range gotalbums { id := v["id"].(float64) title := v["title"].(string) albummap[id] = title } } for _, v := range items { mp := v.(map[string]interface{}) item := MusicItem{} item.Artist = mp["artist"].(string) item.Title = mp["title"].(string) item.Id = mp["id"].(float64) item.Url = mp["url"].(string) lyricsid, present := mp["lyrics_id"].(float64) if !present { lyricsid = 0 } item.LyricsId = lyricsid duration := mp["duration"].(float64) duration_obj, _ := time.ParseDuration(strconv.FormatFloat(duration, 'g', -1, 64) + "s") item.Duration = duration_obj.String() genreId, present := mp["genre_id"].(float64) if present { item.Genre = requestwrapper.VkAudioGenres[genreId] } else { item.Genre = "" } albumid, present := mp["album_id"].(float64) if present { item.Album = albummap[albumid] } else { item.Album = "" } model.Call("appendStruct", item) tmplist = append(tmplist, audioplayer.PtrSong{Artist: item.Artist, Title: item.Title, Url: item.Url, Duration: duration, LyricsId: item.LyricsId}) } player := audioplayer.Engine{} player.Initialize(ae.Token, tmplist) ae.QMLEngine.Context().SetVar("audioplayer", &player) qml.Changed(&player, &player) }