Skip to content

Instantly share code, notes, and snippets.

@hyuki0000
Last active August 29, 2015 14:06
Show Gist options
  • Save hyuki0000/6d59202028e568531e31 to your computer and use it in GitHub Desktop.
Save hyuki0000/6d59202028e568531e31 to your computer and use it in GitHub Desktop.
テキストに書かれている内容をランダムにツイートするbot用のPerlスクリプト
1. Edit tweet.yaml.
2. Prepare tweet.txt (one line for one tweet).
3. Run tweet.pl.
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use YAML::Syck;
use Net::Twitter::Lite::WithAPIv1_1;
my $yaml = 'tweet.yaml';
my $conf = YAML::Syck::LoadFile($yaml) or die "$yaml: $!\n";
my $encoding = $conf->{encoding};
open my $dh, "<:encoding($encoding)", $conf->{datafile} or die "$conf->{datafile}: $!";
my @data = <$dh>;
close $dh;
open my $ih, "<" . $conf->{indexfile} or die "$conf->{indexfile}: $!";
my $index = <$ih>;
close $ih;
$index++;
open my $oh, ">" . $conf->{indexfile} or die "$conf->{indexfile}: $!";
print $oh "$index\n";
close $oh;
srand($index);
my $status = $data[int(rand(scalar(@data))) % scalar(@data)];
my $twitter = Net::Twitter::Lite::WithAPIv1_1->new(
consumer_key => $conf->{consumer_key},
consumer_secret => $conf->{consumer_secret},
ssl => 1,
);
$twitter->access_token($conf->{access_token});
$twitter->access_token_secret($conf->{access_token_secret});
my $result = eval {
$twitter->update($status);
};
warn "$@\n" if $@;
『数学ガール』の著者《結城浩》はメルマガを毎週発行しています。よろしければどうぞ! http://www.hyuki.com/mm/ [自動投稿]
結城浩はnote(ノート)で読み物を公開しています。フォローよろしくです! https://note.mu/hyuki [自動投稿]
『数学ガール』[Kindle版] http://www.amazon.co.jp/exec/obidos/ASIN/B00EYXMA9I/hyam-22/ (固定レイアウトです。無料サンプルで使用感を必ずご確認ください)[自動投稿]
『数学ガールの秘密ノート/式とグラフ』[Kindle版] http://www.amazon.co.jp/exec/obidos/ASIN/B00L0PDMIQ/hyam-22/ (固定レイアウトです。無料サンプルで使用感を必ずご確認ください)[自動投稿]
consumer_key: XXXXXXXXXXXXXXXXXXXXXXXXX
consumer_secret: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
access_token: 1234567-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
access_token_secret: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
datafile: tweet.txt
indexfile: index.txt
encoding: utf-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment