-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.go
62 lines (49 loc) · 1.04 KB
/
playlist.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
package main
import (
"database/sql"
"fmt"
"os"
)
func generatePlaylist(db *sql.DB) error {
filename, err := getEnv("PLAYLIST_FILE_LOCATION")
if err != nil {
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
defer func() {
switch err {
case nil:
err = tx.Commit()
default:
tx.Rollback()
}
}()
f, err := os.Create(filename)
if err != nil {
return err
}
rows, err := tx.Query("SELECT id, filename, artist, title, lenght, share, image FROM details WHERE playlist=? ORDER BY id DESC", "daily")
if err != nil {
return err
}
for rows.Next() {
var s SongDetails
err := rows.Scan(&s.ID, &s.Filename, &s.Artist, &s.Title, &s.Length, &s.Share, &s.Image)
annotation := annotate(s.Artist, s.Title, s.Length, s.Share, s.Filename, s.Image)
fmt.Fprintln(f, annotation)
// fmt.Println(s.ID, s.Filename, s.Artist, s.Title, s.Length, s.Share)
if err != nil {
fmt.Println("Fetching item failed.", err)
return err
}
}
err = f.Close()
if err != nil {
return err
}
defer rows.Close()
return nil
}