forked from remy/html5demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweets.js
201 lines (169 loc) · 5.86 KB
/
tweets.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var html5demoTweets = (function (user, elm, status) {
var elm = document.querySelector(elm),
status = document.querySelector(status),
db = null,
showingTimeline = true,
latest = 0;
function initDb() {
status.innerHTML = 'initialising database';
try {
if (window.openDatabase) {
db = openDatabase("html5demos", "1.0", "HTML 5 Database API example", 200000);
if (db) {
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS tweets (id REAL UNIQUE, text TEXT, created_at TEXT, screen_name TEXT, mention BOOLEAN)", [], function (tx, result) {
clear();
html5demoTweets.timeline();
});
});
} else {
status.innerHTML = 'error occurred trying to open DB';
}
} else {
status.innerHTML = 'Web Databases not supported';
}
} catch (e) {
status.innerHTML = 'error occurred during DB init, Web Database supported?';
}
}
function load(mention, url) {
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM tweets WHERE mention = ? AND id > ? ORDER BY id DESC', [mention, latest], function (tx, results) {
var tweets = [], i, since_id = 0, script;
if (results.rows && results.rows.length) {
status.innerHTML = 'loading ' + (showingTimeline ? 'timeline' : 'mentions') + ' from DB';
for (i = 0; i < results.rows.length; i++) {
if (since_id == 0) {
since_id = results.rows.item(i).id;
}
tweets.push(results.rows.item(i));
}
show(tweets);
} else if (latest) {
since_id = latest;
}
if (since_id) {
url += '&since_id=' + since_id;
}
url += '&_cb=' + Math.random();
script = document.createElement('script');
script.src = url;
script.id = "twitterJSON";
document.body.appendChild(script);
}, function (tx) {
status.innerHTML = 'error occurred, please reset DB';
});
});
}
function show(tweets) {
if (tweets.length) {
// status.innerHTML = 'showing ' + tweets.length + ' tweets';
tweets = tweets.reverse();
var html = '', li;
for (var i = 0; i < tweets.length; i++) {
li = document.createElement('li');
li.innerHTML = ify.clean(tweets[i].text) + ' (<a href="http://twitter.com/' + tweets[i].screen_name + '/status/' + tweets[i].id + '">' + tweets[i].created_at + '</a>)';
if (elm.firstChild) {
elm.insertBefore(li, elm.firstChild);
} else {
elm.appendChild(li);
}
latest = tweets[i].id;
}
}
}
function clear() {
latest = 0;
elm.innerHTML = '';
}
return {
latest: function () {
return latest;
},
init: initDb,
timeline: function () {
status.innerHTML = 'loading timeline';
if (!showingTimeline) {
clear();
}
showingTimeline = true;
var url = 'http://twitter.com/statuses/user_timeline/' + encodeURIComponent(user) + '.json?callback=html5demoTweets.loadTweets';
load(false, url);
},
mentions: function () {
status.innerHTML = 'loading mentions';
if (showingTimeline) {
clear();
}
showingTimeline = false;
var url = 'http://search.twitter.com/search.json?rpp=20&callback=html5demoTweets.loadTweets&q=' + encodeURIComponent('@' + user);
load(true, url);
},
reset: function () {
status.innerHTML = 'resetting database';
db.transaction(function (tx) {
tx.executeSql('DROP TABLE IF EXISTS tweets', [], function () {
status.innerHTML = 'database has been cleared - please reload';
clear();
});
});
},
// public so the JSONP callback can hit it
loadTweets: function (tweets) {
var search = false;
document.body.removeChild(document.getElementById('twitterJSON'));
if (typeof tweets == 'string') {
// error occurred
return;
}
if (tweets.results) {
tweets = tweets.results;
search = true;
}
if (tweets.length) {
status.innerHTML = tweets.length + ' new tweets loaded';
db.transaction(function (tx) {
var i;
for (i = 0; i < tweets.length; i++) {
if (search) {
tweets[i].screen_name = tweets[i].from_user;
} else {
tweets[i].screen_name = tweets[i].user.screen_name;
}
tx.executeSql('INSERT INTO tweets (id, text, created_at, screen_name, mention) values (?, ?, ?, ?, ?)', [tweets[i].id, tweets[i].text, tweets[i].created_at, tweets[i].screen_name, search]);
}
show(tweets);
});
}
}
};
})('rem', '#tweets ol', '#status');
// twitter username, element with the list of tweets, status field
var ify = function() {
var entities = {
'"' : '"',
'&' : '&',
'<' : '<',
'>' : '>'
};
return {
"link": function(t) {
return t.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+[^\.,\)\s*$]/g, function(m) {
return '<a href="' + m + '">' + ((m.length > 25) ? m.substr(0, 24) + '...' : m) + '</a>';
});
},
"at": function(t) {
return t.replace(/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
return m1 + '@<a href="http://twitter.com/' + m2 + '">' + m2 + '</a>';
});
},
"hash": function(t) {
return t.replace(/(^|[^\w]+)\#([a-zA-Z0-9_]+)/g, function(m, m1, m2) {
return m1 + '#<a href="http://search.twitter.com/search?q=%23' + m2 + '">' + m2 + '</a>';
});
},
"clean": function(tweet) {
return this.hash(this.at(this.link(tweet)));
}
};
}();