Skip to content

Commit

Permalink
Merge pull request #2 from firesock/twitter_urls
Browse files Browse the repository at this point in the history
Show data about URIs in Twitter status
  • Loading branch information
marksteward committed Feb 15, 2014
2 parents 726e5ff + bb8ad1c commit 5ef70af
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions earl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ sub title {
return $title;
}

sub get_simple_response {
my $url = shift;

my $response_ref = get_data($url);
return unless $$response_ref->is_success;

my $data = $$response_ref->decoded_content;
my $mime_type = $ft->checktype_contents($data);

$url = canonicalize($url, $response_ref);

if ( $mime_type =~ m'^image/' ) {
return ($url, get_img_title(\$data));
}
# BBC News article: headline and summary paragraph
elsif ( $url =~ m'^http://www\.bbc\.co\.uk/news/[-a-z]*-\d{7,}$' ) {
my $headline = $$response_ref->header( 'X-Meta-Headline' );
my $summary = $$response_ref->header( 'X-Meta-Description' );
return ($url, "$headline \x{2014} $summary");
}
# Everything else: the title
elsif ( my $title = title( $response_ref ) ) {
return ($url, $title);
}
}

sub get_response {
my $url = shift;

Expand All @@ -134,27 +160,7 @@ sub get_response {
if ( $url =~ m'^https?://twitter.com/(?:\?_escaped_fragment_=/)?\w+/status(?:es)?/(\d+)$' ) {
return ($url, get_tweet( $1 ));
} else {
my $response_ref = get_data($url);
return unless $$response_ref->is_success;

my $data = $$response_ref->decoded_content;
my $mime_type = $ft->checktype_contents($data);

$url = canonicalize($url, $response_ref);

if ( $mime_type =~ m'^image/' ) {
return ($url, get_img_title(\$data));
}
# BBC News article: headline and summary paragraph
elsif ( $url =~ m'^http://www\.bbc\.co\.uk/news/[-a-z]*-\d{7,}$' ) {
my $headline = $$response_ref->header( 'X-Meta-Headline' );
my $summary = $$response_ref->header( 'X-Meta-Description' );
return ($url, "$headline \x{2014} $summary");
}
# Everything else: the title
elsif ( my $title = title( $response_ref ) ) {
return ($url, $title);
}
return get_simple_response($url);
}
}

Expand All @@ -168,8 +174,23 @@ sub get_tweet {
return unless $response->is_success;

my $json = decode_json( $response->decoded_content );
my $text = $json->{text};

if (my $entities = $json->{entities}) {
foreach my $entity (@{$entities->{media}}, @{$entities->{urls}}) {
if (my @indices = @{$entity->{indices}} and my $ent_url = $entity->{expanded_url}) {
my $size = $indices[1] - $indices[0];
substr($text, $indices[0], $size) = $ent_url;

if (not defined $entity->{media_url}) {
next unless my (undef, $ent_response) = get_simple_response($ent_url);
$text = $text . " > " . $ent_response;
}
}
}
}

return join( " \x{2014} ", $json->{user}{screen_name}, $json->{text} );
return join( " \x{2014} ", $json->{user}{screen_name}, $text );
}

sub said {
Expand Down

0 comments on commit 5ef70af

Please sign in to comment.