This repository has been archived on 2025-10-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
drupal-meetups-twitterbot/src/Service/TweetFetcher.php

89 lines
2.3 KiB
PHP
Raw Normal View History

2019-01-10 14:04:34 +00:00
<?php
namespace App\Service;
2019-01-10 23:51:40 +00:00
use App\Model\Tweet;
2019-01-10 14:04:34 +00:00
use Codebird\Codebird;
use Tightenco\Collect\Support\Collection;
class TweetFetcher
{
/** @var \Codebird\Codebird */
private $codebird;
private $accounts = [
'drupalbristol',
'drupalsomerset',
'DrupalSurrey',
'DrupalWLondon',
'DrupalYorkshire',
'nwdug',
'opdavies',
'OxDUG',
'swdug',
];
private $hashtags = [
'drupalmeetups',
'drupalmeetup',
];
public function __construct(Codebird $codebird)
{
$codebird::setConsumerKey(
getenv('TWITTER_CONSUMER_KEY'),
getenv('TWITTER_CONSUMER_SECRET')
);
$codebird = $codebird::getInstance();
$codebird->setToken(
getenv('TWITTER_ACCESS_TOKEN'),
getenv('TWITTER_ACCESS_SECRET')
);
$this->codebird = $codebird;
}
public function getTweets(): Collection
{
$response = collect($this->codebird->search_tweets([
'q' => collect($this->params()->all())->implode(' AND '),
// 'since_id' => $this->lastTweetId,
]));
if ($response->get('httpstatus') != 200) {
dump($response);
}
return collect($response->get('statuses'))
2019-01-10 23:51:40 +00:00
->map(function (\stdClass $status) {
return tap(new Tweet(), function (Tweet $tweet) use ($status) {
$tweet->setId($status->id);
$tweet->setText($status->text);
$tweet->setCreated(strtotime($status->created_at));
$tweet->setAuthor($status->user->screen_name);
});
2019-01-10 14:04:34 +00:00
})->reverse();
}
private function params(): Collection
{
return tap(collect(), function (Collection $params) {
// Add account names.
$params->push(
collect($this->accounts)->map(function (string $account) {
return "from:{$account}";
})->implode(' OR ')
);
// Add hashtags.
$params->push(
collect($this->hashtags)->map(function (string $hashtag) {
return "#{$hashtag}";
})->implode(' OR ')
);
});
}
}