Accessing the Deki API with Perl

    Table of contents
    No headers
    #!/usr/bin/perl -w
    #Create N number of pages on your Deki
    #Usage: ./post.pl <numberofpages>
    #Example: ./post.pl 5
    
    use strict;
    use LWP::UserAgent;
    use Data::Dumper;
    use POSIX qw(strftime);
    
    #instantiate our objects
    my $agent = LWP::UserAgent->new;
    my $request = HTTP::Request->new;
    
    #declare our variables
    my $pages = shift; #how many page you want to create
    my $url = 'http://www.example.com/@api/deki';
    my $content = 'i am perl. hear me roar!';
    my $date = strftime '%Y%m%d%H%M%S', localtime;
    my $count = 1;
    my $username = 'USERNAME';
    my $password = 'PASSWORD';
    my $response;
    
    #setup the POST request
    $request->method('POST');
    $request->content_type('text/plain');
    $request->content($content);
    
    #setup the lwp agent so we can login to Deki.
    $agent->cookie_jar({});
    $agent->credentials('www.example.com:80',
                        'DekiWiki',
                        $username => $password);
    
    #this loop creates the number of pages specified on the command line
    while($count <= $pages) {
        #build the URI with the query parameters
        $request->uri("$url/pages/=new_$count/contents?edittime=$date&title=new_$count");
        #make the POST
        $response = $agent->request($request);
    
        #fail on errors, otherwise print confirmation of page creation
        if($response->is_error) {
            die "Error: " . $response->status_line;
        } else {
            print "Page #$count created.\n";
        }
    
        $count++;
    }
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by