Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use Abraham\TwitterOAuth\TwitterOAuth;
  7. use App\Tweet;
  8.  
  9. class ImportTweets extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'import:tweets {account}';
  17.  
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  24.  
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34.  
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. $account = str_replace('@', '', $this->argument('account'));
  43. $tweets = \App\Services\Twitter\TwitterService::getTweetsByUser($account, 50);
  44.  
  45.  
  46. foreach ($tweets as $tweet)
  47. {
  48. $this->createTweet($tweet);
  49. }
  50. }
  51.  
  52.  
  53. private function createTweet($tweet)
  54. {
  55. $newTweet = new Tweet();
  56. $newTweet->content = $tweet->text;
  57. $newTweet->tweet_created_at = date('Y-m-d H:i:s', strtotime($tweet->created_at));
  58. $newTweet->likes = $tweet->favorite_count;
  59. $newTweet->retweets = $tweet->retweet_count;
  60. if($tweet->coordinates !== null)
  61. {
  62. $newTweet->latitude = $tweet->coordinates->coordinates[0];
  63. $newTweet->longitude = $tweet->coordinates->coordinates[1];
  64. }
  65. $newTweet->user = $tweet->user->screen_name;
  66. $newTweet->save();
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement