Skip to content

Commit e5702a2

Browse files
committed
v3 cache function
1 parent e797fae commit e5702a2

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

cache.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os, time
2+
import requests
3+
4+
while True:
5+
# Read donor lists
6+
for file in os.listdir("donors"):
7+
print(file)
8+
try:
9+
donor_object = requests.get("https://api.foldingathome.org/user/{}".format(file))
10+
if donor_object.status_code == 200:
11+
with open(os.path.join("donors", file), "w+") as fb:
12+
fb.write(donor_object.text)
13+
except Exception as e:
14+
print(e)
15+
16+
# Read team lists
17+
for file in os.listdir("teams"):
18+
print(file)
19+
try:
20+
donor_object = requests.get("https://api.foldingathome.org/team/{}".format(file))
21+
if donor_object.status_code == 200:
22+
with open(os.path.join("teams", file), "w+") as fb:
23+
fb.write(donor_object.text)
24+
except Exception as e:
25+
print(e)
26+
27+
time.sleep(5)
28+

donors/.keep

Whitespace-only changes.

fonts/.keep

Whitespace-only changes.

index.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
// Get donor
1616
$donor = $_GET["donor"];
1717

18+
// Get team
19+
$team = $_GET["team"];
20+
1821
// Setting foor ssl
1922
$arr_context_options = array(
2023
"ssl" => array(
@@ -24,14 +27,43 @@
2427
) ,
2528
);
2629

27-
// Request for data
28-
$donor_data = file_get_contents("https://api.foldingathome.org/user/".$donor, false, stream_context_create($arr_context_options)) or die("api failed");
29-
$donor_object = json_decode($donor_data, true);
30-
if ($DEBUG) var_dump($donor_object);
30+
// Read record
31+
if (file_exists("donors/".$donor)) {
32+
$local_donor = file_get_contents("donors/".$donor);
33+
if (!$local_donor) {
34+
// Request for data
35+
$donor_data = @file_get_contents("https://api.foldingathome.org/user/".$donor, false, stream_context_create($arr_context_options)) or die("api failed");
36+
$donor_object = json_decode($donor_data, true);
37+
if (array_key_exists("status", $donor_object)) {
38+
exit("api failed");
39+
}
40+
if ($DEBUG) var_dump($donor_object);
41+
} else {
42+
$donor_object = json_decode($local_donor, true);
43+
}
44+
}
45+
if (file_exists("teams/".$team)) {
46+
$local_team = file_get_contents("teams/".$team);
47+
if (!$local_team) {
48+
// Request for data
49+
$team_data = @file_get_contents("https://api.foldingathome.org/team/".$team, false, stream_context_create($arr_context_options)) or die("api failed");
50+
$team_object = json_decode($team_data, true);
51+
if (array_key_exists("status", $team_object)) {
52+
exit("api failed");
53+
}
54+
if ($DEBUG) var_dump($team_object);
55+
} else {
56+
$team_object = json_decode($local_donor, true);
57+
}
58+
}
3159

32-
$team_data = file_get_contents("https://api.foldingathome.org/team/".$_GET["team"], false, stream_context_create($arr_context_options)) or die("api failed");
33-
$team_object = json_decode($team_data, true);
34-
if ($DEBUG) var_dump($team_object);
60+
// Save record
61+
if (!file_exists("donors/".$donor)) {
62+
file_put_contents("donors/".$donor, "");
63+
}
64+
if (!file_exists("teams/".$team)) {
65+
file_put_contents("teams/".$team, "");
66+
}
3567

3668
// Load canvas
3769
$canvas = imagecreatetruecolor(465, 92);

teams/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)