-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfy.php
144 lines (130 loc) · 3.5 KB
/
fy.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
138
139
140
141
142
143
144
<?php
/**
* Class Ttranslate
* author by carl
*/
class Translate
{
/**
* @var bool
*/
public $debug = true;
/**
* @var string
*/
public $help = 'demo: ttranslate.php "apple"';
/**
* @var string
*/
public $urlFormat = 'http://fanyi.youdao.com/openapi.do'
. '?keyfrom=%s&key=%s&type=data&doctype=json'
. '&version=1.1&q=%s';
/**
* @var string
*/
public $printFormat = <<<'EOT'
=================
word: %s
phonetic:
%s
translation: %s
explains:
=================
%s
internet trans:
=================
%s
EOT;
/**
* @var array
*/
public $errorMaps = array(
0 => '正常',
20 => '要翻译的文本过长',
30 => '无法进行有效的翻译',
40 => '不支持的语言类型',
50 => 'API KEY 无效',
60 => '无词典结果, 仅在获取词典结果生效'
);
/**
* Ttranslate constructor.
*/
public function __construct($argc, $argv)
{
if ($argc == 1 || @$argv[2] == '-h' || @$argv[2] == '--help') {
$this->end($this->help);
}
if ($argc > 1) {
$q = implode(' ', array_slice($argv, 1));
if ($this->isAllChinese($q)) {
$q = urlencode($q);
}
$url = sprintf($this->urlFormat, "jumpaper", 254937526, $q);
$res = file_get_contents($url);
$resJ = json_decode($res, true);
$phonetic = '';
$translation = '';
$explains = '';
$interTrans = '';
if (@$resJ['errorCode']) $this->end($this->errorMaps[$resJ['errorCode']], $url);
if (@$resJ['basic']['uk-phonetic']) $phonetic .= 'uk: ' . $resJ['basic']['uk-phonetic'] . PHP_EOL;
if (@$resJ['basic']['us-phonetic']) $phonetic .= 'us: ' . $resJ['basic']['us-phonetic'];
if (@$resJ['translation']) $translation = implode(PHP_EOL, $resJ['translation']);
if (@$resJ['basic']['explains']) $explains = implode(PHP_EOL, $resJ['basic']['explains']);
if (@$resJ['web']) {
$items = array();
foreach ($resJ['web'] as $item):
$items[] = $item['key'] . ': ' . implode(',', $item['value']);
endforeach;
$interTrans = implode(PHP_EOL, $items);
}
$willPrint = sprintf($this->printFormat, $resJ['query'], $phonetic,
$translation, $explains, $interTrans);
echo $willPrint;
exec('say '.$argv[1]);
}
}
/**
* @param $string
*/
function thdump($string)
{
if ($this->debug)
echo $string, PHP_EOL;
}
/**
* @param $string
* @param string $debugString
*/
function end($string, $debugString = '')
{
echo $string, PHP_EOL;
$this->thdump($debugString);
exit;
}
/**
* @param $str
* @return bool
*/
function isAllChinese($str)
{
if (strpos($str, '·')) {
$str = str_replace("·", '', $str);
if (preg_match('/^[\x7f-\xff]+$/', $str)) {
return true;//全是中文
} else {
return false;//不全是中文
}
} else {
if (preg_match('/^[\x7f-\xff]+$/', $str)) {
return true;//全是中文
} else {
return false;//不全是中文
}
}
}
}
/**
* start
*/
$obj = new Translate($argc, $argv);