root/misc/hatenagraph.pl

Revision 2043 (checked in by miyagawa, 14 years ago)

update TZ to local

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 =head1 DESCRIPTION
6
7 This is a simple command-line interface to Hatena Graph.
8
9 =cut
10
11 use DateTime;
12 use Encode;
13 use ExtUtils::MakeMaker ();
14 use File::HomeDir;
15 use File::Spec;
16 use Getopt::Long;
17 use YAML;
18 use LWP::UserAgent;
19 use Pod::Usage;
20 use Term::ReadPassword;
21
22 our $conf = File::Spec->catfile(File::HomeDir->my_home, ".hatenagraph");
23 our $ua   = LWP::UserAgent->new;
24 our %config = ();
25 our %args   = ();
26 our $changed;
27
28 $ua->env_proxy;
29
30 main();
31
32 END {
33     save_config() if $changed;
34 }
35
36 sub prompt {
37     my $value = ExtUtils::MakeMaker::prompt($_[0]);
38     $changed++;
39     return $value;
40 }
41
42 sub prompt_password {
43     my $value = read_password("$_[0]  ");
44     $changed++;
45     return $value;
46 }
47
48 sub setup_encoding {
49     my $encoding;
50     eval {
51         require Term::Encoding;
52         $encoding = Term::Encoding::get_encoding();
53     };
54     $encoding ||= "utf-8";
55     binmode STDOUT, ":encoding($encoding)";
56     binmode STDIN, ":encoding($encoding)";
57     @ARGV = map decode($encoding, $_), @ARGV;
58 }
59
60 sub main {
61     setup_encoding();
62     GetOptions(\%args,
63                "date=s",
64                "help",
65                "config=s")
66         or pod2usage(2);
67
68     $conf = $args{config} if $args{config};
69     pod2usage(0) if $args{help};
70
71     my %commands = (
72         update => \&update_graphs,
73         setup  => sub { setup_config(1) },
74         null   => sub { },
75     );
76
77     my $command = shift @ARGV || ($changed ? 'null' : 'update');
78     setup_config() unless $command eq 'setup';
79
80     $commands{$command} or pod2usage(-message => "Unknown command: $command", -exitval => 2);
81     $commands{$command}->();
82 }
83
84 sub setup_config {
85     my $force = shift;
86     unless ($force) {
87         my $config = eval { YAML::LoadFile($conf) } || {};
88         %config = %$config;
89     }
90     $config{username} ||= prompt("Hatena username:");
91     $config{password} ||= prompt_password("Hatena password:");
92     $config{graphs}   ||= encode_utf8( prompt("Graph names (comma separated):") );
93
94     $ua->credentials('graph.hatena.ne.jp:80', '', $config{username}, $config{password});
95 }
96
97 sub save_config {
98     YAML::DumpFile($conf, \%config);
99     chmod 0600, $conf;
100 }
101
102 sub update_graphs {
103     $args{date} ||= DateTime->now(time_zone => 'local')->ymd;
104     for my $graph (split /,\s*/, $config{graphs}) {
105         utf8::decode($graph);
106         my $value = ExtUtils::MakeMaker::prompt("$graph on $args{date}:");
107         next unless defined $value && $value =~ /\S/;
108         utf8::encode($graph);
109         my $res = $ua->post( 'http://graph.hatena.ne.jp/api/post', {
110             graphname => $graph,
111             date      => $args{date},
112             value     => $value,
113         });
114         warn $res->content unless $res->code == 201;
115         warn "Data posted.\n"  if $res->code == 201;
116     }
117 }
118
119 =head1 NAME
120
121 hatenagraph.pl - a command-line interface to Hatena Graph
122
123 =head1 SYNOPSIS
124
125   hatenagraph setup
126   hatenagraph [options] update
Note: See TracBrowser for help on using the browser.