forked from kapsolas/MMM-Instagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Instagram.js
140 lines (114 loc) · 4.16 KB
/
MMM-Instagram.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* global Module */
/* Magic Mirror
* Module: MMM-Instagram
*
* By Jim Kapsalis https://github.com/kapsolas
* MIT Licensed.
*/
Module.register('MMM-Instagram', {
defaults: {
format: 'json',
lang: 'en-us',
id: '',
animationSpeed: 1000,
updateInterval: 60000, // 10 minutes
access_token: '',
count: 200,
min_timestamp: 0,
loadingText: 'Loading...'
},
// Define required scripts
getScripts: function() {
return ["moment.js"];
},
/*
// Define required translations
getTranslations: function() {
return false;
},
*/
// Define start sequence
start: function() {
Log.info('Starting module: ' + this.name);
this.data.classes = 'bright medium';
this.loaded = false;
this.images = {};
this.activeItem = 0;
this.url = 'https://api.instagram.com/v1/users/self/media/recent' + this.getParams();
this.grabPhotos();
},
grabPhotos: function() {
// the notifications are not working for some reason... so we won't do anything asynchronously
// we will just make the call to the method to get the object with photo links....
//Log.info('sending socket notification: INSTAGRAM_GET and URL: ' + this.url);
this.sendSocketNotification("INSTAGRAM_GET", this.url);
// this may not be needed... need to think about it.
//setTimeout(this.grabPhotos, this.config.interval, this);
},
getStyles: function() {
return ['instagram.css', 'font-awesome.css'];
},
// Override the dom generator
getDom: function() {
var wrapper = document.createElement("div");
var imageDisplay = document.createElement('div'); //support for config.changeColor
if (!this.loaded) {
wrapper.innerHTML = this.config.loadingText;
return wrapper;
}
// set the first item in the list...
if (this.activeItem >= this.images.photo.length) {
this.activeItem = 0;
}
var tempimage = this.images.photo[this.activeItem];
// image
var imageLink = document.createElement('div');
//imageLink.innerHTML = "<img src='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'>";
imageLink.id = "MMM-Instagram-image";
imageLink.innerHTML = "<img src='" + tempimage.photolink + "'>";
imageDisplay.appendChild(imageLink);
wrapper.appendChild(imageDisplay);
return wrapper;
},
/* scheduleUpdateInterval()
* Schedule visual update.
*/
scheduleUpdateInterval: function() {
var self = this;
Log.info("Scheduled update interval set up...");
self.updateDom(self.config.animationSpeed);
setInterval(function() {
Log.info("incrementing the activeItem and refreshing");
self.activeItem++;
self.updateDom(self.config.animationSpeed);
}, this.config.updateInterval);
},
/*
* getParams()
* returns the query string required for the request to flickr to get the
* photo stream of the user requested
*/
getParams: function() {
var params = '?';
params += 'count=' + this.config.count;
params += '&min_timestamp=' + this.config.min_timestamp;
params += '&access_token=' + this.config.access_token;
return params;
},
// override socketNotificationReceived
socketNotificationReceived: function(notification, payload) {
//Log.info('socketNotificationReceived: ' + notification);
if (notification === 'INSTAGRAM_IMAGE_LIST')
{
//Log.info('received INSTAGRAM_IMAGE_LIST');
this.images = payload;
//Log.info("count: " + this.images.photo.length);
// we want to update the dom the first time and then schedule next updates
if (!this.loaded) {
this.updateDom(1000);
this.scheduleUpdateInterval();
}
this.loaded = true;
}
}
});