Skip to content

Commit

Permalink
Merge branch 'feature/20-declarative-downloading'
Browse files Browse the repository at this point in the history
fixes voc#20
  • Loading branch information
MaZderMind committed Dec 18, 2016
2 parents 738878b + da739f3 commit 23c70f3
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 31 deletions.
163 changes: 163 additions & 0 deletions command/download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

$conf = $GLOBALS['CONFIG']['DOWNLOAD'];

if(isset($conf['REQUIRE_USER']))
{
if(get_current_user() != $conf['require-user'])
{
stderr(
'Not downloading files for user %s, run this script as user %s',
get_current_user(),
$conf['require-user']
);
exit(2);
}
}

$conferences = Conferences::getConferences();

if(isset($conf['MAX_CONFERENCE_AGE']))
{
$months = intval($conf['MAX_CONFERENCE_AGE']);
$conferencesAfter = new DateTime();
$conferencesAfter->sub(new DateInterval('P'.$months.'D'));

stdout('Skipping Conferences before %s', $conferencesAfter->format('Y-m-d'));
$conferences = array_filter($conferences, function($conference) use ($conferencesAfter) {
if($conference->isOpen())
{
stdout(
' %s: %s',
'---open---',
$conference->getSlug()
);

return true;
}

$isBefore = $conference->endsAt() < $conferencesAfter;

if($isBefore) {
stdout(
' %s: %s',
$conference->endsAt()->format('Y-m-d'),
$conference->getSlug()
);
}

return !$isBefore;
});
}

stdout('');
foreach ($conferences as $conference)
{
stdout('== %s ==', $conference->getSlug());

$relive = $conference->getRelive();
if($relive->isEnabled())
{
download(
'relive-json',
$conference,
$relive->getJsonUrl(),
$relive->getJsonCache()
);
}

$schedule = $conference->getSchedule();
if($schedule->isEnabled())
{
download(
'schedule-xml',
$conference,
$schedule->getScheduleUrl(),
$schedule->getScheduleCache()
);
}

foreach($conference->getExtraFiles() as $filename => $url)
{
download(
'extra-file',
$conference,
$url,
get_file_cache($conference, $filename)
);
}
}




function get_file_cache($conference, $filename)
{
return joinpath([$GLOBALS['BASEDIR'], 'configs/conferences', $conference->getSlug(), $filename]);
}

function download($what, $conference, $url, $cache)
{
$info = parse_url($url);
if(!isset($info['scheme']) || !isset($info['host']))
{
stderr(
' !! %s url for conference %s does look like an old-style path: "%s". please update to a full http/https url',
$what,
$conference->getSlug(),
$url
);
return false;
}

stdout(
' downloading %s from %s to %s',
$what,
$url,
$cache
);
if(!do_download($url, $cache))
{
stderr(
' !! download %s for conference %s from %s to %s failed miserably !!',
$what,
$conference->getSlug(),
$url,
$cache
);
}
return true;
}

function do_download($url, $cache)
{
$handle = curl_init($url);
curl_setopt_array($handle, [
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, /* accept all certificates, even self-signed */
CURLOPT_SSL_VERIFYHOST => 2, /* verify hostname is in cert */
CURLOPT_CONNECTTIMEOUT => 3, /* connect-timeout in seconds */
CURLOPT_TIMEOUT => 5, /* transfer timeout im seconds */
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_REFERER => 'https://streaming.media.ccc.de/',
CURLOPT_USERAGENT => '@c3voc Streaming-Website Downloader-Cronjob, Contact voc AT c3voc DOT de in case of problems. Might the Winkekatze be with you',
]);

$return = curl_exec($handle);
$info = curl_getinfo($handle);
curl_close($handle);

if($info['http_code'] != 200)
return false;

$tempfile = tempnam(dirname($cache), 'dl-');
if(false === file_put_contents($tempfile, $return))
return false;

chmod($tempfile, 0644);
rename($tempfile, $cache);

return true;
}
27 changes: 26 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Protokollfreie URLs (welche, die mit // beginnen), werden automatisch mit dem korrekten Protokoll ergänzt.
* In diesem Fall wird auch ein SSL-Umschalt-Button im Header angezeigt
*/
if($_SERVER['SERVER_NAME'] == 'localhost')
if(@$_SERVER['SERVER_NAME'] == 'localhost')
{
// keine Konfiguration -> BASEURL wird automatisch erraten
}
Expand All @@ -38,3 +38,28 @@
// Set a safe Default
$GLOBALS['CONFIG']['BASEURL'] = '//streaming.media.ccc.de/';
}


/**
* Konfiguration für den Datei-Download Cronjob
*/
$GLOBALS['CONFIG']['DOWNLOAD'] = [
/**
* Verweigeren Download, wenn der PHP-Prozess unter einem anderen Benutzer als diesem läuft
* Auskommentieren um alle Benutzer zu erlauben
*/
//'REQUIRE_USER' => 'www-data',

/**
* Wartende HTTP-Downloads nach dieser Anzahl von Sekunden abbrechen
*/
'HTTP_TIMEOUT' => 5 /* Sekunden */,

/**
* Nur Dateien von Konferenzen herunterladen, die weniger als
* diese Aanzahl von Tagen alt sind (gemessen am END_DATE)
*
* Auskommentieren, um alle Konferenzen zu beachten
*/
'MAX_CONFERENCE_AGE' => 14 /* Tage */,
];
6 changes: 6 additions & 0 deletions configs/conferences/32c3/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,5 +667,11 @@
'TEXT' => '#32C3 #%s',
);

$CONFIG['EXTRA_FILES'] = array(
'schedule.xml' => 'https://events.ccc.de/congress/2015/Fahrplan/schedule.xml',
'schedule.json' => 'https://events.ccc.de/congress/2015/Fahrplan/schedule.json',
'everything.schedule.xml' => 'http://data.testi.ber.c3voc.de/32C3/everything.schedule.xml',
'everything.schedule.json' => 'http://data.testi.ber.c3voc.de/32C3/everything.schedule.json',
);

return $CONFIG;
2 changes: 1 addition & 1 deletion configs/conferences/chaosradio/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
*
* Wird diese Zeile auskommentiert, wird der Link nicht angezeigt
*/
'RELIVE_JSON' => 'configs/conferences/chaosradio/vod.json',
'RELIVE_JSON' => 'http://live.dus.c3voc.de/relive/chaosradio/index.json',
);

/**
Expand Down
3 changes: 0 additions & 3 deletions configs/conferences/chaosradio/download.sh

This file was deleted.

3 changes: 1 addition & 2 deletions configs/conferences/datengarten/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@
*
* Wird diese Zeile auskommentiert, wird der Link nicht angezeigt
*/
//'RELIVE_JSON' => 'configs/vod.json',
'RELIVE_JSON' => 'configs/conferences/datengarten/vod.json',
'RELIVE_JSON' => 'http://live.dus.c3voc.de/relive/datengarten/index.json',
);

/**
Expand Down
3 changes: 0 additions & 3 deletions configs/conferences/datengarten/download.sh

This file was deleted.

21 changes: 20 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

if(!ini_get('short_open_tag'))
die('`short_open_tag = On` is required');
die("`short_open_tag = On` is required\n");

$GLOBALS['BASEDIR'] = dirname(__FILE__);
chdir($GLOBALS['BASEDIR']);

require_once('config.php');
require_once('lib/helper.php');
Expand All @@ -27,6 +30,22 @@


ob_start();
if(isset($argv) && isset($argv[1]))
{
require('lib/command-helper.php');

switch($argv[1])
{
case 'download':
require('command/download.php');
exit(0);
}

stderr("Unknown Command: %s", $argv[1]);
exit(1);
}


try {
if(isset($_GET['htaccess']))
{
Expand Down
15 changes: 15 additions & 0 deletions lib/command-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

function stderr($str) {
$args = func_get_args();
$args[0] = $args[0]."\n";
array_unshift($args, STDERR);
call_user_func_array('fprintf', $args);
}

function stdout($str) {
$args = func_get_args();
$args[0] = $args[0]."\n";
array_unshift($args, STDOUT);
call_user_func_array('fprintf', $args);
}
30 changes: 23 additions & 7 deletions model/Conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getTitle() {
}

public function isPreviewEnabled() {
if($GLOBALS['forceopen'])
if(@$GLOBALS['forceopen'])
return true;

if($this->has('PREVIEW_DOMAIN') && ($this->get('PREVIEW_DOMAIN') == $_SERVER['SERVER_NAME']))
Expand All @@ -32,12 +32,22 @@ public function isClosed() {
return !$this->hasBegun() || $this->hasEnded();
}

public function isOpen() {
return !$this->isClosed();
}

public function startsAt() {
return $this->get('CONFERENCE.STARTS_AT');
if(!$this->has('CONFERENCE.STARTS_AT'))
return null;

return DateTime::createFromFormat('U', $this->get('CONFERENCE.STARTS_AT'));
}

public function endsAt() {
return $this->get('CONFERENCE.ENDS_AT');
if(!$this->has('CONFERENCE.ENDS_AT'))
return null;

return DateTime::createFromFormat('U', $this->get('CONFERENCE.ENDS_AT'));
}

public function hasBegun() {
Expand All @@ -62,7 +72,8 @@ public function hasBegun() {
}

if($this->has('CONFERENCE.STARTS_AT')) {
return time() >= $this->get('CONFERENCE.STARTS_AT');
$now = new DateTime('now');
return $now >= $this->startsAt();
} else {
return true;
}
Expand All @@ -84,7 +95,8 @@ public function hasEnded() {
}

if($this->has('CONFERENCE.ENDS_AT')) {
return time() >= $this->get('CONFERENCE.ENDS_AT');
$now = new DateTime('now');
return $now >= $this->endsAt();
} else {
return false;
}
Expand Down Expand Up @@ -128,10 +140,10 @@ public function getAboutLink() {
}

public function hasRelive() {
return $this->has('CONFERENCE.RELIVE_JSON');
return $this->getRelive()->isEnabled();
}
public function getReliveUrl() {
if($this->has('CONFERENCE.RELIVE_JSON'))
if($this->getRelive()->isEnabled())
return joinpath([$this->getSlug(), 'relive']).url_params();

else
Expand Down Expand Up @@ -209,4 +221,8 @@ public function getOverview() {
public function getRelive() {
return new Relive($this);
}

public function getExtraFiles() {
return $this->get('EXTRA_FILES', []);
}
}
4 changes: 2 additions & 2 deletions model/Conferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function getConferencesCount() {

public static function getActiveConferences() {
return array_values(array_filter(
Conferences::getConferences(),
Conferences::getConferencesSorted(),
function($conference) {
return !$conference->isClosed();
}
Expand All @@ -48,7 +48,7 @@ public static function getConferencesSorted() {
$sorted = Conferences::getConferences();

usort($sorted, function($a, $b) {
return $b->startsAt() - $a->endsAt();
return $b->startsAt() > $a->endsAt() ? 1 : -1;
});

return $sorted;
Expand Down
Loading

0 comments on commit 23c70f3

Please sign in to comment.