| author | Riwad Salim <riwad.salim@yahoo.fr> |
| Tue, 27 Oct 2020 14:10:44 +0100 | |
| changeset 1532 | 6f6294d607cd |
| parent 1478 | adb28b75f2c7 |
| child 1557 | 7c67caaafdeb |
| permissions | -rw-r--r-- |
| 836 | 1 |
<?php |
2 |
||
3 |
if (isset ($_GET['callback'])) { |
|
4 |
header("Content-type: text/javascript"); |
|
5 |
} else { |
|
6 |
header("Content-type: application/json"); |
|
7 |
} |
|
8 |
||
9 |
||
10 |
include_once 'common.php'; |
|
|
1427
8b3d57a519eb
remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents:
919
diff
changeset
|
11 |
use Abraham\TwitterOAuth\TwitterOAuth; |
|
1478
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
12 |
use Stash\Pool; |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
13 |
use Stash\Driver\Sqlite; |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
14 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
15 |
$cacheDriver = new Stash\Driver\Sqlite(array('path' => sys_get_temp_dir()."/polemictweet_cache.db")); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
16 |
$cachePool = new Stash\Pool($cacheDriver); |
| 836 | 17 |
|
18 |
/** |
|
19 |
* Check for a POSTed status message to send to Twitter |
|
20 |
*/ |
|
21 |
if (!empty($_GET) |
|
22 |
&& isset($_SESSION['TWITTER_ACCESS_TOKEN'])) { |
|
|
1478
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
23 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
24 |
$itemCachePath="search/tweets?" . http_build_query($_GET); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
25 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
26 |
$cachedStatusesResp = $cachePool->getItem($itemCachePath); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
27 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
28 |
$statusesStr = $cachedStatusesResp->get(); |
| 836 | 29 |
|
|
1478
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
30 |
if ($cachedStatusesResp->isMiss()) { |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
31 |
$cachedStatusesResp->lock(); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
32 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
33 |
$token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
34 |
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
35 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
36 |
|
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
37 |
$statuses = $connection->get("search/tweets", $_GET); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
38 |
$statusesStr = json_encode($statuses); |
| 1474 | 39 |
|
|
1478
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
40 |
$cachedStatusesResp->set($statusesStr)->expiresAfter(3); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
41 |
$cachePool->save($cachedStatusesResp); |
|
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
42 |
} |
|
1427
8b3d57a519eb
remove dependency to Zend 1. Use composer to reduce the number of dependencies
ymh <ymh.work@gmail.com>
parents:
919
diff
changeset
|
43 |
|
|
1478
adb28b75f2c7
Use cache for tweeter request to avoid hitting rate limits
ymh <ymh.work@gmail.com>
parents:
1476
diff
changeset
|
44 |
echo($statusesStr); |
| 836 | 45 |
|
46 |
} else { |
|
47 |
/** |
|
48 |
* Mistaken request? Some malfeasant trying something? |
|
49 |
*/ |
|
50 |
exit('Invalid tweet request. Oops. Sorry.'); |
|
51 |
} |
|
52 |
?> |