Compare commits
9 Commits
814694c97e
...
main
Author | SHA1 | Date | |
---|---|---|---|
f515b9d969 | |||
f22e8199e4 | |||
85ff4c0c6c | |||
d6f93f2f42 | |||
5d4d318a50 | |||
b2166a8425 | |||
2a594e0974 | |||
26e036c71b | |||
6568ffb45c |
104
MMM-Wordpress.js
104
MMM-Wordpress.js
@ -7,55 +7,59 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
Module.register('MMM-Wordpress', {
|
Module.register('MMM-Wordpress', {
|
||||||
defaults: {
|
defaults: {
|
||||||
updateInterval: 600000, // 10 minutes
|
updateInterval: 600000, // 10 minutes
|
||||||
url: 'YOUR_WORDPRESS_API_URL', // replace with your WordPress API endpoint
|
url: 'YOUR_WORDPRESS_API_URL', // replace with your WordPress API endpoint
|
||||||
numberOfPosts: 5, // number of posts to display
|
numberOfPosts: 5, // number of posts to display
|
||||||
posts: 'posts', //Endpoint ("posts" for normal posts, can be changed to custom post type)
|
posts: 'posts', //Endpoint ("posts" for normal posts, can be changed to custom post type)
|
||||||
},
|
},
|
||||||
|
|
||||||
start: function() {
|
|
||||||
this.posts = [];
|
|
||||||
this.getPosts();
|
|
||||||
setInterval(() => {
|
|
||||||
this.getPosts();
|
|
||||||
}, this.config.updateInterval);
|
|
||||||
},
|
|
||||||
|
|
||||||
getPosts: function() {
|
|
||||||
const url = this.config.url;
|
|
||||||
const posts = this.config.posts;
|
|
||||||
const numberOfPosts = this.config.numberOfPosts;
|
|
||||||
|
|
||||||
const request = require('request');
|
|
||||||
|
|
||||||
request.get(`${url}/wp-json/wp/v2/${posts}?per_page=${numberOfPosts}`, (error, response, body) => {
|
|
||||||
if (!error && response.statusCode == 200) {
|
|
||||||
this.posts = JSON.parse(body);
|
|
||||||
this.updateDom();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
getDom: function() {
|
|
||||||
const wrapper = document.createElement('div');
|
|
||||||
const posts = this.posts;
|
|
||||||
|
|
||||||
if (posts.length > 0) {
|
|
||||||
for (let i = 0; i < posts.length; i++) {
|
|
||||||
const post = posts[i];
|
|
||||||
const postElement = document.createElement('div');
|
|
||||||
postElement.innerHTML = `<h2>${post.title.rendered}</h2><p>${post.excerpt.rendered}</p>`;
|
|
||||||
wrapper.appendChild(postElement);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
wrapper.innerHTML = 'No posts found';
|
|
||||||
}
|
|
||||||
|
|
||||||
return wrapper;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
start: function() {
|
||||||
|
this.posts = [];
|
||||||
|
this.getPosts();
|
||||||
|
setInterval(() => {
|
||||||
|
this.getPosts();
|
||||||
|
}, this.config.updateInterval);
|
||||||
|
},
|
||||||
|
|
||||||
|
getPosts: function() {
|
||||||
|
const url = this.config.url;
|
||||||
|
const posts = this.config.posts;
|
||||||
|
const numberOfPosts = this.config.numberOfPosts;
|
||||||
|
|
||||||
|
//hier müssen die posts noch rein!
|
||||||
|
this.sendSocketNotification('GET_POSTS', { url, posts, numberOfPosts });
|
||||||
|
},
|
||||||
|
|
||||||
|
socketNotificationReceived: function(notification, payload) {
|
||||||
|
if (notification === 'POSTS_RESULT' && payload) {
|
||||||
|
this.posts = payload;
|
||||||
|
this.updateDom();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getDom: function() {
|
||||||
|
const wrapper = document.createElement('div');
|
||||||
|
const posts = this.posts;
|
||||||
|
|
||||||
|
if (posts.length > 0) {
|
||||||
|
for (let i = 0; i < posts.length; i++) {
|
||||||
|
const post = posts[i];
|
||||||
|
const postElement = document.createElement('div');
|
||||||
|
//postElement.innerHTML = `<h2>${this.stripTags(post.title.rendered)}</h2><p>${this.stripTags(post.excerpt.rendered)}</p>`;
|
||||||
|
postElement.innerHTML = `<h2>${this.stripTags(post.title.rendered)}</h2><p>${this.stripTags(post.content.rendered)}</p>`;
|
||||||
|
wrapper.appendChild(postElement);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
wrapper.innerHTML = 'Keine Beiträge gefunden';
|
||||||
|
}
|
||||||
|
|
||||||
|
return wrapper;
|
||||||
|
},
|
||||||
|
|
||||||
|
stripTags: function(html) {
|
||||||
|
const doc = new DOMParser().parseFromString(html, 'text/html');
|
||||||
|
return doc.body.textContent || "";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
23
README.md
23
README.md
@ -1,5 +1,28 @@
|
|||||||
|
# Info
|
||||||
|
|
||||||
|
This module for Magic Mirror should display posts from a WordPress site.
|
||||||
|
|
||||||
|
|
||||||
# Install
|
# Install
|
||||||
|
|
||||||
|
```bash
|
||||||
git clone https://github.com/tbuss/MMM-Wordpress.git
|
git clone https://github.com/tbuss/MMM-Wordpress.git
|
||||||
cd MMM-Wordpress
|
cd MMM-Wordpress
|
||||||
npm install
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
# Config
|
||||||
|
|
||||||
|
Put this in your config.js
|
||||||
|
|
||||||
|
```js
|
||||||
|
{
|
||||||
|
module: 'MMM-Wordpress',
|
||||||
|
position: 'bottom_bar', // or any other position
|
||||||
|
config: {
|
||||||
|
updateInterval: 10*60*1000, // 10 minutes
|
||||||
|
url: 'https://www.your_wordpress.url',
|
||||||
|
numberOfPosts: 5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
```
|
24
node_helper.js
Normal file
24
node_helper.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
const NodeHelper = require('node_helper');
|
||||||
|
const request = require('request');
|
||||||
|
|
||||||
|
module.exports = NodeHelper.create({
|
||||||
|
start: function() {
|
||||||
|
console.log('MMM-Wordpress helper started...');
|
||||||
|
},
|
||||||
|
|
||||||
|
socketNotificationReceived: function(notification, payload) {
|
||||||
|
console.log('Notification!');
|
||||||
|
if (notification === 'GET_POSTS') {
|
||||||
|
console.log('Notification: GET_POSTS');
|
||||||
|
const { url, posts, numberOfPosts } = payload;
|
||||||
|
const apiUrl = `${url}/wp-json/wp/v2/${posts}?per_page=${numberOfPosts}`;
|
||||||
|
|
||||||
|
request.get(apiUrl, (error, response, body) => {
|
||||||
|
if (!error && response.statusCode == 200) {
|
||||||
|
const posts = JSON.parse(body);
|
||||||
|
this.sendSocketNotification('POSTS_RESULT', posts);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
Reference in New Issue
Block a user