Add image fetching for articles that support the og:image meta tag

This commit is contained in:
Nathan Leniz
2015-03-09 13:08:39 +09:00
parent 3b3de541b3
commit eb991c4f15
4 changed files with 26 additions and 14 deletions

View File

@ -283,14 +283,16 @@ module.exports = {
return process.env.NODE_ENV; return process.env.NODE_ENV;
}, },
getURLTitle: function(url, callback) { getURLTitle: function(url, callback) {
debug('got url in meta scraping function', url);
(function () { (function () {
var result = {title: ''}; var result = {title: '', image: '', url: ''};
request(url, function (error, response, body) { request(url, function (error, response, body) {
if (!error && response.statusCode === 200) { if (!error && response.statusCode === 200) {
var $ = cheerio.load(body); var $ = cheerio.load(body);
var urlImage = $("meta[property='og:image']").attr('content') ? $("meta[property='og:image']").attr('content') : '';
var title = $('title').text(); var title = $('title').text();
result.title = title; result.title = title;
result.image = urlImage;
callback(null, result); callback(null, result);
} else { } else {
callback('failed'); callback('failed');

View File

@ -84,13 +84,18 @@ exports.preSubmit = function(req, res, next) {
var data = req.params.newStory; var data = req.params.newStory;
data = data.replace(/url=/gi, '').replace(/&title=/gi, ',').split(','); data = data.replace(/url=/gi, ',').replace(/&title=/gi, ',').replace(/&image=/gi, ',').split(',');
// get rid of first blank element from shift
data.shift();
debug('data to send after splitting', data);
var url = data[0]; var url = data[0];
var title = data[1]; var title = data[1];
var image = data[2];
res.render('stories/index', { res.render('stories/index', {
page: 'storySubmission', page: 'storySubmission',
storyURL: url, storyURL: url,
storyTitle: title storyTitle: title,
storyImage: image
}); });
}; };
@ -206,13 +211,11 @@ exports.comments = function(req, res, next) {
exports.newStory = function(req, res, next) { exports.newStory = function(req, res, next) {
var url = req.body.data.url; var url = req.body.data.url;
debug('this is the url', url);
if (url.search(/^https?:\/\//g) === -1) { if (url.search(/^https?:\/\//g) === -1) {
url = 'http://' + url; url = 'http://' + url;
} }
debug('In pre submit with a url', url);
Story.find({'link': url}, function(err, story) { Story.find({'link': url}, function(err, story) {
debug('Attempting to find a story');
if (err) { if (err) {
debug('oops'); debug('oops');
return res.status(500); return res.status(500);
@ -231,19 +234,20 @@ exports.newStory = function(req, res, next) {
resources.getURLTitle(url, processResponse); resources.getURLTitle(url, processResponse);
}); });
function processResponse(err, storyTitle) { function processResponse(err, story) {
if (err) { if (err) {
res.json({ res.json({
alreadyPosted: false, alreadyPosted: false,
storyURL: url, storyURL: url,
storyTitle: '' storyTitle: '',
storyImage: ''
}); });
} else { } else {
storyTitle = storyTitle ? storyTitle : '';
res.json({ res.json({
alreadyPosted: false, alreadyPosted: false,
storyURL: url, storyURL: url,
storyTitle: storyTitle.title storyTitle: story.title,
storyImage: story.image
}); });
} }
} }

View File

@ -38,7 +38,8 @@
} else { } else {
window.location = '/stories/submit/url=' + window.location = '/stories/submit/url=' +
encodeURIComponent(data.storyURL) + encodeURIComponent(data.storyURL) +
'&title=' + encodeURIComponent(data.storyTitle); '&title=' + encodeURIComponent(data.storyTitle) +
'&image=' + encodeURIComponent(data.storyImage)
} }
}); });
} }

View File

@ -3,6 +3,7 @@
script. script.
var storyURL = !{JSON.stringify(storyURL)}; var storyURL = !{JSON.stringify(storyURL)};
var storyTitle = !{JSON.stringify(storyTitle)}; var storyTitle = !{JSON.stringify(storyTitle)};
var storyImage = !{JSON.stringify(storyImage)};
form.form-horizontal.control-label-story-submission#story-submission-form form.form-horizontal.control-label-story-submission#story-submission-form
.col-xs-12 .col-xs-12
.form-group .form-group
@ -21,12 +22,16 @@
.col-xs-12.col-md-11 .col-xs-12.col-md-11
input#description-box.form-control(name="comment-box", placeholder="Start off the discussion with a description of your post" maxlength='140') input#description-box.form-control(name="comment-box", placeholder="Start off the discussion with a description of your post" maxlength='140')
span.pull-left#textarea_feedback span.pull-left#textarea_feedback
.row
img.img-center.img-responsive(src="#{storyImage}", style="max-width: 250px; max-height: 250px")
.spacer .spacer
.form-group .row
button.btn.btn-big.btn-block.btn-primary#story-submit Submit .form-group
button.btn.btn-big.btn-block.btn-primary#story-submit Submit
script. script.
$('#story-url').val(storyURL).attr('disabled', 'disabled'); $('#story-url').val(storyURL).attr('disabled', 'disabled');
$('#story-title').val(storyTitle); $('#story-title').val(storyTitle);
console.log(storyImage);
var text_max = 140; var text_max = 140;
$('#textarea_feedback').html(text_max + ' characters remaining'); $('#textarea_feedback').html(text_max + ' characters remaining');
$('#description-box').keyup(function () { $('#description-box').keyup(function () {