2015-01-03 20:31:29 +01:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* playlist.go
|
2015-01-18 23:44:40 +01:00
|
|
|
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
|
2015-01-03 20:31:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/jmoiron/jsonq"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Playlist type declaration.
|
|
|
|
type Playlist struct {
|
2015-02-12 22:27:04 +01:00
|
|
|
id string
|
|
|
|
title string
|
2015-01-03 20:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new Playlist type. Before returning the new type, the playlist's metadata is collected
|
|
|
|
// via the YouTube Gdata API.
|
2015-02-02 21:05:00 +01:00
|
|
|
func NewPlaylist(user, id string) (*Playlist, error) {
|
2015-01-03 20:31:29 +01:00
|
|
|
jsonUrl := fmt.Sprintf("http://gdata.youtube.com/feeds/api/playlists/%s?v=2&alt=jsonc&maxresults=25", id)
|
|
|
|
jsonString := ""
|
|
|
|
|
|
|
|
if response, err := http.Get(jsonUrl); err == nil {
|
|
|
|
defer response.Body.Close()
|
2015-02-03 04:45:17 +01:00
|
|
|
if response.StatusCode != 400 && response.StatusCode != 404 {
|
2015-02-02 21:05:00 +01:00
|
|
|
if body, err := ioutil.ReadAll(response.Body); err == nil {
|
|
|
|
jsonString = string(body)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Invalid YouTube ID supplied.")
|
2015-01-03 20:31:29 +01:00
|
|
|
}
|
2015-02-03 04:45:17 +01:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("An error occurred while receiving HTTP GET request.")
|
2015-01-03 20:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonData := map[string]interface{}{}
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(jsonString))
|
|
|
|
decoder.Decode(&jsonData)
|
|
|
|
jq := jsonq.NewQuery(jsonData)
|
|
|
|
|
|
|
|
playlistTitle, _ := jq.String("data", "title")
|
|
|
|
playlistItems, _ := jq.Int("data", "totalItems")
|
|
|
|
if playlistItems > 25 {
|
|
|
|
playlistItems = 25
|
|
|
|
}
|
|
|
|
|
2015-02-12 22:27:04 +01:00
|
|
|
playlist := &Playlist{
|
|
|
|
id: id,
|
|
|
|
title: playlistTitle,
|
|
|
|
}
|
|
|
|
|
2015-01-03 20:31:29 +01:00
|
|
|
for i := 0; i < playlistItems; i++ {
|
|
|
|
index := strconv.Itoa(i)
|
|
|
|
songTitle, _ := jq.String("data", "items", index, "video", "title")
|
|
|
|
songId, _ := jq.String("data", "items", index, "video", "id")
|
|
|
|
songThumbnail, _ := jq.String("data", "items", index, "video", "thumbnail", "hqDefault")
|
|
|
|
duration, _ := jq.Int("data", "items", index, "video", "duration")
|
|
|
|
songDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60)
|
|
|
|
newSong := &Song{
|
2015-02-12 22:27:04 +01:00
|
|
|
submitter: user,
|
|
|
|
title: songTitle,
|
|
|
|
youtubeId: songId,
|
|
|
|
duration: songDuration,
|
|
|
|
thumbnailUrl: songThumbnail,
|
|
|
|
playlist: playlist,
|
|
|
|
dontSkip: false,
|
2015-01-03 20:31:29 +01:00
|
|
|
}
|
2015-02-12 22:27:04 +01:00
|
|
|
dj.queue.AddSong(newSong)
|
2015-01-03 20:31:29 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 21:05:00 +01:00
|
|
|
return playlist, nil
|
2015-01-03 20:31:29 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 22:27:04 +01:00
|
|
|
// Adds a skip to the skippers slice for the current playlist.
|
2015-01-03 20:31:29 +01:00
|
|
|
func (p *Playlist) AddSkip(username string) error {
|
2015-02-12 22:27:04 +01:00
|
|
|
for _, user := range dj.playlistSkips[p.id] {
|
2015-01-03 20:31:29 +01:00
|
|
|
if username == user {
|
|
|
|
return errors.New("This user has already skipped the current song.")
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 22:27:04 +01:00
|
|
|
dj.playlistSkips[p.id] = append(dj.playlistSkips[p.id], username)
|
2015-01-03 20:31:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a skip from the skippers slice. If username is not in the slice, an error is
|
|
|
|
// returned.
|
|
|
|
func (p *Playlist) RemoveSkip(username string) error {
|
2015-02-12 22:27:04 +01:00
|
|
|
for i, user := range dj.playlistSkips[p.id] {
|
2015-01-03 20:31:29 +01:00
|
|
|
if username == user {
|
2015-02-12 22:27:04 +01:00
|
|
|
dj.playlistSkips[p.id] = append(dj.playlistSkips[p.id][:i], dj.playlistSkips[p.id][i+1:]...)
|
2015-01-03 20:31:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("This user has not skipped the song.")
|
|
|
|
}
|
|
|
|
|
2015-02-12 22:27:04 +01:00
|
|
|
// Removes skippers entry in dj.playlistSkips.
|
|
|
|
func (p *Playlist) DeleteSkippers() {
|
|
|
|
delete(dj.playlistSkips, p.id)
|
|
|
|
}
|
|
|
|
|
2015-01-03 20:31:29 +01:00
|
|
|
// Calculates current skip ratio based on number of users within MumbleDJ's channel and the
|
|
|
|
// amount of values in the skippers slice. If the value is greater than or equal to the skip ratio
|
|
|
|
// defined in mumbledj.gcfg, the function returns true. Returns false otherwise.
|
|
|
|
func (p *Playlist) SkipReached(channelUsers int) bool {
|
2015-02-12 22:27:04 +01:00
|
|
|
if float32(len(dj.playlistSkips[p.id]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
|
2015-01-03 20:31:29 +01:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|