-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from AudDMusic/v0.2.0-preview3
Added Long Poll
- Loading branch information
Showing
9 changed files
with
343 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module github.com/AudDMusic/audd-go | ||
|
||
go 1.14 | ||
go 1.15 | ||
|
||
require github.com/bogem/id3v2 v1.2.0 // indirect | ||
require github.com/Mihonarium/golongpoll v1.2.0 |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package audd | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"github.com/Mihonarium/golongpoll/go-client/glpclient" | ||
"net/url" | ||
"strconv" | ||
) | ||
|
||
const longPollingUrl = MainAPIEndpoint + "longpoll/" | ||
|
||
func getMD5Hash(text string) string { | ||
hasher := md5.New() | ||
hasher.Write([]byte(text)) | ||
return hex.EncodeToString(hasher.Sum(nil)) | ||
} | ||
|
||
func (c *Client) getLongPollChannel(RadioID int) string { | ||
return getMD5Hash(getMD5Hash(c.ApiToken) + strconv.Itoa(RadioID))[0:9] | ||
} | ||
|
||
type LongPoll struct { | ||
stop chan interface{} | ||
ResultsChan chan StreamCallback | ||
} | ||
|
||
// Stops the LongPoll connection | ||
func (lp *LongPoll) Stop() { | ||
lp.stop <- struct {}{} | ||
} | ||
|
||
// Opens a LongPoll connection to the AudD API and receives the callbacks via LongPoll. | ||
// The callbacks will be sent to both the callback URL and all the LongPoll listeners. | ||
// Won't work unless some URL is set as the URL for callbacks. More info: docs.audd.io/streams/#longpoll | ||
func (c *Client) NewLongPoll(RadioID int) LongPoll { | ||
u, _ := url.Parse(longPollingUrl) | ||
lpC := glpclient.NewClient(u, c.getLongPollChannel(RadioID)) | ||
lpC.LoggingEnabled = false | ||
lp := LongPoll{ | ||
stop: make(chan interface{}, 1), | ||
ResultsChan: make(chan StreamCallback, 1), | ||
} | ||
go func() { | ||
lpC.Start() | ||
for { | ||
select { | ||
case e := <-lpC.EventsChan: | ||
var song StreamCallback | ||
err := json.Unmarshal(e.Data, &song) | ||
if err == nil { | ||
lp.ResultsChan <- song | ||
} | ||
case <-lp.stop: | ||
lpC.Stop() | ||
return | ||
} | ||
} | ||
}() | ||
return lp | ||
} |
This file contains 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
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
File name,Plays On [file],Plays On [song],Artist,Title,Album,Label,Release date,Listen online | ||
04. Dragon.mp3,00:00,02:37,Nick Phoenix,Dragon,Dragon,Two Steps From Hell,2019-01-18,https://lis.tn/Dragon | ||
04. Dragon.mp3,00:00,02:37,"Two Steps From Hell, Nick Phoenix",Dragon,Dragon,Two Steps From Hell,2019-01-18,https://lis.tn/Dragon | ||
04. Dragon.mp3,00:54,01:21,The A.M. Experiment,"Empty House Filled with Memories Haunting Me, Pt. 3",Ex Nihilo,555983 Records DK,2016-11-19,https://lis.tn/tBbZB | ||
04. Dragon.mp3,00:54,02:53,Emanuel Satie,Don't Forget to Go Home (feat. Billy Cobham) [Nicolosi Ambient Mix] (Nicolosi Ambient Mix),Don't Forget to Go Home,Rebirth,2018-06-08,https://lis.tn/atQWk | ||
04. Dragon.mp3,00:54,02:47,Emre Madak,Gelip de Halimi Gördün mü?,Gelip de Halimi Gördün mü?,ARMONİ MÜZİK,2015-09-23,https://lis.tn/FQamD | ||
04.Unstoppable Music - Hyperhero.mp3,00:00,02:00,Eric Prydz,Proper Education,Proper Education,Universal Music Italia srL.,2006-01-01,https://lis.tn/ProperEducation | ||
04.Unstoppable Music - Hyperhero.mp3,00:00,00:12,Fourward,Levels,Lose Control,Elevate Records,2020-07-31,https://lis.tn/aMreaf | ||
04.Unstoppable Music - Hyperhero.mp3,00:00,02:15,Alberico,Caledonia,Caledonia EP,BeatFreak Limited,2019-10-04,https://lis.tn/Caledonia | ||
04.Unstoppable Music - Hyperhero.mp3,00:54,01:08,Unstoppable Music,Hyperhero,The Pulse,AudD Music,2019-10-15,https://lis.tn/Hyperhero | ||
04.Unstoppable Music - Hyperhero.mp3,01:48,02:01,Unstoppable Music,Hyperhero,The Pulse,AudD Music,2019-10-15,https://lis.tn/Hyperhero |
Oops, something went wrong.