From 912e657c8b45a16c65c04ba9df0d8110bd9111eb Mon Sep 17 00:00:00 2001 From: gamah Date: Thu, 19 Nov 2015 01:05:23 -0600 Subject: [PATCH 1/2] Use config vars instead of env vars --- README.md | 13 +++---------- config.gcfg | 4 ++++ main.go | 15 ++++++++------- parseconfig.go | 4 ++++ service_soundcloud.go | 3 +-- service_youtube.go | 7 +++---- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 099f0d1..175373d 100644 --- a/README.md +++ b/README.md @@ -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=""`. Replace \ 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=""`. Replace \ 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! diff --git a/config.gcfg b/config.gcfg index 4f75aa9..f9c81fa 100644 --- a/config.gcfg +++ b/config.gcfg @@ -229,3 +229,7 @@ AdminShuffle = true # Make shuffleon and shuffleoff admin commands? # DEFAULT VALUE: true AdminShuffleToggle = true + +[ServiceKeys] +YouTube = "" +SoundCloud = "" diff --git a/main.go b/main.go index 86c45c0..553c500 100644 --- a/main.go +++ b/main.go @@ -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, diff --git a/parseconfig.go b/parseconfig.go index a4df46b..e09d460 100644 --- a/parseconfig.go +++ b/parseconfig.go @@ -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. diff --git a/service_soundcloud.go b/service_soundcloud.go index bb2adb7..7a11914 100644 --- a/service_soundcloud.go +++ b/service_soundcloud.go @@ -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())) } diff --git a/service_youtube.go b/service_youtube.go index 848cf85..a6b592c 100644 --- a/service_youtube.go +++ b/service_youtube.go @@ -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 } From 7d27f9087babaa64925d91113f23f600e5037e70 Mon Sep 17 00:00:00 2001 From: gamah Date: Thu, 19 Nov 2015 13:40:27 -0600 Subject: [PATCH 2/2] Modify make install to use existing env vars if possible. --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index af8cc7e..91ff6ea 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,8 @@ install: mkdir -p ~/.mumbledj/songs if [ -f ~/.mumbledj/config/mumbledj.gcfg ]; then mv ~/.mumbledj/config/mumbledj.gcfg ~/.mumbledj/config/mumbledj_backup.gcfg; fi; cp -u config.gcfg ~/.mumbledj/config/mumbledj.gcfg + sed -i 's/YouTube = \"/YouTube = \"'$(YOUTUBE_API_KEY)'/' ~/.mumbledj/config/mumbledj.gcfg + sed -i 's/SoundCloud = \"/SoundCloud = \"'$(SOUNDCLOUD_API_KEY)'/' ~/.mumbledj/config/mumbledj.gcfg if [ -d ~/bin ]; then cp -f mumbledj* ~/bin/mumbledj; else sudo cp -f mumbledj* /usr/local/bin/mumbledj; fi; build: