Skip to content

Instantly share code, notes, and snippets.

@Meshiest
Created October 28, 2016 01:06
Show Gist options
  • Save Meshiest/a8b5da3754eddd6c00bc6b2d9dd5f588 to your computer and use it in GitHub Desktop.
Save Meshiest/a8b5da3754eddd6c00bc6b2d9dd5f588 to your computer and use it in GitHub Desktop.
Automatically fetches blog posts from google blogger blogs (fill in blogId and apikey with respective ids)
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src='https://code.jquery.com/jquery-3.1.1.min.js'></script>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
body, html {
width: 100vw;
height: 100vh;
overflow: hidden;
}
body {
display: flex;
flex-direction: horizontal;
justify-content: center;
align-items: center;
}
#posts {
display: flex;
flex-direction: horizontal;
justify-content: center;
align-items: center;
background: rgba(0, 0, 0, 0.2);
}
.post {
flex-grow: 1;
margin: 1em;
}
.post .post-image {
display: block;
height: 300px;
width: 400px;
object-fit: cover;
}
.post .post-title {
display: block;
align-self: flex-end;
font-family: sans-serif;
font-weight: normal;
text-align: center;
padding: 4px;
}
</style>
</head>
<body>
<div id='posts'></div>
<script>
var blogId = '<BLOG_ID>'
var blogUrl = 'https://www.googleapis.com/blogger/v2/blogs/'
var apiKey = '<API_KEY>'
function getImageFromContent(content) {
var matches = content.match(/src="(.+?)"/)
return matches && matches[1] || '';
}
function getItem(postId) {
$.get(blogUrl + blogId + "/posts/" + postId + "?key=" + apiKey, function(data) {
$('#posts').append(
$('<div class="post"/>')
.append($('<img class="post-image"/>').attr("src", getImageFromContent(data.content)))
.append($('<h3 class="post-title"/>').text(data.title))
)
})
}
$.get(blogUrl + blogId + "/posts?key=" + apiKey, function(data) {
var items = data.items
for(var i = 0; i < Math.min(items.length, 3); i++) {
getItem(items[i].id)
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment