Changeset 540

Show
Ignore:
Timestamp:
10/13/08 01:42:40 (3 months ago)
Author:
ogawa
Message:

Instead of hard-coding blog_id, short_name, etc., now employs YAML config file.

Location:
DisqusExporter/trunk
Files:
2 added
1 modified

Legend:

Unmodified
Added
Removed
  • DisqusExporter/trunk/tools/disqus-exporter.pl

    r539 r540  
    1212use warnings; 
    1313 
    14 use lib 'lib', '../lib'; 
     14use lib qw( lib ../lib ); 
    1515use MT::Bootstrap; 
    1616 
    17 use constant BLOG_ID         => 1; 
    18 use constant SHORT_NAME      => 'your forum short name'; 
    19 use constant ANONYMOUS_EMAIL => 'nobody@your.domain'; 
    20 use constant USER_API_KEY    => 'your user api key'; 
     17our $VERSION = '0.1-dev'; 
    2118 
    22 use constant VERBOSE => 0; 
     19use Getopt::Long; 
     20use YAML::Tiny; 
    2321 
    24 our $VERSION = '0.1-dev'; 
     22sub usage { 
     23    print "Usage: $0 --config <config.yaml> [ --verbose ]\n"; 
     24    exit(1); 
     25} 
     26 
     27GetOptions( 'config=s', \my $yaml, 'verbose', \my $verbose ) or usage(); 
     28usage() unless defined $yaml; 
     29my ($cfg) = YAML::Tiny::LoadFile($yaml); 
     30for (qw( blog_id forum_name default_email user_api_key)) { 
     31    die "'$_' is not defined in $yaml" unless defined $cfg->{$_}; 
     32} 
    2533 
    2634use MT; 
     
    3139use WWW::Disqus; 
    3240 
    33 my $api = WWW::Disqus->new( user_api_key => USER_API_KEY ); 
    34 $api->set_forum_api_key_by_forum_name(SHORT_NAME); 
    35  
    3641my $mt = MT->new() or die MT->errstr; 
    37 my $blog = MT::Blog->load(BLOG_ID) 
    38   or die 'Cannot find MT::Blog(ID:' . BLOG_ID . ')'; 
     42my $blog = MT::Blog->load( $cfg->{blog_id} ) 
     43  or die 'Cannot find MT::Blog(ID:' . $cfg->{blog_id} . ')'; 
    3944my $iter = MT::Entry->load_iter( 
    4045    { 
    41         blog_id => BLOG_ID, 
     46        blog_id => $cfg->{blog_id}, 
    4247        status  => MT::Entry::RELEASE, 
    4348    }, 
     
    4550        join => [ 
    4651            'MT::Comment', 'entry_id', 
    47             { blog_id => BLOG_ID, visible => 1 }, { unique => 1 } 
     52            { blog_id => $cfg->{blog_id}, visible => 1 }, { unique => 1 } 
    4853        ], 
    4954    } 
    5055); 
     56 
     57my $api = WWW::Disqus->new( user_api_key => $cfg->{user_api_key} ); 
     58$api->set_forum_api_key_by_forum_name( $cfg->{forum_name} ); 
    5159 
    5260my %stats = ( 
     
    6270    if ($@) { 
    6371        $stats{entry_skipped}++; 
    64         print STDERR "Disqus API error: $@\n" if VERBOSE; 
     72        print STDERR "Disqus API error: $@\n" if $verbose; 
    6573        next; 
    6674    } 
     
    7078        print STDERR "No Disqus thread found for MT::Entry(ID:" 
    7179          . $entry->id . ")\n" 
    72           if VERBOSE; 
     80          if $verbose; 
    7381        next; 
    7482    } 
     
    8290 
    8391        # author_email 
    84         my $author_email = ANONYMOUS_EMAIL; 
     92        my $author_email = $cfg->{default_email}; 
    8593        if ( $comment->email && MT::Util::is_valid_email( $comment->email ) ) { 
    8694            $author_email = $comment->email; 
     
    123131        if ($@) { 
    124132            $stats{comment_failed}++; 
    125             print STDERR "Disqus API error: $@\n" if VERBOSE; 
     133            print STDERR "Disqus API error: $@\n" if $verbose; 
    126134            next; 
    127135        } 
     
    133141          . ") successfully converted to Disqus post(ID:" 
    134142          . $post->{id} . ")\n" 
    135           if VERBOSE; 
     143          if $verbose; 
    136144    } 
    137145    $stats{entry_processed}++;