This commit is contained in:
Matthieu Grieger 2015-04-08 18:47:39 -07:00
parent ccbfe12340
commit 6a836f13b7
7 changed files with 89 additions and 89 deletions

View file

@ -69,7 +69,7 @@ func (c *SongCache) Update() {
}
func (c *SongCache) ClearExpired() {
for _ = range time.Tick(5 * time.Minute) {
for range time.Tick(5 * time.Minute) {
songs, _ := ioutil.ReadDir(fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir))
for _, song := range songs {
hours := time.Since(song.ModTime()).Hours()

16
main.go
View file

@ -144,15 +144,15 @@ var dj = mumbledj{
// args, sets up the gumble client and its listeners, and then connects to the server.
func main() {
if currentUser, err := user.Current(); err == nil {
dj.homeDir = currentUser.HomeDir
}
if currentUser, err := user.Current(); err == nil {
dj.homeDir = currentUser.HomeDir
}
if err := loadConfiguration(); err == nil {
fmt.Println("Configuration successfully loaded!")
} else {
panic(err)
}
if err := loadConfiguration(); err == nil {
fmt.Println("Configuration successfully loaded!")
} else {
panic(err)
}
var address, port, username, password, channel, pemCert, pemKey string
var insecure bool

View file

@ -70,7 +70,7 @@ type DjConfig struct {
AdminCacheSize bool
AdminKill bool
}
}
}
// Loads mumbledj.gcfg into dj.conf, a variable of type DjConfig.
func loadConfiguration() error {

View file

@ -79,7 +79,7 @@ func NewPlaylist(user, id string) (*Playlist, error) {
if dj.conf.General.MaxSongDuration == 0 || duration <= dj.conf.General.MaxSongDuration {
dj.queue.AddSong(newSong)
j += 1
}
}
}
return playlist, nil

View file

@ -9,20 +9,20 @@ package services
// Generic Song interface. Each service will implement these
// functions in their Song types.
type Song interface {
Download()
Play()
Delete()
AddSkip()
RemoveSkip()
SkipReached()
}
type Song interface {
Download()
Play()
Delete()
AddSkip()
RemoveSkip()
SkipReached()
}
// Generic playlist interface. Each service will implement these
// functions in their Playlist types.
type Playlist interface {
AddSkip()
RemoveSkip()
DeleteSkippers()
SkipReached()
}
type Playlist interface {
AddSkip()
RemoveSkip()
DeleteSkippers()
SkipReached()
}

View file

@ -5,81 +5,81 @@
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
*/
package youtube
package youtube
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/jmoiron/jsonq"
"github.com/jmoiron/jsonq"
)
// Collection of metadata for a YouTube video.
type YouTubeVideo struct {
id string
title string
duration string
secondsDuration string
thumbnail string
id string
title string
duration string
secondsDuration string
thumbnail string
}
// Collection of metadata for a YouTube playlist.
type YouTubePlaylist struct {
id string
title string
duration string
secondsDuration string
id string
title string
duration string
secondsDuration string
}
// Retrieves the metadata for a new YouTube video, and creates and returns a
// YouTubeVideo type.
func GetYouTubeVideo(id string) (*YouTubeVideo, error) {
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&id=%s&key=%s",
id, os.Getenv("YOUTUBE_API_KEY"))
jsonString := ""
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&id=%s&key=%s",
id, os.Getenv("YOUTUBE_API_KEY"))
jsonString := ""
if response, err := http.Get(url); err == nil {
defer response.Body.Close()
if response.StatusCode == 200 {
if body, err := ioutil.ReadAll(response.Body); err == nil {
jsonString = string(body)
}
} else {
if response.StatusCode == 403 {
return nil, errors.New("Invalid API key supplied.")
} else {
return nil, errors.New("Invalid YouTube ID supplied.")
}
}
} else {
return nil, errors.New("An error occurred while receiving HTTP GET response.")
}
if response, err := http.Get(url); err == nil {
defer response.Body.Close()
if response.StatusCode == 200 {
if body, err := ioutil.ReadAll(response.Body); err == nil {
jsonString = string(body)
}
} else {
if response.StatusCode == 403 {
return nil, errors.New("Invalid API key supplied.")
} else {
return nil, errors.New("Invalid YouTube ID supplied.")
}
}
} else {
return nil, errors.New("An error occurred while receiving HTTP GET response.")
}
jsonData := map[string]interface{}{}
decoder := json.NewDecoder(strings.NewReader(jsonString))
decoder.Decode(&jsonData)
jq := jsonq.NewQuery(jsonData)
jsonData := map[string]interface{}{}
decoder := json.NewDecoder(strings.NewReader(jsonString))
decoder.Decode(&jsonData)
jq := jsonq.NewQuery(jsonData)
title, _ := jq.String("items", "0", "snippet", "title")
thumbnail, _ := jq.String("items", "0", "snippet", "thumbnails", "high", "url")
duration, _ := jq.String("items", "0", "contentDetails", "duration")
title, _ := jq.String("items", "0", "snippet", "title")
thumbnail, _ := jq.String("items", "0", "snippet", "thumbnails", "high", "url")
duration, _ := jq.String("items", "0", "contentDetails", "duration")
minutes := int(duration[2 : strings.Index(duration, "M")])
seconds := int(duration[strings.Index(duration, "M")+1 : len(duration)-1])
totalSeconds := (minutes * 60) + seconds
durationString := fmt.Sprintf("%d:%d", minutes, seconds)
minutes := int(duration[2:strings.Index(duration, "M")])
seconds := int(duration[strings.Index(duration, "M")+1 : len(duration)-1])
totalSeconds := (minutes * 60) + seconds
durationString := fmt.Sprintf("%d:%d", minutes, seconds)
video := &YoutubeVideo {
id: id,
title: title,
duration: durationString,
secondsDuration: totalSeconds,
thumbnail: thumbnail,
}
return video, nil
video := &YoutubeVideo{
id: id,
title: title,
duration: durationString,
secondsDuration: totalSeconds,
thumbnail: thumbnail,
}
return video, nil
}

View file

@ -8,14 +8,14 @@
package youtube
type Song struct {
submitter string
title string
id string
duration string
thumbnailUrl string
skippers []string
playlist *Playlist
dontSkip bool
submitter string
title string
id string
duration string
thumbnailUrl string
skippers []string
playlist *Playlist
dontSkip bool
}
func NewSong(user, id string, playlist *Playlist) (*Song, error) {