Created
April 18, 2013 15:21
-
-
Save MindTwister/5413587 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"net/url" | |
"os" | |
"regexp" | |
"time" | |
"os/exec" | |
) | |
const ( | |
ECHONEST_API_END = "http://developer.echonest.com/api/v4/" | |
ECHONEST_API_KEY = "1EZWD3FBD1GBQT6B6" | |
) | |
type DrTrack struct { | |
Artist string `json:"displayArtist"` | |
Title string `json:"title"` | |
} | |
func (t *DrTrack) String() string { | |
return fmt.Sprintf("%s - %s", t.Artist, t.Title) | |
} | |
type RadioInfo struct { | |
ServerTime string `json:"serverTime"` | |
Start time.Time `json:"start"` | |
Tracks []*DrTrack `json:"tracks"` | |
} | |
type EchoNestSearchResponse struct { | |
Response struct { | |
Songs []struct { | |
Id string | |
} | |
} | |
} | |
type EchoNestProfileResponse struct { | |
Response struct { | |
Songs []struct { | |
Audio_summary struct { | |
Duration float64 | |
} | |
} | |
} | |
} | |
func main() { | |
previousPlaying := new(DrTrack) | |
fh, _ := os.Open("killlist.txt") | |
defer fh.Close() | |
reader := bufio.NewReader(fh) | |
killstrings := make([]*regexp.Regexp, 0) | |
for { | |
if part, _, err := reader.ReadLine(); err != nil { | |
break | |
} else { | |
expression := regexp.MustCompile(string(part)) | |
killstrings = append(killstrings, expression) | |
} | |
} | |
fmt.Println(killstrings) | |
for { | |
fmt.Println("Fetching") | |
data := new(RadioInfo) | |
GetApiData("http://www.dr.dk/info/musik/service/TrackInfoJsonService.svc/TrackInfo/P3", nil, data) | |
currentlyPlaying := data.Tracks[0] | |
if currentlyPlaying.String() != previousPlaying.String() { | |
match := false | |
for _, reg := range killstrings { | |
if reg.MatchString(currentlyPlaying.String()) { | |
match = true | |
break | |
} | |
} | |
previousPlaying = currentlyPlaying | |
fmt.Println(currentlyPlaying) | |
if match { | |
fmt.Println("Kill it with fire") | |
parameters := map[string]string{ | |
"artist": currentlyPlaying.Artist, | |
"title": currentlyPlaying.Title, | |
"format": "json", | |
"api_key": ECHONEST_API_KEY} | |
echonestRequestUrl := fmt.Sprintf("%ssong/search", ECHONEST_API_END) | |
responseData := new(EchoNestSearchResponse) | |
GetApiData(echonestRequestUrl, parameters, responseData) | |
// We couldnt find the song by searching, bummer :( | |
if len(responseData.Response.Songs) == 0 { | |
fmt.Println("Could not find track, sleeping on it") | |
time.Sleep(time.Second * 30) | |
continue | |
} | |
firstSongId := responseData.Response.Songs[0].Id | |
parameters = map[string]string{ | |
"api_key": ECHONEST_API_KEY, | |
"id": firstSongId, | |
"format": "json", | |
"bucket": "audio_summary"} | |
echonestRequestUrl = fmt.Sprintf("%ssong/profile", ECHONEST_API_END) | |
profileResponse := new(EchoNestProfileResponse) | |
GetApiData(echonestRequestUrl, parameters, profileResponse) | |
go Mute(profileResponse.Response.Songs[0].Audio_summary.Duration) | |
fmt.Println(profileResponse) | |
} else { | |
fmt.Println("Is ok") | |
} | |
} | |
time.Sleep(time.Second * 30) | |
} | |
} | |
func Mute(duration float64) { | |
fmt.Printf("Muting for %v seconds", duration) | |
muteCmd := exec.Command("./nircmdc.exe", "mutesystemvolume", "1") | |
unMuteCmd := exec.Command("./nircmdc.exe", "mutesystemvolume", "0") | |
err := muteCmd.Start() | |
if err != nil { | |
panic(err) | |
} | |
time.Sleep(time.Second * time.Duration(duration)) | |
unMuteCmd.Start() | |
fmt.Println("Unmuting") | |
} | |
func GetApiData(apiurl string, parameters map[string]string, responseStruct interface{}) { | |
if parameters != nil { | |
requestValues := url.Values{} | |
for name, value := range parameters { | |
requestValues.Add(name, value) | |
} | |
apiurl = apiurl + "?" + requestValues.Encode() | |
} | |
response, err := http.Get(apiurl) | |
defer response.Body.Close() | |
if err != nil { | |
panic(err) | |
} | |
decoder := json.NewDecoder(response.Body) | |
decoder.Decode(responseStruct) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment