Skip to content

Commit

Permalink
Merge pull request #20 from hadasiii/godaddy_fb_pages
Browse files Browse the repository at this point in the history
Fetch facebook data from BOT
  • Loading branch information
hadasiii authored Jan 24, 2018
2 parents 2c16861 + e4f1416 commit 05b14f4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
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

0 comments on commit 05b14f4

Please sign in to comment.