Skip to content

Instantly share code, notes, and snippets.

@Metapyziks
Last active December 17, 2015 21:59
Show Gist options
  • Select an option

  • Save Metapyziks/5678522 to your computer and use it in GitHub Desktop.

Select an option

Save Metapyziks/5678522 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
namespace FoobarControlServer
{
class Program
{
static readonly Dictionary<String, String> _actions = new Dictionary<string, string> {
{ "prev", "%({LEFT})" },
{ "pause", "%({DOWN})" },
{ "play", "%({UP})" },
{ "skip", "%({RIGHT})" },
{ "shuffle", "%(^({RIGHT}))" }
};
static Process FoobarProcess
{
get { return Process.GetProcessesByName("foobar2000").FirstOrDefault(); }
}
static void DoHotkey(String key)
{
System.Windows.Forms.SendKeys.SendWait(key);
}
static readonly String _defaultTrackInfo = "?? // ??";
static String GetTrackInfoString()
{
if (FoobarProcess == null) {
return _defaultTrackInfo;
}
String windowTitle = FoobarProcess.MainWindowTitle;
if (!windowTitle.Contains("[foobar2000")) {
return _defaultTrackInfo;
}
windowTitle = windowTitle.Substring(0, windowTitle.LastIndexOf("[foobar2000"));
int artistEnd = windowTitle.IndexOf("- [");
int titleStart = windowTitle.LastIndexOf(']') + 1;
String artist = windowTitle.Substring(0, artistEnd).Trim();
String title = windowTitle.Substring(titleStart).Trim();
if (title.Contains("//")) {
int split = title.IndexOf("//");
artist = title.Substring(split + 2).Trim();
title = title.Substring(0, split);
}
return String.Format("{0} // {1}", title, artist);
}
static void Main(string[] args)
{
Console.WriteLine("Loading index.html ...");
String prefix, suffix;
{
String index = File.ReadAllText("index.html");
int replStart = index.IndexOf("#TRACKINFO#");
int replEnd = replStart + 11;
prefix = index.Substring(0, replStart);
suffix = index.Substring(replEnd);
}
Console.WriteLine("Starting listener ...");
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:80/");
listener.Start();
Console.WriteLine("Ready to accept connections");
while (listener.IsListening) {
var context = listener.GetContext();
String action = context.Request.RawUrl.Substring(1);
if (_actions.ContainsKey(action)) {
Console.WriteLine("> {0}", action);
DoHotkey(_actions[action]);
}
try {
using (var writer = new StreamWriter(context.Response.OutputStream)) {
if (action == "trackinfo") {
writer.Write("{\"info\":\"");
writer.Write(GetTrackInfoString());
writer.Write("\"}");
} else {
writer.Write(prefix);
writer.Write(GetTrackInfoString());
writer.Write(suffix);
}
}
context.Response.OutputStream.Flush();
} catch {
} finally {
context.Response.OutputStream.Close();
}
}
Console.WriteLine("Closing listener ...");
listener.Close();
Console.WriteLine("Exiting ...");
}
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hijack James' Music Player</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
<script type="text/javascript">
function sendCommand(cmd) {
new Ajax.Request("/" + cmd, {
method: "get",
onSuccess: function (transport) {
setTimeout(updateTrackInfo, 100);
}
});
return true;
}
function updateTrackInfo() {
new Ajax.Request("/trackinfo", {
method: "get",
onSuccess: function (transport) {
var json = transport.responseText.evalJSON();
$("trackinfo").update(json.info);
}
});
}
window.onload = function () {
window.setInterval(updateTrackInfo, 2000);
}
</script>
<style type="text/css">
#header { text-align: center; top:32px }
#outer { height:100%; width:100% }
#inner { position:absolute; top:50%; left:50%; height:520px; width:520px; margin-top:-260px; margin-left:-260px }
h1 { font-family: Calibri; font-size: 48px; margin-top:32px; color:#333333 }
h2 { font-family: Calibri; font-size: 24px; color:#666666 }
</style>
</head>
<body>
<div id="outer">
<div id="header">
<h2>Current Track</h2>
<h1 id="trackinfo">#TRACKINFO#</h1>
</div>
<div id="inner">
<a href="#" onclick="return sendCommand('pause');"><img src="http://puu.sh/34pv4" width="256" height="256" /></a>
<a href="#" onclick="return sendCommand('shuffle');"><img src="http://puu.sh/34pv6" width="256" height="256" /></a>
<br />
<a href="#" onclick="return sendCommand('prev');"><img src="http://puu.sh/34pv5" width="256" height="256" /></a>
<a href="#" onclick="return sendCommand('skip');"><img src="http://puu.sh/34pv0" width="256" height="256" /></a>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment