From 32093fdf0348acc4eb6f002fce106364f2bfcd5e Mon Sep 17 00:00:00 2001 From: ci7lus <7887955+ci7lus@users.noreply.github.com> Date: Sun, 20 Feb 2022 15:43:00 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20[twitter]=20=E5=85=AC=E5=BC=8FTw?= =?UTF-8?q?itter=E3=81=8B=E3=82=89=E3=82=BF=E3=82=B0=E3=82=92=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=8B=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E3=81=A8=E3=81=8D=E3=81=AB=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=82=B0=E3=82=92=E8=A6=8B=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/miraktest-twitter/components/Tweet.tsx | 166 ++++++++++----------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/src/miraktest-twitter/components/Tweet.tsx b/src/miraktest-twitter/components/Tweet.tsx index 78e5552..744ebb5 100644 --- a/src/miraktest-twitter/components/Tweet.tsx +++ b/src/miraktest-twitter/components/Tweet.tsx @@ -213,101 +213,101 @@ export const TweetComponent: React.FC<{ )} title="タグを取得する" disabled={suggestObtaining} - onClick={() => { + onClick={async () => { const lowServiceTags = serviceTags.map((tag) => tag.toLowerCase() ) if (sayaDef && TWITTER_MAPPING[sayaDef.name]) { // 番組情報の公式ツイートからタグを取得する const screenName = TWITTER_MAPPING[sayaDef.name] - twitter.accountsAndUsers + const user = await twitter.accountsAndUsers .usersShow({ screen_name: screenName, }) - .then((_user: UsersShow | { data: UsersShow }) => { - const user = "data" in _user ? _user.data : _user - if (!user.status) { - return - } - setSuggestedTags( - ( - user.status.entities - .hashtags as Status["entities"]["hashtags"] - ) - ?.map((hashtag) => "#" + hashtag.text) - .filter( - (hashtag) => - !lowServiceTags.includes(hashtag.toLowerCase()) - ) || [] + .then((_user: UsersShow | { data: UsersShow }) => + "data" in _user ? _user.data : _user + ) + if (user.status) { + const tags = + ( + user.status.entities + .hashtags as Status["entities"]["hashtags"] ) - }) - } else { - // ハッシュタグからタグを取得する - if (!serviceTags || serviceTags.length === 0) { - return + ?.map((hashtag) => "#" + hashtag.text) + .filter( + (hashtag) => + !lowServiceTags.includes(hashtag.toLowerCase()) + ) || [] + if (0 < tags.length) { + setSuggestedTags(tags) + return + } } - setFailed("") - setSuggestObtaining(true) - twitter.tweets - .search({ - q: serviceTags.join(" OR ") + " exclude:retweets", - locale: "ja", - result_type: "recent", - count: 30, - }) - .then((tweets: Search | { data: Search }) => { - const statuses = - "data" in tweets - ? tweets.data.statuses - : tweets.statuses - console.debug(statuses) - const dedupedByUser = Array.from( - statuses - .reduce((arr, status) => { - arr.set(status.user.id_str, status) - return arr - }, new Map()) - .values() - ) - const sortedHashtags = Object.entries( - dedupedByUser - .map( - (status) => - ( - status.entities - .hashtags as Status["entities"]["hashtags"] - )?.map((hashtag) => "#" + hashtag.text) ?? [] - ) - .flat() - .filter( - (hashtag) => - !lowServiceTags.includes(hashtag.toLowerCase()) - ) - .reduce((record: Record, tag) => { - if (tag in record) { - record[tag]++ - } else { - record[tag] = 1 - } - return record - }, {}) - ).sort((a, b) => b[1] - a[1]) - console.info("タグ候補:", sortedHashtags) - const hashtags = sortedHashtags.every(([, n]) => n === 1) - ? sortedHashtags.map(([tag]) => tag) - : sortedHashtags - .filter(([, n]) => n > 1) - .map(([tag]) => tag) - setSuggestedTags(hashtags) - }) - .catch((e) => { - console.error(e) - setFailed("タグの取得に失敗しました") - }) - .finally(() => { - setSuggestObtaining(false) - }) } + + // ハッシュタグからタグを取得する + if (!serviceTags || serviceTags.length === 0) { + return + } + setFailed("") + setSuggestObtaining(true) + const statuses = await twitter.tweets + .search({ + q: serviceTags.join(" OR ") + " exclude:retweets", + locale: "ja", + result_type: "recent", + count: 30, + }) + .then((tweets: Search | { data: Search }) => + "data" in tweets ? tweets.data.statuses : tweets.statuses + ) + .catch((e) => { + console.error(e) + setFailed("タグの取得に失敗しました") + }) + .finally(() => { + setSuggestObtaining(false) + }) + if (!statuses) { + return + } + console.debug(statuses) + const dedupedByUser = Array.from( + statuses + .reduce((arr, status) => { + arr.set(status.user.id_str, status) + return arr + }, new Map()) + .values() + ) + const sortedHashtags = Object.entries( + dedupedByUser + .map( + (status) => + ( + status.entities + .hashtags as Status["entities"]["hashtags"] + )?.map((hashtag) => "#" + hashtag.text) ?? [] + ) + .flat() + .filter( + (hashtag) => + !lowServiceTags.includes(hashtag.toLowerCase()) + ) + .reduce((record: Record, tag) => { + if (tag in record) { + record[tag]++ + } else { + record[tag] = 1 + } + return record + }, {}) + ).sort((a, b) => b[1] - a[1]) + console.info("タグ候補:", sortedHashtags) + const hashtags = sortedHashtags.every(([, n]) => n === 1) + ? sortedHashtags.map(([tag]) => tag) + : sortedHashtags.filter(([, n]) => n > 1).map(([tag]) => tag) + setSuggestedTags(hashtags) }} >