Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a Top 10 list of your tweets
#/usr/bin/env perl
use strict;
use warnings;
use Text::xSV;
my $file = "tweet_activity_metrics.csv";
my $csv = new Text::xSV;
my $i = 0;
my $all_impressions = 0;
my $means_impressions = 0;
my %tweets = ();
my $id = undef;
$csv->open_file($file);
$csv->read_header();
while ( $csv->get_row() ) {
$i++;
($id) = $csv->extract( "Tweet id");
(
$tweets{$id}->{tweet},
$tweets{$id}->{impressions},
$tweets{$id}->{url}
)
= $csv->extract( "Tweet text", "impressions",
"Tweet permalink" );
$all_impressions += $tweets{$id}->{impressions};
}
$means_impressions = $all_impressions / $i;
printf( "%s Zeilen analysiert, durchschnittliche Impressions: %.2f\n",
$i, $means_impressions );
# Top ten
print "\n* Impressions\n\n";
$i = 0;
foreach
my $k ( sort { $tweets{$b}->{impressions} <=> $tweets{$a}->{impressions} }
keys %tweets )
{
$i++;
next if $i > 10;
# print as html
# printf("<li><b><a href=\"%s\" target=\"_blank\">%s</a></b>: %s</li>\n", $tweets{$k}->{url}, $tweets{$k}->{tweet},$tweets{$k}->{impressions} );
#print as text
printf(
"%s: %s (%s) %s impressions\n",
$i,
$tweets{$k}->{tweet},
$tweets{$k}->{url},
$tweets{$k}->{impressions}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.