Created
August 22, 2016 22:23
-
-
Save JamesTheHacker/5faa7bd4ef17e52d029210473b8e4f44 to your computer and use it in GitHub Desktop.
Promise based Facebook Event ID scraper
This file contains hidden or 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
'use strict' | |
let Promise = require('bluebird') | |
let _ = require('lodash') | |
class Event { | |
constructor(session) { | |
this.session = session; | |
this.scrapedIDs = []; | |
} | |
/* | |
* Parse guests and cursor hash from response | |
*/ | |
parseGuestsAndCursor(body, guestType) { | |
let ids, json; | |
json = JSON.parse(body.substring(9)); | |
ids = json['payload'][guestType]['sections'][2][1].map((user) => user['uniqueID']); | |
return { | |
ids: _.uniq(ids), | |
cursor: json['payload'][guestType]['cursor'] | |
} | |
} | |
cursorRequest(cursor, eventID, guestType) { | |
return new Promise((resolve, reject) => { | |
let url = `https://facebook.com/events/typeahead/guest_list/?event_id=${eventID}&tabs[0]=${guestType}&order[${guestType}]=affinity&bucket_schema[${guestType}]=friends&cursor[${guestType}]=${cursor}&dpr=1&__user=${this.session.uid}&__a=1`; | |
this.session.request(url, (err, resp, body) => { | |
if(err) { | |
reject(err) | |
return; | |
} | |
let guests; | |
try { | |
guests = this.parseGuestsAndCursor(body, guestType); | |
} catch(err) { | |
reject(err); | |
return; | |
} | |
resolve(guests); | |
}) | |
}) | |
} | |
queryCursor(cursor, eventID, guestType) { | |
return this.cursorRequest(cursor, eventID, guestType).then((guests) => { | |
if(guests.ids.length > 0) { | |
let lastIDCount = this.scrapedIDs.length; | |
guests.ids.forEach((id) => this.scrapedIDs.push(id)); | |
this.scrapedIDs = _.uniq(this.scrapedIDs); | |
/* | |
* Compare the lastIDCount with the new id count after ID's have | |
* been added. If they're the same this means there's no more ID's | |
* left to scrape. | |
*/ | |
if(lastIDCount === this.scrapedIDs.length) { | |
return Promise.resolve(this.scrapedIDs); | |
} | |
return this.queryCursor(guests.cursor, eventID, guestType); | |
} | |
}) | |
} | |
members(eventID, guestType) { | |
let ids = []; | |
return new Promise((resolve, reject) => { | |
let url = `https://facebook.com/events/typeahead/guest_list/?event_id=${eventID}&tabs[0]=watched&tabs[1]=going&tabs[2]=invited&order[declined]=affinity&order[going]=affinity&order[invited]=affinity&order[maybe]=affinity&order[watched]=affinity&order[ticket_purchased]=affinity&bucket_schema[watched]=friends&bucket_schema[going]=friends&bucket_schema[invited]=friends&bucket_schema[ticket_purchased]=friends&dpr=1&__user=${this.session.uid}&__a=1`; | |
this.session.request(url, (err, resp, body) => { | |
if(err) { | |
reject(err); | |
return; | |
} | |
let guests; | |
try { | |
guests = this.parseGuestsAndCursor(body, guestType); | |
} catch(err) { | |
reject(err); | |
return; | |
} | |
this.queryCursor(guests.cursor, eventID, guestType) | |
.then(ids => resolve(ids)); | |
}) | |
}) | |
} | |
} | |
module.exports = Event; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment