Skip to content
This repository
Newer
Older
100644 80 lines (64 sloc) 1.961 kb
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
1 <?php
2
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
3 // Enable errors for easy debugging.
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
4 error_reporting(E_ALL);
5 ini_set('display_errors', '1');
6
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
7 // Configurable stuff.
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
8 define('URL', 'http://search.twitter.com/search.json');
9 define('SEARCH_PREFIX', '(ivegotmybluebeanieonnowwhat.com OR movethewebforward.com OR movethewebforward.org) AND ');
10 define('RPP', 100);
11
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
12 // Hashtags to search twitter for.
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
13 $queries = array(
14 "#learn",
15 "#ask4help",
16 "#helpothers",
17 "#feedback",
18 "#explore",
19 "#write",
20 "#filebugs",
21 "#hack"
22 );
23
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
24 // Don't let random scallywags run this script.
25 if ((isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] !== '127.0.0.1') || PHP_SAPI !== 'cli')
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
26 die('☹');
27
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
28 // Search twitter, return full json_decode()'d response.
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
29 function search($query, $page = 1) {
30 $url = URL . '?' . http_build_query(array(
31 'q' => SEARCH_PREFIX . $query,
32 'rpp' => RPP,
33 'page' => $page
34 ));
35
36 $ch = curl_init($url);
37 curl_setopt($ch, CURLOPT_NOBODY, 0);
38 curl_setopt($ch, CURLOPT_HEADER, 0);
39 curl_setopt($ch, CURLOPT_USERAGENT, 'movethewebforward.org');
40 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
41 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
42
43 $response = curl_exec($ch);
44 $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
45 curl_close($ch);
46
47 if ($status == 200) {
48 return json_decode($response);
49 }
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
50 else {
51 die("Twitter error: " . $status);
52 }
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
53 }
54
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
55 // Get all results for a query.
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
56 function getAll($query) {
57 $page = 0;
58 $avatars = array();
59
60 do {
61 $results = search($query, ++$page);
62
63 foreach ($results->results as $result) {
64 $avatars[$result->from_user] = $result->profile_image_url;
65 }
66 }
67 while (count($results->results) == RPP);
68
69 return $avatars;
70 }
71
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
72 // Merge new avatars into our file of existing avatars.
ba45f0dd »
2011-12-10 Use a PHP script (needs to run as a cron job) to save twitter avatars…
73 $avatars = json_decode(file_get_contents('avatars.json'));
74
75 foreach ($queries as $query) {
76 $avatars->$query = array_merge((array)$avatars->$query, getAll($query));
77 }
78
1d0ef9a8 »
2011-12-11 Use file_put_contents() because it's sexier and stuff. Improve check …
79 file_put_contents('avatars.json', json_encode($avatars));
Something went wrong with that request. Please try again.