public function TweetFeed::pullDataFromFeed in Tweet Feed 8.3
Same name and namespace in other branches
- 4.x src/Controller/TweetFeed.php \Drupal\tweet_feed\Controller\TweetFeed::pullDataFromFeed()
Get Twitter Data
Pull data from the feed given our internal feed id. Our feed object also contains the information about the account associated with this feed (reference) so we have everything we need to connect via the Twitter API and retrieve the data.
Parameters
string feed_machine_name: The machine name of the feed with which we wish to procure the data
Return value
array tweets An array of all the tweets for this feed. FALSE if there are problems.
File
- src/
Controller/ TweetFeed.php, line 235
Class
- TweetFeed
- Class TweetFeed.
Namespace
Drupal\tweet_feed\ControllerCode
public function pullDataFromFeed($feed_machine_name) {
\Drupal::logger("tweet_feed")
->notice(dt('Beginning Twitter import of ') . $feed_machine_name);
/** Get a list of all the available feeds. */
$config = \Drupal::service('config.factory')
->getEditable('tweet_feed.twitter_feeds');
$feeds = $config
->get('feeds');
if (!empty($feeds[$feed_machine_name])) {
$feed = $feeds[$feed_machine_name];
/** I do not want to replicate this data in the settings for feeds so we will just */
/** assign it ad-hoc to the array here. */
$feed['machine_name'] = $feed_machine_name;
/** Get the account of the feed we are processing */
$accounts_config = \Drupal::service('config.factory')
->get('tweet_feed.twitter_accounts');
$accounts = $accounts_config
->get('accounts');
if (!empty($accounts[$feed['aid']])) {
$account = $accounts[$feed['aid']];
}
// If we have selected to clear our prior tweets for this particular feed, then we need
// to do that here.
if (!empty($feed['clear_prior'])) {
\Drupal::logger("tweet_feed")
->notice(dt('Clearing existing tweet entities'));
$query = new EntityFieldQuery();
$entities = $query
->entityCondition('entity_type', 'tweet_feed')
->condition('feed_machine_name', $feed_machine_name, '=')
->execute();
if (isset($entities['tweet_feed'])) {
foreach ($result['tweet_feed'] as $entity_id => $entity) {
$tweet = Drupal\tweet_feed\Entity\TweetEntity::load($entity_id);
$tweet
->delete();
}
}
}
// Build TwitterOAuth object with client credentials
$con = new TwitterOAuth2($account['consumer_key'], $account['consumer_secret'], $account['oauth_token'], $account['oauth_token_secret']);
// $since_id = NULL;
// $entities = \Drupal::entityQuery('tweet_entity')
// ->condition('feed_machine_name', $feed_machine_name, '=')
// ->sort('id', 'DESC')
// ->range(0, 1)
// ->execute();
// if (!empty($entities)) {
// $tweet_entity = new TweetEntity([], 'tweet_entity');
// $high_water_entity_id = array_pop($entities);
// $high_water_entity = $tweet_entity->load($high_water_entity_id);
// $since_id = $high_water_entity->getTweetId() + 1;
// }
// Get the number of tweets to pull from our list & variable init.
$tweets = [];
$tweet_count = 0;
$process = TRUE;
$number_to_get = $feed['pull_count'];
$params = $feed['query_type'] == 3 || $feed['query_type'] == 2 ? array(
'screen_name' => $feed['timeline_id'],
'count' => 100,
'tweet_mode' => 'extended',
) : array(
'q' => $feed['search_term'],
'count' => 100,
'tweet_mode' => 'extended',
);
// $max_id overrides $since_id
if (!empty($max_id)) {
$params['max_id'] = $max_id;
}
//\Drupal::logger("tweet_feed")->warning(dt('Since ID: ' . $since_id));
while ($process === TRUE) {
switch ($feed['query_type']) {
case 2:
$response = $con
->get("statuses/user_timeline", $params);
if (!empty($response)) {
if (empty($response->errors)) {
$tdata = $response;
}
}
else {
$process = FALSE;
}
break;
case 3:
$params = [
'slug' => $feed['list_name'],
'owner_screen_name' => $feed['timeline_id'],
'count' => 100,
'tweet_mode' => 'extended',
];
$response = $con
->get("lists/statuses", $params);
$tdata = json_decode($response);
break;
case 1:
default:
$tdata = json_decode($con
->get("search/tweets", $params));
if (empty($tdata->search_metadata->next_results)) {
//\Drupal::logger("tweet_feed")->error(dt('STOP PROCESSING: ' . __LINE__));
$process = FALSE;
}
break;
}
if (!empty($tdata)) {
if (!empty($tdata->errors)) {
foreach ($tdata->errors as $error) {
//\Drupal::logger("tweet_feed")->error(dt('STOP PROCESSING: ' . __LINE__));
$process = FALSE;
$tweets = [];
}
}
else {
if ($feed['query_type'] == 2 || $feed['query_type'] == 3) {
$end_of_the_line = array_pop($tdata);
array_unshift($tdata, $end_of_the_line);
$max_id = $end_of_the_line->id_str;
$tweet_data = $tdata;
}
else {
$tweet_data = $tdata->statuses;
}
// If this is FALSE, then we have hit an error and need to stop processing
if (isset($tweet_data['tweets']) && $tweet_data['tweets'] === FALSE) {
$process = FALSE;
break;
}
if (count($tweet_data) > 0) {
$duplicate = 0;
foreach ($tweet_data as $key => $tweet) {
if ($tweet_count >= $number_to_get) {
$process = FALSE;
continue;
}
$saved = $this
->saveTweet($tweet, $feed);
// If we have three duplicates in a row, assume we've reached the last imported
// tweets and stop here.
if ($saved == FALSE) {
$duplicate++;
if ($duplicate >= 3) {
$process = FALSE;
break 2;
}
continue;
}
else {
$duplicate = 0;
}
$tweet_count++;
if ($tweet_count % 50 == 0) {
\Drupal::logger("tweet_feed")
->notice(dt('Total Tweets Processed: ') . $tweet_count . dt('. Max to Import: ') . $number_to_get . ": {$tweet->id}");
}
}
}
else {
$process = FALSE;
}
}
}
if ($process == TRUE) {
$params['max_id'] = $max_id - 1;
}
}
\Drupal::logger("tweet_feed")
->notice(dt('Tweet Feed import of the feed: ') . $feed_machine_name . dt(' completed. ' . $tweet_count . ' Tweets imported.'));
}
}