-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmss.js
97 lines (77 loc) · 3 KB
/
mss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fetchDataAndDisplay = () => {
// Clear previous data
document.getElementById('posts').innerHTML = '';
const querykeys = ['rss', 'foss','opensource','mastodon',]; // replace these hashtag searches with your own
const domains = ['mastodon.social', 'mstdn.social','mastodon.online','mastodon.world','techhub.social'];
// Replace these instances with your own
const corsProxyUrl = 'https://corsproxy.io/?';
// Create an array to hold all the results
let allPosts = [];
// Create a Set to hold hashes of posts to track duplicates
let seen = new Set();
// Create an array to hold all the fetch promises
let fetchPromises = [];
// Iterate through each domain
domains.forEach((domain) => {
querykeys.forEach((querykey) => {
const url = 'https://' + domain + '/api/v1/timelines/tag/' + querykey + '?limit=40';
let fetchPromise = fetch(corsProxyUrl + url)
.then((response) => {
if (!response.ok) {
throw new Error('HTTP error ' + response.status);
}
return response.json();
})
.then((json) => {
json.forEach((post) => {
const hash = post.uri;
if (!seen.has(hash)) {
allPosts.push(post);
seen.add(hash);
}
});
})
.catch(function () {
console.log('An error occurred while fetching the data.');
});
fetchPromises.push(fetchPromise);
});
});
Promise.all(fetchPromises).then(() => {
allPosts.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
let top15Posts = allPosts.slice(0, 15);
// Inserting data into HTML
const postsContainer = document.getElementById('posts');
// Create two columns
const column1Div = document.createElement('div');
column1Div.className = 'column';
const column2Div = document.createElement('div');
column2Div.className = 'column';
// Iterate through the top 15 posts, and append them to the appropriate column
top15Posts.forEach((post, index) => {
const postDiv = document.createElement('div');
postDiv.className = 'post';
if (index % 2 === 0) {
postDiv.classList.add('shaded');
}
// Create a temporary container to hold the HTML content
let tempDiv = document.createElement('div');
tempDiv.innerHTML = post.content;
// Append the manipulated content
postDiv.innerHTML = tempDiv.innerHTML;
// Depending on the index, append the post to the appropriate column
if (index < 7) {
column1Div.appendChild(postDiv);
} else if (index < 14) {
column2Div.appendChild(postDiv);
}
});
// Append the two columns to the posts container
postsContainer.appendChild(column1Div);
postsContainer.appendChild(column2Div);
});
};
// Call the function immediately to fetch and display data
fetchDataAndDisplay();
// Schedule the function to run every 90 seconds, you can of course adjust this
setInterval(fetchDataAndDisplay, 90000);