This repository was archived by the owner on Sep 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchData.php
More file actions
61 lines (49 loc) · 1.97 KB
/
fetchData.php
File metadata and controls
61 lines (49 loc) · 1.97 KB
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
<?php
function curl_get_contents( $url ){
$CH = curl_init();
curl_setopt( $CH, CURLOPT_URL, $url );
curl_setopt( $CH, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $CH, CURLOPT_HEADER, false );
$data = curl_exec( $CH );
if( $data == false ){
die("<br><br>Something went wrong!\n" . curl_error( $CH ) );
}
curl_close( $CH );
return $data;
}
$ShouldUPDATE = TRUE;
$CurrentData = file_get_contents("data/vatsim-data.txt");
foreach( explode( "\n", $CurrentData ) as $line ){
if( !preg_match('/UPDATE = /', $line ) ){ continue; }
$line = explode( "UPDATE = ", $line );
$date = $line[1];
$y = substr( $date, 0, 4 );
$mo = substr( $date, 4, 2 );
$d = substr( $date, 6, 2 );
$h = substr( $date, 8, 2 );
$mi = substr( $date, 10, 2 );
$s = substr( $date, 12, 2 );
if( time() - mktime( $h, $mi, $s, $mo, $d, $y ) < 120 ){
$ShouldUPDATE = FALSE;
}else if( time() - mktime( $h, $mi, $s, $mo, $d, $y ) < 30 ){
$ShouldUPDATE = TRUE;
sleep( time() - mktime( $h, $mi, $s, $mo, $d, $y ) );
}
}
if( $ShouldUPDATE ){
echo "Data Expired, fetching from: ";
if( filemtime( "data/status.txt" ) + ( 60*60*12 ) < time() ){
$VATSIMStatus = curl_get_contents( "https://status.vatsim.net/status.txt" );
file_put_contents( "data/status.txt", $VATSIMStatus );
$VATSIMStatus = explode( "\n", $VATSIMStatus );
}else{
$VATSIMStatus = explode( "\n", file_get_contents( "data/status.txt" ) );
}
$DATAServers = array();
foreach( $VATSIMStatus as $VS ){ if( preg_match('/url0=/', $VS ) ){ array_push( $DATAServers, explode( "url0=", $VS )[1] ); } }
echo $DATAServers[ array_rand( $DATAServers ) ];
file_put_contents( "data/vatsim-data.txt", curl_get_contents( trim( $DATAServers[ array_rand( $DATAServers ) ] ) ), LOCK_EX );
include( "doTime.php" );
}else{
echo "Data has not expired, not re-fetching!";
}