source: lab.git/show_status.pl @ 5f2d507

twitter-0.1twitter-0.1@65twitter-0.1@66
Last change on this file since 5f2d507 was 5f2d507, checked in by mitty <mitty@…>, 14 years ago
  • help script for Dump tweet information

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

  • Property mode set to 100755
File size: 2.1 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        print Dumper $res;
32    }
33};
34if ($@) {
35    evalrescue($@);
36}
37print "done\n";
38
39
40sub loadconf {
41    # load configration data from yaml formatted file
42    #   param   => scalar string of filename
43    #   ret     => hash object of yaml data
44   
45    my $file = shift @_;
46   
47    my $yaml = YAML::Tiny->read($file);
48   
49    if ($!) {
50        warn "$0: '$file' $!\n";
51    }
52   
53    return $yaml->[0];
54}
55
56sub login {
57    # make Net::Twitter::Lite object and login
58    #   param   => hash object of configration
59    #   ret     => Net::Twitter::Lite object
60   
61    my $conf = shift @_;
62   
63    my $bot = Net::Twitter::Lite->new(
64        consumer_key    => $conf->{consumer_key},
65        consumer_secret => $conf->{consumer_secret},
66    );
67   
68    $bot->access_token($conf->{access_token});
69    $bot->access_token_secret($conf->{access_token_secret});
70   
71    return $bot;
72}
73
74sub evalrescue {
75    # output error message at eval error
76   
77    use Scalar::Util qw(blessed);
78   
79    if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
80        warn $@->error;
81        if ($@->twitter_error) {
82            my %twitter_error = %{$@->twitter_error};
83            map {
84                $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
85                delete $twitter_error{$_}
86            } keys %twitter_error;
87            warn join("", %twitter_error);
88        }
89    }
90    else {
91        warn $@;
92    }
93}
Note: See TracBrowser for help on using the repository browser.