public function TweetFeed::saveTweet in Tweet Feed 8.3
Same name and namespace in other branches
- 4.x src/Controller/TweetFeed.php \Drupal\tweet_feed\Controller\TweetFeed::saveTweet()
Save the tweet to our tweet entity.
Parameters
object $tweet: The tweet as it is retrieved from Twitter.
array $feed: The information on the feed from which this feed is being fetched.
1 call to TweetFeed::saveTweet()
- TweetFeed::pullDataFromFeed in src/
Controller/ TweetFeed.php - Get Twitter Data
File
- src/
Controller/ TweetFeed.php, line 30
Class
- TweetFeed
- Class TweetFeed.
Namespace
Drupal\tweet_feed\ControllerCode
public function saveTweet($tweet, $feed) {
$language = Language::LANGCODE_DEFAULT;
// Check to see if we already have this tweet in play.
// If so, don't reimport it.
$entities = \Drupal::entityQuery('tweet_entity')
->condition('tweet_id', $tweet->id_str, '=')
->execute();
if (!empty($entities)) {
return FALSE;
}
// Get the creation time of the tweet and store it.
$creation_timestamp = strtotime($tweet->created_at);
// Add our hash tags to the hashtag taxonomy. If it already exists, then get the tid
// for that term. Returns an array of tid's for hashtags used.
$hashtags = $this
->processTaxonomy($tweet->entities->hashtags, 'twitter_hashtag_terms');
// Add our user mentions to it's relative taxonomy. Handled just like hashtags
$user_mentions = $this
->processTaxonomy($tweet->entities->user_mentions, 'twitter_user_mention_terms');
// Process the tweet. This linkes our twitter names, hash tags and converts any
// URL's into HTML.
$tweet_text = $tweet->full_text;
$tweet_html = tweet_feed_format_output($tweet_text, $feed['new_window'], $feed['hash_taxonomy'], $hashtags);
$specific_tweets = [];
$uuid_service = \Drupal::service('uuid');
// Populate our tweet entity with the data we will need to save
$entity = new TweetEntity([], 'tweet_entity');
$entity
->setOwnerId(1);
$entity
->setUuid($uuid_service
->generate());
$entity
->setCreatedTime(strtotime($tweet->created_at));
$entity
->setFeedMachineName($feed['machine_name']);
$entity
->setTweetId($tweet->id_str);
$entity
->setTweetTitle(mb_substr(Html::decodeEntities($tweet->user->screen_name) . ': ' . Html::decodeEntities($tweet_text), 0, 255));
$entity
->setTweetFullText(tweet_feed_format_output($tweet->full_text, $feed['new_window'], $feed['hash_taxonomy'], $hashtags));
$entity
->setTweetUserProfileId($tweet->user->id);
$entity
->setIsVerifiedUser((int) $tweet->user->verified);
$entity
->setUserMentions($tweet->entities->user_mentions);
/** Re-Tweet*/
if (!empty($tweet->retweeted_status->id_str)) {
$entity
->setTypeOfTweetReference('retweeted');
$entity
->setReferencedTweetId($tweet->retweeted_status->id_str);
$specific_tweets[] = $tweet->retweeted_status->id_str;
}
/** Tweet Reply*/
if (!empty($tweet->in_reply_to_status_id_str)) {
$entity
->setTypeOfTweetReference('replied');
$entity
->setReferencedTweetId($tweet->in_reply_to_status_id_str);
$specific_tweets[] = $tweet->in_reply_to_status_id_str;
}
/** Quoted Tweet w/Comment */
if (!empty($tweet->is_quote_status)) {
$entity
->setTypeOfTweetReference('quoted');
$entity
->setReferencedTweetId($tweet->quoted_status_id_str);
$specific_tweets[] = $tweet->quoted_status_id_str;
}
/** Handle media and by media I mean images attached to this tweet. */
if (!empty($tweet->entities->media) && is_array($tweet->entities->media)) {
$files = [];
foreach ($tweet->entities->media as $key => $media) {
if (is_object($media)) {
/** Edge case - a really big image could push a PHP max memory issue. I need to research */
/** alternative ways of doing this. */
$image = file_get_contents($media->media_url . ':large');
if (!empty($image)) {
$this
->checkPath('public://tweet-feed-tweet-images', TRUE);
$file_temp = file_save_data($image, 'public://tweet-feed-tweet-images/' . date('Y-m') . '/' . $tweet->id_str . '.jpg', 1);
if (is_object($file_temp)) {
$fid = $file_temp->fid
->getvalue()[$key]['value'];
$files[] = [
'target_id' => $fid,
'alt' => '',
'title' => $media->id,
'width' => $media->sizes->large->w,
'height' => $media->sizes->large->h,
];
}
unset($file_temp);
unset($image);
}
}
}
$entity
->setLinkedImages($files);
}
if (!empty($hashtags)) {
$entity
->setHashtags($hashtags);
}
if (!empty($user_mentions)) {
$entity
->setUserMentionsTags($user_mentions);
}
if (!empty($tweet->place) && is_object($tweet->place)) {
$bb = json_encode($tweet->place->bounding_box->coordinates[0]);
$entity
->setGeographicCoordites($bb);
}
if (!empty($tweet->place->full_name)) {
$entity
->setGeographicPlace($tweet->place->full_name);
}
if (!empty($tweet->source)) {
$entity
->setSource($tweet->source);
}
$entity
->setLinkToTweet('https://twitter.com/' . $tweet->user->screen_name . '/status/' . $tweet->id_str);
$entity
->setTweetUserProfileId($tweet->user->id);
$tweet->user->profile_image_url = str_replace('_normal', '', $tweet->user->profile_image_url);
$file = $this
->processTwitterImage($tweet->user->profile_image_url, 'profile', $tweet->user->id_str, FALSE);
if ($file !== FALSE) {
$file_array = [];
$file_array[] = $file;
$entity
->setProfileImage($file_array);
}
\Drupal::moduleHandler()
->alter('tweet_feed_tweet_save', $entity, $tweet);
$entity
->save();
if (empty($entity)) {
return TRUE;
}
$entity = new TwitterProfileEntity([], 'twitter_profile');
// If we are creating a user profile for the person who made this tweet, then we need
// to either create it or update it here. To determine create/update we need to check
// the hash of the profile and see if it matches our data.
$profile_hash = md5(serialize($tweet->user));
$query = \Drupal::entityQuery('twitter_profile')
->condition('twitter_user_id', $tweet->user->id)
->execute();
// If we have a result, then we have a profile! Then we need to check to see if the hash
// of the profile is the same as the hash of the user data. If so, then update. If not,
// then skip.
if (!empty($query)) {
$keys = array_keys($query);
$entity = $entity
->load($keys[0]);
$entity_hash = $entity
->getHash();
if ($profile_hash == $entity_hash) {
\Drupal::moduleHandler()
->alter('tweet_feed_twitter_profile_save', $entity, $tweet->user);
return TRUE;
}
}
// Populate our entity with the data we will need to save
$entity
->setOwnerId(1);
$entity
->setUuid($uuid_service
->generate());
$entity
->setTwitterUserId($tweet->user->id_str);
$entity
->setName($tweet->user->name);
$entity
->setDescription($tweet->user->description);
$entity
->setScreenName($tweet->user->screen_name);
$entity
->setLocation($tweet->user->location);
$entity
->setFollowersCount($tweet->user->followers_count);
$entity
->setVerified((int) $tweet->user->verified);
$entity
->setStatusesCount($tweet->user->statuses_count);
$entity
->setHash($profile_hash);
// Handle the user profile image obtained from twitter.com
$file = $this
->processTwitterImage($tweet->user->profile_image_url, 'user-profile', $tweet->user->id_str, FALSE);
if ($file !== FALSE) {
$file_array = [];
$file_array[] = $file;
$entity
->setProfileImage($file_array);
}
// Handle the user profile banner image obtained from twitter.com
$file = $this
->processTwitterImage($tweet->user->profile_banner_url, 'banner-image', $tweet->user->id_str, FALSE);
if ($file !== FALSE) {
$file_array = [];
$file_array[] = $file;
$entity
->setBannerImage($file_array);
}
\Drupal::moduleHandler()
->alter('tweet_feed_twitter_profile_save', $entity, $tweet->user);
$entity
->save();
return TRUE;
}