/
resourceload.go
138 lines (132 loc) · 4.38 KB
/
resourceload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"strconv"
"time"
"govkmedia/dialogboxes"
"govkmedia/audioplayer"
"govkmedia/requestwrapper"
"gopkg.in/qml.v1"
)
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) 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)
}
type MusicItem struct {
Artist string
Title string
Duration string
Id float64
Url string
LyricsId float64
Genre string
Album string
}
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)
}
func (ae *AppEngine) LoadGroupAudios() {
go func() {
gid:=dialogboxes.SelectGroupDialog(ae.UserId,ae.Token)
if (gid!=-1) { ae.LoadAudios(-gid) }
}()
}