Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch facebook data from BOT #20

Merged
merged 3 commits into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var pageStream = new FacebookInsightStream( options )

### FacebookInsightStream options:

* `pastdays` (string-required)#number of collected days from today e.g "30"
* `pastdays` (string)#number of collected days from today e.g "30", disregard to fetch beginning of time
* `node` (string-required)#type of insight one of the two ( "app", "page" )
* `token` (string-required)#valid facebook oAuth token with 'read_insigt' scope
* `period` (string-required)#the time period to collect according to the relevant api
Expand Down
23 changes: 9 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var stream = require( "stream" );
var extend = require( "extend" );
var request = require( "request" );
var Promise = require( "bluebird" );
const queryString = require('query-string')

request = Promise.promisifyAll( request )

Expand Down Expand Up @@ -90,23 +91,17 @@ FacebookInsightStream.prototype._init = function ( callback ) {
"{metric}",
].join( "/" )

var query = [
"access_token=" + options.token,
"period=" + options.period,
"since=" + since,
"until=" + until,
].join( "&" );

var hasEvents = options.events && options.events.length;
var breakdowns = options.breakdowns;

if ( hasEvents ) {
query += "&event_name={ev}"
}

if ( options.aggregate ) {
query += "&aggregateBy={agg}";
}
let query = queryString.stringify({
since: options.pastdays ? since : undefined,
until: until,
period: options.period,
access_token: options.token,
event_name: hasEvents ? '{ev}' : undefined,
aggregateBy: options.aggregate ? '{agg}' : undefined
})

if ( breakdowns && breakdowns.length ) {
for ( var i = 0; i < breakdowns.length; i += 1 ) {
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
},
"homepage": "https://github.com/panoplyio/facebook-insight-stream#readme",
"dependencies": {
"sugar": "1.4.1",
"extend": "3.0.0",
"request": "2.53.0",
"bluebird": "2.9.32"
"bluebird": "2.9.32",
"extend": "3.0.0",
"query-string": "^5.0.1",
"request": "2.53.0",
"sugar": "1.4.1"
},
"devDependencies": {
"mocha": "2.3.4"
Expand Down
40 changes: 38 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var BASEURL = "https://graph.facebook.com/";
var METRICS = require( "./metric-list" );

var req_get = request.get;
let calledUrl

describe( "Skip missing data", function () {
var result = {};
Expand Down Expand Up @@ -162,15 +163,50 @@ describe( "collect", function () {
})
})

describe( "Fetch beginning of time", function () {
var result = {};
var source = {
apps: [ 'myApp' ],
ignoreMissing: true
}

var response = { "myApp": { error: {}, name: "myApp", data: dataGenerator( 1, null ) } }

before( initialize( result, response, source, true ) )
after( reset )

it( 'Fetch insights from beginning of time', function () {
assert.equal(calledUrl.includes('&since'), false)
})
})

describe( "Fetch x Days ago", function () {
var result = {};
var source = {
apps: [ 'myApp' ],
ignoreMissing: true
}

var response = { "myApp": { error: {}, name: "myApp", data: dataGenerator( 1, null ) } }

before( initialize( result, response, source ) )
after( reset )

it( 'Fetch insights for past x days', function () {
assert.equal(calledUrl.includes('&since'), true)
})
})


function initialize( result, response, source ) {
function initialize( result, response, source, fetchBOT ) {

result.batchCount = 0;

return function ( done ) {

request.get = function ( url, callback ) {
var metric;
calledUrl = url
url = url.split( BASEURL )[ 1 ];
var params = url.split( "?" )[ 0 ].split( "/" );
var app = params[ 1 ];
Expand Down Expand Up @@ -200,7 +236,7 @@ function initialize( result, response, source ) {
}

var options = {
pastdays: "30",
pastdays: fetchBOT ? undefined : "30",
node: "app",
period: "daily",
metrics: METRICS,
Expand Down