cheshire137 / soma-chrome

Chrome extension to listen to SomaFM web radio stations from your browser.
https://chrome.google.com/webstore/detail/somaplayer/dpcghdgbhjkihgnnbojldhjmcbieofgo?hl=en&gl=US&authuser=1
MIT License
58 stars 20 forks source link

Should grab current channels from SomaFM API #5

Closed rustyhodge closed 8 years ago

rustyhodge commented 9 years ago

The list of current channels should be grabbed from http://api.somafm.com/channels.xml so you don't have to worry about updating the source when a new channel is added. I looks like this:

<![CDATA[ Groove Salad ]]> ambient|electronica http://somafm.com/img/groovesalad120.png http://somafm.com/logos/256/groovesalad256.png http://somafm.com/logos/512/groovesalad512.png groovesalad 1417644939 http://somafm.com/groovesalad.pls http://somafm.com/groovesalad56.pls http://somafm.com/groovesalad130.pls http://somafm.com/groovesalad64.pls http://somafm.com/groovesalad32.pls 1716
code-for-coffee commented 9 years ago

It looks like we need to do the following to do so:

I'm not fluent in coffeescript but I'll be happy to give this a shot when I have some free time.

cheshire137 commented 9 years ago

Oh hey, this would be nice! I just released version 0.0.0.4 with Folk Forward and some other new stations. I'd be happy with it trying to fetch the XML list of stations, and if that fails for whatever reason, falling back to a hardcoded list of known stations.

cheshire137 commented 9 years ago

One thing holding me back from doing this is the lack of CORS headers on the Soma server. When my extension's popup tries to make an AJAX request to http://api.somafm.com/channels.xml, I get the following error:

screen shot 2015-06-20 at 2 35 51 pm

You would have to add particular headers to your server to allow me to fetch this XML feed via AJAX. See http://enable-cors.org/ for more info.

rustyhodge commented 9 years ago

Out of curiosity, if you just got to somafm.com/channels.xml does it work?

On Jun 20, 2015, at 11:36 AM, Sarah Vessels notifications@github.com wrote:

One thing holding me back from doing this is the lack of CORS headers on the Soma server. When my extension's popup tries to make an AJAX request to http://api.somafm.com/channels.xml, I get the following error:

You would have to add particular headers to your server to allow me to fetch this XML feed via AJAX. See http://enable-cors.org/ for more info.

— Reply to this email directly or view it on GitHub.

rustyhodge commented 9 years ago

Actually, I think I just changed the server config so that header is there now.

Can you confirm that this is working for http://api.somafm.com/channels.xml

thanks

On Jun 20, 2015, at 11:36 AM, Sarah Vessels notifications@github.com wrote:

One thing holding me back from doing this is the lack of CORS headers on the Soma server. When my extension's popup tries to make an AJAX request to http://api.somafm.com/channels.xml, I get the following error:

You would have to add particular headers to your server to allow me to fetch this XML feed via AJAX. See http://enable-cors.org/ for more info.

— Reply to this email directly or view it on GitHub.

code-for-coffee commented 9 years ago

It did not appear to work. I am still seeing an error when independently trying to fetch the XML from another sever. Did you restart the server that you updated the settings on? Also, really cool of you to be this involved with this extension :+1:

rustyhodge commented 9 years ago

We are sending that header in the request for curl -i api.somafm.com/channels.xml

I wonder if it has to do with the fact that we're by default gzipping it?

HTTP/1.1 200 OK Server: nginx/1.1.19 Date: Sun, 21 Jun 2015 23:30:00 GMT Content-Type: application/xml Content-Length: 4249 Connection: keep-alive Last-Modified: Sun, 21 Jun 2015 23:29:45 GMT ETag: "37c0d5-6b5a-5190f88454c40"-gzip Vary: Accept-Encoding Content-Encoding: gzip Expires: Sun, 21 Jun 2015 23:30:10 GMT Cache-Control: max-age=10 Access-Control-Allow-Origin: * X-Proxy-Cache: HIT Accept-Ranges: bytes

On Jun 21, 2015, at 4:26 PM, James notifications@github.com wrote:

It did not appear to work. I am still seeing an error when independently trying to fetch the XML from another sever. Did you restart the server that you updated the settings on? Also, really cool of you to be this involved with this extension

� Reply to this email directly or view it on GitHub.

code-for-coffee commented 9 years ago

That could be; I am not sure and some Google/StackOverflow searching didn't yield much (to be fair I just browsed the first two pages of search results). Curl requests on my local machine and http://onlinecurl.com/ do work, though.

Here's something you can use to test it using jQuery's $.ajax. You may need to open the developer console to see the specific CORS error if it exists. If you want to play around and setup another endpoint that doesn't serve a GZipped version I left you the ability to change the URL. http://jsfiddle.net/codeforcoffee/t911vapt/

rustyhodge commented 9 years ago

Does fetching it from http://somafm.com/channels.xml work?

That's still being served off the main server with Apache not nginx.

On Jun 21, 2015, at 5:03 PM, James notifications@github.com wrote:

That could be; I am not sure and some Google/StackOverflow searching didn't yield much (to be fair I just browsed the first two pages of search results). Curl requests on my local machine and http://onlinecurl.com/ do work, though.

Here's something you can use to test it using jQuery's $.ajax. You may need to open the developer console to see the specific CORS error if it exists. If you want to play around and setup another endpoint that doesn't serve a GZipped version I left you the ability to change the URL. http://jsfiddle.net/codeforcoffee/t911vapt/

— Reply to this email directly or view it on GitHub.

code-for-coffee commented 9 years ago

I cleared my browser cache and the request to http://api.somafm.com/channels.xml returns a valid XMLDocument object. So this should work a-ok now! I'll start fiddling with this to see if I can write anything helpful. I have very little exposure to CoffeeScript but I'll do my best.

code-for-coffee commented 9 years ago

@moneypenny I threw this together to return an array of channel objects from the current API. Inside of popup.coffee you could update your $.get call inside of fetch_soma_channels() to something like this; I've went ahead and parsed the XML so hopefully you find this somewhat helpful.

$.ajax({ 
            url: 'http://api.somafm.com/channels.xml', 
            dataType: "xml",
            success:function(data) {

                var channels = data.getElementsByTagName("channel");

                var channelsObjArray = [];

                for (var inc = 0; inc < channels.length; inc++) {
                    var channel = channels[inc].childNodes;
                    //channel[3].textContent is the channel description
                    //channel[11] is the image for the station
                    //channel[21] is I believe the highest quality station URL (highestpls)
                    //console.log("Title: " + channel[1].textContent);
                    //console.log("Description: " + channel[3].textContent);
                    //console.log("MP3: " + channel[21].textContent);

                    var channelObj = {
                        title: channel[1].textContent,
                        description: channel[3].textContent,
                        mp3: channel[21].textContent
                    };

                    channelsObjArray.push(channelObj);
                }

                console.log(channelsObjArray);

            }
        });

Tested and working here: http://jsfiddle.net/codeforcoffee/t911vapt/ (check the Developer Console the results)

@rustyhodge I saw that the API documentation on Soma.FM is currently not up to available; could you confirm if a channel's highestpls is the best quality playlist?

cheshire137 commented 9 years ago

Thanks, James! I'll try this out.

code-for-coffee commented 8 years ago

I just started tinkering around with this in my own fork. I've had some luck. Good news! The JSON loads now because CORS is enabled. Thanks, @rustyhodge.

@moneypenny, in the popup.coffee file, I've been tinkering with the following method (and googling coffee script along the way). I've found that I can get the stations to load correctly using something along these lines:

  fetch_soma_channels: ->
    # Github Issue #5 fix by code-for-coffee
    # Fetching from the soma.fm channels JSON now that CORS is enabled
    console.log 'Fetching channels.json...'
    url = 'http://api.somafm.com/channels.json'
    on_success = (data) ->
      console.log 'Retrieved channels.json successfully!'
      console.log data
      for station in data
        @station_option = $('option')
        @station_option.prop 'value': station.id
        console.log @station_option
        #append to @station_select.append('<option value="test">hi</option>')

I need to clean it up a bit, verify everything works, and then submit a PR. Would you prefer me to call fetch_soma_channels in the constructor? Great news is that the JSON object from the channels.json file contains the exact value you need in your select list value; for each station in the array of objects there is an id key that is equal to what you had in the HAML file.

No ETA on finishing this up and submitting a PR but I should clean it up soon.

code-for-coffee commented 8 years ago

I have finally finished this and submitted a pull request to @moneypenny.

https://github.com/moneypenny/soma-chrome/pull/20

cheshire137 commented 8 years ago

Fixed in version 0.0.0.7 of the extension.