Use config vars instead of env vars

pull/100/head
gamah 2015-11-19 01:05:23 -06:00
parent ca08f5710f
commit 912e657c8b
6 changed files with 23 additions and 23 deletions

View File

@ -85,11 +85,7 @@ Effective April 20th, 2015, all requests to YouTube's API must use v3 of their A
**5)** Add the IP address of the machine MumbleDJ will run on in the box that appears. Click "Create".
**6)** You should now see that an API key has been generated. Copy it.
**7)** Open up `~/.bashrc` with your favorite text editor (or `~/.zshrc` if you use `zsh`). Add the following line to the bottom: `export YOUTUBE_API_KEY="<your_key_here>"`. Replace \<your_key_here\> with your API key.
**8)** Close your current terminal window and open another one up. You should be able to use Youtube on MumbleDJ now!
**6)** You should now see that an API key has been generated, make a note of it.
###SOUNDCLOUD API KEYS
A SoundCloud API key is required for SoundCloud integration. If no SoundCloud API key is found, then the service will be disabled (YouTube links will still work however).
@ -98,11 +94,8 @@ A SoundCloud API key is required for SoundCloud integration. If no SoundCloud AP
**2)** Now to get the API key create a new app here: [http://soundcloud.com/you/apps/new](http://soundcloud.com/you/apps/new)
**3)** Copy the Client ID (not the Client Secret).
**3)** Make a note of the Client ID (not the Client Secret).
**4)** Open up `~/.bashrc` with your favorite text editor (or `~/.zshrc` if you use `zsh`). Add the following line to the bottom: `export SOUNDCLOUD_API_KEY="<your_key_here>"`. Replace \<your_key_here\> with your API key.
**5)** Close your current terminal window and open another one up. You should be able to use SoundCloud on MumbleDJ now!
**NOTE:** If you get errors when trying to play SoundCloud audio, make sure to update `youtube-dl` with `youtube-dl -U`!
@ -124,7 +117,7 @@ $ make
$ make install
```
**7)** Edit `~/.mumbledj/config/mumbledj.gcfg` to your liking. This file will be overwritten if the config file structure is changed in a commit, but a backup is always stored at
**7)** Edit `~/.mumbledj/config/mumbledj.gcfg` to your liking, make sure to include your API keys! This file will be overwritten if the config file structure is changed in a commit, but a backup is always stored at
`~/.mumbledj/config/mumbledj_backup.gcfg`.
**8)** Execute the command shown at the top of this `README` document with your credentials, and the bot should be up and running!

View File

@ -229,3 +229,7 @@ AdminShuffle = true
# Make shuffleon and shuffleoff admin commands?
# DEFAULT VALUE: true
AdminShuffleToggle = true
[ServiceKeys]
YouTube = ""
SoundCloud = ""

15
main.go
View File

@ -136,17 +136,18 @@ func CheckAPIKeys() {
anyDisabled := false
// Checks YouTube API key
if os.Getenv("YOUTUBE_API_KEY") == "" {
if dj.conf.ServiceKeys.Youtube == "" {
anyDisabled = true
fmt.Printf("The youtube service has been disabled as you do not have a YouTube API key defined in your environment variables.\n")
fmt.Printf("The youtube service has been disabled as you do not have a YouTube API key defined in your config file!\n")
} else {
services = append(services, YouTube{})
}
// Checks Soundcloud API key
if os.Getenv("SOUNDCLOUD_API_KEY") == "" {
if dj.conf.ServiceKeys.SoundCloud == "" {
anyDisabled = true
fmt.Printf("The soundcloud service has been disabled as you do not have a Soundcloud API key defined in your environment variables.\n")
fmt.Printf("The soundcloud service has been disabled as you do not have a Soundcloud API key defined in your config file!\n")
} else {
services = append(services, SoundCloud{})
}
@ -181,8 +182,6 @@ var dj = mumbledj{
// args, sets up the gumble client and its listeners, and then connects to the server.
func main() {
CheckAPIKeys()
if currentUser, err := user.Current(); err == nil {
dj.homeDir = currentUser.HomeDir
}
@ -231,7 +230,9 @@ func main() {
}
dj.defaultChannel = strings.Split(channel, "/")
CheckAPIKeys()
dj.client.Attach(gumbleutil.Listener{
Connect: dj.OnConnect,
Disconnect: dj.OnDisconnect,

View File

@ -78,6 +78,10 @@ type DjConfig struct {
AdminShuffle bool
AdminShuffleToggle bool
}
ServiceKeys struct {
Youtube string
SoundCloud string
}
}
// Loads mumbledj.gcfg into dj.conf, a variable of type DjConfig.

View File

@ -10,7 +10,6 @@ package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
@ -50,7 +49,7 @@ func (sc SoundCloud) NewRequest(user *gumble.User, url string) ([]Song, error) {
var songArray []Song
var err error
timesplit := strings.Split(url, "#t=")
url = fmt.Sprintf("http://api.soundcloud.com/resolve?url=%s&client_id=%s", timesplit[0], os.Getenv("SOUNDCLOUD_API_KEY"))
url = fmt.Sprintf("http://api.soundcloud.com/resolve?url=%s&client_id=%s", timesplit[0], dj.conf.ServiceKeys.SoundCloud)
if apiResponse, err = PerformGetRequest(url); err != nil {
return nil, errors.New(fmt.Sprintf(INVALID_API_KEY, sc.ServiceName()))
}

View File

@ -10,7 +10,6 @@ package main
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
@ -78,7 +77,7 @@ func (yt YouTube) NewRequest(user *gumble.User, url string) ([]Song, error) {
// NewSong gathers the metadata for a song extracted from a YouTube video, and returns the song.
func (yt YouTube) NewSong(user *gumble.User, id, offset string, playlist Playlist) (Song, error) {
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s", id, os.Getenv("YOUTUBE_API_KEY"))
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=%s&key=%s", id, dj.conf.ServiceKeys.Youtube)
if apiResponse, err := PerformGetRequest(url); err == nil {
title, _ := apiResponse.String("items", "0", "snippet", "title")
thumbnail, _ := apiResponse.String("items", "0", "snippet", "thumbnails", "high", "url")
@ -144,7 +143,7 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) {
var songArray []Song
var err error
// Retrieve title of playlist
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", id, os.Getenv("YOUTUBE_API_KEY"))
url := fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=%s&key=%s", id, dj.conf.ServiceKeys.Youtube)
if apiResponse, err = PerformGetRequest(url); err != nil {
return nil, err
}
@ -165,7 +164,7 @@ func (yt YouTube) NewPlaylist(user *gumble.User, id string) ([]Song, error) {
// Retrieve items in this page of the playlist
url = fmt.Sprintf("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%s&key=%s&pageToken=%s",
id, os.Getenv("YOUTUBE_API_KEY"), pageToken)
id, dj.conf.ServiceKeys.Youtube, pageToken)
if apiResponse, err = PerformGetRequest(url); err != nil {
return nil, err
}