Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Piwigo as a source. #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ The following properties can be configured:
|`"icloud:<album id>"`|Cycles through random selections of the specified album.|
|`"lightroom:<user.myportfolio.com/album>"`|Cycles through random selections of the specified album.|
|`"local:</path/to/directory>"`|Cycles through random selections of the images in the specified local directory.|
|`"piwigo:<url>"`|Cycles through random images from a private piwigo gallery.|
|`"synology-moments:<url>"`|Cycles through the latest images from the specified Synology moments album.|
|`"/r/<subreddit>"`|Cycles through the most recent `hot` image posts from the subreddit.|
|`"/user/<username>/m/<subreddit>"`|Cycles through the most recent `hot` image posts from the specified multireddit.|
Expand Down Expand Up @@ -130,6 +131,18 @@ local:
|---|---|---|
|`"recurseLocalDirectories"`|`false`|Whether to recurse into subdirectories when looking for images.|

piwigo:

|Option|Description|
|---|---|
|`"piwigoUsername"` | A username to log into piwigo.|
|`"piwigoPassword"` | Password for the above user. |
|`"piwigoCategory"` | Piwigo category/album to take pictures from. Should be a numer as in the URL. |
|`"maximumEntries"` | How many images to loop through. Piwigo limits this to 500. |

*Note:*
* The URL is to the base of the piwigo gallery.

## Notifications

MMM-Wallpaper can react to the following notifications sent by other modules:
Expand Down
77 changes: 77 additions & 0 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ function b62decode(s) {
return result;
}

function parsePWGCookies(response) {
// It seems three cookies are returned. Two pwg_id cookies of which we need
// the last one, and pwg_remember which is set to deleted. We don't need
// that. So we will just get the last pwg_id cookie.
const cookies = response.headers.get('set-cookie');
const cookiestart = cookies.substring(cookies.lastIndexOf('pwg_id'));
return cookiestart.split(';')[0];
}

module.exports = NodeHelper.create({
start: function() {
var self = this;
Expand Down Expand Up @@ -171,6 +180,17 @@ module.exports = NodeHelper.create({
self.request(config, {
url: `https://api.nasa.gov/planetary/apod?api_key=${config.nasaApiKey}&start_date=${startDate}`,
});
} else if (source.startsWith("piwigo:")) {
self.piwigoState = "create_session";
self.request(config, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"cookie": '',
},
url: `${config.source.substring(7)}/ws.php?format=json`,
body: `method=pwg.session.login&username=${encodeURIComponent(config.piwigoUsername)}&password=${encodeURIComponent(config.piwigoPassword)}`
});
} else {
self.request(config, {
url: `https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=${config.maximumEntries}`,
Expand Down Expand Up @@ -282,6 +302,8 @@ module.exports = NodeHelper.create({
images = self.processNasaData(config, JSON.parse(body));
} else if ((source === "apod") || (source === "apodhd")) {
images = self.processApodData(config, JSON.parse(body));
} else if (source.startsWith("piwigo:")) {
images = self.processPiwigoData(config, body, response);
} else {
images = self.processBingData(config, JSON.parse(body));
}
Expand All @@ -308,6 +330,61 @@ module.exports = NodeHelper.create({
return images;
},

processPiwigoData: function(config, body, response) {
var self = this;
var cache_entry = self.getCacheEntry(config);
var api_url = `${config.source.substring(7)}/ws.php?format=json`
var images = [];
var data;
var cookies;
var caption;
var space;

if (response.status !== 200) {
console.error(`ERROR: ${response.status} -- ${body}`);
} else if (self.piwigoState === "create_session") {
data = JSON.parse(body);
cookies = parsePWGCookies(response);
if (data.stat = "ok") {
cache_entry.session_cookie = cookies;
self.piwigoState = "browse_item";
self.request(config, {
url: `${api_url}&method=pwg.categories.getImages&cat_id=${config.piwigoCategory}&per_page=${config.maximumEntries}&order=random`,
headers: {
"accept": '*/*',
"cookie": cookies
},
"method": 'GET',
});
} else {
console.log("Looks like piwigo login failed.");
}
} else {
data = JSON.parse(body)
for (var image of data.result.images) {
caption = "";
space="";
if ( image.name && !(image.name === image.file)) {
caption = caption + space + image.name;
space=" ";
}
if ( image.comment ) {
caption = caption + space + image.comment;
space=" ";
}
if ( image.date_creation ) {
caption = caption + space + image.date_creation;
space=" ";
}
images.push({
"url": `${image.derivatives.large.url}`,
"caption": caption,
});
}
}
return images;
},

processBingData: function(config, data) {
var self = this;
var width = (config.orientation === "vertical") ? 1080 : 1920;
Expand Down