-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (118 loc) · 5.27 KB
/
index.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
// Generated by CoffeeScript 1.8.0
(function() {
var TwitterFetcher, jsonp;
jsonp = require('jsonp-promise');
/*
* Twitter Post Fetcher v10.0
* Coded by Jason Mayes 2013. A present to all the developers out there.
* www.jasonmayes.com
* Please keep this disclaimer with my code if you use it. Thanks. :-)
* Got feedback or questions, ask here:
* http://www.jasonmayes.com/projects/twitterApi/
*/
TwitterFetcher = (function() {
function TwitterFetcher() {}
/**
* @param {string} text
* @return {string}
*/
TwitterFetcher.prototype.stripGarbage = function(text) {
return text.replace(/<b[^>]*>(.*?)<\/b>/g, function(dataAndEvents, match) {
return match;
}).replace(/(?:class|data-[a-z-]+|rel|target)=".*?"/g, '');
};
/**
* @param {String} id - Your Twitter widget ID.
* @return {Promise} Promise for the raw JSON
*/
TwitterFetcher.prototype.fetchRaw = function(id) {
return jsonp("//cdn.syndication.twimg.com/widgets/timelines/" + id + "?&lang=en&suppress_response_codes=true&rnd=" + (Math.random())).then((function(_this) {
return function(data) {
var authorHTML, avatarHTML, result, tweet, tweetElement, tweets, _i, _len, _ref;
result = document.createElement('div');
result.innerHTML = data.body;
tweets = [];
_ref = result.getElementsByClassName('tweet');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
tweetElement = _ref[_i];
tweet = {
id: tweetElement.getAttribute('data-tweet-id'),
isRetweet: 0 < tweetElement.getElementsByClassName('retweet-credit').length,
content: _this.stripGarbage(tweetElement.getElementsByClassName('e-entry-title')[0].innerHTML),
time: new Date(tweetElement.getElementsByClassName('dt-updated')[0].getAttribute('datetime').replace(/-/g, '/').replace('T', ' ').split('+')[0])
};
authorHTML = tweetElement.getElementsByClassName('p-author')[0];
tweet.authorURL = authorHTML.getElementsByClassName('u-url')[0].getAttribute('href');
tweet.author = tweet.authorURL.match(/[^/]+$/)[0];
tweet.authorFullName = authorHTML.getElementsByClassName('full-name')[0].textContent.trim();
avatarHTML = authorHTML.getElementsByClassName('u-photo')[0];
tweet.authorAvatar = "<img src=\"" + (avatarHTML.getAttribute('src')) + "\" data-src-2x=\"" + (avatarHTML.getAttribute('data-src-2x')) + "\">";
tweets.push(tweet);
}
return tweets;
};
})(this));
};
/**
* @param {String} id - Your Twitter widget ID.
* @param {Int} [opts.maxTweets=20] - The maximum number of tweets you want
* returned. Must be a number between 0 and 20.
* @param {Boolean} [opts.showUser=true] - Set false if you don't want user
* photo / name for tweet to show.
* @param {Boolean} [opts.showTime=true] - Set false if you don't want time of
* tweet to show.
* @param {Boolean} [opts.showRt=true] - Show retweets or not. Set false to
* not show.
* @param {Boolean} [opts.showInteraction=true] Show links for reply, retweet,
* favorite.
* @return {Promise} Promise for the HTML
*/
TwitterFetcher.prototype.fetch = function(id, opts) {
if (opts.maxTweets == null) {
opts.maxTweets = 20;
}
if (opts.showUser == null) {
opts.showUser = true;
}
if (opts.showTime == null) {
opts.showTime = true;
}
if (opts.showRt == null) {
opts.showRt = true;
}
if (opts.showInteraction == null) {
opts.showInteraction = true;
}
return this.fetchRaw(id).then(this.formatData.bind(this, opts))["catch"](function(e) {
return console.error(e);
});
};
/**
* @param {Array} data
* @return {undefined}
*/
TwitterFetcher.prototype.formatData = function(opts, tweets) {
var result, tweet, _i, _len;
tweets = tweets.slice(0, opts.maxTweets);
result = '';
for (_i = 0, _len = tweets.length; _i < _len; _i++) {
tweet = tweets[_i];
result += '<li>';
if (opts.showUser) {
result += "<div class=\"user\">\n <a href=\"" + tweet.authorURL + "\">\n " + tweet.authorAvatar + "\n <span class=\"full-name\">" + tweet.authorFullName + "</span>\n <span class=\"screen-name\">@" + tweet.author + "</span>\n </a>\n</div>";
}
result += "<p class=\"tweet\">" + tweet.content + "</p>";
if (opts.showTime) {
result += "<p class=\"timePosted\">" + tweet.time + "</p>";
}
if (opts.showInteraction) {
result += "<p class=\"interact\">\n <a href=\"https://twitter.com/intent/tweet?in_reply_to=" + tweet.id + "\" class=\"twitter_reply_icon\">Reply</a>\n <a href=\"https://twitter.com/intent/retweet?tweet_id=" + tweet.id + "\" class=\"twitter_retweet_icon\">Retweet</a>\n <a href=\"https://twitter.com/intent/favorite?tweet_id=" + tweet.id + "\" class=\"twitter_fav_icon\">Favorite</a>\n</p>";
}
result += '</li>';
}
return "<ul>" + result + "</ul>";
};
return TwitterFetcher;
})();
module.exports = TwitterFetcher;
}).call(this);