OSDN Git Service

update config handling
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 20 Oct 2012 21:32:30 +0000 (06:32 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 20 Oct 2012 21:32:30 +0000 (06:32 +0900)
get_facebook_accesscode.pl

index c787be0..dccceff 100755 (executable)
@@ -2,33 +2,37 @@
 
 use strict;
 use Facebook::Graph;
-use Config::Simple;
+use Config::Any;
+use Hash::Merge::Simple;
+use Data::Recursive::Encode;
 use Data::Dumper;
 
 my %cfg;
 
 my $config_name = $ARGV[0] || 'not_found';
 
-my @config_path = ('/etc/mubot4fb/', $ENV{HOME} . '/.mubot4fb/', $ENV{PWD} . '/mubot4fb_');
+my $cfg = {};
+my $config_path = ['/etc/mubot4fb/'.$config_name,
+                  $ENV{HOME} . '/.mubot4fb/'. $config_name,
+                  $ENV{PWD} . '/mubot4fb_' . $config_name];
 
-foreach my $c (@config_path) {
-        my $config = $c . $config_name . '.conf';
-       print $config . "\n";
-        Config::Simple->import_from($config, \%cfg) if (-e $config);
+my $c = Config::Any->load_stems({stems => $config_path,  use_ext => 1, flatten_to_hash => 1});
+foreach my $i (keys %$c) {
+       $cfg = Hash::Merge::Simple->merge($cfg, $c->{$i});
 }
+die 'missing config file' unless (keys %$cfg);
 
-die 'missing some config parameters should be defined (fb_app_id, fb_app_secret, fb_access_code, fb_page_id fb_postback_url)'
-    if (!defined $cfg{'fb_app_id'});
-#      || !defined $cfg{'fb_app_secret'}
-#      || !defined $cfg{'fb_postback_url'}
-#    );
+die 'missing some config parameters should be defined (irc_server, fb_app_id, fb_app_secret, fb_access_code, fb_page_id fb_postback_url)'
+    if (!defined $cfg->{'fb_app_id'});
 
-my $fb = Facebook::Graph->new(app_id   => $cfg{'fb_app_id'},
-                             secret   => $cfg{'fb_app_secret'},
-                             postback => $cfg{'fb_postback_url'});
+$cfg = Data::Recursive::Encode->decode('utf8', $cfg);
+
+my $fb = Facebook::Graph->new(app_id   => $cfg->{'fb_app_id'},
+                             secret   => $cfg->{'fb_app_secret'},
+                             postback => $cfg->{'fb_postback_url'});
 my $page_id = $cfg{'page_id'};
 
-my $uri_t = $fb->authorize->extend_permissions(qw(offline_access publish_stream manage_pages))->uri_as_string;
+my $uri_t = $fb->authorize->extend_permissions(qw(offline_access publish_stream status_update))->uri_as_string;
 print $uri_t . "\n";
 
 print "\n\n↑この表示されてる uri をブラウザにつっこむと access_code が得られる redirect さきの url に code パラメタとしてくっついてくるので それを設定ファイルの access_code パラメータにコピペ...\n";