public function storePost(Request $request)
 {
     if (\Auth::check()) {
         $post = new SocialMedia();
         $post->username = \Auth::user()->name;
         $post->profile_pic_url = 'http://ghchsnd.com/assets/images/logofixed.png';
         $post->tweet = 'N/A';
         $post->caption = 'N/A';
         $post->message = $request->input('content');
         $post->source = 'Admin-Post';
         $post->link = 'N/A';
         $post->resize = 'N/A';
         $post->approved = 'Approved';
         $post->approver_id = \Auth::user()->admin_id;
         $post->datetime_posted = \Carbon\Carbon::now();
         $post->save();
         $request->session()->flash('added-post', 'Post Added.');
         $subscribers = DB::table('subscribers')->where('confirmed', 1)->get();
         foreach ($subscribers as $subscriber) {
             Mail::send('email.announcement', ['admin' => $post->username, 'post' => $post->message, 'code' => $subscriber->unsubscribe], function ($message) use($subscriber, $post) {
                 $message->from('*****@*****.**', 'GHSnD');
                 $message->to($subscriber->email, 'The Team')->subject('New Announcement from ' . $post->username);
             });
         }
         return view('auth.post');
     } else {
         return redirect('admin/login');
     }
 }
 public function getMyTweets()
 {
     $settings = array('oauth_access_token' => env('TWITTER_ACCESS_TOKEN'), 'oauth_access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'), 'consumer_key' => env('TWITTER_CONSUMER_KEY'), 'consumer_secret' => env('TWITTER_CONSUMER_SECRET'));
     $url = 'https://api.twitter.com/1.1/search/tweets.json';
     $getField = 'q=from%3Aghchsnd';
     $requestMethod = 'GET';
     $twitter = new TwitterAPIExchange($settings);
     $content = $twitter->setGetfield($getField)->buildOauth($url, $requestMethod)->performRequest();
     $json = json_decode($content, true);
     var_dump($json);
     foreach ($json['statuses'] as $tweet_info) {
         $tweet = $tweet_info['text'];
         $datetime_posted = $tweet_info['created_at'];
         $date = explode(' ', $datetime_posted);
         $month = date('m', strtotime($date[1]));
         $day = $date[2];
         $year = $date[5];
         $time = $date[3];
         $datetime = Carbon::createFromFormat('Y-m-d H:i:s', $year . "-" . $month . "-" . $day . " " . $time, 'UTC');
         $datetime = $datetime->setTimezone('America/Los_Angeles');
         $username = $tweet_info['user']['screen_name'];
         $profile_img = $tweet_info['user']['profile_image_url'];
         $link = $imgUrl = $resize = 'N/A';
         $height = $width = -1;
         if (isset($tweet_info['entities']['media'][0]['expanded_url'])) {
             $link = $tweet_info['entities']['media'][0]['expanded_url'];
         }
         if (isset($tweet_info['entities']['media'][0]['media_url_https'])) {
             $imgUrl = $tweet_info['entities']['media'][0]['media_url_https'];
         }
         if (isset($tweet_info['entities']['media'][0]['sizes']['large']['h'])) {
             $height = $tweet_info['entities']['media'][0]['sizes']['large']['h'];
         }
         if (isset($tweet_info['entities']['media'][0]['sizes']['large']['w'])) {
             $width = $tweet_info['entities']['media'][0]['sizes']['large']['w'];
         }
         if (isset($tweet_info['entities']['media'][0]['sizes']['large']['resize'])) {
             $resize = $tweet_info['entities']['media'][0]['sizes']['large']['resize'];
         }
         if (SocialMedia::where('$tweet', $tweet)->exists()) {
             echo "This post already exists";
         } else {
             $newTweet = new SocialMedia();
             $newTweet->username = $username;
             $newTweet->profile_pic_url = $profile_img;
             $newTweet->tweet = $tweet;
             $newTweet->caption = 'N/A';
             $newTweet->message = 'N/A';
             $newTweet->imgUrl = $imgUrl;
             $newTweet->link = $link;
             $newTweet->width = $width;
             $newTweet->height = $height;
             $newTweet->source = 'Admin-Twitter';
             $newTweet->approved = 'Approved';
             $newTweet->approver_id = -1;
             $newTweet->datetime_posted = $datetime;
             $newTweet->save();
             echo "Saved!";
         }
     }
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . env('INSTAGRAM_ACCESS_TOKEN');
     $content = file_get_contents($url);
     $json = json_decode($content, true);
     foreach ($json['data'] as $images) {
         $insta_id = $images['id'];
         $imgUrl = $images['images']['standard_resolution']['url'];
         $description = $images['caption']['text'];
         $datetime_posted = $images['caption']['created_time'];
         $datetime_posted = Carbon::createFromTimestamp($datetime_posted)->toDateTimeString();
         $username = $images['user']['username'];
         $profilePic = $images['user']['profile_picture'];
         $link = $images['link'];
         if (SocialMedia::where('insta_id', $insta_id)->exists() || $description == null) {
             echo "This post already exists";
         } else {
             $insta = new SocialMedia();
             $insta->username = $username;
             $insta->insta_id = $insta_id;
             $insta->profile_pic_url = $profilePic;
             $insta->tweet = 'N/A';
             $insta->caption = $description;
             $insta->message = 'N/A';
             $insta->source = 'Admin-Insta';
             $type = substr($imgUrl, -3);
             if (!file_exists('public/instagrams')) {
                 mkdir('public/instagrams', 0777, true);
             }
             $localUrl = 'public/instagrams/' . $insta_id . '.jpg';
             file_put_contents('public/instagrams/' . $insta_id . '.jpg', file_get_contents($imgUrl));
             $insta->link = $link;
             $insta->resize = 'fit';
             $insta->approved = 'Approved';
             $insta->approver_id = -1;
             $insta->datetime_posted = $datetime_posted;
             $insta->save();
             echo "Saved!";
         }
     }
     runSync();
     // $tag = 'ghsnd';
     // $url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.env('INSTAGRAM_CLIENT_ID');
     // $content = file_get_contents($url);
     // $json = json_decode($content, true);
     // foreach($json['data'] as $images) {
     //   $imgUrl = $images['images']['standard_resolution']['url'];
     //   $description = $images['caption']['text'];
     //   $datetime_posted = $images['caption']['created_time'];
     //   $datetime_posted = Carbon::createFromTimestamp($datetime_posted)->toDateTimeString();
     //   $username = $images['caption']['from']['username'];
     //   $profilePic = $images['caption']['from']['profile_picture'];
     //   $link = $images['link'];
     //   if (SocialMedia::where('caption', $description)->exists()) {
     // 	  echo "This post already exists";
     //   }
     //   else {
     // 	$insta = new SocialMedia;
     // 	$insta->username = $username;
     // 	$insta->profile_pic_url = $profilePic;
     // 	$insta->tweet = 'N/A';
     // 	$insta->caption = $description;
     // 	$insta->imgUrl = $imgUrl;
     // 	$insta->message = 'N/A';
     // 	$insta->source = 'Instagram';
     // 	$insta->link = $link;
     // 	$insta->width = 0;
     // 	$insta->height = 0;
     // 	$insta->resize = 'fit';
     // 	$insta->approved = 'Pending';
     // 	$insta->approver_id = -1;
     // 	$insta->datetime_posted = $datetime_posted;
     // 	$insta->save();
     // 	echo "Saved!";
     //   }
     // }
     echo "FINISHED";
 }