Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: HTTPControl - is there a template that only shows the playing info? (Read 3647 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

HTTPControl - is there a template that only shows the playing info?

I cant really code HTML, only a bit of LUA and Python, but I need a HTTPControl template that only shows the  playing info, for use in automation. In the defaul setup one of the first lines looks like this when I use httpget in LUA to retreieve it:

Code: [Select]
<html><head><title> 10:03 Alltid Nyheter 10:25 Sport 10:30 Alltid Nyheter 11:25 Sport - foobar2000</title>

All I need is what's between <title> and  - foobar2000. This will be retrieved every second in up to 8 playing zones (worst case scenario if every zone in the house is playing a different channel, the probable scenario is 2-3, but just in case...), and that takes a heavy toll on the automation system if it has to retrieve around 300 lines every time. So is there such a template out there somewhere? I'm afraid the "simple" templates are far too complicated for my use.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #1
I have gotten a bit further with trial and error. I can delete parts from the default code and remove most of the stuff. But I have a big, fat problem: The text doesn't update with changes in the display when called from the automation system. If I have opened a socket from there it also stops updating automatically in the browser, I have to use F5 to update. Can this be because the LUA call from the automation system (Girder, btw) isn't running the Javascript code?

Re: HTTPControl - is there a template that only shows the playing info?

Reply #2
And to continue my thread on my own (yeah, I know anybody has had time to answer yet): I have arrived at the smallest amount of code that does what I want, sort of. The file foo_httpcontrol_controls_tpl.html now consists of this code:

Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<META HTTP-EQUIV="refresh" CONTENT="1">
<br>[HELPER1X]</p>
</body>
</html>

But it still doesn't load new lines after startup into my automation system unless I have a webbrowser somewhere calling the page as well. I could of course have a VM on the server with the sole purpose of running this query every second, or every five seconds, for each of the zones, but that does sound a bit silly...  :))

Edit: This only works when the full page has been loaded first, so I would have to replace the code in the page with the minimal code. It could probably be done by having two files and firing the first one when I startplayback and then the second one when the page is loaded once, but that's messy...

Re: HTTPControl - is there a template that only shows the playing info?

Reply #3
An idea: is it perhaps possible to get the HELPER1X (which obviously is the text I'm looking for) directly from FooBar's HTTP control instead of going via a webpage? That could perhaps solve my problem.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #4
Hi,
You could just do an HTTP request retrieveing the info you need.
You can define your own .json file with the info you need.
Here's the template I defined for my Android app:

config file:
Code: [Select]
[foo_httpcontrol]
url=/index.html
playlist_row={"artist": "$replace(%artist%,\,\\,",\")", "album": "$replace(%album%,\,\\,",\")", "track": "$replace(%title%,\,\\,",\")", "len": "%length%", "rating": "%rating%", "queued":"$if(%queue_indexes%,'('%queue_indexes%') ',)","playCount": "%play_count%"}
helper1=[%rating%]
helper2=[$replace(%artist%,\,\\,",\") - $replace(%title%,\,\\,",\") - $replace(%album%,\,\\,",\")]
helper3=[%codec% | %bitrate%kbps | %samplerate%Hz |[ %__bitspersample%-bit |] %channels%]


albumart=$directory_path(%path%)\Cover.jpg|$directory_path(%path%)\Cover.png|$directory_path(%path%)\%artist%*-*%album%*front*.*|$directory_path(%path%)\%artist% - %album%*.jpg|$directory_path(%path%)\%artist% - %album%*.png|$directory_path(%path%)\%artist%*.jpg|$directory_path(%path%)\%artist%*.png|$directory_path(%path%)\$if(not(%album%),%artist%*,%album%*.jpg)|$directory_path(%path%)\$if(not(%album%),%artist%*,%album%*.png)|$directory_path(%path%)\*front*.jpg|$directory_path(%path%)\*front*.png|$directory_path(%path%)\*.jpg|$directory_path(%path%)\*.png
albumart_not_found=foobar2000controller/img/nocover.png
albumart_not_available=foobar2000controller/img/nocover.png
albumart_prefer_embedded=1
albumart_limit_size=15728640
playlist_items_per_page=60

example of .json file:
Code: [Select]
{
"codec":"[HELPER3]",
"helper2":"[HELPER2]",
"isPlaying":"[IS_PLAYING]",
"isPaused":"[IS_PAUSED]",
"elapsedTime":"[ITEM_PLAYING_POS]",
"trackLength":"[ITEM_PLAYING_LEN]",
"volume":"[VOLUME]",
"volumeDB":"[VOLUME_DB]",
"order":"[PLAYBACK_ORDER]",
"albumArt":"[ALBUMART]",
"itemplaying":"[PLAYLIST_ITEM_PLAYING]",
"page":"[PLAYLIST_PAGE]",
"itemsPage":"[PLAYLIST_ITEMS_PER_PAGE]",
"search":"[SEARCH_REQUEST]",
"sac":"[SAC]",
"nowPlayingRating":"[HELPER1]"
}


Then you would do an http request as:
http://IP:PORT/templateName/?param3=yourfile.json

You can try that from the browser to read the response. You'll have to repeat that query every X seconds to be updated.

If you need that to be in a browser page you should have to do an AJAX request, parse the response and print it in html.

Recommended reading:
http://www.w3schools.com/jquery/ajax_ajax.asp
https://developer.mozilla.org/ca/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

jQuery will help you also print the json in html.

Let me know if you have any help.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #5
Interesting, thanks! I will check that out, that template looks very simple and clean! I messed around a lot yesterday and got something working, I think the problem is that the automation system doesn't do the queries in a way Foobar understands, which means that it doesn't get updated. But the solution with a separate VM that runs these queries to update the output actually did work, I have that set to update every five seconds. With your template that would not tax the system much, I think. :)

Re: HTTPControl - is there a template that only shows the playing info?

Reply #6
I'm not sure how the automation system (was it Girder?) is sending the requests to foobar, but I'm sure it is able to send simple HTTP requests like the one I sent you and with that you shouldn't have any issue.
As I don't fully know you setup and requirements maybe that's why I don't understand why do you need to have a VM to do that query.
Well, just let me know if you need any help.
I really like home automation and yours seems an interesting case.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #7
Well, I find it intersting too! I have a full setup of Girder and EventGhost, with EventGhost controlling most of the hardware and Girder most of the software. Then I can use IR remotes (with RF transmission battery) and a webpage to control everything. :) Now I have tried it, and unfortunately it doesn't update the currently playing info. That only happens if I use the default template with my own minimal HTML and let that update. Do you have any idea what stops your code from updating the playback info when I use it? I changed the .json file to show all I need (but that was after I tried your full file, so that didn't change anything):

Code: [Select]
{{
[HELPER2]
}

I also changed the config file to my needs:

Code: [Select]
[foo_httpcontrol]
url=/index.html
helper2=[$replace(%title%,\,\\,",\")]

And it does get weirder. I think I mentioned it, but I need to use the full default HTML on my first run (well, I can probably trim it a bit for cover art and such, but that's not important), and then I can go to my minimal Default, which is this code:

Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<META HTTP-EQUIV="refresh" CONTENT="10">
</head>
<body onload="mouse_capture_init(); fit();" onresize="fit();">
<br>[HELPER1X]</p>
</body>
</html>

As long as that minimal code updates every five seconds in a browser I get updates to your code in a browser and Girder as well.

Btw here's my webpages just to show off (I must admit I'm pretty proud of the system I have created). One is to change radio stations, one has the temperature overview and shows what heaters are on, the current temp and the target temp and a few other things (clicking on them leads to graphs that shows the last two weeks for a zone). The final one is to control the heaters, where you can choose between different thermostat modes, like day, night, frostguard and special temps, and you can change those temps by typing in new numbers, But back to the point, FooBar. :)


Re: HTTPControl - is there a template that only shows the playing info?

Reply #8
Oh, I should point out that the system is so flexible that I can use as many radio channels and zones as I want to. I sold my house last summer, with an identical system, and that has 20 zones. At the moment I'm working on lightning control, Z-Wave door lock control and a few other things as well.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #9
That is awesome! Too bad I don't speak Norsk to completely understand the system, but it looks great!
The only automation I've done at home is a raspberry pi to control the central heating system in my house, so your system is quite on another level.

Back to foobar.. If you type directly in the browser the url with the http://......?param3=xxx.json do you see the correct info? That should work and then next step would be chaning the approach to retrieve the data.

If this is your template (I guess changing HELPER1X for HELPER2), you are doing it wrong.
Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<META HTTP-EQUIV="refresh" CONTENT="10">
</head>
<body onload="mouse_capture_init(); fit();" onresize="fit();">
<br>[HELPER1X]</p>
</body>
</html>


For testing purposes, try this instead:
Code: [Select]
<!DOCTYPE html>
<html>
<body>

<div id="test">
<button type="button" onclick="refresh()">Refresh</button>
</div>

<script>
function refresh() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("test").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "http://IP:PORT/templateName/?param3=yourfile.json", true);
  xhttp.send();
}
</script>

</body>
</html>

Re: HTTPControl - is there a template that only shows the playing info?

Reply #10
Thanks! I've been doing this automation stuff for more than ten years, but I still suck at coding...  :))

And thanks for ther new code! When loading that code I get a button with Refresh on it, and clicking on that gives me the current text from Foobar. And that makes the text my automation setup takes out of it change too. Then, using

Code: [Select]
<META HTTP-EQUIV="refresh" CONTENT="1">

to refresh every second, it gives me the correct changes to my automation setup in realtime! Of course going down to 5 seconds would probably be wise, but I don't think it uses much resources to refresh that simple code, right? Removing the Div with the button still doesn't damage anything. So I do constant refreshes of the code that I'm left with, and that fixes my problem from the automation system. Running the exact same code from the automation system in LUA:

Code: [Select]
FooBar1 = socket.http.request('http://192.168.0.1:8889/default')
FooBar = socket.http.request('http://192.168.0.1:8889/minimal/?param3=minimal.json')

print (FooBar)

does not update. So there's something in the way Girder reads the file that doesn't update it. Looks like I may have to run with a system where I let Chrome update the minimal minimal page from your code to get updates to the system. Still, I have more than enough CPU ressources and memory to get a new VM into the server. If you don't have an idea what the non-updates from Girder can be, that is.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #11
It's a very simple request, it will use virtually no resources, so you are safe to refresh it every 1 second if needed.

To debug the problem you are seeing with LUA, you could check the foobar2000 output console (make sure HTTP Control setting to log to console is checked) to see if there's any error logged there.
Another thing is checking the http request sent by LUA and compare it with the one sent by the web browser. You'll need a packet sniffer, such as WireShark, to do that.

Additionally, you could try another LUA library like this one:
https://github.com/JakobGreen/lua-requests

Code: [Select]
response = requests.get('http://192.168.0.1:8889/minimal/?param3=minimal.json')

Re: HTTPControl - is there a template that only shows the playing info?

Reply #12
I have no idea what the heck (I was about to write something stronger) happened, but suddenly the code over in Girder actually works! So it seems I can do with only the code, as long as I first run the request for default with your code and then the request for the minimal code. Thanks a lot for the help! If you could be bothered, maybe you could please help me a bit more by changing the code so that I see what's refreshing in a browser, just for debugging if I should need it. For now I'm on this (with the button removed):

Code: [Select]
<!DOCTYPE html>
<html>
<body>
<META HTTP-EQUIV="refresh" CONTENT="1">


<script>
function refresh() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("test").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "http://192.168.0.1:8889/Default/?param3=minimal.json", true);
  xhttp.send();
}
</script>

</body>
</html>
And that doesn't show any text.
Btw was that the magic of Google that NRK is a Norwegian channel and that our language is called norsk, or did you already know that? :)

Re: HTTPControl - is there a template that only shows the playing info?

Reply #13
I'm glad it works now!

The change in the html code is fairly easy. You need to tell the web page to call the function when te page is loaded.

Code: [Select]
<!DOCTYPE html>
<html>
<body onLoad="refresh()">
<META HTTP-EQUIV="refresh" CONTENT="1">
<script>
function refresh() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("test").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "http://192.168.0.1:8889/Default/?param3=minimal.json", true);
  xhttp.send();
}
</script>
</body>
</html>

I like learning and know about different languages, especially when they are from people, sort of, close from where I live.
It's not that I know much about norsk, but I know more or less how it looks. You could probably fool me with a danish text anyway :)

Re: HTTPControl - is there a template that only shows the playing info?

Reply #14
I could fool somebody from Oslo with Danish! ;) I live in the southern Norway (actually as far south as you can get), and many from Oslo think we sound Danish. Of course we are closer to Denmark to them... ;)

But I do still not see any text in the Firefox window when logging on to Default. I only see the console changing:

Code: [Select]
foo_httpcontrol: [ 17.01 16:50:12 ] 192.168.0.157 	/Default/&param3=minimal.json  	: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:48.0) Gecko/20100101 Firefox/48.0

Re: HTTPControl - is there a template that only shows the playing info?

Reply #15
Sorry, I didn't realize you had deleted the <div> too.

This should do..
Code: [Select]
<!DOCTYPE html>
<html>
<body onLoad="refresh()">
<META HTTP-EQUIV="refresh" CONTENT="1">
<div id="output">
</div>
<script>
function refresh() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("output").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "http://192.168.0.1:8889/Default/?param3=minimal.json", true);
  xhttp.send();
}
</script>
</body>
</html>


When I watched Mr Robot TV Show first season about 1 year ago, I read about mutual intelligibility between danish, swedish and norsk, being the latter then one recommended to learn first so the rest would be easier to learn and understand later.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #16
This is getting funny! Now it's the webpage that doesn't update before the Girder LUA code sends an update! I use http://192.168.0.1:8889/default/ as the address.

And I actually know what article you mean, I think. "The Scandinavian Languages — Three For The Price Of One?" on Babbel, right? I have read that myself, and it's quite fitting.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #17
It's starting to not make sense at all! lol
If both LUA and the html are both requesting the same .json file with the same url, both should get updated results. I've tested multiple devices requesting to the same foobar2000 at the same time and that works fine.
I wonder what may be happening...
Another alternative is using another command to retrieve only the info you need.
http://192.168.0.1:8889/default/?cmd=FormatTitles&param1=%25title%25
That'll return something like:
Code: [Select]
["Title of the song"]

Yes, that's one of the things I read about the topic :)

Re: HTTPControl - is there a template that only shows the playing info?

Reply #18
Oh, maybe I was unclear. I do not use the same code in Girder to do the refresh and get the title (and artist on the channels who broadcast them separately, which is mainly Radio Rock). I first run the code in LUA that refreshes:

Code: [Select]
FooBar1 = socket.http.request('http://192.168.0.1:8889/default')

This gives me output like this:

Code: [Select]
<!DOCTYPE html>
<html>
<body onLoad="refresh()">
<META HTTP-EQUIV="refresh" CONTENT="1">
<div id="output">
</div>
<script>
function refresh() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("output").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "http://192.168.0.1:8889/Default/?param3=minimal.json", true);
  xhttp.send();
}
</script>
</body>
</html>

Then I run the code to get what I need:

Code: [Select]
FooBar = socket.http.request('http://192.168.0.1:8889/minimal/?param3=minimal.json')

That gives me this:

Code: [Select]
Tonight, Tonight - The Smashing Pumpkins

And on another channel this:

Code: [Select]
Her og nå. Norges største nyhetsmagasin på radio. Trafikkoppdateringer.

(A news program).

And the code you have above picks out the title, yeah. But I will put the LUA code for Girder that I have shown above into production and see what happens. Thanks for your help! I'll let you know how it pans out in a few days (maybe I won't have time to put it fully in use until the weekend).

Re: HTTPControl - is there a template that only shows the playing info?

Reply #19
Good! Yes, please let me know when you progress on it.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #20
Well, I have implemented this now, and there is one hiccup: Norwegian characters (æøåÆØÅ) are not coded correctly. I see them as that when I open a webpage to my (well, your...) json (which now is renamed to mastiff.json to avoid any mixups). So this code:

Code: [Select]
http://192.168.0.1:8889/mastiff/?param3=mastiff.json

gives me

Quote
Brønsj

in Firefox. But the same code in LUA:

Code: [Select]
socket.http.request('http://192.168.0.1:888'.. NettradioInfoSone .. '/mastiff/?param3=mastiff.json')

gives me

Quote
Brønsj

in the automation system. But that is be a problem with the automation because when I use that code on another site, like a Norwegian newspaper, I get the same errors. So I have set up a bunch of gsub (substitutes characters in a string) that fixes it for now, and I will try to find out from the LUA experts on a forum if I can receive this in the right codepage in any way. If not the subs doesn't really take up much CPU. Thanks lot for the help!  :)

Re: HTTPControl - is there a template that only shows the playing info?

Reply #21
That would appear to be UTF-8 being treated as ISO-8859-1. Your http request appears to be ignoring the specified character set from the request response headers.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #22
Yeah, I just don't know where to change something so it works. if it is in Girder, I don't know if it can be fixed because I use a version that isn't supported anymore. But I have asked a question at the forum there.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #23
It's definitely in Girder. Someone just reported the same exact issue a few days ago.

Re: HTTPControl - is there a template that only shows the playing info?

Reply #24
I see. Sure it wasn't me? :)