Skip to content

Commit

Permalink
Added marcs office app
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoensing committed Sep 24, 2018
1 parent c5fbdfa commit 552456c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
33 changes: 24 additions & 9 deletions classes/LaMetricAbfahrten.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ class LaMetricAbfahrten {
public $destination = '';
public $destination_only;
public $replace_in_output = '';
public $prefix = '';
public $postfix = ' Min';

/**
* @param string $prefix
*/
public function setPrefix( $prefix ) {
$this->prefix = $prefix;
}

/**
* @param string $postfix
*/
public function setPostfix( $postfix ) {
$this->postfix = $postfix;
}

public function __construct( $origin, $destination) {
$this->destination = $destination;
Expand Down Expand Up @@ -55,9 +71,6 @@ public function setJourneys( $journeys ) {

public function getLaMetricJSONResponse() {

$title = $this->journeys[0]->origin . ' in Richtung ' . $this->destination;
$title = str_replace( $this->replace_in_output[0], $this->replace_in_output[1], $title );

$delay = '';

$frames = array();
Expand All @@ -66,10 +79,10 @@ public function getLaMetricJSONResponse() {

foreach ( $this->journeys as $journey ) {

$text = $journey->getRealtime() . ' Min';
$text = $journey->getRealtime();

$frames[] = [
"text" => $text,
"text" => $this->prefix. $text . $this->postfix,
"icon" => $this->frame_icon

];
Expand All @@ -85,10 +98,12 @@ public function getLaMetricJSONResponse() {
"frames" => $frames,
);

header( "Cache-Control: no-store, no-cache, must-revalidate, max-age=0" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: no-cache" );
header( "Content-type: application/json; charset=utf-8" );
if($this->debug != true) {
header( "Cache-Control: no-store, no-cache, must-revalidate, max-age=0" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: no-cache" );
header( "Content-type: application/json; charset=utf-8" );
}

$json = json_encode( $responseArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );

Expand Down
72 changes: 72 additions & 0 deletions marcs-office.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
function getResponse() {

$json = file_get_contents( 'https://api.marc.tv/' );
$obj = json_decode( $json );
$current_users = $obj->row->visitors;
$max_users = getMaxUsers( $current_users );

$response = $current_users . '(' . $max_users . ')';

return $response;
}


function getMaxUsers( $current_users ) {
$filename = 'marctvmaxusers.txt';
$local_cache_file = sys_get_temp_dir() . '/' . $filename;
$local_cache_file_day = sys_get_temp_dir() . '/day_' . $filename;
$today = date('d');

if ( ! file_exists( $local_cache_file ) ) {
file_put_contents( $local_cache_file, $current_users );
}

if ( ! file_exists( $local_cache_file_day ) ) {
file_put_contents( $local_cache_file_day, $today );
}

$cached_day = intval( file_get_contents( $local_cache_file_day ) );

/* invalidate cache if it is the next day */
if($cached_day != $today){
file_put_contents( $local_cache_file_day, $today );
file_put_contents( $local_cache_file, '0' );
}

$cached_users_raw = intval( file_get_contents( $local_cache_file ) );

if ( is_int( $cached_users_raw ) ) {
if ( $cached_users_raw < $current_users ) {
file_put_contents( $local_cache_file, $current_users );
$max_users = $current_users;
} else {
$max_users = $cached_users_raw;
}
} else {
return - 1;
}

return $max_users;
}



spl_autoload_register( function ( $class_name ) {
include 'classes/' . $class_name . '.class.php';
} );

$LaMetricAbfahrten = new LaMetricAbfahrten(
"Lister Platz (U), Hannover",
"Misburg, Hannover"
);

$LaMetricAbfahrten->setPostfix('\' '.getResponse());
$LaMetricAbfahrten->setFrameIcon('i23135');

echo $LaMetricAbfahrten->getLaMetricJSONResponse();




?>

0 comments on commit 552456c

Please sign in to comment.