Removed playlist.go and song.go

This commit is contained in:
Matthieu Grieger 2015-04-09 13:21:28 -07:00
parent 985f48572f
commit 73fb4bd82b
2 changed files with 0 additions and 289 deletions

View file

@ -1,125 +0,0 @@
/*
* MumbleDJ
* By Matthieu Grieger
* playlist.go
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/jmoiron/jsonq"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// Playlist type declaration.
type Playlist struct {
id string
title string
}
// Returns a new Playlist type. Before returning the new type, the playlist's metadata is collected
// via the YouTube Gdata API.
func NewPlaylist(user, id string) (*Playlist, error) {
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()
if response.StatusCode != 400 && response.StatusCode != 404 {
if body, err := ioutil.ReadAll(response.Body); err == nil {
jsonString = string(body)
}
} else {
return nil, errors.New("Invalid YouTube ID supplied.")
}
} else {
return nil, errors.New("An error occurred while receiving HTTP GET request.")
}
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
}
playlist := &Playlist{
id: id,
title: playlistTitle,
}
j := 0
for i := 0; i < playlistItems; i++ {
index := strconv.Itoa(j)
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{
submitter: user,
title: songTitle,
youtubeId: songId,
duration: songDuration,
thumbnailUrl: songThumbnail,
playlist: playlist,
dontSkip: false,
}
// Don't spam the chat if a playlist contains songs that are too long
if dj.conf.General.MaxSongDuration == 0 || duration <= dj.conf.General.MaxSongDuration {
dj.queue.AddSong(newSong)
j += 1
}
}
return playlist, nil
}
// Adds a skip to the skippers slice for the current playlist.
func (p *Playlist) AddSkip(username string) error {
for _, user := range dj.playlistSkips[p.id] {
if username == user {
return errors.New("This user has already skipped the current song.")
}
}
dj.playlistSkips[p.id] = append(dj.playlistSkips[p.id], username)
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 {
for i, user := range dj.playlistSkips[p.id] {
if username == user {
dj.playlistSkips[p.id] = append(dj.playlistSkips[p.id][:i], dj.playlistSkips[p.id][i+1:]...)
return nil
}
}
return errors.New("This user has not skipped the song.")
}
// Removes skippers entry in dj.playlistSkips.
func (p *Playlist) DeleteSkippers() {
delete(dj.playlistSkips, p.id)
}
// 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 {
if float32(len(dj.playlistSkips[p.id]))/float32(channelUsers) >= dj.conf.General.PlaylistSkipRatio {
return true
} else {
return false
}
}

164
song.go
View file

@ -1,164 +0,0 @@
/*
* MumbleDJ
* By Matthieu Grieger
* song.go
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/jmoiron/jsonq"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
)
// Song type declaration.
type Song struct {
submitter string
title string
youtubeId string
duration string
thumbnailUrl string
skippers []string
playlist *Playlist
dontSkip bool
}
// Returns a new Song type. Before returning the new type, the song's metadata is collected
// via the YouTube Gdata API.
func NewSong(user, id string, playlist *Playlist) (*Song, error) {
jsonUrl := fmt.Sprintf("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc", id)
jsonString := ""
if response, err := http.Get(jsonUrl); err == nil {
defer response.Body.Close()
if response.StatusCode != 400 && response.StatusCode != 404 {
if body, err := ioutil.ReadAll(response.Body); err == nil {
jsonString = string(body)
}
} else {
return nil, errors.New("Invalid YouTube ID supplied.")
}
} else {
return nil, errors.New("An error occurred while receiving HTTP GET request.")
}
jsonData := map[string]interface{}{}
decoder := json.NewDecoder(strings.NewReader(jsonString))
decoder.Decode(&jsonData)
jq := jsonq.NewQuery(jsonData)
videoTitle, _ := jq.String("data", "title")
videoThumbnail, _ := jq.String("data", "thumbnail", "hqDefault")
duration, _ := jq.Int("data", "duration")
videoDuration := fmt.Sprintf("%d:%02d", duration/60, duration%60)
if dj.conf.General.MaxSongDuration > 0 && duration > dj.conf.General.MaxSongDuration {
return nil, errors.New("video exceeds the maximum allowed duration.")
}
song := &Song{
submitter: user,
title: videoTitle,
youtubeId: id,
duration: videoDuration,
thumbnailUrl: videoThumbnail,
playlist: playlist,
dontSkip: false,
}
return song, nil
}
// Downloads the song via youtube-dl if it does not already exist on disk.
// All downloaded songs are stored in ~/.mumbledj/songs and should be automatically cleaned.
func (s *Song) Download() error {
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.youtubeId)); os.IsNotExist(err) {
cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s.m4a`, s.youtubeId), "--format", "m4a", "--", s.youtubeId)
if err := cmd.Run(); err == nil {
if dj.conf.Cache.Enabled {
dj.cache.CheckMaximumDirectorySize()
}
return nil
} else {
return errors.New("Song download failed.")
}
} else {
return nil
}
}
// Plays the song. Once the song is playing, a notification is displayed in a text message that features the video thumbnail, URL, title,
// duration, and submitter.
func (s *Song) Play() {
if err := dj.audioStream.Play(fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.youtubeId), dj.queue.OnSongFinished); err != nil {
panic(err)
} else {
if s.playlist == nil {
dj.client.Self.Channel.Send(fmt.Sprintf(NOW_PLAYING_HTML, s.thumbnailUrl, s.youtubeId, s.title,
s.duration, s.submitter), false)
} else {
dj.client.Self.Channel.Send(fmt.Sprintf(NOW_PLAYING_PLAYLIST_HTML, s.thumbnailUrl, s.youtubeId,
s.title, s.duration, s.submitter, s.playlist.title), false)
}
}
}
// Deletes the song from ~/.mumbledj/songs if the cache is disabled.
func (s *Song) Delete() error {
if dj.conf.Cache.Enabled == false {
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.youtubeId)
if _, err := os.Stat(filePath); err == nil {
if err := os.Remove(filePath); err == nil {
return nil
} else {
return errors.New("Error occurred while deleting audio file.")
}
} else {
return nil
}
} else {
return nil
}
}
// Adds a skip to the skippers slice. If the user is already in the slice AddSkip() returns
// an error and does not add a duplicate skip.
func (s *Song) AddSkip(username string) error {
for _, user := range s.skippers {
if username == user {
return errors.New("This user has already skipped the current song.")
}
}
s.skippers = append(s.skippers, username)
return nil
}
// Removes a skip from the skippers slice. If username is not in the slice, an error is
// returned.
func (s *Song) RemoveSkip(username string) error {
for i, user := range s.skippers {
if username == user {
s.skippers = append(s.skippers[:i], s.skippers[i+1:]...)
return nil
}
}
return errors.New("This user has not skipped the song.")
}
// 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 (s *Song) SkipReached(channelUsers int) bool {
if float32(len(s.skippers))/float32(channelUsers) >= dj.conf.General.SkipRatio {
return true
} else {
return false
}
}