From fa06932a9c1bb289cae6cbaa046c6b9c07bf5b1a Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 15 Jan 2017 07:57:40 -0800 Subject: [PATCH] use XRay to parse tweets --- controllers/controllers.php | 43 ++++++++++++++------------ lib/helpers.php | 61 +------------------------------------ views/new-post.php | 7 +++-- 3 files changed, 30 insertions(+), 81 deletions(-) diff --git a/controllers/controllers.php b/controllers/controllers.php index 6eb258d..4de4aee 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -420,28 +420,33 @@ $app->get('/reply/preview', function() use($app) { } $entry = false; - // Convert Tweets to h-entry + + $xray = [ + 'url' => $reply_url + ]; + if(preg_match('/twitter\.com\/(?:[^\/]+)\/statuse?s?\/(.+)/', $reply_url, $match)) { - $tweet_id = $match[1]; if($user->twitter_access_token) { - $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret, - $user->twitter_access_token, $user->twitter_token_secret); - } else { - $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret); + $xray['twitter_api_key'] = Config::$twitterClientID; + $xray['twitter_api_secret'] = Config::$twitterClientSecret; + $xray['twitter_access_token'] = $user->twitter_access_token; + $xray['twitter_access_token_secret'] = $user->twitter_token_secret; } - $tweet = $twitter->get('statuses/show/'.$tweet_id); - $entry = tweet_to_h_entry($tweet); - } else { - // Pass to X-Ray to see if it can expand the entry - $ch = curl_init('https://xray.p3k.io/parse?url='.urlencode($reply_url)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($ch); - $data = @json_decode($response, true); - if($data && isset($data['data']) && $data['data']['type'] == 'entry') { - $entry = $data['data']; - // Create a nickname based on the author URL - if(array_key_exists('author', $entry) && $entry['author']['url']) { - $entry['author']['nickname'] = display_url($entry['author']['url']); + } + + // Pass to X-Ray to see if it can expand the entry + $ch = curl_init('https://xray.p3k.io/parse'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xray)); + $response = curl_exec($ch); + $data = @json_decode($response, true); + if($data && isset($data['data']) && $data['data']['type'] == 'entry') { + $entry = $data['data']; + // Create a nickname based on the author URL + if(array_key_exists('author', $entry)) { + if($entry['author']['url']) { + if(!isset($entry['author']['nickname']) || !$entry['author']['nickname']) + $entry['author']['nickname'] = display_url($entry['author']['url']); } } } diff --git a/lib/helpers.php b/lib/helpers.php index 8948f92..4a4da11 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -59,24 +59,9 @@ function k($a, $k, $default=null) { } } -function get_timezone($lat, $lng) { - try { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'http://atlas.p3k.io/api/timezone?latitude='.$lat.'&longitude='.$lng); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $response = curl_exec($ch); - $tz = @json_decode($response); - if($tz) - return new DateTimeZone($tz->timezone); - } catch(Exception $e) { - return null; - } - return null; -} - function display_url($url) { $parts = parse_url($url); - if($parts['path'] != '' && $parts['path'] != '/') { + if(isset($parts['path']) && $parts['path'] != '' && $parts['path'] != '/') { return preg_replace('/^https?:\/\//','', $url); } else { return $parts['host']; @@ -395,47 +380,3 @@ function correct_photo_rotation($filename) { $image->writeImage($filename); } } - -function tweet_to_h_entry($tweet) { - // Converts to XRay's h-entry format - - $entry = [ - 'type' => 'entry', - 'url' => 'https://twitter.com/'.$tweet->user->screen_name.'/status/'.$tweet->id_str, - ]; - - $published = strtotime($tweet->created_at); - $entry['published'] = date('c', $published); - - $entry['content'] = [ - 'text' => $tweet->text - ]; - - if($tweet->entities->urls) { - foreach($tweet->entities->urls as $url) { - $entry['content']['text'] = str_replace($url->url, $url->expanded_url, $entry['content']['text']); - } - } - - $entry['author'] = [ - 'type' => 'card', - 'url' => 'https://twitter.com/'.$tweet->user->screen_name, - 'name' => $tweet->user->name, - 'nickname' => $tweet->user->screen_name, - 'photo' => $tweet->user->profile_image_url_https - ]; - - if($tweet->user->url) { - $entry['author']['url'] = $tweet->user->entities->url->urls[0]->expanded_url; - } - - if($tweet->entities->hashtags) { - $entry['category'] = []; - foreach($tweet->entities->hashtags as $tag) { - $entry['category'][] = $tag->text; - } - } - - return $entry; -} - diff --git a/views/new-post.php b/views/new-post.php index 6ce8095..de56ddb 100644 --- a/views/new-post.php +++ b/views/new-post.php @@ -11,10 +11,10 @@