Skip to content

Instantly share code, notes, and snippets.

@ixtgorilla
Last active March 13, 2018 16:57
Show Gist options
  • Save ixtgorilla/021483c6fb1a0233c5bf5d84264dad1e to your computer and use it in GitHub Desktop.
Save ixtgorilla/021483c6fb1a0233c5bf5d84264dad1e to your computer and use it in GitHub Desktop.
Scraper for Twitter followers count
// Aggregating Consts Object
function Settings() {
// defined col datas
this.COL_TW_ID_NUM = 1;
this.COL_TW_URL_NUM = 2;
this.COL_TW_FOLLOWER_NUM = 3;
// defined start row numbers
this.START_ROW_NUM = 2;
}
Settings = new Settings();
function getTwitterFollowers () {
var book = SpreadsheetApp.getActiveSpreadsheet();
var sheetData = book.getSheetByName('sheet1');
var rowStartData = Settings.START_ROW_NUM;
var rowEndData = sheetData.getDataRange().getLastRow();
for (var i = rowStartData; i <= rowEndData; i += 1) {
twitterId = sheetData.getRange(i, Settings.COL_TW_ID_NUM).getValue();
twitterClient = new TwitterClient(twitterId);
result = twitterClient.getFollowerCount();
sheetData.getRange(i, Settings.COL_TW_URL_NUM).setValue(result.twitterUrl);
sheetData.getRange(i, Settings.COL_TW_FOLLOWER_NUM).setValue(result.followers);
}
}
// Object: TwitterClient
//
// @param [String] twitterId
function TwitterClient(twitterId){
this.twitterId = twitterId;
this.html = '';
// Get Followers Count
//
// @return [Object]
this.getFollowerCount = function() {
var response = this._request();
return { 'twitterUrl': this._generateTwitterUrl(), 'followers':this._parseFollowersCount(response) };
}
// Parsing Followers Count from Response
//
// @param [String] response
// @return [String]
this._parseFollowersCount = function(response) {
var indexNum = response.indexOf(this._searchStr());
//Logger.log(indexNum);
if (indexNum == -1) { return ''; }
var htmlBlock = response.substring(indexNum);
var indexNum = htmlBlock.indexOf('</a>');
if (indexNum == -1) { return ''; }
var htmlBlock = htmlBlock.substring(0, indexNum + 4);
var numPattern = new RegExp(/\b\d{1,3}(,\d{3})*\b/);
return numPattern.exec(htmlBlock)[0];
}
// @return [String]
this._request = function(){
var response = UrlFetchApp.fetch(this._generateTwitterUrl());
return response.getContentText('UTF-8');
}
// @return [String]
this._searchStr = function(){
return '<a href="/' + this.twitterId + '/followers"';
}
// @return [String]
this._generateTwitterUrl = function() {
return 'https://twitter.com/' + this.twitterId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment