-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
197 lines (183 loc) · 6.64 KB
/
index.html
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
<!-- Originally taken from http://awardwinningfjords.com/2011/12/27/emberjs-collections.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Twitter Outreach : by @atmos</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {background: #333; font-size: 64px; color: #999; margin: 0; padding: 0}
h1 { margin: 0; border-bottom: 1px solid #000; padding-left: 15px; font-size: 64px; }
ul { margin-top: 5px; padding-left: 0; }
img { border: 1px solid #fff; }
.tweet {font-size: 38px; }
.time-ago {padding: 0px; line-height: 64px; font-size: 48px;}
.twitter-posts {padding: 10px; margin-left: 5px; list-style-position: inside; list-style-type:none; border-bottom: 1px solid #000;}
.twitter-posts a {text-decoration:none; color: #FFF;}
li.twitter-posts {margin-left: 0; }
</style>
<script src="https://github.com/themes/minimal/javascripts/scale.fix.js"></script>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="js/twitter-text.min.js"></script>
<script src="js/ember-0.9.5.min.js"></script>
<script src="js/ember/data.js"></script>
<script src="js/ember/localStorage.js"></script>
</head>
<body>
<script type="text/x-handlebars">
<h1>On {{Twitter.account}}...</h1>
<div>
<ul>
{{#each Twitter.searchResults}}
<li class="twitter-posts">
<a href="{{unbound userLink}}" class="profile" target="tab">
<img src="{{unbound avatar}}" alt="{{unbound from_user}}" />
</a>
<a href="{{unbound link}}" target="tab">@{{from_user}}({{followers}})</a>
<span class="time-ago" data-created-at="{{unbound createdAtEpoch}}">said about {{timeAgoInWords}} ago.</span>
<div class="tweet">{{{tweetText}}}</div>
</li>
{{/each}}
</ul>
</div>
</script>
<script>
// Nest all sorts of crazy shit off of this
Twitter = Em.Application.create({
ready: function() {
var self = this
setInterval(function() {
Twitter.searchResults.refresh()
Twitter.refreshTimestamps()
}, Twitter.get("refreshRate"))
Twitter.searchResults.set("query", self.get("account"))
this._super()
},
params: function() {
var qs = window.location.search.replace('?', '')
, pairs = qs.split('&')
, nvpair = {}
$.each(pairs, function(i, v){
var pair = v.split('=')
nvpair[pair[0]] = pair[1]
})
return nvpair
}.property(),
account: function() {
return(this.get("params")['q'] || '@GitHub')
}.property(),
refreshRate: function() {
var rate = this.get("params")['r'] || 60
if(rate < 30) { rate = 30 }
return(rate * 1000)
}.property(),
minFollowers: function() {
var rate = this.get("params")['c'] || 500
if(rate < 10) { rate = 10 }
return(rate)
}.property(),
timeAgoInWords: function(timestamp) {
var now = (new Date).getTime()
, result = null
if(timestamp.match(/\d+/)) {
var dt = Math.round(parseInt((now / 1000) - timestamp)/60)
if(dt == 0) { result = "less than a minute" }
else if(dt == 1) { result = "1 minute" }
else if(dt >= 2 && dt <= 45) { result = dt + ' minutes' }
else if(dt >= 46 && dt <= 90) { result = '1 hour' }
else if(dt >= 90 && dt <= 1440) { result = Math.round(parseFloat(dt)/60.0) + ' hours' }
else if(dt >= 1440 && dt <= 2880) { result = '1 day' }
else { result = Math.round(parseInt(dt)/1440) + ' days' }
}
return(result)
},
refreshTimestamps: function() {
$('span.time-ago').toArray().forEach(function(el) {
var result = Twitter.timeAgoInWords($(el).attr('data-created-at'))
, content = $(el).html().replace(/.*? ago/, 'said about ' + result + ' ago')
$(el).html(content)
})
}
})
// Find a user or look up their follower count from the API
Twitter.User = Em.Object.extend({ })
Twitter.User.findByName = function(name, cb) {
var url = "http://outreach.herokuapp.com/users/" + name
, user = null
$.getJSON(url, function(profile) {
user = Twitter.User.create(profile)
cb(null, user)
})
}
// Seems verbose
Twitter.Tweet = Em.Object.extend({
link: function() {
return(this.get("userLink") + "/status/" + this.get("id_str"))
}.property(),
userLink: function() {
return "https://twitter.com/" + this.get("from_user")
}.property(),
tweetText: function() {
return twttr.txt.autoLink(this.get("text"))
}.property(),
user: function(cb) {
return this.get("user")
}.property(),
followers: function() {
return this.get("user").get("followers_count")
}.property(),
avatar: function() {
return this.get("user").get("profile_image_url")
}.property(),
createdAtEpoch: function() {
return(new Date(this.get("created_at")).getTime() / 1000)
}.property(),
timeAgoInWords: function() {
return(Twitter.timeAgoInWords(this.get("createdAtEpoch").toString()))
}.property()
})
Twitter.Tweet.reopenClass({
createTweet: function(tweetData, cb) {
Twitter.User.findByName(tweetData.from_user, function(err, userData) {
tweetData.user = userData
cb(err, Twitter.Tweet.create(tweetData))
})
}
})
// Results from twitter
Twitter.searchResults = Em.ArrayController.create({
query: null, // What gets searched for on each refresh
content: [], // An unpredicitably ordered array
_idCache: {}, // id based mapping to avoid dupes
addTweet: function(tweet) {
var id = tweet.get("id")
if (typeof this._idCache[id] === "undefined") {
if(tweet.get("followers") > Twitter.get("minFollowers")) {
this.unshiftObject(tweet)
}
this._idCache[id] = tweet.id
}
},
refresh: function() {
var self = this
, query = this.get("query")
console.log("Searching for " + query)
var url = "http://search.twitter.com/search.json?limit=10000&rpp=100&q=" + query + "&callback=?"
$.getJSON(url, function(searches) {
if(searches.results !== undefined) {
for (i = searches.results.length - 1; i > 0; i--) {
Twitter.Tweet.createTweet(searches.results[i], function(err, tweet) {
self.addTweet(tweet)
})
}
} else {
console.log("No results for \"" + query + "\" on page 1")
}
})
}.observes("query")
})
</script>
</body>
</html>