-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
81 lines (69 loc) · 2.8 KB
/
index.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
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
<?php
header('Content-Type: application/json');
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://octodex.github.com/');
$xpath = new DOMXpath($dom);
$elements = $xpath->query("//*/div[@class='item-shell']");
if (!is_null($elements)) {
$octodex = array();
foreach ($elements as $element) {
$octocat = array();
$tempDom = new DOMDocument();
$tempDom->appendChild($tempDom->importNode($element, true));
$tempDom->saveHTML();
$links = $tempDom->getElementsByTagName('a');
$images = $tempDom->getElementsByTagName('img');
$octocatName = $links->item(2)->nodeValue;
$octocatPage = $links->item(1)->getAttribute('href');
$octocatImage = $images->item(0)->getAttribute('data-src');
$octocatAuthor = $images->item(1)->getAttribute('alt');
$octocatNumber = $tempDom->getElementsByTagName('p')->item(0)->nodeValue;
$octocatAuthorURL = $links->item(3)->getAttribute('href');
$octocatAuthorAvatar = $images->item(1)->getAttribute('src');
$octocat['name'] = $octocatName;
$octocat['page'] = $octocatPage;
$octocat['image'] = $octocatImage;
$octocat['author'] = $octocatAuthor;
$octocat['number'] = str_replace('#', '', $octocatNumber);
$octocat['authorURL'] = $octocatAuthorURL;
$octocat['authorAvatar'] = $octocatAuthorAvatar;
/*
This is to remove all of the parameters passed to Gravatar, but I don't think that's necessary, as it gives a pretty big image...
$octocat['authorAvatar'] = substr($octocatAuthorAvatar, 0, strpos($octocatAuthorAvatar,'?'));
*/
$octodex[] = $octocat;
// var_dump($dom->saveHTML($element));
}
}
if (array_key_exists('random', $_GET)) {
if (array_key_exists('number', $_GET)) {
echo json_encode(array("error" => "Can not provide random and numbered Octocat..."));
}
else {
// Though this is not the most efficient way to grab a random octocat, it is not significantly slower, so I'll leave it for the time being...
echo json_encode($octodex[array_rand($octodex)]);
}
}
else if (array_key_exists('number', $_GET)) {
if (array_key_exists('random', $_GET)) {
echo json_encode(array("error" => "Can not provide random and numbered Octocat..."));
}
else {
if (!empty($_GET['number'])) {
if ($_GET['number'] <= count($octodex)) {
// Though this is not the most efficient way to grab a numbered octocat, it is not significantly slower, so I'll leave it for the time being...
foreach ($octodex as $octocat) {
if ($octocat['number'] == $_GET['number']) {
echo json_encode($octocat);
exit();
}
else continue;
}
}
else echo json_encode(array("error" => "Octocat numbered ".$_GET['number']." does not exist!"));
}
else echo json_encode(array("error" => "Please pass a number for an Octocat..."));
}
}
else echo json_encode($octodex);
?>