forked from werkshy/pickup
/
main.go
116 lines (102 loc) · 3.36 KB
/
main.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
//usr/bin/env go run "$0" $@; exit
package main
import (
"fmt"
"github.com/werkshy/pickup/config"
"github.com/werkshy/pickup/handlers"
"github.com/werkshy/pickup/model"
"github.com/werkshy/pickup/player"
flag "launchpad.net/gnuflag"
"log"
"net/http"
"os"
)
func main() {
var action = flag.String("action", "serve", "Action to perform (serve|refresh)")
conf := config.Config{}
conf.MusicDir = flag.String("music-dir", "/music", "Music dir")
conf.Port = flag.Int("port", 8080, "Pickup port")
conf.MpdAddress = flag.String("mpd-address", "localhost:6600", "MPD address")
conf.MpdPassword = flag.String("mpd-password", "", "MPD Password")
var query = flag.String("query", "", "Search query")
flag.Parse(true)
fmt.Println("Action is:", *action)
fmt.Printf("Mpd address: '%s' password: '%s'\n", *conf.MpdAddress,
*conf.MpdPassword)
//collection := loadOrRefresh(*conf.MusicDir)
music, err := model.RefreshMpd(&conf)
if err != nil {
log.Fatalf("Couldn't get files from mpd: \n%s\n", err)
}
switch *action {
case "stats":
stats(music.Categories[0])
case "search":
search(music, *query)
case "serve":
serve(&conf, music)
case "test-playback":
testPlayback(&conf, music)
case "refresh":
os.Exit(0)
default:
fmt.Println("Unknown action", *action)
}
}
func serve(conf *config.Config, music model.Collection) bool {
categoryHandler := handlers.CategoryHandler{music}
albumHandler := handlers.AlbumHandler{music}
artistHandler := handlers.ArtistHandler{music}
playlistHandler := handlers.NewPlaylistHandler(music, conf)
controlHandler := handlers.NewControlHandler(conf)
http.Handle("/categories/", categoryHandler)
http.Handle("/albums/", albumHandler)
http.Handle("/artists/", artistHandler)
http.Handle("/playlist/", playlistHandler)
http.Handle("/control/", controlHandler)
staticDir, _ := os.Getwd()
staticDir = staticDir + "/static"
fmt.Printf("Serving static files from %s\n", staticDir)
// strip '/static' from the url to get the name of the file within the
// static dir.
http.Handle("/static/", http.StripPrefix("/static/",
http.FileServer(http.Dir(staticDir))))
http.HandleFunc("/", handlers.Index)
var bind = fmt.Sprintf(":%d", *conf.Port)
fmt.Printf("Serving from %s on %s\n", *conf.MusicDir, bind)
http.ListenAndServe(bind, nil)
return true
}
func stats(category *model.Category) {
fmt.Printf("Stats: %d tracks, %d albums, %d artists\n",
len(category.Tracks), len(category.Albums),
len(category.Artists))
}
func search(music model.Collection, query string) {
matching := model.Search(music, query)
fmt.Printf("Matches for '%s':\n", query)
stats(&matching)
fmt.Println("\nMatching Tracks:")
for _, track := range matching.Tracks {
fmt.Printf("%-40s (%-20s)\n", track.Name, track.Artist)
}
fmt.Println("\n\nMatching Albums:")
for _, album := range matching.Albums {
fmt.Printf("%-40s (%s)\n", album.Name, album.Artist)
}
}
func testPlayback(conf *config.Config, music model.Collection) error {
playlist := player.NewMpdPlaylist(conf)
playlist.Clear()
// add any old album
album := music.Categories[0].Artists[3].Albums[2]
log.Printf("Playing album %s - %s\n", album.Artist, album.Name)
playlist.AddAlbum(album)
// get the contents of the playlist
currentTracks, err := playlist.List()
log.Printf("Current playlist: (%d tracks)", len(currentTracks))
for _, track := range currentTracks {
log.Printf("%s\n", track)
}
return err
}