aboutsummaryrefslogtreecommitdiff
path: root/command/download.php
blob: d761ff36e2948468512ec2ce7d8292be40e46648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?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;
		}

		if($conference->endsAt() == null)
			return false;

		$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_for_conference(
			'relive-json',
			$conference,
			$relive->getJsonUrl(),
			$relive->getJsonCache()
		);
	}

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

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

stdout('');
stdout('== eventkalender ==');
download(
	'eventkalender',
	'https://c3voc.de/eventkalender/events.json?filter=upcoming&streaming=yes',
	joinpath([$GLOBALS['BASEDIR'], 'configs/upcoming.json'])
);





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

function download_for_conference($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
	);
	$resp = do_download($url, $cache);
	if($resp !== true)
	{
		stderr(
			'  !! download %s for conference %s from %s to %s failed miserably: %s !!',
			$what,
			$conference->getSlug(),
			$url,
			$cache,
			$resp
		);
	}
	return true;
}

function download($what, $url, $cache)
{
	stdout(
		'  downloading %s from %s to %s',
		$what,
		$url,
		$cache
	);
	$resp = do_download($url, $cache);
	if($resp !== true)
	{
		stderr(
			'  !! download %s from %s to %s failed miserably: %s !!',
			$what,
			$url,
			$cache,
			$resp
		);
	}
	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_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 'http-code = '.$info['http_code'];

	$tempfile = tempnam(dirname($cache), 'dl-');
	if(!$tempfile)
		return 'could not create tempfile in '.dirname($cache);

	if(false === file_put_contents($tempfile, $return))
		return 'could write data into tempfile '.$tempfile;

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

	return true;
}