-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (113 loc) · 4.06 KB
/
index.js
File metadata and controls
145 lines (113 loc) · 4.06 KB
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
139
140
141
142
143
144
145
const { VK } = require("vk-io");
const Promise = require("bluebird");
const Static = require("./lib/static");
const HTTPClient = require("./lib/http");
const DownloadInstance = require("./lib/hls");
const Audio = require("./lib/requests/audio");
const Playlists = require("./lib/requests/playlists");
const Search = require("./lib/requests/search");
const Artists = require("./lib/requests/artists");
const General = require("./lib/requests/general");
const Recommendations = require("./lib/requests/recommendations");
const Explore = require("./lib/requests/explore");
const OfficialAPI = require("./lib/requests/official");
const defaultParams = {
debug: false
};
class AudioAPI extends Static {
constructor (token, vkParams = {}, params = defaultParams) {
super(null, params);
this.vk = new VK({
token,
apiHeaders: {
"User-Agent": this.VKAndroidAppUA
},
...vkParams
});
this.params = params;
this.HTTPClient = HTTPClient;
this.DownloadInstance = DownloadInstance;
}
async login (credits, authParams = {}) {
if (!credits.user) {
const response = await this.vk.api.users.get();
this.vk.user = response[0].id;
} else this.vk.user = credits.user;
this.client = await new HTTPClient(this.vk).login({
...credits,
...authParams
});
this.audio = new Audio(this.client, this.vk, this.params);
this.playlists = new Playlists(this.client, this.vk, this.params);
this.search = new Search(this.client, this.vk, this.params);
this.artists = new Artists(this.client, this.vk, this.params);
this.general = new General(this.client, this.vk, this.params);
this.recommendations = new Recommendations(this.client, this.vk, this.params);
this.explore = new Explore(this.client, this.vk, this.params);
this.official = new OfficialAPI(this.client, this.vk, this.params);
return this;
}
async getAll (params = {}) {
params.owner_id = params.owner_id ? Number(params.owner_id) : this.vk.user;
params.playlist_id = params.playlist_id ? Number(params.playlist_id) : -1;
return ~params.playlist_id
? this.playlists.getAllSongs(params)
: this.audio.getAll(params);
}
async getFriendsUpdates (params = {}) {
const { payload } = await this.request({
act: "section",
al: 1,
claim: 0,
is_layer: 0,
owner_id: this.vk.user,
section: "updates"
});
try {
return await Promise.map(payload[1][1].playlists, async playlist => ({
owner_id: playlist.ownerId,
audios: await this.audio.parseAudios(playlist.list, params)
}));
} catch (e) {
return [];
}
}
// --------------------- STATUS ----------------------
async getStatusExportHash () {
const res = await this.audio.mainPage();
const hash = res.match(/statusExportHash: \'(.*?)\'/)[1];
this.statusExportHash = hash;
return hash;
}
async toggleAudioStatus (params = {}) {
/*
enable: boolean
raw_audio_id: string
owner_id?: number
*/
await this.request({
act: "toggle_status",
al: 1,
exp: Number(params.enable),
hash: this.statusExportHash || await this.getStatusExportHash(),
id: params.raw_audio_id,
oid: params.owner_id ? Number(params.owner_id) : this.vk.user,
top: 0
});
return true;
}
async changeAudioStatus (params = {}) {
/*
raw_audio_id: string
*/
await this.request({
act: "audio_status",
al: 1,
hash: this.statusExportHash || await this.getStatusExportHash(),
full_id: params.raw_audio_id,
top: 0
});
return true;
}
}
module.exports = AudioAPI;