forked from remy/twivatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
225 lines (189 loc) · 6.96 KB
/
index.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
function grab_url($url) {
$c = curl_init();
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $c, CURLOPT_URL, $url );
// 500ms timeout - should be quick enough, tests show around 300ms-450ms
curl_setopt( $c, CURLOPT_CONNECTTIMEOUT_MS, 500 );
// 1 second timeout for the entire transaction
curl_setopt( $c, CURLOPT_TIMEOUT, 1 );
$res = curl_exec( $c );
return $res ? $res : '';
}
function grab_and_store($user, $db) {
include('twitter_auth.php');
if (!@$twitter_auth) {
$twitter_auth = '';
}
$user_profile = json_decode(grab_url('http://' . $twitter_auth . 'twitter.com/users/' . $user . '.json'));
if (!$user_profile) {
return "http://static.twitter.com/images/default_profile_bigger.png";
} else {
$image_url = $user_profile->profile_image_url;
if ($db) {
$sql = sprintf('replace into twitter_avatar (user, url) values ("%s", "%s")', mysql_real_escape_string($user), $image_url);
mysql_query($sql, $db);
}
return $image_url;
}
}
function head($image_url) {
$c = curl_init();
// TODO should we include a connection & read timeout here too?
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' );
curl_setopt( $c, CURLOPT_HEADER, 1 );
curl_setopt( $c, CURLOPT_NOBODY, true );
curl_setopt( $c, CURLOPT_URL, $image_url );
$res = curl_exec( $c );
if (preg_match('@HTTP/1.1 404 Not Found@', $res)) {
return false;
} else {
return true;
}
}
function size_image($image_url, $size) {
if ($size == 'original') {
$image_url = preg_replace('/_normal\./', '.', $image_url);
} else if ($size != 'normal') {
$image_url = preg_replace('/_normal\./', '_' . $size . '.', $image_url);
}
return $image_url;
}
function redirect($image_url, $size, $db) {
$image_url = size_image($image_url, $size);
if ($db) {
mysql_close($db);
}
header('location: ' . $image_url);
}
$user = strtolower(@$_GET['user']);
$size = strtolower(isset($_GET['size']) && in_array(strtolower($_GET['size']), array('mini', 'bigger', 'normal', 'original')) ? $_GET['size'] : 'normal');
$db = null;
$result = null;
// skipping DB to save some performance from my own box, if you host this yourself, set to true
$use_db = false;
if ($user) {
// use in case of emergencies: skips twitter entirly
if (false) {
redirect("http://static.twitter.com/images/default_profile_bigger.png", $size, false);
exit;
}
// connect to DB
if ($use_db) {
$db = mysql_connect('localhost', 'root');
mysql_select_db('twivatar', $db);
$result = mysql_query(sprintf('select url from twitter_avatar where user="%s"', mysql_real_escape_string($user)), $db);
}
if (!$result || mysql_num_rows($result) == 0) {
// grab and store - then redirect
$image_url = grab_and_store($user, $db);
redirect($image_url, $size, $db);
} else if (mysql_num_rows($result) > 0) {
// test if URL is available - then redirect
$row = mysql_fetch_object($result);
// if the url returned is one of Twitter's O_o static ones, then do a grab
if (!preg_match('/static\.twitter\.com/', $row->url) && head($row->url)) {
redirect($row->url, $size, $db);
} else { // else grab and store - then redirect
$image_url = grab_and_store($user, $db);
redirect($image_url, $size, $db);
}
}
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<title>Twivatar - Twitter Avatar API</title>
<style>
body {
font: normal 16px/20px Helvetica, sans-serif;
background: rgb(237, 237, 236);
margin: 0;
margin-top: 40px;
padding: 0;
}
section, header, footer {
display: block;
}
#wrapper {
width: 600px;
margin: 0 auto;
background: #fff url(images/shade.jpg) repeat-x center bottom;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-top: 1px solid #fff;
padding-bottom: 76px;
}
h1 {
padding-top: 10px;
}
h2 {
font-size: 100%;
font-style: italic;
}
header,
article > p,
article > h3,
article > code,
footer a {
margin: 20px;
}
footer a {
margin: 20px;
color: #999;
}
footer a:hover:after {
content: '...quickly';
}
</style>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<section id="wrapper">
<header>
<h1>Twivatar</h1>
<h2>Twitter Avatar API</h2>
</header>
<article>
<p>Twivatar is a <a href="http://en.wikipedia.org/wiki/REST" title="Rest - Wikipedia, the free encyclopedia">RESTful</a> API to a Twitter user's avatar built out of frustration of external Twitter apps breaking when the avatar url is stored, and then changed by that user later on Twitter - the result is a broken image on that app unless they constantly check for profile changes.</p>
<h3>Usage</h3>
<code><img src="http://twivatar.org/[<em>screen_name</em>]" /></code>
<p>Alternatively you can specify the size image you want from:</p>
<ul>
<li>mini (24x24)</li>
<li>normal (48x48 - default)</li>
<li>bigger (73x73)</li>
<li>original</li>
</ul>
<code><img src="http://twivatar.org/[<em>screen_name</em>]/[<em>size</em>]" /></code>
<h3>Behind the scenes</h3>
<p>This is a simple one script app that stores the url of the avatar. When the avatar is requested for <em>x</em> user, it runs the following logic:</p>
<ol>
<li>Get the stored avatar url</li>
<li>If there's no record, go to Twitter and pull the profile_image_url</li>
<li>If a record is found, perform a <a href="http://en.wikipedia.org/wiki/HTTP%23Request_methods" title="Wikipedia Entry: HTTP#Request methods">HEAD</a> request to test the avatar url</li>
<li>Finally use a <code>location</code> redirect to the avatar url</li>
</ol>
<p>All the code is available on <a href="http://github.com/remy/twivatar/">GitHub</a>, so feel free to fork and contribute.</p>
<h3>Todo</h3>
<p>I'd like to upgrade the entire app to read the ETags from S3 (or some kind of cache control), and send them back to the client, so that the browser uses it's local cache if the avatar is available and up to date.</p>
</article>
<footer><a href="http://twitter.com/rem">@rem built this</a></footer>
</section>
<script>
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script>
try {
var pageTracker = _gat._getTracker("UA-1656750-17");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>