From f97dae71025861800cd189de09429de2f6b0e2f8 Mon Sep 17 00:00:00 2001 From: DavideSegullo Date: Mon, 11 Apr 2022 18:50:54 +0200 Subject: [PATCH] feat: :sparkles: add date range check --- code/.env.example | 4 +++- code/src/tweets/twitter.service.ts | 16 +++++++++++++++- code/src/utils/date.ts | 11 +++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/code/.env.example b/code/.env.example index c876ce8..57696c8 100644 --- a/code/.env.example +++ b/code/.env.example @@ -5,4 +5,6 @@ MONGODB_DB_NAME=stream-analyzer TWITTER_API_URL=https://api.twitter.com/2/tweets/ TWITTER_BEARER_TOKEN= TWITTER_HASHTAG=BitSong -TWITTER_BITSONG_ACCOUNT=BitSongOfficial \ No newline at end of file +TWITTER_BITSONG_ACCOUNT=BitSongOfficial +ANALYZER_START_DATE= +ANALYZER_END_DATE= \ No newline at end of file diff --git a/code/src/tweets/twitter.service.ts b/code/src/tweets/twitter.service.ts index 009ccee..b851c5a 100644 --- a/code/src/tweets/twitter.service.ts +++ b/code/src/tweets/twitter.service.ts @@ -1,3 +1,4 @@ +import { isBetweenStartEnd } from 'src/utils'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { TwitterApi } from 'twitter-api-v2'; @@ -14,6 +15,8 @@ export class TwitterService { } async getSearchStream() { + console.log('Starting twitter search stream...'); + const stream = this.twitterClient.v2.searchStream({ expansions: ['author_id', 'entities.mentions.username'], 'tweet.fields': [ @@ -35,7 +38,18 @@ export class TwitterService { }); for await (const { data, includes } of stream) { - this.mongo.create(data, includes); + if ( + isBetweenStartEnd( + process.env.ANALYZER_START_DATE, + process.env.ANALYZER_END_DATE, + data.created_at, + ) + ) { + this.mongo.create(data, includes); + } else { + console.log('Out of env dates range, exit...'); + break; + } } } } diff --git a/code/src/utils/date.ts b/code/src/utils/date.ts index 2201969..42d6f29 100644 --- a/code/src/utils/date.ts +++ b/code/src/utils/date.ts @@ -1,4 +1,7 @@ import * as dayjs from 'dayjs'; +import * as isBetween from 'dayjs/plugin/isBetween'; + +dayjs.extend(isBetween); export const differenceByMonths = (startDate: string, endDate?: string) => { const rightDate = endDate ? dayjs(endDate) : dayjs(); @@ -6,3 +9,11 @@ export const differenceByMonths = (startDate: string, endDate?: string) => { return rightDate.diff(leftDate, 'M', true); }; + +export const isBetweenStartEnd = ( + startDate: string, + endDate: string, + date?: string, +) => { + return dayjs(date ?? new Date()).isBetween(startDate, endDate); +};