-
-
Save kevjs1982/ce9075bd102225719f38f77c82dab5b9 to your computer and use it in GitHub Desktop.
TVHeadend Search Data to ICS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
mb_internal_encoding("UTF-8"); | |
$urls = array(); | |
$urls['f1'][] = "http://tvheadend:9981/api/epg/events/grid?start=0&limit=999999&title=Grand%20Prix&fulltext=false&sort=start&dir=ASC"; | |
$urls['f1'][] = "http://tvheadend:9981/api/epg/events/grid?start=0&limit=999999&title=F1&fulltext=false&sort=start&dir=ASC"; | |
$urls['fe'][] = "http://tvheadend:9981/api/epg/events/grid?start=0&limit=999999&title=Formula%20E&fulltext=false&sort=start&dir=ASC"; | |
foreach($urls as $sport=>$lists) | |
{ | |
print_r($lists); | |
$title = ($sport == 'f1') ? 'Formula 1' : 'Formula E'; | |
$entries = array(); | |
$events = array(); | |
foreach($lists as $url) | |
{ | |
$entries = array_merge($entries,json_get($url)); | |
} | |
// print_r(json_get($url)); | |
$out = ""; | |
$out .= "BEGIN:VCALENDAR\n"; | |
$out .= "VERSION:2.0\n"; | |
$out .= "PRODID:-//kjs/tv//NONSGML v1.0//EN\n"; | |
$out .= "X-WR-CALNAME:Live {$title} on TV\n"; | |
foreach($entries as $entry) | |
{ | |
$start = new DateTime("@$entry->start",new DateTimeZone("Europe/London")); | |
$end = new DateTime("@$entry->stop",new DateTimeZone("Europe/London")); | |
$add = false; | |
if ( in_array($start->format('D'),array('Sat','Sun'))) | |
{ | |
switch($entry->channelName) | |
{ | |
case "Channel 4 HD": | |
case "Sky Sports F1 HD": | |
if ( stripos($entry->title,"Live")!==false && stripos($entry->title,"Practice")===false) | |
{ | |
echo "{$entry->channelName}\t{$entry->title}\t" . $start->format('Y-m-d H:i:s') . " to " . $end->format('Y-m-d H:i:s') . "\n"; | |
$add = true; | |
$events[$start->format("YmdHis") . "-" . $entry->eventId . "." . $entry->episodeId] = $entry; | |
} | |
break; | |
case "5 Spike": | |
case "5 HD": | |
if ( stripos($entry->title,"Highlights")===false) | |
{ | |
echo "{$entry->channelName}\t{$entry->title}\t" . $start->format('Y-m-d H:i:s') . " to " . $end->format('Y-m-d H:i:s') . "\n"; | |
$events[$start->format("YmdHis") . "-" . $entry->eventId . "." . $entry->episodeId] = $entry; | |
} | |
break; | |
default: | |
// Nothing | |
} | |
} | |
} | |
foreach($events as $entry) | |
{ | |
$entry->title = str_replace("FIA","",$entry->title); | |
$start = new DateTime("@$entry->start",new DateTimeZone("Europe/London")); | |
$end = new DateTime("@$entry->stop",new DateTimeZone("Europe/London")); | |
$start->setTimeZone(new DateTimeZone("UTC")); | |
$end->setTimeZone(new DateTimeZone("UTC")); | |
$out .= "BEGIN:VEVENT\n"; | |
$out .= "UID:".$start->format("YmdHis") . "-" . $entry->eventId . "." . $entry->episodeId ."@example.com\n"; | |
$out .= "DTSTAMP:".$start->format('Ymd\THis\Z')."\n"; | |
$out .= "DTSTART:".$start->format('Ymd\THis\Z')."\n"; | |
$out .= "DTEND:".$end->format('Ymd\THis\Z')."\n"; | |
$out .= "SUMMARY:{$entry->title} on {$entry->channelName}\n"; | |
$out .= "LOCATION:{$entry->channelName}\n"; | |
$out .= "DESCRIPTION:" . ical_split('DESCRIPTION:', $entry->summary) . "\n"; | |
$out .= "END:VEVENT\n"; | |
} | |
$out .= "END:VCALENDAR\n"; | |
$out = str_replace("\n","\r\n",$out); | |
file_put_contents("/var/www/scripts/cal-{$sport}.ics",$out); | |
RemoteFiles::Post("https://www.example.com/f1fe.php?sport={$sport}",array("ics"=>$out)); | |
} | |
// ======================================================================================================================================= | |
function json_get($url,$index='entries') | |
{ | |
$json = json_decode(file_get_contents($url)); | |
if ($index === false) | |
{ | |
return $json; | |
} | |
else | |
{ | |
return $json->$index; | |
} | |
} | |
$desc = <<<TEXT | |
<p>Lines of text SHOULD NOT be longer than 75 octets, (och hör på den) excluding the line break. Long content lines SHOULD be split into a multiple line representations using a line "folding" technique.</p> | |
That is, a long line can be split between any two characters by inserting a CRLF | |
immediately followed by a single linear white space character (i.e., | |
SPACE, <b>US-ASCII</b> decimal 32 or HTAB, US-ASCII decimal 9). Any sequence | |
of CRLF followed immediately by a single linear white space character | |
is ignored (i.e., removed) when processing the content type. | |
TEXT; | |
function ical_split($preamble, $value) { | |
$value = trim($value); | |
$value = strip_tags($value); | |
$value = preg_replace('/\n+/', ' ', $value); | |
$value = preg_replace('/\s{2,}/', ' ', $value); | |
$preamble_len = strlen($preamble); | |
$lines = array(); | |
while (strlen($value)>(75-$preamble_len)) { | |
$space = (75-$preamble_len); | |
$mbcc = $space; | |
while ($mbcc) { | |
$line = mb_substr($value, 0, $mbcc); | |
$oct = strlen($line); | |
if ($oct > $space) { | |
$mbcc -= $oct-$space; | |
} | |
else { | |
$lines[] = $line; | |
$preamble_len = 1; // Still take the tab into account | |
$value = mb_substr($value, $mbcc); | |
break; | |
} | |
} | |
} | |
if (!empty($value)) { | |
$lines[] = $value; | |
} | |
return join($lines, "\n\t"); | |
} | |
class RemoteFiles | |
{ | |
public static function Post($url,$params,$headers=false,$user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3") | |
{ | |
$args = array(); | |
if (is_array($params)) | |
{ | |
foreach($params as $key=>$var) | |
{ | |
$args[$key] = urlencode($var); | |
} | |
$fields_string =""; | |
foreach($args as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string, '&'); | |
} | |
else | |
{ | |
$fields_string = $params; | |
} | |
$ch = curl_init(); | |
if ($headers !== false) | |
{ | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch,CURLOPT_POST, count($args)); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); | |
if($user_agent!=false) | |
{ | |
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); | |
} | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
return curl_exec ($ch); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment