-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
56 lines (46 loc) · 1.1 KB
/
file.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
package gotube
import (
"fmt"
"os"
)
const (
TempFileName = "all_songs"
)
// WriteAllSongsToFile goes through all playlists and write all songs to file
// it returns pointer to file
func (yt Youtube) WriteAllSongsToFile() error {
file, err := os.Create(TempFileName)
if err != nil {
return err
}
// get all playlists of mine
call := yt.Service.Playlists.List(snippetContentDetails)
call = call.MaxResults(50).Mine(true)
response, err := call.Do()
if err != nil {
return err
}
// goes through all playlists
for _, pl := range response.Items {
file.WriteString(fmt.Sprintf("___%s\n", pl.Snippet.Title))
pageToken := ""
for {
call := yt.Service.PlaylistItems.List(snippetContentDetails)
call = call.PlaylistId(pl.Id).MaxResults(50)
response, err := call.PageToken(pageToken).Do()
if err != nil {
return err
}
// move pageToken to another page
pageToken = response.NextPageToken
for _, item := range response.Items {
file.WriteString(fmt.Sprintf("%s\n", item.Snippet.Title))
}
// if there are no pages
if pageToken == "" {
break
}
}
}
return nil
}