Skip to content

Instantly share code, notes, and snippets.

@Cside
Last active December 10, 2015 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cside/4398084 to your computer and use it in GitHub Desktop.
Save Cside/4398084 to your computer and use it in GitHub Desktop.
A twitter bot posts your github's public activities
#!/usr/bin/env perl
=pod
INSTALL
cpanm Script::State Net::Twitter::Lite autobox::Core JSON::XS
=cut
use 5.10.0;
use strict;
use warnings;
use Script::State;
use LWP::UserAgent;
use autobox::Core;
use JSON::XS qw(decode_json);
use Net::Twitter::Lite;
# FIXME
my $user_name = 'YOUR LOGIN NAME';
my $consumer_key = 'XXXXX';
my $consumer_secret = 'XXXXX';
my $access_token = 'XXXXX';
my $access_token_secret = 'XXXXX';
my $base_url = 'https://github.com';
script_state my $last_event_id;
$last_event_id ||= 0;
my $twitter = Net::Twitter::Lite->new(
legacy_lists_api => 0,
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
access_token => $access_token,
access_token_secret => $access_token_secret,
);
my $ua = LWP::UserAgent->new;
my $res = $ua->get("https://api.github.com/users/$user_name/received_events");
my $events = decode_json $res->content;
for my $event (reverse @$events) {
next unless $event->{id} > $last_event_id ;
my $payload = $event->{payload};
my $repo_name = $event->{repo}->{name};
my $msg = "$event->{actor}->{login} ";
my $url = '';
given ($event->{type}) {
when ('PublicEvent') {
$url = "$base_url/$repo_name";
$msg .= "open sourced $repo_name";
}
when ('ForkEvent') {
$url = $payload->{forkee}->{clone_url};
$msg .= "forked $repo_name";
}
when ('PushEvent') {
$url = "$base_url/$repo_name/commits/$payload->{head}";
$msg .= "pushed $payload->{size} commit" . ($payload->{size} > 1 ? 's' : '') . " to $repo_name";
}
when ('PullRequestEvent') {
$url = $payload->{pull_request}->{html_url};
my $id = $url->split('/')->pop;
$msg .= "$payload->{action} pull request #$id in $repo_name";
}
when ('IssuesEvent') {
$url = $payload->{issue}->{html_url};
$msg .= "$payload->{action} issue #$payload->{issue}->{number} in $repo_name";
}
when ('IssueCommentEvent') {
$url = $payload->{comment}->{url};
my $id = $payload->{issue}->{number};
$msg .= "commented an issue#$id on $repo_name";
}
when ('CommitCommentEvent') {
$url = $payload->{comment}->{html_url};
my $commit_hash = $payload->{comment}->{commit_id}->substr(0, 6);
$msg .= "commented commit\@$commit_hash on $repo_name";
}
when ('GistEvent') {
$url = $payload->{gist}->{html_url};
my $action = "$payload->{action}ed";
$action =~ s/eed/ed/;
$msg .= "${action}ed a gist";
}
when ('WatchEvent') {
$url = "$base_url/$repo_name";
$msg .= "$payload->{action} watching $repo_name";
}
when ('FollowEvent') {
my $target_name = $payload->{target}->{login};
$url = "$base_url/$target_name";
$msg .= "started following $target_name";
}
when ('CreateEvent') {
$url = "$base_url/$repo_name";
my $repo_name_short = $repo_name->split('/')->pop;
$msg .= "created repository $repo_name_short";
}
when ('GollumEvent') {
my $page = $payload->{pages}->[0];
$url = $page->{html_url};
$msg .= "$page->{action} wiki: '$page->{page_name}' in $repo_name";
}
when ('PullRequestReviewCommentEvent') {
$url = $payload->{comment}->{_links}->{pull_request}->{href};
my $id = $payload->{comment}->{_links}->{pull_request}->{href}->split('/')->pop;
$msg .= "commented discussion of pull request #$id in $repo_name";
}
default {
warn "Unknown Event $event->{type}";
}
}
local $@;
eval { $twitter->update("$msg $url"); print "$msg $url\n" };
warn $@ if $@;
}
$last_event_id = $events->[0]->{id};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment