source: lab.git/twitter/show_status.pl @ 035465b

trunk
Last change on this file since 035465b was 035465b, checked in by mitty <mitty@…>, 14 years ago
  • skip unimportant elements from the Dump

git-svn-id: https://lab.mitty.jp/svn/lab/trunk@51 7d2118f6-f56c-43e7-95a2-4bb3031d96e7

  • Property mode set to 100755
File size: 2.6 KB
Line 
1#! /usr/bin/perl -w
2
3use strict;
4use warnings;
5use utf8;
6
7## IMPORTANT ##
8# When Net::Twitter::Lite encounters a Twitter API error or a network error,
9# it throws a Net::Twitter::Lite::Error object.
10# You can catch and process these exceptions by using eval blocks and testing $@
11## from http://search.cpan.org/perldoc?Net::Twitter::Lite#ERROR_HANDLING
12use Net::Twitter::Lite;
13use FindBin qw($Bin);
14use YAML::Tiny;
15
16my $conf = loadconf("$Bin/config.yml");
17if (! defined $conf) {
18    die "$0: cannot parse config file.\n";
19}
20
21my $bot = login($conf);
22if (! $bot->authorized) {
23    die "$0: this client is not yet authorized.\n";
24}
25
26
27eval {
28    foreach my $id (@ARGV) {
29        my $res = $bot->show_status($id);
30        use Data::Dumper;
31        foreach my $line (split /\n/, Dumper $res) {
32            if ($line =~ /undef/) { next; }
33            unless ($line =~ / => {/
34                ||  $line =~ / = /
35                ||  $line =~ /status/
36                ||  $line =~ /'text'/
37                ||  $line =~ /created/
38                ||  $line =~ /'id'/
39                ||  $line =~ /name/
40                ||  $line =~ / },/
41                ||  $line =~ / };/
42            ) { next; }
43            print $line, "\n";
44        }
45    }
46};
47if ($@) {
48    evalrescue($@);
49}
50print "done\n";
51
52
53sub loadconf {
54    # load configration data from yaml formatted file
55    #   param   => scalar string of filename
56    #   ret     => hash object of yaml data
57   
58    my $file = shift @_;
59   
60    my $yaml = YAML::Tiny->read($file);
61   
62    if ($!) {
63        warn "$0: '$file' $!\n";
64    }
65   
66    return $yaml->[0];
67}
68
69sub login {
70    # make Net::Twitter::Lite object and login
71    #   param   => hash object of configration
72    #   ret     => Net::Twitter::Lite object
73   
74    my $conf = shift @_;
75   
76    my $bot = Net::Twitter::Lite->new(
77        consumer_key    => $conf->{consumer_key},
78        consumer_secret => $conf->{consumer_secret},
79    );
80   
81    $bot->access_token($conf->{access_token});
82    $bot->access_token_secret($conf->{access_token_secret});
83   
84    return $bot;
85}
86
87sub evalrescue {
88    # output error message at eval error
89   
90    use Scalar::Util qw(blessed);
91   
92    if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
93        warn $@->error;
94        if ($@->twitter_error) {
95            my %twitter_error = %{$@->twitter_error};
96            map {
97                $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
98                delete $twitter_error{$_}
99            } keys %twitter_error;
100            warn join("", %twitter_error);
101        }
102    }
103    else {
104        warn $@;
105    }
106}
Note: See TracBrowser for help on using the repository browser.