This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvkgetmusic.go
72 lines (69 loc) · 1.81 KB
/
vkgetmusic.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"github.com/Kutabe/vk"
"github.com/antonholmquist/jason"
"github.com/kennygrant/sanitize"
)
func getMusicAsync(wg *sync.WaitGroup, responseURL string, responseArtist string, responseTitle string) {
fmt.Printf("URL GET:%s\n", responseURL)
defer wg.Done()
response, err := http.Get(responseURL)
if err != nil {
log.Println(err)
return
}
defer response.Body.Close()
audio, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println(err)
return
}
filename := sanitize.BaseName(responseArtist+" - "+responseTitle) + ".mp3"
err = ioutil.WriteFile(filename, audio, 0777)
if err != nil {
log.Println(err)
return
}
fmt.Printf("FILE DONE:%s\n", filename)
}
func main() {
var wg sync.WaitGroup
var password string
maxConnectionCount := flag.Int("maxcon", 10, "Maximum number of download threads")
login := flag.String("login", "", "vk.com login(Email or Phone)")
flag.Parse()
fmt.Print("Automatic VK.com music downloader\n")
if *login == "" {
fmt.Print("Login(Email or Phone): ")
fmt.Scanf("%s\n", &login)
}
fmt.Print("Password: ")
fmt.Scanf("%s\n", &password)
user, err := vk.Auth(*login, password)
if err != nil {
log.Fatal(err)
}
responseJSON, _ := vk.Request("audio.get", nil, user)
responseObject, _ := jason.NewObjectFromBytes(responseJSON)
responseArray, _ := responseObject.GetObjectArray("response")
conCounter := 0
for _, responseElement := range responseArray {
responseURL, _ := responseElement.GetString("url")
responseArtist, _ := responseElement.GetString("artist")
responseTitle, _ := responseElement.GetString("title")
conCounter++
wg.Add(1)
go getMusicAsync(&wg, responseURL, responseArtist, responseTitle)
if conCounter > *maxConnectionCount {
wg.Wait()
conCounter = 0
}
}
wg.Wait()
}