Skip to content

Commit

Permalink
Merge branch 'feature/better-web-for-obs'
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Apr 13, 2021
2 parents 9f13d9f + d74bf10 commit 6a29593
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 53 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SSLScrobbler v0.25
# SSLScrobbler v0.26

[![Testing sslscrobbler](https://github.com/ben-xo/sslscrobbler/actions/workflows/testing.yml/badge.svg)](https://github.com/ben-xo/sslscrobbler/actions/workflows/testing.yml)

Expand Down Expand Up @@ -135,6 +135,7 @@ It is more flexible when used from Terminal.
**TO QUIT:**

To quit SSL Scrobbler, click on its window and press Ctrl-C.
(or in the macOS app, press the quit button in the bottom right)


## 1.3 Quick HOWTO
Expand All @@ -158,7 +159,11 @@ TO SCROBBLE SEVERAL PEOPLE IN THE ROOM:
TO TWEET AS YOU PLAY

php historyreader.php -T twitterusername



MAKE TRACK DATA AVAILABLE FOR OBS (OR WHATEVER)

php historyreader.php -J 8080


## 1.3 Options
Expand Down Expand Up @@ -227,6 +232,13 @@ Put the now playing track into an IRC channel using IRCCat. (IRCCat sold
separately - https://github.com/RJ/irccat).


**JSON Server options**
`-J` or `--json port`
Makes the current playing track info available at `http://<your ip>:<port>/nowplaying.json`.
Also, makes it available in a way which can be styled using CSS (which e.g. for OBS) at
`http://<your ip>:<port>/nowplaying.html`


# 2. HOW IT WORKS


Expand Down
7 changes: 5 additions & 2 deletions SSL/HistoryReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ public function main($argc, array $argv)

try
{
// do this now so that we can still use defailt logging during parsing options
$this->setupLogging();

$this->parseOptions($argv);
// do this as early as possible, but not before parsing options which may affect it.

// do it again, as parsing options may have altered the logging setup
$this->setupLogging();

if (!$this->dir_provided) {
Expand Down
62 changes: 57 additions & 5 deletions SSL/Plugins/JsonServer/CLIJsonServerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
class CLIJsonServerPlugin implements CLIPlugin
{
/**
* @var array of JsonServerPlugin
* @var array[JsonServerPlugin]
*/
protected $plugins = array();

protected $used_ports = array();

protected $config;

Expand All @@ -45,8 +47,18 @@ public function setConfig(array $config)

public function usage($appname, array $argv)
{
echo "JSON Server options:\n";
echo " -J or --json <port> : Listen on port at http://localhost:<port>/nowplaying.json and server the details of the current playing track\n";
echo "JSON / HTML Server options:\n";
echo " -J or --json <port>\n";
echo " or -H or --html <port> : Enable local web server for the details of the current playing track.\n";
echo " The info will be available as JSON at http://<your ip>:<port>/nowplaying.json and\n";
echo " also as HTML for styling with OBS at http://<your ip>:<port>/nowplaying.html\n";
echo "\n";
echo " If OBS runs on the same computer you can use URLs such as http://localhost:<port>/nowplaying.html\n";
echo "\n";
echo " By default, all available fields are displayed, but you can narrow it down to just the fields you want\n";
echo " using URLs such as http://<your ip>:<port>/nowplaying.html?artist&title which you can style with CSS.\n";
echo "\n";
echo " --html-template <file>: A optional file containing strings like {{artist}} or {{title}}.\n";
echo "\n";
}

Expand All @@ -57,19 +69,58 @@ public function usage($appname, array $argv)
*/
public function parseOption($arg, array &$argv)
{
if($arg == '--json' || $arg == '-J')
if($arg == '--json' || $arg == '-J' || $arg == '--html' || $arg == '-H')
{
$port = array_shift($argv);

if(in_array($port, $this->used_ports))
throw new RuntimeException("Port $port is already claimed by another JSON / HTML Server. Did you mean to specify more than one?");

$this->used_ports[] = $port;
$this->plugins[] = $this->newJsonServerPlugin($this->config, $port);
return true;
}

if($arg == '--html-template')
{
$template_filename = array_shift($argv);

if(!file_exists($template_filename) || !is_file($template_filename))
throw new RuntimeException("File '$template_filename' does not exist.");

if(!is_readable($template_filename))
throw new RuntimeException("File '$template_filename' can not be read.");

$template_body = file_get_contents($template_filename);
if($template_body === false)
throw new RuntimeException("Could not load file '$template_body'");

$this->config['template'] = $template_body;

$match_count = preg_match_all('/{{([a-zA-Z0-9]+?)}}/', $template_body, $matches);
if(!$match_count)
throw new RuntimeException("Template $template_filename does not contain any fields such as {{artist}} or {{title}}");

$this->config['template_fields'] = $matches[1];

L::level(L::INFO) &&
L::log(L::INFO, __CLASS__, "loaded HTML template from %s with %d templated fields",
array($template_filename, $match_count));

L::level(L::DEBUG) &&
L::log(L::DEBUG, __CLASS__, "Fields in this template: %s",
array(implode(', ', $matches[1])));

return true;
}

return false;
}

public function addPrompts(array &$argv)
{
$port = 8080;
echo "Json Server: now playing info will be available at http://localhost:$port/nowplaying.json\n";

$argv[] = '-J';
$argv[] = $port;
}
Expand All @@ -82,6 +133,7 @@ public function addPluginsTo(SSLPluggable $sslpluggable)

foreach($this->plugins as $plugin)
{
$plugin->setConfig($this->config);
$sslpluggable->addPlugin($plugin);
}
}
Expand Down
Loading

0 comments on commit 6a29593

Please sign in to comment.