-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-functions.php
137 lines (131 loc) · 5.5 KB
/
api-functions.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
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
<?php
namespace Hexapla;
use DateTime;
require_once "dbconnect.php";
require_once "general-functions.php";
/**
* @var string $oxfordAppID
* @var string $oxfordAppKey
* @var resource $db
*/
/**
* @param $word
* @param $langId
* @return array|false
*/
function getDefinitionAPI($word, $langId) {
global $oxfordAppID, $oxfordAppKey;
if ($langId === '1') {
// TODO: after upgrading to Developer-level API authorization, switch to "words" API instead of "lemmas" + "entries"
$oxfordBaseUrl = 'https://od-api.oxforddictionaries.com/api/v2';
$curl = curl_init($oxfordBaseUrl . "/lemmas/en/" . $word);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['app_id: ' . $oxfordAppID, 'app_key: ' . $oxfordAppKey]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$lemmaResult = curl_exec($curl);
if ($lemmaResult === false) return false;
$lemmaResult = json_decode($lemmaResult);
if (!multiarray_key_exists($lemmaResult, 'results', 0, 'lexicalEntries', 0, 'inflectionOf', 0, 'id')) {
return false;
}
$lemma = $lemmaResult['results'][0]['lexicalEntries'][0]['inflectionOf'][0]['id'];
curl_setopt($curl, CURLOPT_URL, $oxfordBaseUrl . '/entries/en-us/' . $lemma);
$result = curl_exec($curl);
if ($result === false) return false;
$result = json_decode($result);
$output['lemma'] = $lemma;
if (multiarray_key_exists($result, 'results', 0, 'lexicalEntries', 0, 'entries', 0)) {
$result = $result['results'][0]['lexicalEntries'][0]['entries'][0];
} else {
return false;
}
if (multiarray_key_exists($result, 'etymologies')) {
$output['etymology'] = $result['etymologies'];
}
foreach($result['pronunciations'] as $pronunciation) {
if (isset($pronunciation['audioFile'])) {
$output['pronunciation'][] = ['phonetic' => $pronunciation['phoneticSpelling'], 'link' => $pronunciation['audioFile']];
}
}
foreach($result['senses'] as $sense) {
if (isset($sense['definitions'])) {
$output['definition'][] = $sense['definitions'];
} elseif (isset($sense['shortDefinitions'])) {
$output['definition'][] = $sense['shortDefinitions'];
}
}
curl_close($curl);
return $output;
}
return false;
}
function getLemmaAPI($word, $langId) {
global $oxfordAppID, $oxfordAppKey;
if ($langId === '1') {
$oxfordLemmaUrl = 'https://od-api.oxforddictionaries.com/api/v2/lemmas/en/';
$curl = curl_init($oxfordLemmaUrl . $word);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['app_id: ' . $oxfordAppID, 'app_key: ' . $oxfordAppKey]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$lemmaResult = curl_exec($curl);
curl_close($curl);
if ($lemmaResult === false) return false;
$lemmaResult = json_decode($lemmaResult);
return $lemmaResult['results'][0]['lexicalEntries'][0]['inflectionOf'][0]['id'];
}
return null;
}
function getInflectionsAPI($word, $langId) {
// FIXME: this API won't work until we upgrade
global $oxfordAppID, $oxfordAppKey;
$results = [];
if ($langId === '1') {
$oxfordInflectionUrl = 'https://od-api.oxforddictionaries.com/api/v2/inflections/en-us/';
$curl = curl_init($oxfordInflectionUrl . $word);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['app_id: ' . $oxfordAppID, 'app_key: ' . $oxfordAppKey]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$inflectionResult = curl_exec($curl);
if ($inflectionResult === false) return false;
$inflectionResult = json_decode($inflectionResult);
if (multiarray_key_exists($inflectionResult, 'results', 0, 'lexicalEntries', 0, 'inflections')) {
foreach ($inflectionResult['results'][0]['lexicalEntries'][0]['inflections'] as $inflection) {
$results[] = $inflection['inflectedForm'];
}
}
curl_close($curl);
}
return $results;
}
function getLiturgicalColor($clientDate) {
$baseUrl = 'http://calapi.inadiutorium.cz/api/v0/en/calendars/default';
$phpDate = new DateTime($clientDate);
$day = $phpDate->format('d');
$month = $phpDate->format('m');
$year = $phpDate->format('Y');
$curl = curl_init("$baseUrl/$year/$month/$day");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$liturgicalResult = curl_exec($curl);
if ($liturgicalResult === false) return false;
$liturgicalResult = json_decode($liturgicalResult);
if (+($phpDate->format('w')) === 0 &&
((strtolower($liturgicalResult['season']) === 'lent' && $liturgicalResult['season_week'] === 4) ||
(strtolower($liturgicalResult['season']) === 'advent' && $liturgicalResult['season_week'] === 3))) {
$color = 'rose'; // Laetare or Gaudete Sunday
} else {
$lowestRank = 100.0;
$lowestColor = 'green';
foreach($liturgicalResult['celebrations'] as $celebration) {
if ($celebration['rank_num'] < $lowestRank) {
$lowestRank = $celebration['rank_num'];
$lowestColor = $celebration['colour'];
}
}
if (strtolower($lowestColor) === 'violet') {
$color = 'purple';
} elseif (strtolower($lowestColor) === 'white') {
$color = 'gold';
} else { // red or green
$color = strtolower($lowestColor);
}
}
curl_close($curl);
return $color;
}