StatusTwitter.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: logicp
  5. * Date: 6/9/17
  6. * Time: 4:12 PM
  7. */
  8. namespace Drupal\statusmessage;
  9. //require_once DRUPAL_ROOT .'/vendor/autoload.php';
  10. require_once __DIR__ . './../includes/TwitterAPIExchange.php';
  11. use TwitterAPIExchange;
  12. use Drupal\statusmessage\Entity;
  13. use Drupal\taxonomy\Entity\Term;
  14. use Drupal\node\Entity\Node;
  15. use Drupal\file\Entity\File;
  16. class StatusTwitter {
  17. protected $oauthAccessToken;
  18. protected $oauthAccessTokenSecret;
  19. protected $consumerKey;
  20. protected $consumerSecret;
  21. protected $parameter;
  22. private $twitterConfig;
  23. public function __construct($parameter) {
  24. $this->twitterConfig = \Drupal::config('twitter_api.settings');
  25. $this->parameter = $parameter;
  26. }
  27. /**
  28. * @return mixed
  29. */
  30. public function getConsumerSecret()
  31. {
  32. return $this->consumerSecret;
  33. }
  34. /**
  35. * @param mixed $consumerSecret
  36. */
  37. public function setConsumerSecret($consumerSecret)
  38. {
  39. $this->consumerSecret = $consumerSecret;
  40. }
  41. /**
  42. * @return mixed
  43. */
  44. public function getConsumerKey()
  45. {
  46. return $this->consumerKey;
  47. }
  48. /**
  49. * @param mixed $consumerKey
  50. */
  51. public function setConsumerKey($consumerKey) {
  52. $this->consumerKey = $consumerKey;
  53. }
  54. /**
  55. * @return mixed
  56. */
  57. public function getOauthAccessTokenSecret()
  58. {
  59. return $this->oauthAccessTokenSecret;
  60. }
  61. /**
  62. * @param mixed $oauthAccessTokenSecret
  63. */
  64. public function setOauthAccessTokenSecret($oauthAccessTokenSecret) {
  65. $this->oauthAccessTokenSecret = $oauthAccessTokenSecret;
  66. }
  67. /**
  68. * @return mixed
  69. */
  70. public function getOauthAccessToken() {
  71. return $this->oauthAccessToken;
  72. }
  73. /**
  74. * @param mixed $oauthAccessToken
  75. */
  76. public function setOauthAccessToken($oauthAccessToken) {
  77. $this->oauthAccessToken = $oauthAccessToken;
  78. }
  79. private function getApiStatusParameter() {
  80. return 'https://api.twitter.com/1.1/statuses/show.json';
  81. }
  82. private function generateRequest($url) {
  83. $twid = $this->parseUrl($url);
  84. $settings = [
  85. 'oauth_access_token' => $this->twitterConfig->get('oauth_access_token'),
  86. 'oauth_access_token_secret' => $this->twitterConfig->get('oauth_access_token_secret'),
  87. 'consumer_key' => $this->twitterConfig->get('consumer_key'),
  88. 'consumer_secret' => $this->twitterConfig->get('consumer_secret'),
  89. ];
  90. $twitterApi = new TwitterAPIExchange($settings);
  91. $getField = '?id=' . $twid . '&tweet_mode=extended';
  92. return $twitterApi
  93. ->setGetfield($getField)
  94. ->buildOauth($this->getApiStatusParameter(), 'GET');
  95. }
  96. public function sendRequest() {
  97. if ($response = $this->generateRequest($this->parameter)->performRequest()) {
  98. $data = json_decode($response);
  99. $tweetNode = $this->setNodeData($data);
  100. $media = $this->getTweetMedia($data);
  101. if ($media->images) {
  102. $tweetNode->set('field_tweet_images', $media->images);
  103. }
  104. if ($media->video) {
  105. $tweetNode->set('field_tweet_video', $media->video);
  106. }
  107. if ($media->userImage) {
  108. $tweetNode->set('field_user_image', $media->userImage);
  109. }
  110. if ($tweetNode->save()) {
  111. return $tweetNode->id();
  112. }
  113. return null;
  114. }
  115. }
  116. private function parseUrl ($text) {
  117. return explode('status/', $text)[1];
  118. }
  119. private function setNodeData($data) {
  120. //Create datetime object for title, media file path and content date field
  121. $nowTime = new \DateTime();
  122. $user = \Drupal::currentUser();
  123. $ip = \Drupal::request()->getClientIp();//get user's IP
  124. $links = [];
  125. $terms = $this->processTerms($data);
  126. if (!empty($data->entities->urls)) {
  127. foreach ($data->entities->urls as $url) {
  128. $links[] = $url->display_url;
  129. }
  130. }
  131. //Check for attached media and create a directory for saving
  132. if (isset($data->extended_entities->media)) {
  133. $media = $this->getTweetMedia($data);
  134. }
  135. if ($data->user->profile_image_url_https) {
  136. //TODO get profile image
  137. }
  138. $node = Node::create([
  139. 'type' => 'tweet',
  140. 'title' => $data->user->screen_name . '_' . $nowTime->format('Y.m.d.Hi'),
  141. 'uid' => $user->id(),
  142. 'field_tags' => $terms->tags,
  143. 'field_tweet_url' => $this->parameter,
  144. 'field_twit_id' => $data->id,
  145. 'field_post_date' => strtok($data->created_at, ' +'),
  146. 'field_username' => $terms->username,
  147. 'field_users' => $terms->users,
  148. 'field_links' => $links,
  149. 'status' => 1,
  150. ]);
  151. $node->set('body', $data->full_text);
  152. return $node;
  153. }
  154. private function getTweetMedia($data) {
  155. $media = new \stdClass();
  156. $images = [];
  157. $video = null;
  158. $userImage = null;
  159. if ($data->user->profile_image_url_https) {
  160. $userImage = file_get_contents($data->user->profile_image_url_https);
  161. $file = file_save_data($userImage);
  162. $userImage = $file->id();
  163. }
  164. foreach($data->extended_entities->media as $media) {
  165. $image = file_get_contents($media->media_url);
  166. $file = file_save_data($image);
  167. $images[] = $file->id();
  168. }
  169. if(!empty($data->extended_entities->media[0]->video_info->variants)) {
  170. $z = null;
  171. $vidUrl = null;
  172. $bitrate = new \stdClass();
  173. $bitrate->value = null;
  174. $bitrate->index = null;
  175. for ($z = 0; $z < $data->extended_entities->media[0]->video_info->variants; $z++) {
  176. if (!empty($data->extended_entities->media[0]->video_info->variants[$z]->bitrate) &&
  177. $data->extended_entities->media[0]->video_info->variants[$z]->content_type === 'video/mp4') {
  178. if ($data->extended_entities->media[0]->video_info->variants[$z]->bitrate > $bitrate->value) {
  179. $bitrate->value = $data->extended_entities->media[0]->video_info->variants[$z]->bitrate;
  180. $bitrate->index = $z;
  181. }
  182. }
  183. }
  184. if ($bitrate->index !== null) {
  185. $data->extended_entities->media[0]->video_info->variants[$bitrate->index]->url;
  186. // $video = system_retrieve_file($vidUrl, null, TRUE);
  187. // $file = File::create([
  188. // 'id' => 'id',
  189. // ])->save();
  190. $video = file_get_contents($data->extended_entities->media[0]->video_info->variants[$bitrate->index]->url);
  191. $file = file_save_data($video);
  192. $video = $file->id();
  193. }
  194. }
  195. $media->images = $images;
  196. $media->video = $video;
  197. $media->userImage = $userImage;
  198. return $media;
  199. }
  200. private function processTerms($data) {
  201. $terms = new \stdClass();
  202. $terms->tags = [];
  203. $terms->users = [];
  204. $terms->username = -1;
  205. if ($data->user->screen_name) {
  206. $term = \Drupal::entityQuery('taxonomy_term')
  207. ->condition('name', $data->user->screen_name)
  208. ->condition('vid', 'twitter_user')
  209. ->execute();
  210. if (count($term) < 1) {
  211. $term = Term::create(['name' => $data->user->screen_name, 'vid' => 'twitter_user']);
  212. if ($term->save()) {
  213. $terms->username = $term->id();
  214. } else {
  215. \Drupal::logger('StatusTwitter')->warning('Could not save term with name %name', array('%name' => $data->user->screen_name));
  216. }
  217. } else {
  218. $terms->username = array_values($term)[0];
  219. }
  220. }
  221. $term = null;
  222. foreach($data->entities->hashtags as $key => $h) {
  223. $term = \Drupal::entityQuery('taxonomy_term')
  224. ->condition('name', $h->text)
  225. ->condition('vid', 'twitter')
  226. ->execute();
  227. if (count($term) < 1) {
  228. $term = Term::create(['name' => $h->text, 'vid' => 'twitter']);
  229. if ($term->save()) {
  230. $terms->tags[] = $term->id();
  231. } else {
  232. \Drupal::logger('StatusTwitter')->warning('Could not save term with name %name', array('%name' => $h->text));
  233. }
  234. } else {
  235. $terms->tags[] = array_values($term)[0];
  236. }
  237. }
  238. $term = null;
  239. foreach($data->entities->user_mentions as $u) {
  240. $term = \Drupal::entityQuery('taxonomy_term')
  241. ->condition('name', $u->text)
  242. ->condition('vid', 'twitter_user')
  243. ->execute();
  244. if (count($term) < 1) {
  245. $term = Term::create(['name' => $u->screen_name, 'vid' => 'twitter_user']);
  246. if ($term->save()) {
  247. $terms->users[] = $term->id();
  248. } else {
  249. \Drupal::logger('StatusTwitter')->warning('Could not save term with name %name', array('%name' => $u->screen_name));
  250. }
  251. } else {
  252. $terms->users[] = array_values($term)[0];
  253. }
  254. }
  255. return($terms);
  256. }
  257. }