-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd01d60
commit 51d65f8
Showing
3 changed files
with
267 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use Alfred\Workflows\Workflow; | ||
|
||
class Updater | ||
{ | ||
private $currentVersion; | ||
private $workflow; | ||
|
||
const RELEASE_API = "https://api.github.com/repos/wensonsmith/youdaotranslate/releases"; | ||
|
||
public function __construct($version) | ||
{ | ||
$this->workflow = new Workflow; | ||
$this->currentVersion = $version; | ||
} | ||
|
||
public function fetchReleases() | ||
{ | ||
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'; | ||
|
||
$response = $this->workflow->request(self::RELEASE_API, [CURLOPT_USERAGENT => $userAgent]); | ||
$this->result = json_decode($response); | ||
|
||
foreach($this->result as $release) { | ||
$version = $release->tag_name; | ||
if(version_compare($version, $this->currentVersion) === 1) { | ||
$releaseAt = '发布于 ' . date('Y-m-d H:i:s', strtotime($release->published_at));; | ||
$downloadUrl = $release->assets[0]->browser_download_url; | ||
$this->addItem($version, $releaseAt, $downloadUrl); | ||
} | ||
} | ||
|
||
if(empty($this->workflow->results)) { | ||
$this->addItem('已经是最新版本', '没有可用更新', null); | ||
} | ||
|
||
return $this->workflow->output(); | ||
} | ||
|
||
private function addItem($title, $subtitle, $arg) | ||
{ | ||
$this->workflow->result() | ||
->title($title) | ||
->subtitle($subtitle) | ||
->arg($arg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
class Workflow | ||
{ | ||
protected $results = []; | ||
public $results = []; | ||
|
||
/** | ||
* Add a result to the workflow | ||
|