Skip to content

Commit

Permalink
加上訂閱噗浪關鍵字的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
CQD committed Apr 18, 2021
1 parent 6cf965e commit b4c0010
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ installNoDev:
installWithDev:
-composer install -o

deploy: installNoDev
deploy: installNoDev credential/plurk.php
gcloud app deploy -v 'prod' --project='feeder-230308' --promote --stop-previous-version $(OPTIONS)
@$(MAKE) post-deploy

Expand All @@ -21,3 +21,6 @@ post-deploy:

server: installWithDev
php -S localhost:8080 -t public/

credential/plurk.php:
echo "噗浪功能需要的 credential 檔案 $@ 不存在!" && false
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@
- /vocus/publication/{id}
- 某個專題的最新文章
- 例如:https://feed.cqd.tw/vocus/publication/sophist4ever

## 噗浪 Plurk

- /plurk/search/{keyword}
- 用搜尋功能找到符合關鍵字的噗,僅包含公開噗
- 範例
- https://feed.cqd.tw/plurk/search/Love
- https://feed.cqd.tw/plurk/search/%E7%B3%9F%E7%B3%95%E7%89%A9 (糟糕物)
8 changes: 8 additions & 0 deletions credential/plurk.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'appkey' => '',
'appsecret' => '',
'tokenkey' => '',
'tokensecret' => '',
];
1 change: 1 addition & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function getRouteConfig()
'/github/repo/{user}/{repo}/issuecomment' => 'IssueCommentController',
'/vocus/user/{user}' => 'VocusUserController',
'/vocus/publication/{id}' => 'VocusPublicationController',
'/plurk/search/{keyword}' => 'PlurkSearchController',
'/' => 'IndexController',
];
}
Expand Down
69 changes: 69 additions & 0 deletions src/Controller/PlurkSearchController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Q\Feeder\Controller;

use Qlurk\ApiClient as Qlurk;

class PlurkSearchController extends ControllerBase
{
public function logic(array $params): array
{
$credential = require __DIR__ . '/../../credential/plurk.php';
$qlurk = new Qlurk(
$credential['appkey'],
$credential['appsecret'],
$credential['tokenkey'],
$credential['tokensecret']
);

["keyword" => $keyword] = $params;
$keyword = urldecode($keyword);

$resp = $qlurk->call('/APP/PlurkSearch/search', ['query' => $keyword]);

$users = $resp['users'] ?? [];
$plurks = $resp['plurks'] ?? [];

$plurks = array_filter($plurks, function ($plurk) {
return null === ($plurk['limited_to'] ?? null);
});

return [
'lang' => 'zh-TW',
'title' => "噗浪關鍵字搜尋:「{$keyword}",
'desc' => "噗浪上搜尋「{$keyword}」的最新結果",
'link' => sprintf("https://www.plurk.com/search?q=%s", urlencode($keyword)),
'items' => array_map(function($plurk) use ($users) {
$userId = $plurk['user_id'];
$nickName = $users[$userId]['nick_name'] ?? sprintf('[%s]', $userId);

$content = $plurk['content_raw'] ?? '???';
$title = mb_substr($content, 0, 30);

$url = sprintf('https://www.plurk.com/p/%s', $this->base62Enc($plurk['plurk_id']));
return [
'title' => sprintf('%s | %s%s', $nickName, str_replace("\n", " ", $title), ($title === $content) ? '' : '...'),
'url' => $url,
'body' => $plurk['content'] ?? $content ?? '???',
'guid' => $url,
'time' => strtotime($plurk['posted']),
];
}, $plurks),
];
}

public function base62Enc($orig)
{
$pool = "0123456789abcdefghijklmnopqrstuvwxyz";
$pool_len = strlen($pool);

$result = '';
while ($orig > 0) {
$remain = $orig % $pool_len;
$result .= $pool[$remain];
$orig -= $remain;
$orig /= $pool_len;
}
return strrev($result);
}
}

0 comments on commit b4c0010

Please sign in to comment.