-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistory-downloader.php
46 lines (34 loc) · 1.02 KB
/
history-downloader.php
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
<?php
// Downloads historical data from https://coinmarketcap.com/historical/
// beginning of historical data
define('BIG_BANG', '2013-04-28');
define('HIST_URL', 'https://coinmarketcap.com/historical/%s/');
define('DOWNLOAD_DIR', 'history/raw/');
// delay between consecutive page downloads -- please be considerate!
define('DELAY', 2);
$date = BIG_BANG;
$today = date('Y-m-d');
@mkdir(DOWNLOAD_DIR, 0755, true);
while ($date < $today) {
download($date);
$date = addWeek($date);
}
/*************************************************************************/
function getUrl($date) {
return sprintf(HIST_URL, str_replace('-', '', $date));
}
function addWeek($date) {
$d = DateTime::createFromFormat('Y-m-d', $date);
$d->modify('+1 week');
return $d->format('Y-m-d');
}
function download($date) {
$file = sprintf('%s%s.html', DOWNLOAD_DIR, $date);
if (file_exists($file)) {
return;
}
$url = getUrl($date);
printf("downloading $url to $file\n");
file_put_contents($file, file_get_contents($url));
sleep(2);
}