Created
March 17, 2016 06:15
-
-
Save francisbrito/d0e2cb2f18efad370340 to your computer and use it in GitHub Desktop.
Gets group information (name, id, description, url, logo url) from Facebook Graph API.
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 http = require('got'); | |
const coroutine = require('co'); | |
const querystring = require('querystring'); | |
const ACCESS_TOKEN = '<ACCESS_TOKEN>'; | |
const groupNamesOrIds = [ | |
'JavaScript Dominicana', | |
1398701893767285, // Ruby.do | |
'hfsantodomingo', | |
'mobilenetSDQ', | |
'cddvorg', | |
'uiuxdominicana', | |
'csharp.do', | |
'devdominicanos', | |
'cocos2dx.do' | |
]; | |
const getGroupsIdPromise = groupNamesOrIds.map(n => { | |
if (isNumber(n)) { | |
return Promise.resolve(n); | |
} | |
return getGroupIdByName(n); | |
}); | |
function getGroupIdByName(name) { | |
const url = getGraphApiUrlFor({ | |
endpoint: 'search', query: {q: name, type: 'group'} | |
}); | |
return getAsJson(url) | |
.catch(e => console.log(e)) | |
.then(response => { | |
return response; | |
}) | |
.then(response => response.body.data[0].id) | |
.then(id => Number(id)); | |
} | |
function getGraphApiUrlFor(options) { | |
const path = options.path; | |
const query = options.query; | |
const endpoint = options.endpoint; | |
const accessToken = options.accessToken || ACCESS_TOKEN; | |
const baseGraphApiUrl = 'https://graph.facebook.com/v2.5'; | |
const queryString = querystring.stringify(query); | |
const url = `${baseGraphApiUrl}` + (endpoint ? `/${endpoint}` : '') + (path ? `/${path}` : '') + `/?access_token=${accessToken}` + (query ? `&${queryString}` : ''); | |
return url; | |
} | |
/* main */ | |
coroutine(function* () { | |
const groupIds = yield getGroupsIdPromise; | |
const groups = yield groupIds.map(getGroupInfoById); | |
console.log(JSON.stringify(groups, null, 2)); | |
}) | |
.catch(e => { | |
console.error(e.stack); | |
}); | |
/* end main */ | |
function getGroupInfoById(id) { | |
const url = getGraphApiUrlFor({ | |
path: id, | |
query: { | |
fields: 'id,name,icon,description' | |
} | |
}); | |
return getAsJson(url) | |
.then(response => response.body) | |
.then(groupInfo => { | |
groupInfo.facebookUrl = `https://www.facebook.com/groups/${id}`; | |
return groupInfo; | |
}) | |
.then(groupInfo => { | |
return Promise.all([ | |
groupInfo, | |
getGroupCreationDateById(id) | |
]); | |
}) | |
.then(promises => { | |
const groupInfo = promises[0]; | |
const creationDate = promises[1]; | |
return Object.assign({}, groupInfo, {creationDate}); | |
}); | |
} | |
function getGroupCreationDateById(id) { | |
return getGroupEventsById(id) | |
.then(response => response.data) | |
.then(events => events.map(e => new Date(e.start_time))) | |
.then(eventDates => eventDates.map(e => e.getTime())) | |
.then( | |
eventTimes => eventTimes.sort((a, b) => { | |
return a - b; | |
}) | |
) | |
.then(eventTimes => eventTimes[0]); | |
} | |
function getGroupEventsById(id) { | |
const url = getGraphApiUrlFor({ | |
path: 'events', | |
endpoint: id | |
}); | |
return getAsJson(url).then(response => response.body); | |
} | |
function getAsJson(url) { | |
const options = { | |
json: true | |
}; | |
return http.get(url, options); | |
} | |
/* Helpers */ | |
function isNumber(n) { | |
return typeof n === 'number'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment