public function TweetFeed::pullDataFromFeed in Tweet Feed 4.x
Same name and namespace in other branches
- 8.3 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
File
- src/
Controller/ TweetFeed.php, line 269
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'));
$entities = \Drupal::entityQuery('tweet_entity')
->condition('feed_machine_name', $feed_machine_name, '=')
->execute();
if (isset($entities)) {
foreach ($entities as $entity_id) {
$entity = \Drupal::entityTypeManager()
->getStorage('tweet_entity')
->load($entity_id);
$entity
->deleteLinkedImages();
$entity
->deleteProfileImage();
$entity
->delete();
}
}
}
// Build TwitterOAuth object with client credentials
$con = new TwitterOAuth2($account['consumer_key'], $account['consumer_secret'], $account['oauth_token'], $account['oauth_token_secret']);
// Get the number of tweets to pull from our list & variable init.
$tweets = [];
$tweet_count = 0;
$max_id = 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' => 200,
'tweet_mode' => 'extended',
) : array(
'q' => $feed['search_term'],
'count' => 200,
'tweet_mode' => 'extended',
);
// $max_id overrides $since_id
if (!empty($max_id)) {
$params['max_id'] = $max_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' => 200,
'tweet_mode' => 'extended',
];
$tdata = $con
->get("lists/statuses", $params);
break;
case 1:
default:
$tdata = $con
->get("search/tweets", $params);
break;
}
if (!empty($tdata)) {
if (!empty($tdata->errors)) {
foreach ($tdata->errors as $error) {
$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);
}
}
}
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.'));
}
}