Skip to content

Commit

Permalink
Merge pull request #48 from adi-miller/date_label
Browse files Browse the repository at this point in the history
Added the ability to show a date label
  • Loading branch information
eouia authored Jan 19, 2020
2 parents 0624701 + f50d2fb commit c052a46
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
17 changes: 16 additions & 1 deletion MMM-GooglePhotos.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,28 @@ Module.register("MMM-GooglePhotos", {
wrapper.style.minWidth = this.config.showWidth
wrapper.style.minHeight = this.config.showHeight
wrapper.style.backgroundSize = this.config.mode

if (this.config.showDateLabel === true) {
var creationDate = document.createElement("span")
creationDate.id = "GPDATE"
creationDate.style.position = "absolute"
creationDate.style.bottom = "40px"
creationDate.style.left = "40px"
wrapper.appendChild(creationDate);
}

return wrapper
},

showImage: function(payload) {
var url = payload.url
var image = document.getElementById("GPHOTO")

image.style.opacity = 0
setTimeout(()=>{
image.style.backgroundImage = "unset"
image.style.backgroundImage = "url('" + url + "')"
image.style.opacity = this.config.opacity;
image.style.opacity = this.config.opacity
if (this.config.mode == "hybrid") {
var rect = image.getBoundingClientRect()
var rr = ((rect.width / rect.height) > 1) ? "h" : "v"
Expand All @@ -50,6 +61,10 @@ Module.register("MMM-GooglePhotos", {
} else {
image.style.backgroundSize = this.config.mode
}
if (this.config.showDateLabel === true) {
var creationDate = document.getElementById("GPDATE")
creationDate.innerText = payload.time
}
}, 2000)

},
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,21 @@ travel to paris : AGj1epU5VMNoBGK9GDX3k_zDQaPT16Fe56o0N93eN6aXn-f21M98
module: "MMM-GooglePhotos",
position: "top_right",
config: {
albumId: ["YOUR_GOOGLE_PHOTOS_ALBUM_ID","NEXT_GOOGLE_PHOTOS_ALBUM_ID"], // your album id(s) from result of `auth_and_test.js`
refreshInterval: 1000*60,
scanInterval: 1000*60*10, // too many scans might cause API quota limit also.

albumId: ["ALBUM_ID1", "ALBUM_ID2"], // your album id(s) from result of `auth_and_test.js`
refreshInterval: 1000*60, // Number of milliseconds before showing a different photo
scanInterval: 1000*60*10, // too many scans might cause API quota limit also
//note(2018-07-29). It is some weird. API documents said temporal image url would live for 1 hour, but it might be broken shorter. So, per 10 min scanning could prevent dead url.

sort: "time", //'time', 'reverse', 'random'
showWidth: "800px", // how large the photo will be shown as. (e.g;'100%' for fullscreen)
sort: "time", //'time', 'reverse', 'random'
showWidth: "800px", // how large the photo will be shown as. (e.g;'100%' for fullscreen)
showHeight: "600px",
originalWidthPx: 800, // original size of loaded image. (related with image quality)
originalHeightPx: 600, // Bigger size gives you better quality, but can give you network burden.
opacity: 1, // target "opacity" property (https://www.w3schools.com/cssref/css3_pr_opacity.asp)
mode: "hybrid", // "cover" or "contain" (https://www.w3schools.com/cssref/css3_pr_background-size.asp)
//ADDED. "hybrid" : if you set as "hybrid" it will change "cover" and "contain" automatically by aspect ratio.
originalWidthPx: 800, // original size of loaded image. (related with image quality)
originalHeightPx: 600, // Bigger size gives you better quality, but can give you network burden
opacity: 1, // target "opacity" property (https://www.w3schools.com/cssref/css3_pr_opacity.asp)
mode: "hybrid", // "cover" or "contain" (https://www.w3schools.com/cssref/css3_pr_background-size.asp)
// "hybrid": will change "cover" and "contain" automatically based on aspect ratio
showDateLabel: true, // If True, shows a label of how long ago the photo was taken (e.g. 2 years ago, 7 days ago, etc...)
}
},
```
Expand Down
33 changes: 30 additions & 3 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = NodeHelper.create({
//this.auth = null
},

initializeAfterLoading: function (config) {
initializeAfterLoading: function(config) {
if (config.scanInterval > 1000 * 60 * 10) {
config.scanInterval = 1000 * 60 * 10 // By using baseUrl from :search directly, there needs to maintain scanInterval under 1hour because risk of expiration of baseUrl.
}
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = NodeHelper.create({

},

socketNotificationReceived: function (notification, payload) {
socketNotificationReceived: function(notification, payload) {
switch(notification) {
case 'INIT':
this.initializeAfterLoading(payload)
Expand Down Expand Up @@ -194,6 +194,33 @@ module.exports = NodeHelper.create({
})
},
*/
timeSince: function(date) {
// Credit: https://stackoverflow.com/users/242897/sky-sanders
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);

if (interval > 1) {
return interval + " years ago";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months ago";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days ago";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours ago";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes ago";
}
return Math.floor(seconds) + " seconds ago";
},

getPhoto: function() {
if (this.items.length <= 0) {
console.log("There is no scanned photo currently.")
Expand All @@ -212,7 +239,7 @@ module.exports = NodeHelper.create({
var payload = {
"id":photo.id,
"url":photo.baseUrl + "=w" + this.config.originalWidthPx + "-h" + this.config.originalHeightPx,
"time": Date.parse(photo.creationTime),
"time": this.timeSince(photo.creationTime),
"width": photo.width,
"height": photo.height
}
Expand Down

0 comments on commit c052a46

Please sign in to comment.