From 03e4dae5075662012cf209b16025b815a18a55a6 Mon Sep 17 00:00:00 2001 From: anzharip <10259593+anzharip@users.noreply.github.com> Date: Sun, 22 Nov 2020 09:48:16 +0700 Subject: [PATCH 1/4] refactor: tweet-utilities --- src/utility/twitter/tweet-utilities.ts | 28 +++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/utility/twitter/tweet-utilities.ts b/src/utility/twitter/tweet-utilities.ts index 6f37690..a9ebea3 100644 --- a/src/utility/twitter/tweet-utilities.ts +++ b/src/utility/twitter/tweet-utilities.ts @@ -1,5 +1,6 @@ import sw from "stopword"; import TextCleaner from "text-cleaner"; +import { Tweet } from "../../interfaces/twitter/tweet.interface"; import { WordFrequency } from "../../interfaces/word-frequency.interface"; import { axiosClient } from "../axios-retry-configuration"; import { analyseSentiment } from "../gcp/sentiment"; @@ -10,7 +11,9 @@ import { regexTwitterHandle, regexURL } from "../regex"; const token = process.env.TWITTER_BEARER_TOKEN || ""; const recentSearchURL = "https://api.twitter.com/2/tweets/search/recent"; -export async function recentSearch(username: string) { +export async function recentSearch( + username: string +): Promise<{ length: number; data: [] }> { // Edit query parameters below const parameters = { query: `from:${username} -is:retweet`, @@ -30,17 +33,19 @@ export async function recentSearch(username: string) { return response.data; } -export async function generateWordFrequency(words: string[]) { +export async function generateWordFrequency( + words: string[] +): Promise<[string, number][] | void> { if (words.length === 0) { - return; + throw new Error("The received words length is zero. "); } const wordFrequency: WordFrequency = {}; words.forEach((element: string) => { // Keep track of occurences if (Object.prototype.hasOwnProperty.call(wordFrequency, element)) { - wordFrequency[element] += 1; + wordFrequency[String(element)] += 1; } else { - wordFrequency[element] = 1; + wordFrequency[String(element)] = 1; } }); @@ -53,13 +58,13 @@ export async function generateWordFrequency(words: string[]) { }); } -export async function generateWordsArray(tweets: any) { - if (tweets.length === 0) { - return; - } +export async function generateWordsArray(tweets: { + length: number; + data: []; +}): Promise { let recentTweetsConcat = ""; if (tweets["data"]) { - tweets["data"].forEach((element: any) => { + tweets["data"].forEach((element: { text: string }) => { if (element.text) recentTweetsConcat = recentTweetsConcat + " " + element.text; }); @@ -82,7 +87,7 @@ export async function generateWordsArray(tweets: any) { ]); } -export async function generateSummary(question: any) { +export async function generateSummary(question: Tweet): Promise { try { const recentTweets = await recentSearch(question.in_reply_to_user_id_str); const wordsArray = await generateWordsArray(recentTweets); @@ -96,6 +101,5 @@ export async function generateSummary(question: any) { }; } catch (error) { logger.error(error); - return; } } From 852a4d40ede6fae37a1e4695758630de778b5fc3 Mon Sep 17 00:00:00 2001 From: anzharip <10259593+anzharip@users.noreply.github.com> Date: Sun, 22 Nov 2020 10:08:48 +0700 Subject: [PATCH 2/4] refactor: outgoing --- src/outgoing.ts | 23 +++++++++++++++++++---- src/utility/twitter/tweet-utilities.ts | 10 +++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/outgoing.ts b/src/outgoing.ts index f1214fd..1633846 100644 --- a/src/outgoing.ts +++ b/src/outgoing.ts @@ -1,16 +1,31 @@ +import { google } from "@google-cloud/language/build/protos/protos"; import { logger } from "./utility/logger"; import { twitterClient } from "./utility/twitter/twitter-client"; -export async function sendAnswer(summary: any): Promise { +export async function sendAnswer(summary: { + wordFrequency: [string, number][]; + replyToStatusId: string; + sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; +}): Promise { const client = twitterClient(); const status = summary.wordFrequency .splice(0, 5) - .map((element: any) => element[0]) + .map((element: [string, number]) => element[0]) .join(", "); + if (summary.sentiment.documentSentiment === null) { + throw new Error("documentSentiment is null. "); + } + if (summary.sentiment.documentSentiment === undefined) { + throw new Error("documentSentiment is undefined. "); + } const sentimentScore = summary.sentiment.documentSentiment.score || 0; - const sentimentScoreRounded = Number.parseFloat(sentimentScore).toFixed(2); + const sentimentScoreRounded = Number.parseFloat( + String(sentimentScore) + ).toFixed(2); const magnitudeScore = summary.sentiment.documentSentiment.magnitude || 0; - const magnitudeScoreRounded = Number.parseFloat(magnitudeScore).toFixed(2); + const magnitudeScoreRounded = Number.parseFloat( + String(magnitudeScore) + ).toFixed(2); let sentimentScoreString = "Neutral"; if (sentimentScore <= -0.25 && sentimentScore >= -1) { sentimentScoreString = "Negative"; diff --git a/src/utility/twitter/tweet-utilities.ts b/src/utility/twitter/tweet-utilities.ts index a9ebea3..2e126e8 100644 --- a/src/utility/twitter/tweet-utilities.ts +++ b/src/utility/twitter/tweet-utilities.ts @@ -1,3 +1,4 @@ +import { google } from "@google-cloud/language/build/protos/protos"; import sw from "stopword"; import TextCleaner from "text-cleaner"; import { Tweet } from "../../interfaces/twitter/tweet.interface"; @@ -87,7 +88,13 @@ export async function generateWordsArray(tweets: { ]); } -export async function generateSummary(question: Tweet): Promise { +export async function generateSummary( + question: Tweet +): Promise<{ + wordFrequency: void | [string, number][]; + replyToStatusId: string; + sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; +} | void> { try { const recentTweets = await recentSearch(question.in_reply_to_user_id_str); const wordsArray = await generateWordsArray(recentTweets); @@ -101,5 +108,6 @@ export async function generateSummary(question: Tweet): Promise { }; } catch (error) { logger.error(error); + return; } } From e6e38d06df34a92cfd9e4a0db13ff4c296963a52 Mon Sep 17 00:00:00 2001 From: anzharip <10259593+anzharip@users.noreply.github.com> Date: Sun, 22 Nov 2020 10:14:47 +0700 Subject: [PATCH 3/4] refactor: queue --- src/utility/queue.ts | 9 +++++++-- src/utility/twitter/tweet-utilities.ts | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utility/queue.ts b/src/utility/queue.ts index 0d1d1b6..8457b93 100644 --- a/src/utility/queue.ts +++ b/src/utility/queue.ts @@ -1,5 +1,10 @@ +import { google } from "@google-cloud/language/build/protos/protos"; import { Tweet } from "../interfaces/twitter/tweet.interface"; export const queueQuestion: Tweet[] = []; -export const queueSummary: any[] = []; -export const queueReport: any[] = []; +export const queueSummary: { + wordFrequency: [string, number][]; + replyToStatusId: string; + sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; +}[] = []; +export const queueReport: unknown[] = []; diff --git a/src/utility/twitter/tweet-utilities.ts b/src/utility/twitter/tweet-utilities.ts index 2e126e8..052d140 100644 --- a/src/utility/twitter/tweet-utilities.ts +++ b/src/utility/twitter/tweet-utilities.ts @@ -36,7 +36,7 @@ export async function recentSearch( export async function generateWordFrequency( words: string[] -): Promise<[string, number][] | void> { +): Promise<[string, number][]> { if (words.length === 0) { throw new Error("The received words length is zero. "); } @@ -91,7 +91,7 @@ export async function generateWordsArray(tweets: { export async function generateSummary( question: Tweet ): Promise<{ - wordFrequency: void | [string, number][]; + wordFrequency: [string, number][]; replyToStatusId: string; sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; } | void> { From e4aecf2f9324dcdb57049acbfbfb7648943cdbb8 Mon Sep 17 00:00:00 2001 From: anzharip <10259593+anzharip@users.noreply.github.com> Date: Sun, 22 Nov 2020 10:21:10 +0700 Subject: [PATCH 4/4] refactor: implement Summary interface --- src/interfaces/summary.interface.ts | 7 +++++++ src/outgoing.ts | 8 ++------ src/utility/queue.ts | 8 ++------ src/utility/twitter/tweet-utilities.ts | 8 ++------ 4 files changed, 13 insertions(+), 18 deletions(-) create mode 100644 src/interfaces/summary.interface.ts diff --git a/src/interfaces/summary.interface.ts b/src/interfaces/summary.interface.ts new file mode 100644 index 0000000..fa2a469 --- /dev/null +++ b/src/interfaces/summary.interface.ts @@ -0,0 +1,7 @@ +import { google } from "@google-cloud/language/build/protos/protos"; + +export interface Summary { + wordFrequency: [string, number][]; + replyToStatusId: string; + sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; +} diff --git a/src/outgoing.ts b/src/outgoing.ts index 1633846..17e16e9 100644 --- a/src/outgoing.ts +++ b/src/outgoing.ts @@ -1,12 +1,8 @@ -import { google } from "@google-cloud/language/build/protos/protos"; +import { Summary } from "./interfaces/summary.interface"; import { logger } from "./utility/logger"; import { twitterClient } from "./utility/twitter/twitter-client"; -export async function sendAnswer(summary: { - wordFrequency: [string, number][]; - replyToStatusId: string; - sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; -}): Promise { +export async function sendAnswer(summary: Summary): Promise { const client = twitterClient(); const status = summary.wordFrequency .splice(0, 5) diff --git a/src/utility/queue.ts b/src/utility/queue.ts index 8457b93..99f3d97 100644 --- a/src/utility/queue.ts +++ b/src/utility/queue.ts @@ -1,10 +1,6 @@ -import { google } from "@google-cloud/language/build/protos/protos"; +import { Summary } from "../interfaces/summary.interface"; import { Tweet } from "../interfaces/twitter/tweet.interface"; export const queueQuestion: Tweet[] = []; -export const queueSummary: { - wordFrequency: [string, number][]; - replyToStatusId: string; - sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; -}[] = []; +export const queueSummary: Summary[] = []; export const queueReport: unknown[] = []; diff --git a/src/utility/twitter/tweet-utilities.ts b/src/utility/twitter/tweet-utilities.ts index 052d140..53f4e6d 100644 --- a/src/utility/twitter/tweet-utilities.ts +++ b/src/utility/twitter/tweet-utilities.ts @@ -1,6 +1,6 @@ -import { google } from "@google-cloud/language/build/protos/protos"; import sw from "stopword"; import TextCleaner from "text-cleaner"; +import { Summary } from "../../interfaces/summary.interface"; import { Tweet } from "../../interfaces/twitter/tweet.interface"; import { WordFrequency } from "../../interfaces/word-frequency.interface"; import { axiosClient } from "../axios-retry-configuration"; @@ -90,11 +90,7 @@ export async function generateWordsArray(tweets: { export async function generateSummary( question: Tweet -): Promise<{ - wordFrequency: [string, number][]; - replyToStatusId: string; - sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; -} | void> { +): Promise { try { const recentTweets = await recentSearch(question.in_reply_to_user_id_str); const wordsArray = await generateWordsArray(recentTweets);