Knowledgetree Command Line Drop Box in Perl

On February 19, 2010, in Uncategorized, by admin

I needed an easy way to push documents to the KT implementation. After looking at how KT’s windows drop box software works I wrote something similar in Perl that works from the command line.

I plan to wrap an Automator action around this to upload documents easily.

#stephen
#www.stephenjc.com
#ktdrop.pl
#command line drop box
#usage: ./ktdrop.pl username password fullpath_to_file

use SOAP::Lite;
use Data::Dumper;
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::Simple;

#username and password for KT
$username = $ARGV[0];
$password = $ARGV[1];

#prefix http or https
$serverPre = 'https://';

#full server url with port
$serverUrl = 'server.com:port/kt';

#document type for uploaded documents, make sure nothing is required because we will not send any metadata
$ktDocumenttype = 'default';

#if you have http basic leave this, otherwise change it to ''
$httpAuth = $username . ':' . $password . '@';
$fullUrl = $serverPre.$httpAuth.$serverUrl.'/ktwebservice/';
$session = SOAP::Lite->new(proxy => $fullUrl . 'webservice.php?wsdl');

$file = $ARGV[2];

#login
$r = $session->login($username,$password,'127.0.0.1');
$ktsessionid = $r->valueof('//message');

#dropbox folder id for user
$r = $session->get_folder_detail_by_name($ktsessionid,"DroppedDocuments/$username");
$dropboxid = $r->valueof('//id');

#upload document
$uploadRes = ktUpload();

#move file to dms
$r = $session->add_document($ktsessionid,$dropboxid,$uploadRes->{name},$uploadRes->{name},$ktDocumenttype,$uploadRes->{filename});
if ($r->valueof('//status_code') == 0)
{
$r = $session->logout($ktsessionid);
exit(0);
}
else
{
print "The upload did not work\n";
exit(1);
}

sub ktUpload
{
my $ua = LWP::UserAgent->new;
my $browser = HTTP::Request->new();
my $response = $ua->request(POST $fullUrl . 'upload.php', 
Content_Type => 'form-data', 
Content =>[
session_id => $ktsessionid, 
action => "A",
output => "xml",
file => [$file] 
]
);
my $uploadres = XMLin($response->content);
#print Dumper($uploadres->{upload_status}->{file});
return $uploadres->{upload_status}->{file};
}
Tagged with:  

3 Responses to Knowledgetree Command Line Drop Box in Perl

  1. emaildamien says:

    Hi Stephen,

    Some great KnowledgeTree tutuorials and tips you have here. Would you mind contributing them to our wiki?

    http://wiki.knowledgetree.com

    Damien Williams,
    KnowledgeTree Inc.

  2. Andrea says:

    Hi,
    your script is very powerful….
    Can you help me? I have a problem….
    How can I use metadata field to store a file?
    Probably i am a dummy in XML structure…

    Thanks

    Andrea Nannicini

  3. Stephen says:

    I never got around to writing an interface to upload metadata in perl. I remember doing it on VB.net and it was not easy.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>