-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
51 lines (46 loc) · 1.63 KB
/
api.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
<?php
require_once('lib/Utils.php');
require_once('lib/ApiResponse.php');
require_once('lib/Dictionary.php');
require_once('lib/Scrap.php');
require_once('lib/Cache.php');
$settings = require_once('settings.php');
$output = null;
switch ($_GET['method']) {
case 'scrap':
$url = $_GET['url'];
$url_hash = $_GET['url_hash'];
if (!empty($url)) {
$options = ['url' => $url];
if ($data = EnglishStudy\Cache::fetch($options)) {
$output = ['success' => true, 'data' => $data];
} else {
$scrap = new EnglishStudy\Scrap($url);
$data = $scrap->getHTML();
$key = EnglishStudy\Cache::saveCache($options, $data);
$data['__key__'] = $key;
$output = ['success' => true, 'data' => $data];
}
} else if (!empty($url_hash)) {
if ($data = EnglishStudy\Cache::fetchByKey($url_hash)) {
$output = ['success' => true, 'data' => $data];
} else {
$output = ['success' => false];
}
}
break;
case 'dictionary':
$word = $_GET['query'];
$engine = 'oxford';
$options = ['word' => $word, 'engine' => $engine];
if ($data = EnglishStudy\Cache::fetch($options)) {
$output = ['success' => true, 'data' => $data];
} else {
$dictionary = new EnglishStudy\Dictionary($word, $engine, $settings);
$data = $dictionary->search();
EnglishStudy\Cache::saveCache($options, $data);
$output = ['success' => true, 'data' => $data];
}
break;
}
EnglishStudy\ApiResponse::toJson($output);