Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reading Twitter timeline history.
{
"key": "_your_key_here_",
"secret": "_your_secret_here_"
}
// Libraries
const dateformat = require( 'dateformat' );
const fs = require( 'fs' );
const jsonfile = require( 'jsonfile' );
const request = require( 'request-promise-native' );
// Determine start the report
let start = null;
if( process.argv.length == 3 ) {
// Date provided
// Assumed start of week
// Monday
start = new Date( process.argv[2] );
} else {
// Week in which report is run
// Rewind start of week to Monday
start = new Date();
start.setUTCDate( start.getDate() - start.getDay() + 1 );
start.setUTCHours( 0 );
start.setUTCMinutes( 0 );
start.setUTCSeconds( 0 );
start.setUTCMilliseconds( 0 );
}
// Determine end of the report
let end = new Date( start.getTime() );
end.setUTCDate( end.getUTCDate() + 6 );
end.setUTCHours( 23 );
end.setUTCMinutes( 59 );
end.setUTCSeconds( 59 );
end.setUTCMilliseconds( 999 );
// Display range of report
console.log( 'Start: ' + start );
console.log( 'End: ' + end );
// Hold all tweets
let max = null;
let tweets = [];
// Authorization token
let token = null;
// Load Twitter credentials
let consumer = jsonfile.readFileSync( 'credentials.json' );
// Get Twitter access token
request( {
method: 'POST',
url: 'https://api.twitter.com/oauth2/token',
auth: {
user: consumer.key,
password: consumer.secret
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: 'grant_type=client_credentials',
json: true
} ).then( ( auth ) => {
// Store token for requests
token = auth.access_token;
// Get merged Twitter status updates
return getWeeklyTimelines();
} ).then( () => {
// Sort status updates by date
// Oldest to newest (date ascending)
tweets.sort( ( a, b ) => {
let created_a = new Date( a.created_at );
let created_b = new Date( b.created_at );
if( created_a.getTime() > created_b.getTime() ) { return 1; }
if( created_a.getTime() < created_b.getTime() ) { return -1; }
return 0;
} );
// Path to report
// Using date
let path = 'twitter/' + dateformat( start, 'UTC:yyyy-mm-dd' ) + '.md';
// Open file
let stream = fs.createWriteStream( path );
// Write markdown report for status updates
for( let t = 0; t < tweets.length; t++ ) {
let created = new Date( tweets[t].created_at );
let metar = '\[' + dateformat( created, 'UTC:ddhhmm' ) + 'Z\]';
let status = 'https://twitter.com/' + tweets[t].user.screen_name + '/status/' + tweets[t].id;
stream.write( '[' + metar + '](' + status + ') **&lt;' + tweets[t].user.screen_name + '&gt;** ' + tweets[t].text );
// No line rule at the end
if( t < tweets.length - 1 ) {
stream.write( ' \n' );
}
}
// Close file
stream.end();
// Inform user of completion
console.log( 'Created: ' + path );
} );
// Async function to get status updates for team
async function getWeeklyTimelines() {
// Read list of team members
// JSON array
let ibmers = jsonfile.readFileSync( 'team.json' );
// For each team member
for( let i = 0; i < ibmers.length; i++ ) {
// Which user
console.log( 'User: ' + ibmers[i] );
// Reset cursor
max = null;
// Get tweets in range
await getUserTweets( ibmers[i] );
}
}
async function getUserTweets( user ) {
// Query criteria
let query = {
count: 100,
screen_name: user,
exclude_replies: false,
include_rts: true
};
// Use cursor if needed
if( max ) {
query.max_id = max;
}
// Load status updates from Twitter
let timeline = await request( {
method: 'GET',
url: 'https://api.twitter.com/1.1/statuses/user_timeline.json',
qs: query,
auth: {
bearer: token
},
json: true
} );
// Store tweets in range
for( let t = 0; t < timeline.length; t++ ) {
let created = new Date( timeline[t].created_at );
if( created.getTime() > start.getTime() && created.getTime() < end.getTime() ) {
tweets.push( timeline[t] );
}
}
// Are there even tweets present
if( timeline.length > 0 ) {
// Check oldest tweet
let created = new Date( timeline[timeline.length - 1].created_at );
// Still within range
if( created.getTime() > start.getTime() ) {
// Set cursor
max = timeline[timeline.length - 1].id;
// Get older tweets
await getUserTweets( user );
}
}
}
[
"b0neskull",
"horeaporutiu",
"k_bankole",
"krhoyt",
"mrsanjaysaxena",
"sanjeevghimire",
"thomasj"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.