-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-TweetsByTimelineOrList.js
180 lines (177 loc) · 6.69 KB
/
MMM-TweetsByTimelineOrList.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* global Module */
/* MMM-TwitterTrendsByPlace.js
*
* Magic Mirror
* Module: MMM-TwitterTrendsByPlace
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* Module MMM-TwitterTrendsByPlace By Adam Moses http://adammoses.com
*/
Module.register("MMM-TweetsByTimelineOrList", {
// setup the default config options
defaults: {
// required
consumer_key: null,
consumer_secret: null,
access_token_key: null,
access_token_secret: null,
screenName: null,
listToShow: 'TIMELINE',
// optional
tweetsToShowAtATime: 5,
onScreenRefreshRate: 25 * 1000,
tweetUpdateRefreshRate: 5 * 60 * 1000,
moduleWidth: "300px",
animationSpeed: 3 * 1000,
showHeader: false,
totalTweetsPerUpdate: 150,
excludeTweetsWithQuotes: true,
excludeRetweets: true,
excludeMediaTweets: false,
excludeLinkTweets: false,
excludeTweetLengthLessThan: 16,
excludeTweetsWithoutText: [ ],
maxTweetsPerUser: 1,
maxTweetAgeMins: 360,
allowSpecialCharacters: false,
displayColors: [ '#888', '#aaa', ],
},
// the start function
start: function() {
// log starting
Log.info("Starting module: " + this.name);
// check refresh rate is not faster than 1 minute
this.config.refreshRate = Math.max(60, this.config.tweetUpdateRefreshRate);
// set loaded, error, and the update to init values
this.loaded = false;
this.errorMessage = null;
this.tweetList = null;
this.tweetIndex = null;
this.config.identifier = this.identifier;
this.updateTimer = null;
// set the header to this place
if (this.config.showHeader) {
this.data.header = this.config.screenName + " - " + this.config.listToShow;
}
// if not missing required config fields start things up
if ( (this.config.consumer_key != null) && (this.config.consumer_secret != null)
&& (this.config.access_token_key != null) && (this.config.access_token_secret != null)
&& (this.config.screenName != null) ) {
// add this config to the helper functions
this.sendSocketNotification('TWEETS_REGISTER_CONFIG', this.config);
}
// otherwise set error message
else {
this.error = "Required config missing.";
}
},
// the socket handler
socketNotificationReceived: function(notification, payload) {
// if an update was received
if (notification === "TWEETS_UPDATE") {
// check this is for this module based on the woeid
if (payload.identifier === this.identifier)
{
// set loaded flag, set the update, and call update dom
this.tweetList = payload.tweetList;
this.tweetIndex = 0;
if (!this.loaded) {
this.loaded = true;
this.updateDom(this.config.animationSpeed);
var self = this;
this.updateTimer = setInterval(
function() { self.updateDom(self.config.animationSpeed); },
this.config.onScreenRefreshRate );
}
}
}
// if sent error notice
if (notification === "TWEETS_TOO_MANY_ERRORS") {
this.errorMessage = "There was an error.";
if (this.updateTimer !== null)
clearTimeout(this.updateTimer);
this.updateTimer = null;
this.updateDom();
}
},
// gathers the current set of tweets from the full list to display
getTweetsToShow: function() {
if (this.tweetList.length <= this.config.tweetsToShowAtATime)
return this.tweetList;
var indexStart = this.tweetIndex % this.tweetList.length;
var indexEnd = indexStart + this.config.tweetsToShowAtATime - 1;
var returnTweets = [ ];
if (indexEnd < this.tweetList.length) {
returnTweets = this.tweetList.slice(indexStart, indexEnd + 1);
}
else {
returnTweets = this.tweetList.slice(indexStart, this.tweetList.length);
var tweetsRemaining = this.config.tweetsToShowAtATime - returnTweets.length;
returnTweets = returnTweets.concat(this.tweetList.slice(0, tweetsRemaining));
}
this.tweetIndex += this.config.tweetsToShowAtATime;
return returnTweets;
},
// get the age of the tweet for display
getStringTimeDifference: function (theTimestamp) {
var nowTime = Date.now();
var thenTime = Date.parse(theTimestamp);
var calcTime = nowTime - thenTime;
var diffSecs = Math.round(calcTime / 1000);
if (diffSecs < 60) {
return diffSecs + "s";
}
if (diffSecs < (60 * 60)) {
var diffMins = Math.round(diffSecs / 60);
return diffMins + "m";
}
var diffHours = Math.round(diffSecs / (60 * 60));
return diffHours + "h";
},
// the get dom handler
getDom: function() {
// if an error, say so
if (this.errorMessage !== null) {
var wrapper = document.createElement("div");
wrapper.className = "small";
wrapper.innerHTML = this.errorMessage;
return wrapper;
}
// if nothing loaded yet, put in placeholder text
if (!this.loaded) {
var wrapper = document.createElement("div");
wrapper.className = "small";
wrapper.innerHTML = "Awaiting Update...";
return wrapper;
}
var wrapper = document.createElement("table");
var currentDisplayTweets = this.getTweetsToShow();
for (var cIndex = 0; cIndex < currentDisplayTweets.length; cIndex++){
var tweet = currentDisplayTweets[cIndex];
var colorValue = "color:" +
this.config.displayColors[cIndex % this.config.displayColors.length];
var tweetTR = document.createElement("tr");
var tweetTD = document.createElement("td");
tweetTD.align = "right";
tweetTD.className = "xsmall";
tweetTD.style = colorValue;
tweetTD.width = this.config.moduleWidth;
tweetTD.innerHTML = tweet.name + ' - ' + this.getStringTimeDifference(tweet.timestamp);
tweetTR.appendChild(tweetTD);
var usertimeTR = document.createElement("tr");
var usertimeTD = document.createElement("td");
usertimeTD.align = "left";
usertimeTD.className = "small";
usertimeTD.style = colorValue;
usertimeTD.width = this.config.moduleWidth;
usertimeTD.innerHTML = tweet.text;
usertimeTR.appendChild(usertimeTD);
wrapper.appendChild(usertimeTR);
wrapper.appendChild(tweetTR);
}
return wrapper;
}
});
// ------------ end -------------