Browse Source

use XRay to parse tweets

pull/82/head
Aaron Parecki 8 years ago
parent
commit
fa06932a9c
No known key found for this signature in database GPG Key ID: 276C2817346D6056
  1. 27
      controllers/controllers.php
  2. 61
      lib/helpers.php
  3. 7
      views/new-post.php

27
controllers/controllers.php

@ -420,27 +420,32 @@ $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));
$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) && $entry['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']);
}
}

61
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;
}

7
views/new-post.php

@ -11,10 +11,10 @@
<span class="loading hidden glyphicon glyphicon-refresh glyphicon-spin form-control-feedback"></span>
</div>
<div class="reply-context hidden">
<div>
<div class="reply-author">
<img src="" width="48" class="author-img">
</div>
<div>
<div class="reply-content">
<img src="" class="post-img hidden">
<div class="author"><span class="name"></span> <span class="url"></span></div>
<h4 class="post-name hidden"></h4>
@ -144,6 +144,9 @@
max-height: 140px;
overflow-y: hidden;
}
.reply-context .reply-content {
flex: 1 0;
}
.reply-context img.author-img {
border-radius: 4px;
width: 48px;

Loading…
Cancel
Save