Update script.js

- Fetch for new postings every 10 seconds
This commit is contained in:
Ralf Stockmann 2023-05-17 15:46:48 +02:00 committed by GitHub
parent 4394b6d2f3
commit ef4b7ed9f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,46 +1,38 @@
$(document).ready(function() {
let existingPosts = [];
let existingPosts = [];
// Function to calculate relative time
const timeAgo = function(date) {
let seconds = Math.floor((new Date() - date) / 1000);
let interval = seconds / 31536000;
if (interval > 1) {
return Math.floor(interval) + " years ago";
}
interval = seconds / 2592000;
if (interval > 1) {
return Math.floor(interval) + " months ago";
}
interval = seconds / 86400;
if (interval > 1) {
return Math.floor(interval) + " days ago";
}
interval = seconds / 3600;
if (interval > 1) {
return Math.floor(interval) + " hours ago";
}
interval = seconds / 60;
if (interval > 1) {
return Math.floor(interval) + " minutes ago";
}
return Math.floor(seconds) + " seconds ago";
// Function to calculate relative time
const timeAgo = function(date) {
let seconds = Math.floor((new Date() - date) / 1000);
let interval = seconds / 31536000;
if (interval > 1) {
return Math.floor(interval) + " years ago";
}
// Function to fetch posts for a given hashtag
const getPosts = function(hashtag) {
return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`);
interval = seconds / 2592000;
if (interval > 1) {
return Math.floor(interval) + " months ago";
}
interval = seconds / 86400;
if (interval > 1) {
return Math.floor(interval) + " days ago";
}
interval = seconds / 3600;
if (interval > 1) {
return Math.floor(interval) + " hours ago";
}
interval = seconds / 60;
if (interval > 1) {
return Math.floor(interval) + " minutes ago";
}
return Math.floor(seconds) + " seconds ago";
}
// Initialize Masonry
$('.masonry-grid').masonry({
itemSelector: '.col-sm-3',
columnWidth: '.col-sm-3',
percentPosition: true,
transitionDuration: '0.2s'
});
// Function to fetch posts for a given hashtag
const getPosts = function(hashtag) {
return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`);
}
// Function to fetch and display posts
const fetchAndDisplayPosts = function() {
// Fetch posts for each hashtag
$.when(getPosts('bibliocon23'), getPosts('111bibliocon'), getPosts('bibliocon')).then(function(bibliocon23Posts, bibliocon111Posts, biblioconPosts) {
let allPosts = bibliocon23Posts[0].concat(bibliocon111Posts[0], biblioconPosts[0]);
@ -56,8 +48,8 @@ $(document).ready(function() {
existingPosts.push(post.id);
let cardHTML = `
<div class="col-sm-3 mb-2">
<div class="card p-2">
<div class="col-sm-3">
<div class="card m-2 p-2">
<div class="d-flex align-items-center mb-2">
<img src="${post.account.avatar}" class="rounded-circle mr-2" width="50" height="50">
<h5 class="card-title m-0"><a href="${post.account.url}" target="_blank">${post.account.username}</a></h5>
@ -67,11 +59,7 @@ $(document).ready(function() {
<p class="card-text text-right"><a href="${post.url}" target="_blank">${timeAgo(new Date(post.created_at))}</a></p>
</div>
</div>
`;
`;
// Convert the HTML string into a jQuery object
let $card = $(cardHTML);
@ -83,6 +71,20 @@ $(document).ready(function() {
$('.masonry-grid').masonry('prepended', $card);
}
});
});
};
$(document).ready(function() {
// Initialize Masonry
$('.masonry-grid').masonry({
itemSelector: '.col-sm-3',
columnWidth: '.col-sm-3',
percentPosition: true
});
// Fetch posts for each hashtag on page load
fetchAndDisplayPosts();
// Fetch posts for each hashtag every 10 seconds
setInterval(fetchAndDisplayPosts, 10000);
});