mirror of
https://github.com/riwiwa/muzi.git
synced 2026-04-20 11:25:51 -07:00
add top artists display to profile
This commit is contained in:
@@ -34,6 +34,10 @@ type ProfileData struct {
|
||||
TemplateName string
|
||||
NowPlayingArtist string
|
||||
NowPlayingTitle string
|
||||
TopArtists []db.TopArtist
|
||||
TopArtistsPeriod string
|
||||
TopArtistsLimit int
|
||||
TopArtistsView string
|
||||
}
|
||||
|
||||
// Render a page of the profile in the URL
|
||||
@@ -85,6 +89,72 @@ func profilePageHandler() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
period := r.URL.Query().Get("period")
|
||||
if period == "" {
|
||||
period = "all_time"
|
||||
}
|
||||
|
||||
view := r.URL.Query().Get("view")
|
||||
if view == "" {
|
||||
view = "grid"
|
||||
}
|
||||
|
||||
maxLimit := 30
|
||||
if view == "grid" {
|
||||
maxLimit = 8
|
||||
}
|
||||
|
||||
limitStr := r.URL.Query().Get("limit")
|
||||
limit := 10
|
||||
if limitStr != "" {
|
||||
limit, err = strconv.Atoi(limitStr)
|
||||
if err != nil || limit < 5 {
|
||||
limit = 10
|
||||
}
|
||||
if limit > maxLimit {
|
||||
limit = maxLimit
|
||||
}
|
||||
}
|
||||
|
||||
profileData.TopArtistsPeriod = period
|
||||
profileData.TopArtistsLimit = limit
|
||||
profileData.TopArtistsView = view
|
||||
|
||||
var startDate, endDate *time.Time
|
||||
now := time.Now()
|
||||
switch period {
|
||||
case "week":
|
||||
start := now.AddDate(0, 0, -7)
|
||||
startDate = &start
|
||||
case "month":
|
||||
start := now.AddDate(0, -1, 0)
|
||||
startDate = &start
|
||||
case "year":
|
||||
start := now.AddDate(-1, 0, 0)
|
||||
startDate = &start
|
||||
case "custom":
|
||||
startStr := r.URL.Query().Get("start")
|
||||
endStr := r.URL.Query().Get("end")
|
||||
if startStr != "" {
|
||||
if t, err := time.Parse("2006-01-02", startStr); err == nil {
|
||||
startDate = &t
|
||||
}
|
||||
}
|
||||
if endStr != "" {
|
||||
if t, err := time.Parse("2006-01-02", endStr); err == nil {
|
||||
t = t.AddDate(0, 0, 1)
|
||||
endDate = &t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
topArtists, err := db.GetTopArtists(userId, limit, startDate, endDate)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Cannot get top artists: %v\n", err)
|
||||
} else {
|
||||
profileData.TopArtists = topArtists
|
||||
}
|
||||
|
||||
if pageInt == 1 {
|
||||
if np, ok := scrobble.GetNowPlaying(userId); ok {
|
||||
profileData.NowPlayingArtist = np.Artist
|
||||
|
||||
43
web/utils.go
43
web/utils.go
@@ -19,6 +19,49 @@ func add(a int, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// Divides two integers (integer division)
|
||||
func div(a int, b int) int {
|
||||
if b == 0 {
|
||||
return 0
|
||||
}
|
||||
return a / b
|
||||
}
|
||||
|
||||
// Returns a % b
|
||||
func mod(a int, b int) int {
|
||||
return a % b
|
||||
}
|
||||
|
||||
// Returns a slice of a slice from start to end
|
||||
func slice(a []db.TopArtist, start int, end int) []db.TopArtist {
|
||||
if start >= len(a) {
|
||||
return []db.TopArtist{}
|
||||
}
|
||||
if end > len(a) {
|
||||
end = len(a)
|
||||
}
|
||||
return a[start:end]
|
||||
}
|
||||
|
||||
func gridReorder(artists []db.TopArtist) []db.TopArtist {
|
||||
if len(artists) < 2 {
|
||||
return artists
|
||||
}
|
||||
if len(artists)%2 == 0 {
|
||||
return artists
|
||||
}
|
||||
remaining := len(artists) - 1
|
||||
perRow := remaining / 2
|
||||
rest := artists[1:]
|
||||
firstRow := rest[:perRow]
|
||||
secondRow := rest[perRow:]
|
||||
var reordered []db.TopArtist
|
||||
reordered = append(reordered, artists[0])
|
||||
reordered = append(reordered, secondRow...)
|
||||
reordered = append(reordered, firstRow...)
|
||||
return reordered
|
||||
}
|
||||
|
||||
// Put a comma in the thousands place, ten-thousands place etc.
|
||||
func formatInt(n int) string {
|
||||
if n < 1000 {
|
||||
|
||||
@@ -33,6 +33,10 @@ func init() {
|
||||
funcMap := template.FuncMap{
|
||||
"sub": sub,
|
||||
"add": add,
|
||||
"div": div,
|
||||
"mod": mod,
|
||||
"slice": slice,
|
||||
"gridReorder": gridReorder,
|
||||
"formatInt": formatInt,
|
||||
"formatTimestamp": formatTimestamp,
|
||||
"formatTimestampFull": formatTimestampFull,
|
||||
|
||||
Reference in New Issue
Block a user