Last active
July 28, 2019 19:43
-
-
Save franklsm1/0002637f6ff09e8737d70857f35e6760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const baseTwitterSearchUrl = 'https://api.twitter.com/1.1/search/tweets.json'; | |
const defaultFetchOptions = { | |
method: 'GET', | |
headers: { | |
'Authorization': `Bearer ${process.env.TWITTER_BEARER_TOKEN}`, | |
}, | |
}; | |
/* | |
* This function will call the standard Twitter search API finding non retweeted tweets starting | |
* from the date passed in (or today by default if no value is provided) using the keywords passed in. | |
* | |
* Note: This API call is limiting the language to English only with the 'lang=en' parameter | |
*/ | |
const getNonRetweetedTweetsByKeywordsAndDate = async (keywords, dateSince = new Date()) => { | |
const dateSinceFormatted = `${dateSince.getFullYear()}-${dateSince.getMonth() + 1}-${dateSince.getDate()}`; | |
const queryParam = `${keywords} -filter:retweets since:${dateSinceFormatted}`; | |
console.log('Query Param: ', queryParam); | |
// Query Param: #allstate OR @allstate -filter:retweets since:2019-7-28 | |
const response = await fetch(`${baseTwitterSearchUrl}?q=${encodeURIComponent(queryParam)}&include_entities=0&lang=en`, defaultFetchOptions); | |
const responseJson = await response.json(); | |
return responseJson.statuses; | |
}; | |
/** | |
TEST | |
**/ | |
const fetchMock = require('fetch-mock'); | |
describe('getNonRetweetedTweetsByKeywordsAndDate', () => { | |
afterEach(() => { | |
fetchMock.reset(); | |
}); | |
it('should get tweets by keyword and date', async () => { | |
const mockSearchResponse = { | |
statuses: [ | |
{ | |
id: 123, | |
id_str: "123", | |
text: 'test tweet #allstate', | |
created_at: new Date() | |
} | |
], | |
}; | |
const keywords = '#allstate OR @allstate'; | |
fetchMock.mock("begin:" + baseTwitterSearchUrl, { | |
status: 200, | |
headers: defaultFetchOptions.headers, | |
body: mockSearchResponse | |
}); | |
await getNonRetweetedTweetsByKeywordsAndDate(keywords, new Date()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment