diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa13693 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +develop/ \ No newline at end of file diff --git a/node_modules/facebook-testers-tool/lib/facebook-testers-tool.js b/node_modules/facebook-testers-tool/lib/facebook-testers-tool.js new file mode 100644 index 0000000..ed92b8a --- /dev/null +++ b/node_modules/facebook-testers-tool/lib/facebook-testers-tool.js @@ -0,0 +1,297 @@ +var querystring = require("querystring"); +var urlFetcher = require("./url-fetcher"); +var fs = require("fs"); +var path = require("path"); + +var endPoints = { + create: "https://graph.facebook.com/:appId/accounts/test-users", + makeFriend: "https://graph.facebook.com/:user1/friends/:user2", + access: "https://graph.facebook.com/:appId/accounts/test-users" +} + +function FBTesterTool(newOptions) +{ + var that = this; + + if (!newOptions.appId) + { + throw "Please init with your FB App ID!" + } + + if (!newOptions.appSecret) + { + throw "Please init with your app secret" + } + + if (!newOptions.dataPath) + { + throw "Please init with your the path for saving the users info" + } + + var options = { + appId: newOptions.appId, + appSecret: newOptions.appSecret, + dataPath: newOptions.dataPath + } + + var queue = []; + var running = false; + + this.create = function(name, createOptions) + { + var command; + var dataFile = readUsers(); + + if (!dataFile[name]) + { + + command = (function() + { + + var createUrl = endPoints["create"].replace(":appId", options.appId); + + var baseOptions = { + installed: true, + permissions: "read_stream", + name: name, + method: "post", + access_token: options.appId + "|" + options.appSecret + } + + if (!createOptions) + { + createUrl += "?" + querystring.stringify(baseOptions); + } + + + console.log("Creating user '" + name + "'"); + + urlFetcher.get(createUrl, function(result) { + + var resultData = null; + try + { + resultData = JSON.parse(result); + } + catch (err) + { + console.log(err); + } + + + console.log("\tDone! Login url: " + resultData.login_url); + resultData.name = name; + + // save user + saveUser(resultData); + + running = false; + runQueue(); + + }); + + }); + + } + else + { + command = (function() + { + // access + var createUrl = endPoints["access"].replace(":appId", options.appId); + + var baseOptions = { + access_token: options.appId + "|" + options.appSecret + } + + createUrl += "?" + querystring.stringify(baseOptions); + + console.log("Accessing user '" + name + "'"); + + urlFetcher.get(createUrl, function(result) { + + var resultData = null; + try + { + resultData = JSON.parse(result); + } + catch (err) + { + console.log(err); + } + + + for(var i = 0, ii = resultData.data.length; i < ii; i++) + { + if (resultData.data[i].id == dataFile[name].id) + { + dataFile[name].access_token = resultData.data[i].access_token; + dataFile[name].login_url = resultData.data[i].login_url; + + console.log("\tLogin url: " + resultData.data[i].login_url); + + // save user + saveUser(dataFile[name]); + } + } + + running = false; + runQueue(); + + }); + + }); + + + } + + queue.push(command); + runQueue(); + + return this; + + } + + this.makeFriend = function(name1, name2) + { + + var command = (function() + { + var dataFile = readUsers(); + + if (!dataFile[name1]) + { + throw "no user found with the name " + name1; + return that; + } + + if (!dataFile[name2]) + { + throw "no user found with the name " + name2; + return that; + } + + var fb1 = dataFile[name1].id; + var fb2 = dataFile[name2].id; + + var createUrl1 = endPoints["makeFriend"].replace(":user1", fb1).replace(":user2", fb2); + var createUrl2 = endPoints["makeFriend"].replace(":user1", fb2).replace(":user2", fb1); + + var baseOptions1 = { + access_token: dataFile[name1].access_token + } + + var baseOptions2 = { + access_token: dataFile[name2].access_token + } + + createUrl1 += "?" + querystring.stringify(baseOptions1); + createUrl2 += "?" + querystring.stringify(baseOptions2); + + + urlFetcher.get(createUrl1, {method: "POST", params:{access_token: dataFile[name1].access_token}}, function(result) { + + var resultData = null; + try + { + resultData = JSON.parse(result); + } + catch (err) + { + console.log(err); + } + + if (resultData.error) + { + console.log(resultData); + running = false; + runQueue(); + return; + } + + urlFetcher.get(createUrl2, {method: "POST", params:{access_token: dataFile[name2].access_token}}, function(result) { + + var resultData = null; + try + { + resultData = JSON.parse(result); + } + catch (err) + { + console.log(err); + } + + if (resultData.error) + { + console.log(resultData); + running = false; + runQueue(); + return; + } + + // save user + saveUser(resultData); + + running = false; + runQueue(); + + }); + + + }); + + }); + + queue.push(command); + runQueue(); + + return this; + + } + + + + function runQueue() + { + // console.log(queue) + if (queue.length > 0 && !running) + { + running = true; + var command = queue.shift(); + + command(); + } + } + + + function saveUser(data) + { + var dataFile = readUsers(); + dataFile[data.name] = data; + fs.writeFileSync(options.dataPath, JSON.stringify(dataFile, null, "\t"), "utf8"); + } + + function readUsers() + { + var dataFile = {}; + if (path.existsSync(options.dataPath)) + { + dataFile = JSON.parse(fs.readFileSync(options.dataPath, "utf8")); + } + else + { + fs.writeFileSync(options.dataPath, JSON.stringify(dataFile), "utf8"); + } + return dataFile; + } + +} + + + + +exports.init = function(options) +{ + var instance = new FBTesterTool(options); + return instance; +} \ No newline at end of file diff --git a/node_modules/facebook-testers-tool/lib/url-fetcher.js b/node_modules/facebook-testers-tool/lib/url-fetcher.js new file mode 100644 index 0000000..229a4e1 --- /dev/null +++ b/node_modules/facebook-testers-tool/lib/url-fetcher.js @@ -0,0 +1,109 @@ +var http = require("http"); +var https = require("https"); +var URL = require("url"); + + +function getUrlContent(url, callOptions, callback) +{ + if (callback == null && typeof callOptions == "function") + { + callback = callOptions; + delete callOptions; + } + + var urlData = URL.parse(url); + + if (urlData.protocol == "http:") + { + var parsedUrl = url.replace(/htt.*?\:\/\//, ""); + + var urlParts = parsedUrl.split("/"); + var host = urlParts[0]; + var path = parsedUrl.replace(host, ""); + + var body = ""; + var data = null; + var bodyLength = 0; + + var client = http.createClient(80, host); + + var request = client.request('GET', path, {'host': host, 'user-agent': "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"}); + + request.on('response', + function (response) + { + if (response.statusCode != "200") + { + console.log("ERROR in status code", response.statusCode) + } + response.setEncoding('utf8'); + response.on('data', + function (chunk) + { + body += chunk; + } + ); + + response.on("end", + function() + { + callback(body); + } + ) + } + ); + + request.end(); + } + else + { + + var host = urlData.host; + var path = urlData.pathname; + + var options = { + host: host, + path: path + "?" + urlData.query, + port: 443, + method: callOptions.method || "GET" + } + + + + var body = ""; + var data = null; + var bodyLength = 0; + + var client = http.createClient(4, host); + + var request = https.request(options, function(httpsRes) + { + httpsRes.on('data', + function (chunk) + { + body += chunk; + } + ); + + httpsRes.on("end", + function() + { + + callback(body); + } + ) + } + ); + + if (options.params) + { + console.log(querystring.stringify(options.params)) + response.write(querystring.stringify(options.params)); + } + + request.end(); + + } +} + +exports.get = getUrlContent; \ No newline at end of file diff --git a/node_modules/facebook-testers-tool/package.json b/node_modules/facebook-testers-tool/package.json new file mode 100644 index 0000000..8050104 --- /dev/null +++ b/node_modules/facebook-testers-tool/package.json @@ -0,0 +1,3 @@ +{ + "main": "lib/facebook-testers-tool.js" +}