Skip to content

Commit

Permalink
Merge pull request #72 from wensonsmith/dev
Browse files Browse the repository at this point in the history
v2.7.0
  • Loading branch information
wensonsmith authored Aug 11, 2020
2 parents 2e9cd0b + 87fe5e1 commit 65a294d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 160 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
*.log
.idea
cookie
Test.php
src/info copy.plist
.DS_Store
history
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

YoudaoTranslate | 有道翻译
YoudaoTranslate | 有道翻译 <div style="float:right"> ![visitors](https://visitor-badge.laobi.icu/badge?page_id=wensonsmith.YoudaoTranslate)</div>
===============

![screenshot_1](screenshot/screenshot_1.png)
Expand Down Expand Up @@ -90,10 +90,14 @@ $ ln -s /path/to/YoudaoTranslate/src/ ~/Library/Application\ Support/Alfred\ 3/A

## 四、Contributors

<a href="https://iwenson.com" target="_blank"><img src="https://avatars0.githubusercontent.com/u/2544185?s=60&v=4" /></a>
<a href="https://blog.zthxxx.me" target="_blank"><img src="https://avatars3.githubusercontent.com/u/15135943?s=60&v=4" /></a>
<a href="https://iwenson.com" target="_blank"><img src="https://avatars1.githubusercontent.com/u/2544185?s=120&v=4" height="60"/></a>
<a href="https://blog.zthxxx.me" target="_blank"><img src="https://avatars0.githubusercontent.com/u/15135943?s=120&v=4" height="60"/></a>
<a href="https://www.zzaning.com/#/" target="_blank"><img src="https://avatars2.githubusercontent.com/u/12035097?s=88&u=7e419cd2eb7b9fec5ba061d8135c4875a4c32323&v=4" height="60"/></a>

## 五、 使用库和参考资料
## 五、~~Buy me a coffee~~ 🍼冲奶粉
![微信支付](./screenshot/sponsor.PNG)!

## 六、 使用库和参考资料

- /~https://github.com/joetannenbaum/alfred-workflow
- https://www.alfredapp.com/help/workflows/inputs/script-filter/json/
Expand Down
Binary file added screenshot/sponsor.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 51 additions & 81 deletions src/WordBook.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
require 'vendor/autoload.php';
use Alfred\Workflows\Workflow;

/**
* 生词本功能
Expand All @@ -14,17 +16,14 @@ class WordBook
/**
* 生词本添加地址
*/
const ADD_WORD_URL = 'http://dict.youdao.com/wordbook/wordlist?action=add';
const ADD_WORD_URL = 'http://dict.youdao.com/wordbook/ajax?action=addword&le=eng&';

/**
* Cookie 文件
*/
const COOKIE_FILE = 'cookie';
const COOKIE_FILE = './cookie';

/**
* @var
*/
private $cookie;
private $workflow;
/**
* @var
*/
Expand All @@ -41,24 +40,27 @@ class WordBook
*/
public function __construct($username, $password)
{
$this->workflow = new Workflow;
$this->username = $username;
$this->password = $password;

$this->loadCookie();
// 如果cookie文件不存在,先新建一个新文件
// 否则curl无法保存cookie
if (!file_exists(self::COOKIE_FILE)) {
file_put_contents(self::COOKIE_FILE, '');
}
}

/**
* @param string $word 单词
* @param string $phonetic 发音
* @param string $desc 释义
*/
public function add($word, $phonetic, $desc)
public function add($word)
{
if ($this->pushWord($word, $phonetic, $desc)) {
if ($this->pushWord($word)) {
echo $word . ' 已加入生词本';
} else {
if ($this->login()) {
if ($this->pushWord($word, $phonetic, $desc)) {
if ($this->pushWord($word)) {
echo $word . ' 已加入生词本';
} else {
echo '添加到生词本失败';
Expand All @@ -69,83 +71,50 @@ public function add($word, $phonetic, $desc)
}
}


/**
* 登录
* @return bool
*/
private function login()
{
$response = $this->request(self::LOGIN_URL, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $this->buildHeader(),
CURLOPT_POSTFIELDS => http_build_query($this->buildForm()),
CURLOPT_POSTFIELDS => http_build_query($this->buildForm())
]);

list($header, $body) = explode("\r\n\r\n", $response, 2);
list($header) = explode("\r\n\r\n", $response, 1);

$matches = [];

preg_match_all('/Set-Cookie:(?<cookie>.*)\b/m', $header, $matches);

$cookie = $matches['cookie'];

if (count($cookie) === 1) {
if (count($cookie) < 3) {
return false;
}

$this->cookie = trim(implode(",", $cookie));
$this->saveCookie();

return true;
}

/**
* @param $word
* @param $phonetic
* @param $desc
* @return bool
*/
private function pushWord($word, $phonetic, $desc)
private function pushWord($word)
{

$tags = 'Alfred';

$word = compact('word', 'phonetic', 'desc', 'tags');
$params = [ 'q' => $word, 'tags'=> 'Alfred' ];
$url = self::ADD_WORD_URL . http_build_query($params);

$header = $this->buildHeader();
$header[] = 'Referer:http://dict.youdao.com/wordbook/wordlist';

$response = $this->request(self::ADD_WORD_URL, [
$response = $this->request( $url, [
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => http_build_query($word),
CURLOPT_COOKIE => $this->cookie,
]);

list($header, $body) = explode("\r\n\r\n", $response, 2);

$matches = [];
preg_match('/Location\:.*/', $header, $matches);

return trim($matches[0]) === 'Location: http://dict.youdao.com/wordbook/wordlist';

}

/**
* 加载 cookie
*/
private function loadCookie()
{
if (file_exists(self::COOKIE_FILE)) {
$this->cookie = file_get_contents(self::COOKIE_FILE);
}
}

/**
* 保存cookie
*/
private function saveCookie()
{
file_put_contents(self::COOKIE_FILE, $this->cookie);
$result = explode("\r\n\r\n", $response, 2);

return $result[1] == '{"message":"adddone"}';
}

/**
Expand All @@ -172,12 +141,12 @@ private function buildForm()
return [
'app' => 'web',
'tp' => 'urstoken',
'cf' => 3,
'cf' => 7,
'fr' => 1,
'ru' => 'http://dict.youdao.com/wordbook/wordlist?keyfrom=null',
'ru' => 'http://dict.youdao.com/wordbook/wordlist?keyfrom=dict2.index#/',
'product' => 'DICT',
'type' => 1,
'um' => true,
'um' => 'true',
'username' => $this->username,
'password' => md5($this->password),
'agreePrRule' => 1,
Expand All @@ -196,40 +165,41 @@ private function buildForm()
*/
public function request($url = null, $options = null)
{
if (is_null($url)):
if (is_null($url)) {
return false;
endif;
}

$defaults = array( // Create a list of default curl options
CURLOPT_RETURNTRANSFER => true, // Returns the result as a string
$defaults = [ // Create a list of default curl options
CURLOPT_RETURNTRANSFER => true, // Returns the result as a string
CURLOPT_URL => $url, // Sets the url to request
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_POST => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
);
CURLOPT_COOKIEJAR => realpath(self::COOKIE_FILE), // 保存返回的Cookie
CURLOPT_COOKIEFILE => realpath(self::COOKIE_FILE), // 读取现有Cookie, 不需要自己维护cookie变量
CURLOPT_TIMEOUT => 30 // 增加超时
];

if ($options):
foreach ($options as $k => $v):
if ($options) {
foreach ($options as $k => $v) {
$defaults[$k] = $v;
endforeach;
endif;

}
}
array_filter($defaults, array($this, 'empty_filter')); // Filter out empty options from the array

$ch = curl_init(); // Init new curl object
curl_setopt_array($ch, $defaults); // Set curl options
$out = curl_exec($ch); // Request remote data
$ch = curl_init(); // Init new curl object
curl_setopt_array($ch, $defaults); // Set curl options
$out = curl_exec($ch); // Request remote data
$err = curl_error($ch);
curl_close($ch); // End curl request
curl_close($ch); // End curl request

if ($err):
if ($err) {
return $err;
else:
} else {
return $out;
endif;
}
}

/**
Expand All @@ -241,10 +211,10 @@ public function request($url = null, $options = null)
*/
private function empty_filter($a)
{
if ($a == '' || $a == null): // if $a is empty or null
if ($a == '' || $a == null){ // if $a is empty or null
return false; // return false, else, return true
else:
} else {
return true;
endif;
}
}
}
}
Loading

0 comments on commit 65a294d

Please sign in to comment.