Skip to content

Instantly share code, notes, and snippets.

@twalther
Last active November 18, 2015 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twalther/83a97b694b69c03bfc07 to your computer and use it in GitHub Desktop.
Save twalther/83a97b694b69c03bfc07 to your computer and use it in GitHub Desktop.
webos-blogpost
<!doctype html>
<html>
<head>
<title>Meridium blogginlägg</title>
<style>
body {
font-family: 'Nunito', HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-weight: 300;
margin: 0;
padding: 0;
}
#logo {
position: fixed;
background-color: #000;
}
#logo img {
height: 20px;
margin: 6px 8px;
}
.blog-image {
background: url(/images/empty.png) #ffbe43 no-repeat;
width: 364px;
height: 300px;
background-size: 826px 300px;
display: inline-block;
display: table-cell;
}
h1 {
font-size: 3em;
line-height: 1em;
vertical-align: top;
padding: 0.5em 0 0 0;
margin: 0;
}
.author-metadata {
font-size: 1em;
margin-top: -15px;
}
.author-metadata p {
font-weight: 400;
display: inline-block;
}
.author-metadata i,
.author-metadata time {
font-weight: 300;
display: inline-block;
}
.author-metadata i {
margin-left: 1em;
margin-right: 0.2em;
}
.blog-intro {
font-size: 1.33em;
margin-top: 0;
}
.blog-teaser.selected {
background-color: #eee;
}
.blog-teaser {
display: table;
width: 100%;
margin-bottom: 2px;
}
.blog-wrapper {
display: table-cell;
padding: 0 25px;
}
.blog-teaser:hover {
background-color: #eee;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function() {
var baseUrl = 'https://www.meridium.se'
$.getJSON('http://localhost:3000/json',
function( data ) {
var style = document.createElement('style');
style.type = 'text/css';
var css = '';
var value;
var mid = 0;
for(var i=0;i<data.results.length;i++) {
data.results[i].value.authorRef = data.results[i].value.authorUrl.replace(/\/sv\/vilka-vi-ar\/anstallda\//, '').replace(/\//, '');
}
for(var i=0;i<data.results.length;i++) {
value = data.results[i].value;
mid = (826 / 5000) * (value.mid - 1100);
css += '.blog-image.' + value.authorRef + ' { background: url(' + baseUrl + value.authorImage + ') #ffbe43 no-repeat; background-size: 826px 300px; background-position: -' + mid + 'px 0px; } ';
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.getElementsByTagName("head")[0].appendChild(style);
var content = '';
for(var i=0;i<data.results.length;i++) {
value = data.results[i].value;
content += '<div class="blog-teaser">';
content += '<div class="blog-image ' + value.authorRef + '"></div>';
content += '<div class="blog-wrapper">';
content += '<h1>' + value.title + '</h1>';
content += '<div class="author-metadata">';
content += '<p>' + value.author + '</p>';
content += '<i class="fa fa-clock-o"></i>';
content += '<time datetime="' + value.dateTime + '">' + value.time + '</time>';
content += '</div>';
content += '<p class="blog-intro">';
content += value.intro;
content += '</p>';
content += '</div>';
content += '</div>';
}
$('#content').html(content);
});
});
</script>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Nunito:400,300,700' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="logo">
<img src="https://www.meridium.se/static/images/logotype.png">
</div>
<div id="content"></div>
</body>
</html>
var when = require('when');
var request = require('request');
var express = require('express');
var cv = require('opencv');
var ReadWriteLock = require('rwlock');
var lock = new ReadWriteLock();
var app = express();
app.get('/', function(req, res) {
var mid = 0;
var deferred = when.defer();
request({ url: req.query.url, encoding: null }, function(error, response, html) {
if(!error) {
lock.writeLock(function (release) {
cv.readImage(html, function(err, im) {
im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) {
var max = 0;
for (var i=0;i<faces.length; i++){
var x = faces[i]
if(x.width > max) {
max = i;
}
}
release();
if(faces.length >= 1) {
var x = faces[max];
mid = parseInt(x.x + x.width/2);
}
console.log(mid);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ 'mid': mid }));
deferred.resolve();
});
});
});
}
});
return deferred.promise;
});
app.listen(3000);
var when = require('when');
var request = require('request');
var cheerio = require('cheerio');
var _ = require('lodash');
var db = require('orchestrate')('enter-your-key-here');
var url = 'https://www.meridium.se/sv/blogg/';
var baseUrl = 'https://www.meridium.se';
var facialRecognitionUrl = 'http://localhost:3000?url=';
var items = [];
request(url, function(error, response, html) {
if(!error) {
var $ = cheerio.load(html);
var blogItems = $('.block__blogarchive > ul > li');
_.forEach(blogItems, function(blogItem) {
var h2 = $(blogItem).find('h2').first();
var url = $(h2).find('a').attr('href');
var intro = $(blogItem).find('p').text();
var meta = $(blogItem).find('.meta--author');
var authorUrl = $(meta).find('a').attr('href');
var author = $(meta).find('strong').text();
var dateTime = $(meta).find('time').attr('datetime');
var time = $(meta).find('time').text();
items.push({
title: h2.text(),
url: url,
intro: intro,
authorUrl: authorUrl,
author: author,
dateTime: dateTime,
time: time
});
});
var urls = [];
_.forEach(items, function(item) {
urls.push(baseUrl + item.authorUrl);
});
_request(urls, function(responses) {
_.forEach(items, function(item) {
var $ = cheerio.load(responses[baseUrl + item.authorUrl].body);
var image = $('section.campaign--profile').attr('style');
image = image
.replace(/background-image\:url\(\'/, '')
.replace(/\'\)/, '');
item.authorImage = image;
});
var imageUrls = [];
_.forEach(items, function(item) {
imageUrls.push(facialRecognitionUrl + baseUrl + item.authorImage);
});
_request(imageUrls, function(responses) {
for(var key in responses) {
var item = _.filter(items, function(item) {
return endsWith(key, item.authorImage);
});
var json = JSON.parse(responses[key].body);
item[0].mid = json.mid;
}
_.forEach(items, function(item) {
db.put('webos-blogpost', item.url, item, false)
.then(function (res) {
console.log(res);
})
.fail(function (err) {});
});
});
});
}
});
var _request = function (urls, callback) {
var results = {}, t = urls.length, c = 0,
handler = function (error, response, body) {
var url = response.request.uri.href;
results[url] = { error: error, response: response, body: body };
if (++c === urls.length) { callback(results); }
};
while (t--) { request(urls[t], handler); }
};
function endsWith(str, suffix) {
return str.slice(-suffix.length) == suffix;
}
var when = require('when');
var express = require('express');
var db = require('orchestrate')('enter-your-key-here');
var app = express();
// houses index.html for local testing
app.use(express.static(__dirname + '/public'));
app.get('/json', function(req, res) {
var mid = 0;
var deferred = when.defer();
db.newSearchBuilder()
.collection('webos-blogpost')
.sort('dateTime', 'desc')
.limit(20)
.query("*")
.then(function (dbRes) {
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify({ 'results': dbRes.body.results }));
deferred.resolve();
});
return deferred.promise;
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment