Skip to content
Permalink
Browse files

Merge branch 'feat/add-stackexchange-call' into develop

  • Loading branch information
adrianmatei-me committed Dec 9, 2019
2 parents 10699a1 + e952332 commit b0fe37c522998f4e8a7d79352b24299c95875181
@@ -1,6 +1,6 @@
{
"name": "bookmarks.dev-api",
"version": "6.6.0",
"version": "6.7.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
@@ -9,9 +9,10 @@
},
"env_production" : {
"NODE_ENV": "production",
"YOUTUBE_API_KEY" : "Axxxx",
"YOUTUBE_API_KEY" : "CHANGE_ME_WITH_VALID_API_KEY",
"STACK_EXCHANGE_API_KEY": "CHANGE_ME_WITH_VALID_API_KEY",
"MONGODB_BOOKMARKS_USERNAME": "bookmarks",
"MONGODB_BOOKMARKS_PASSWORD": "xxx",
"MONGODB_BOOKMARKS_PASSWORD": "CHANGE_ME_WITH_VALID_MONGO_PASSWORD",
"MONGODB_BOOKMARKS_COLLECTION" : "dev-bookmarks",
"MONGODB_HOST": "localhost",
"MONGODB_PORT": "27017"
@@ -0,0 +1,21 @@
db.bookmarks.find().forEach(
function(e) {
var bookmarkUrl = e.location;
var isStackoverflowQuestion= false;
var stackoverflowQuestionId = '';
var regExpMatchArray = bookmarkUrl.match(/stackoverflow\.com\/questions\/(\d+)/);
if (regExpMatchArray) {
stackoverflowQuestionId = regExpMatchArray[1];
isStackoverflowQuestion = true;
}
if(isStackoverflowQuestion) {
print('stackoverflowQuestionId ' + stackoverflowQuestionId);
print('with location ' + e.location);
e.stackoverflowQuestionId = stackoverflowQuestionId;
//db.bookmarks.save(e);
}
}
);

db.bookmarks.find({location: /stackverflow\.com/}).count()
db.bookmarks.find({location: /youtu\.be/}).count()
@@ -8,24 +8,26 @@ module.exports = {
buildBookmarkFromRequest: function (req) {
const descriptionHtml = req.body.descriptionHtml ? req.body.descriptionHtml : converter.makeHtml(req.body.description);
const youtubeVideoId = req.body.youtubeVideoId;
const stackoverflowQuestionId = req.body.stackoverflowQuestionId;
const bookmark = new Bookmark({
_id: req.body._id,
name: req.body.name,
location: req.body.location,
language: req.body.language,
description: req.body.description,
descriptionHtml: descriptionHtml,
category: req.body.category,
tags: req.body.tags,
publishedOn: req.body.publishedOn,
githubURL: req.body.githubURL,
userId: req.body.userId || req.params.userId,
shared: req.body.shared,
starredBy: req.body.starredBy,
lastAccessedAt: req.body.lastAccessedAt,
likes: req.body.likes,
youtubeVideoId: youtubeVideoId ? youtubeVideoId :null
});
_id: req.body._id,
name: req.body.name,
location: req.body.location,
language: req.body.language,
description: req.body.description,
descriptionHtml: descriptionHtml,
category: req.body.category,
tags: req.body.tags,
publishedOn: req.body.publishedOn,
githubURL: req.body.githubURL,
userId: req.body.userId || req.params.userId,
shared: req.body.shared,
starredBy: req.body.starredBy,
lastAccessedAt: req.body.lastAccessedAt,
likes: req.body.likes,
youtubeVideoId: youtubeVideoId ? youtubeVideoId : null,
stackoverflowQuestionId: stackoverflowQuestionId ? stackoverflowQuestionId : null
});

return bookmark;
}
@@ -16,7 +16,8 @@ const bookmarkSchema = new Schema({
starredBy: [String],
lastAccessedAt: Date,
likes: Number,
youtubeVideoId: {type:String, required: false}
youtubeVideoId: {type:String, required: false},
stackoverflowQuestionId: {type:String, required: false},
},
{
timestamps: true
@@ -75,6 +75,18 @@ router.get('/scrape', async function (request, response, next) {
}
});

/* GET stackoverflow question data */
router.get('/scrape', async function (request, response, next) {
const stackoverflowQuestionId = request.query.stackoverflowQuestionId;
if (stackoverflowQuestionId) {
const webpageData = await PublicBookmarksService.getStackoverflowQuestionData(stackoverflowQuestionId)

return response.send(webpageData);
} else {
next();
}
});

/* GET title of bookmark given its url */
router.get('/scrape', async function () {
throw new ValidationError('Missing parameters - url or youtubeVideoId',
@@ -69,7 +69,6 @@ let getBookmarkById = async function (bookmarkId) {
};

let getYoutubeVideoData = async (youtubeVideoId) => {
//let youtubeVideoId = req.query.youtubeVideoId;
const response = await request
.get('https://www.googleapis.com/youtube/v3/videos')
.query({id: youtubeVideoId})
@@ -95,7 +94,27 @@ let getYoutubeVideoData = async (youtubeVideoId) => {
}

return webpageData;
}

let getStackoverflowQuestionData = async (stackoverflowQuestionId) => {
const response = await request
.get(`https://api.stackexchange.com/2.2/questions/${stackoverflowQuestionId}`)
.query({site: 'stackoverflow'})
.query({key: process.env.STACK_EXCHANGE_API_KEY || "change-me-with-a-valid-stackexchange-key-if-you-need-me"});

const tags = response.body.items[0].tags;
const title = response.body.items[0].title;
const creationDateMillis = response.body.items[0].creation_date * 1000;
const creationDate = new Date(creationDateMillis).toISOString();
const publishedOn = creationDate.substring(0, creationDate.indexOf('T'));

const webpageData = {
title: title,
tags: tags,
publishedOn: publishedOn
}

return webpageData;
}

/**
@@ -125,5 +144,6 @@ module.exports = {
getBookmarksForTag: getBookmarksForTag,
getBookmarkById: getBookmarkById,
getScrapedDataForLocation: getScrapedDataForLocation,
getYoutubeVideoData: getYoutubeVideoData
getYoutubeVideoData: getYoutubeVideoData,
getStackoverflowQuestionData: getStackoverflowQuestionData
};

0 comments on commit b0fe37c

Please sign in to comment.
You can’t perform that action at this time.