Skip to content

Commit

Permalink
Add Pinterest API to the platform. Finish the User and Self part
Browse files Browse the repository at this point in the history
See title
  • Loading branch information
ghostlilunan committed Apr 14, 2017
1 parent 0307135 commit 59bcbd7
Show file tree
Hide file tree
Showing 9 changed files with 805 additions and 9 deletions.
1 change: 0 additions & 1 deletion API/instagramAPI.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var Promise = require('promise');
var request = require('request');
var config = require('../config');

Expand Down
216 changes: 216 additions & 0 deletions API/pinterestAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
var request = require('request');
var config = require('../config');

var access_token = config.pinterest.access_token_key;


function pinterest(args, fname){
return new Promise((resolve, reject) =>{
console.log("In PInterest API, switch functionality now");
console.log(fname);
var queryUrl = "https://api.pinterest.com/v1";
switch(fname){
case "pUserRoot":
queryUrl = pUserRoot(args, queryUrl);
break;
case "pSelfBoards":
queryUrl = pSelfBoards(args, queryUrl);
break;
case "pSelfBoardsSuggested":
queryUrl = pSelfBoardsSuggested(args, queryUrl);
break;
case "pSelfFollower":
queryUrl = pSelfFollower(args, queryUrl);
break;
case "pSelfFollowingBoards":
queryUrl = pSelfFollowingBoards(args, queryUrl);
break;
case "pSelfInterests":
queryUrl = pSelfInterests(args, queryUrl);
break;
case "pSelfFollowing":
queryUrl = pSelfFollowing(args, queryUrl);
break;
case "pSelfLikes":
queryUrl = pSelfLikes(args, queryUrl);
break;
case "pSelfPins":
queryUrl = pSelfPins(args, queryUrl);
break;
case "pSearchBoards":
queryUrl = pSearchBoards(args, queryUrl);
break;
case "pSearchPins":
queryUrl = pSearchPins(args, queryUrl);
break;
}

var options = {
method: 'GET',
uri: queryUrl,
};

request(options, function(error, response){
if(error) {
console.log("Reuqest error")
reject(error);
}

if(response) {
console.log("send response data back")
console.log(JSON.parse(response.body))
resolve(JSON.parse(response.body));
}
});
});
}

function selfEncode(args){
var ret = ''

for(var key in args){
if(args[key] && key != 'query')
ret += key + '%2C';
}

return ret.substring(0, ret.length-3);
}

function queryEncode(query){
var ret = 'query=';
query = query.split(' ');
for(i = 0; i < query.length; i++){
ret += query[i] + '+';
}

return ret.substring(0, ret.length-1);
}

function pUserRoot(args, queryUrl){
queryUrl += ('/me/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfBoards(args, queryUrl){
queryUrl += ('/me/boards/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfBoardsSuggested(args, queryUrl){
queryUrl += ('/me/boards/suggested/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfFollower(args, queryUrl){
queryUrl += ('/me/followers/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfFollowingBoards(args, queryUrl){
queryUrl += ('/me/following/boards/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfInterests(args, queryUrl){
queryUrl += ('/me/following/interests/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfFollowing(args, queryUrl){
queryUrl += ('/me/following/users/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfLikes(args, queryUrl){
queryUrl += ('/me/likes/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSelfPins(args, queryUrl){
queryUrl += ('/me/pins/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
console.log(queryUrl);
return queryUrl
}

function pSearchBoards(args, queryUrl){
queryUrl += ('/me/search/boards/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
query = queryEncode(args.query);
queryUrl += '&' + query;
console.log(queryUrl);
return queryUrl
}

function pSearchPins(args, queryUrl){
queryUrl += ('/me/search/pins/?access_token=' + access_token );
var para = selfEncode(args);
if(para != ''){
queryUrl += '&fields='
queryUrl += para
}
query = queryEncode(args.query);
queryUrl += '&' + query;
console.log(queryUrl);
return queryUrl
}

module.exports = {
pinterest
}
3 changes: 0 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ app.use(function(err, req, res, next) {
res.render('error');
});




module.exports = {
app
};
Expand Down
3 changes: 3 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ config.instagram.access_token_key = process.env.INSTAGRAM_ACCESS_TOKEN_KEY;
config.instagram.client_id = process.env.INSTAGRAM_CLIENT_ID;
config.instagram.client_secret = process.env.INSTAGRAM_CLIENT_SECRET;

config.pinterest = {};
config.pinterest.access_token_key = process.env.PINTEREST_ACCESS_TOKEN;

module.exports = config;
Loading

0 comments on commit 59bcbd7

Please sign in to comment.