Build a daily digest across platforms
A single cron script that pulls the day's best of everything into one list.
PHP
<?php
$key = getenv('SOCIALCRAWL_API_KEY');
function sc(string $path): array {
$ch = curl_init("http://datatrend.nl" . $path);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['x-api-key: ' . getenv('SOCIALCRAWL_API_KEY')],
]);
return json_decode(curl_exec($ch), true) ?? [];
}
$digest = [];
foreach ([
'/v1/hackernews/top',
'/v1/reddit/subreddit?name=programming',
] as $path) {
$body = sc($path);
foreach (array_slice($body['data']['items'] ?? [], 0, 5) as $item) {
$digest[] = [
'platform' => $body['platform'],
'likes' => $item['post']['engagement']['likes'] ?? 0,
'title' => mb_substr((string)$item['post']['content']['text'], 0, 80),
'url' => $item['post']['url'],
];
}
}
usort($digest, fn($a, $b) => $b['likes'] <=> $a['likes']);
foreach ($digest as $d) {
printf("%-12s %6d %s\n", $d['platform'], $d['likes'], $d['title']);
}
You'll need a key. It takes ten seconds and starts with 100 credits.
Get a key