This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
166 lines (150 loc) · 5.46 KB
/
script.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
if (!thinkup_api_url) {
thinkup_api_url = installation_url + 'api/v1/post.php';
}
var currentDate = new Date();
// TODO Use Date objects and just convert them to strings when required
// TODO Create Date() to Thinkup date style string
var untilDate = (currentDate.getFullYear()+"-"+(currentDate.getMonth()+1)+"-"+currentDate.getDate()+" "+currentDate.getHours()+":"+currentDate.getMinutes()+":"+currentDate.getSeconds());
var fromDate = (currentDate.getFullYear()+"-"+(currentDate.getMonth()+1)+"-"+(currentDate.getDate())+" 00:00:00");
// Document ready
$(document).ready(function() {
// Load the timeline
load_day();
}); // end of $(document).ready
function load_day() {
fetch_timeline();
fetch_mentions();
return true;
};
function parse_timeline(record) {
// Add the update to the document output
var placeholder = document.createElement('div');
placeholder.className = 'placeholder';
var conversation = document.createElement('div');
conversation.className = 'conversation';
var update = document.createElement('div');
update.className = 'update';
var updateTime = (new Date(record.created_at)).toLocaleTimeString();
// TODO create function to build the profile_image block
$(update).append('<img src="'+record.user.profile_image_url+'" width=36 height=36 class="profile_image" title="@'+record.user.screen_name+'">');
$(update).append('<p class="update_text">'+filter_text(record.text)+'</p>');
$(conversation).append(update);
// Check if there's a url associated with it.
if(record.links) {
var links = record.links;
for (var i=0; i < links.length; i++) {
process_links(update, links[i]);
};
}
// Check if there are replies
if(record.replies) {
$.each(record.replies, function(i, record){
var reply = document.createElement('div');
reply.className = 'reply';
$(reply).append('<p class="reply_text">'+filter_text(record.text)+'</p>');
$(reply).append('<p class="reply_screen_name">'+filter_text('@'+record.user.screen_name)+'</p>')
$(reply).append('<a href="http://www.twitter.com/'+record.user.screen_name+'">'+'<img src="'+record.user.profile_image_url+'" width=36 height=36 class="profile_image_reply" title="@'+record.user.screen_name+'"></a>');
$(conversation).append(reply);
// Check if there's a url associated with it.
if(record.links) {
var links = record.links;
for (var i=0; i < links.length; i++) {
process_links(reply, links[i]);
};
}
});
}
$(placeholder).append('<div class="updateTime">'+updateTime+'</div>');
$(placeholder).append(conversation);
$("#timeline").append(placeholder);
};
function fetch_timeline() {
$.getJSON(thinkup_api_url,
{
type: "user_posts_in_range",
include_replies: 1,
username: twitterUser,
from: fromDate,
until: untilDate
},
function(data) {
for (var i = 0; i < data.length; i++) {
record = data[i];
parse_timeline(record);
};
}
);
};
function process_links(update, link) {
$.embedly(
link.expanded_url,
{
maxWidth: 300,
maxHeight: 400,
key: embedly_key,
success: function(oembed, dict){
// test the contents returned
if(oembed.html) {
$(update).append("<br/>"+oembed.html);
}
else {
if(oembed.title) {
$(update).append('<br/><a href="'+oembed.url+'">'+oembed.title+'</a><br/>');
}
if(oembed.description) {
$(update).append('<p class="link_description">'+oembed.description+"</p>");
}
if(oembed.thumbnail_url) {
// Thumbnails aren't subject to the maxWidth parameter in the embedly call above so scale the image
var scaledWidth = oembed.thumbnail_width;
var scaledHeight = oembed.thumbnail_height;
if(oembed.thumbnail_width > 300) {
scaledWidth = 300;
scaledHeight = oembed.thumbnail_height*(300/oembed.thumbnail_width);
}
$(update).append('<img src="'+oembed.thumbnail_url+'" width='+scaledWidth+' height='+scaledHeight+'>');
}
}
}
});
}
function fetch_mentions() {
$.getJSON(thinkup_api_url,
{
type: "user_mentions",
username: "tisJames",
count: 40,
include_rts: 1,
include_replies: 0,
page: 1
},
function(data) {
for (var i = 0; i < data.length; i++) {
record = data[i];
parse_mentions(record);
};
}
);
};
function parse_mentions(record) {
var updateTime = (new Date(record.created_at)).toLocaleTimeString();
// Want freestanding mentions; replies are caught in the main timeline
if(!record.in_reply_to_post_id && ((new Date(record.created_at)) > (new Date(fromDate)))) {
var mention = document.createElement('div');
mention.className = 'mention';
var placeholder = document.createElement('div');
placeholder.className = 'placeholder';
$(placeholder).append('<div class="updateTime">'+updateTime+'</div>');
$(mention).append('<p class="reply_text">'+record.text+'</p>');
$(mention).append('<p class="reply_screen_name">@'+record.user.screen_name+'</p>')
$(mention).append('<img src="'+record.user.profile_image_url+'" width=36 height=36 class="profile_image_reply" title="@'+record.user.screen_name+'">');
$(placeholder).append(mention);
}
$("#mentions").append(placeholder);
};
function filter_text(input) {
input = input.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"<a href='$1'>$1</a>");
input = input.replace(/(^|\s)@(\w+)/g, "$1<a href='http://www.twitter.com/$2'>@$2</a>");
input = input.replace(/(^|\s)#(\w+)/g, "$1<a href='http://search.twitter.com/search?q=%23$2'>#$2</a>");
return input;
};