Skip to content

Commit

Permalink
Merge pull request #3 from firesock/master
Browse files Browse the repository at this point in the history
Replace Twitter media URIs with display links only
  • Loading branch information
marksteward committed Feb 16, 2014
2 parents 5ef70af + b38531a commit 49355ff
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions earl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,30 @@ sub get_tweet {
my $text = $json->{text};

if (my $entities = $json->{entities}) {
foreach my $entity (@{$entities->{media}}, @{$entities->{urls}}) {
my @text_array = split("", $text);
# Perl is bonkers
my @replace_array = ("") x $#text_array;

foreach my $entity (@{$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;
# Second index is next character after URL
@text_array[$indices[0]..($indices[1] - 1)] = @replace_array;
$text_array[$indices[0]] = $ent_url;

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

foreach my $entity (@{$entities->{media}}) {
if (my @indices = @{$entity->{indices}}) {
# Second index is next character after URL
@text_array[$indices[0]..($indices[1] - 1)] = @replace_array;
$text_array[$indices[0]] = "*IMG*";
}
}

$text = join("", @text_array);
}

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

0 comments on commit 49355ff

Please sign in to comment.