Skip to content

Commit

Permalink
feat: ✨ add date range check
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideSegullo committed Apr 11, 2022
1 parent 7fa2339 commit f97dae7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion code/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
TWITTER_BITSONG_ACCOUNT=BitSongOfficial
ANALYZER_START_DATE=
ANALYZER_END_DATE=
16 changes: 15 additions & 1 deletion code/src/tweets/twitter.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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': [
Expand All @@ -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;
}
}
}
}
11 changes: 11 additions & 0 deletions code/src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
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();
const leftDate = dayjs(startDate);

return rightDate.diff(leftDate, 'M', true);
};

export const isBetweenStartEnd = (
startDate: string,
endDate: string,
date?: string,
) => {
return dayjs(date ?? new Date()).isBetween(startDate, endDate);
};

0 comments on commit f97dae7

Please sign in to comment.