Skip to content

Instantly share code, notes, and snippets.

@silvers
Created December 1, 2013 08:29
Show Gist options
  • Save silvers/7729861 to your computer and use it in GitHub Desktop.
Save silvers/7729861 to your computer and use it in GitHub Desktop.
twitter_bot 二年ぐらい前のapiがアレする前に書いたやつなので動かないよ。
use strict;
use warnings;
use utf8;
use lib "$ENV{HOME}/extlib/lib/perl5";
use AnyEvent;
use AnyEvent::Feed;
use AnyEvent::IRC::Client;
use Encode;
use Encode::Guess;
use Data::Dumper;
use DateTime;
use Furl;
use HTML::Entities;
use JSON;
use LWP::UserAgent;
use MF::NoPaste::Client;
use Net::SSH qw/ssh_cmd/;
use Regexp::Common qw/URI/;
use URI;
use URI::Escape;
use XML::Simple;
use YAML::Syck;
my $channel = '#your_channel';
my $server = 'hoge';
my $CONFIG = {
server => $server,
port => 6667,
info => {
nick => 'twitter_bot',
real => 'twitter_bot',
}
};
my $config_file = shift(@ARGV) || die "Usage: $0 backup_filename";
our $rss_list = reload();
warn Dumper $rss_list;
my $cv = AnyEvent->condvar;
my $irc = AnyEvent::IRC::Client->new;
my $feed_render = {};
for my $feed ( @{ $rss_list } ) {
$feed->{channel} = decode_utf8($feed->{channel});
$irc->send_srv('JOIN', $feed->{channel});
$feed_render->{$feed->{desc}} = AnyEvent::Feed->new(
url => $feed->{url},
interval => 60 * 3,
on_fetch => sub {
my ( $feed_reader, $ent, $fee, $er ) = @_;
if ( defined $er ) {
warn "ERROR: $er\n";
$irc->send_chan( $feed->{channel}, 'NOTICE', $feed->{channel}, "cannot get $feed->{desc}" );
#$cv->send;
return;
}
if ( $feed->{type} eq 'twitter' ) {
_twitter($feed, $ent);
} elsif ( $feed->{type} eq '2ch' ) {
_2ch($feed, $ent);
} elsif ( $feed->{type} eq 'atnd' ) {
_atnd($feed, $ent);
}
}
);
}
$irc->connect( $CONFIG->{server}, $CONFIG->{port}, $CONFIG->{info} );
$irc->send_srv("JOIN", $channel);
$cv->recv;
$irc->disconnect;
sub reload {
if ( -f $config_file ) {
return YAML::Syck::LoadFile($config_file);
} else {
return +{};
}
}
# http://2ch.repy.info/ でRSS化したものを読む前提
sub _2ch {
my ($feed, $ent) = @_;
my $msg = "";
my $okent = 0;
my $ch = $feed->{channel};
for (reverse @$ent) {
eval {
my $title = decode_utf8($_->[1]->{entry}->{title});
my $comment = decode_utf8($_->[1]->{entry}->{description});
$comment =~ s/<\/?div([^>]*)?>//g; # タグを取り除くよ
$comment =~ s/<\/?a([^>]*)?>//g; # タグを取り除くよ
$comment =~ s/<\/?img([^>]*)?>//g; # タグを取り除くよ
$comment =~ s/<br>/\n/g; # タグを取り除くよ
$comment =~ s/(2ch to RSS)//g;
$comment = decode_entities($comment);
$msg .= "$title $comment\n";
}
}
my $url = MF::NoPaste::Client->post(
title => $feed->{desc},
name => '2ch_bot',
body => $msg,
channel => $ch,
);
#$irc->send_chan( $ch, 'NOTICE', $ch, $url );
}
sub _atnd {
my ($feed, $ent) = @_;
my $msg = "";
my $ch = $feed->{channel};
for (reverse @$ent) {
eval {
my $entry = $_->[1];
my $link = decode_utf8($entry->link);
my $title = decode_utf8($entry->title);
use Data::Dumper;
warn Dumper $link;
warn Dumper $title;
$msg .= "$title $link\n";
}
}
$irc->send_chan( $ch, "NOTICE", $ch, $msg );
}
sub _twitter {
my ($feed, $ent) = @_;
my $msg = "";
my $okent = 0;
my $ch = $feed->{channel};
for (reverse @$ent) {
eval {
my $num;
my $link = decode_utf8($_->[1]->link);
my $title = decode_utf8($_->[1]->title);
$num = $2 if ( $link =~ m/(.*)\/statuses\/([0-9]*)/);
if ( $num > $feed->{max_num} ) {
$feed->{max_num} = $num;
$msg .= "$title $link\n";
$okent++;
}
}
}
$irc->send_chan( $ch, "NOTICE", $ch, $msg );
}
-
desc: hoge_at_twitter
url: 'http://search.twitter.com/search.atom?q=hoge'
channel: '#twitter'
max_num: 1
type: 'twitter'
-
desc: html5_and_ui_at_atnd
url: 'http://api.atnd.org/events/?keyword_or=html5,UX,UI,CSS,JavaScript&format=atom'
channel: '#ATND'
max_num: 1
type: 'atnd'
-
desc: hoge_at_2ch
url: RSS化したなにか
channel: '#2ch'
max_num: 600
type: '2ch'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment