Skip to main content

Creating a Twitter List of CPAN Authors

·470 words·3 mins·
metacpan perl Programming
❤️ It's great to see you here! I'm currently available on evenings and weekends for consulting and freelance work. Let's chat about how I can help you achieve your goals.

Recently I found a great little Twitter command line tool called t. It does a lot of useful things, including building and editing Twitter lists. For example, with the following commands we can:

  • create a Twitter list called “my-list-of-people”
  • add the @metacpan account to the list
  • display the accounts which are now in the list we’ve just created
t list create my-list-of-people
t list add my-list-of-people @metacpan
t list members my-list-of-people</code></pre>

I thought it would be fun to create a Twitter list of CPAN authors using some of the data in the MetaCPAN API. Authors can update their profiles on MetaCPAN to add one or more Twitter accounts. This data can then be found in the author type of the MetaCPAN API, in the profile key. To find out how many Twitter usernames of CPAN authors we can get, we’ll create a script that looks something like this:

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw( say );

use MetaCPAN::Client;

my $mc = MetaCPAN::Client-&gt;new( version =&gt; &#039;v1&#039; );

# Search for authors with a listed Twitter account
my $search = $mc-&gt;author( { &#039;profile.name&#039; =&gt; &#039;twitter&#039; } );

my @handles;
while ( my $author = $search-&gt;next ) {

    # grep matching author profiles to extract only the Twitter id
    my @profiles = grep { $_-&gt;{name} eq &#039;twitter&#039; } @{ $author-&gt;profile };

    foreach my $profile (@profiles) {
        my $id = $profile-&gt;{id};

        # not every handle returned by the API is prefixed by "@", so
        # we&#039;ll add it when it&#039;s missing
        $id = &#039;@&#039; . $id unless $id =~ m{\A@};
        push @handles, $id;
    }
}

# print a case-insensitive alpha-sorted list for humans to enjoy
say $_ for sort { "\L$a" cmp "\L$b" } @handles;</code></pre>

At this point you should be able to run this script, which prints the Twitter account names we are looking for. Now comes the Twitter integration. We could do this with a Perl module, but the point of this exercise is to use t.

The first thing you need to do is install t, the Twitter command line tool. Twitter OAuth is a bit cumbersome, so you’ll also need to create your own OAuth app. The client will guide you through this process. If you run into any problems getting started once you’ve entered your OAuth keys, check issue #402 and specifically this comment.

Now all that’s left is to create a list, run the script and pipe the output of the script to the new list.

$ t list create cpan-authors
$ perl twitter.pl | xargs t list add cpan-authors
$ t list cpan-authors members

That’s it. Your list has now been created and you can curate it to your heart’s content. As part of this exercise, I created an authors list for my own Twitter account. You can see my cpan-authors list in action.


Related

How I Spent My Perl Toolchain Summit v2019
·3040 words·15 mins
metacpan perl Programming Perl Toolchain Summit
meta::hack 3 Wrap Report
·1279 words·7 mins
metacpan perl Programming
Perl Toolchain Summit 2018 Wrap-up Report
·1664 words·8 mins
metacpan perl Programming Perl Toolchain Summit