Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Commit

Permalink
- Added possibility to search for tags
Browse files Browse the repository at this point in the history
- We are now using doc.search instead of doc.getList
- Fixed an issue, with the document search. If a search returns only one
  document, the script would fail. This has been fixed for all affected
  functions. 
  !! IMPORTANT: For this fix you have to upgrade to Ipernity::API 0.07 !!


git-svn-id: svn+ssh://svn.neessen.net/svn/IperBackup@18 1954a2be-70b0-4a97-b48e-c3e6d65e13ce
  • Loading branch information
doomy committed Sep 28, 2010
1 parent 6ea1225 commit f453995
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
44 changes: 39 additions & 5 deletions IperBackup.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# $Id$
#
# Last modified: [ 2010-09-27 14:29:09 ]
# Last modified: [ 2010-09-28 11:52:14 ]

## This is the IperBackup::Main package {{{
package IperBackup::Main;
Expand All @@ -28,7 +28,7 @@ package IperBackup::Main;
use constant EXT_DEBUG => 0; ## Enable extended debug-logging
use constant LOGLEVEL => 'INFO'; ## Set the log level
use constant OUTDIR => '/var/tmp'; ## Default output directory
use constant VERSION => '0.01'; ## Current version number
use constant VERSION => '0.03'; ## Current version number
# }}}

## Define global variables {{{
Expand All @@ -47,6 +47,17 @@ package IperBackup::Main;
Log::Log4perl->wrapper_register(__PACKAGE__);
# }}}

### Check version of Ipernity::API module {{{
BEGIN
{

my $API = $Ipernity::API::VERSION;
do{ print "Ipernity::API v0.07 or higher required.\n"; exit 127; }
unless( $API >= 0.07 );

}
# }}}

### Main subroutine // main() {{{
sub main
{
Expand Down Expand Up @@ -91,6 +102,7 @@ sub main
api => $api,
config => $myconfig,
media => $config->{ 'media' } || DEFAULT_MEDIA,
tags => $config->{ 'tags' } || undef,

});
# }}}
Expand All @@ -113,8 +125,15 @@ sub main
## Get list of documents
my $documents = $iper->getDocsList();

## Calculate number of documents to fetch
my $docCount = scalar( keys %{ $documents } );

## Don't process any further if no document was found
exit 127 if $docCount == 0;

## Inform the user about the number of documents to be fetched
$log->info( 'Will fetch ' . scalar( keys %{ $documents } ) . ' documents of the media type(s): ' . join( ', ', split( /,/, $config->{ 'media' } || DEFAULT_MEDIA ) ) );
$log->info( 'Specified tags: ' . join( ', ', split( /,/, $config->{ 'tags' } ) ) ) if defined $config->{ 'tags' };

## Decide which action to perform... the real download {{{
if( defined( $config->{ 'download' } ) )
Expand Down Expand Up @@ -185,6 +204,7 @@ sub getArgs
'list|l' => \$config->{ 'list' },
'download|d' => \$config->{ 'download' },
'media|m=s' => \$config->{ 'media' },
'tags|t=s' => \$config->{ 'tags' },

);
showHelp() if( $config->{ 'help' } );
Expand All @@ -204,6 +224,19 @@ sub getArgs

}

## Check tags
if( defined( $config->{ 'tags' } ) )
{

## Clean up whitespaces
$config->{ 'tags' } =~ s/,\s/,/g;

## Count number of provided tags
my @list = split( /,/, $config->{ 'tags' } );
showHelp() if( $#list > 19 );

}

}
# }}}

Expand All @@ -213,13 +246,14 @@ sub showHelp

## Print message
print "Usage: $0 [OPTIONS]\n";
print "\n\t-o, --outdir\t\tSpecify absolute path to the output directory (Default: /var/tmp)";
print "\n\t-c, --config\t\tSpecify absolute path to config file (Default: /etc/IperBackup.conf)";
print "\n\t-d, --download\t\tTell IperBackup to download all files in your account";
print "\n\t-h, --help\t\tDisplay this help message.";
print "\n\t-l, --list\t\tTell IperBackup to create a list of files in you account";
print "\n\t-m, --media\t\tSpecify which media type to fetch. Possiblities are: audio, photo, other, video (Default: all)";
print "\n\t-h, --help\t\tDisplay this help message.\n";
print "\n";
print "\n\t-o, --outdir\t\tSpecify absolute path to the output directory (Default: /var/tmp)";
print "\n\t-t, --tags\t\tForce IperBackup to fetch only files with a specific tag (max. 20 tags)";
print "\n\n\n";

## Exit with non-zero error code
exit 127;
Expand Down
38 changes: 26 additions & 12 deletions IperBackup/Process.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# $Id$
#
# Last modified: [ 2010-09-27 13:26:04 ]
# Last modified: [ 2010-09-28 11:47:56 ]

## This is the IperBackup::Process package {{{
package IperBackup::Process;
Expand All @@ -22,7 +22,7 @@ use Time::HiRes;
## Defined constants {{{
use constant EXT_DEBUG => 0; ## Enable extended debug logging
use constant PER_PAGE => 100; ## Number of documents per page to fetch
use constant VERSION => '0.100'; ## This modules version
use constant VERSION => '0.03'; ## This modules version
# }}}

## Constuctor // new() {{{
Expand Down Expand Up @@ -59,6 +59,13 @@ sub new
$self->{ 'media' } = $args->{ 'media' };

}

## Get the tags
if( defined( $args->{ 'tags' } ) )
{
$self->{ 'tags' } = $args->{ 'tags' };

}

## API object needs to be provided
unless( defined( $self->{ 'api' } ) )
Expand Down Expand Up @@ -135,12 +142,13 @@ sub getNumberDocs

method => 'user.get',
media => $self->{ 'media' },
tags => $self->{ 'tags' } || undef,
auth_token => $self->{ 'config' }->{ 'IPER_API_AUTHTOKEN' },

);

## Return number of docs to caller
return $userinfo->{ 'user' }->{ 'count' }->{ 'docs' } || undef;
return $userinfo->{ 'user' }->[0]->{ 'count' }->[0]->{ 'docs' } || undef;

}
# }}}
Expand All @@ -165,7 +173,7 @@ sub getUserInfo
);

## Return requested user information to caller
return $userinfo->{ 'user' }->{ $type } || undef;
return $userinfo->{ 'user' }->[0]->{ $type } || undef;

}
# }}}
Expand All @@ -191,7 +199,10 @@ sub getDocsList

## Get number of pages to be fetched
my $pages = $self->getNumberPages();
$log->debug( 'There a 6 pages of documents (' . PER_PAGE . ' documents each) to be fetched...' );
$log->debug( 'There are ' . $pages . ' of documents (' . PER_PAGE . ' documents each) to be fetched...' ) if defined $pages;

## Don't process if document list is empty
return undef unless defined $pages;

## Retrieve all document ids and URLs from every page
for my $page ( 1 .. $pages )
Expand All @@ -200,16 +211,16 @@ sub getDocsList
## Log a debug message
$log->debug( "Retrieving docs from page $page..." );

## Get document list and run through it
## Run through document list
foreach my $doc ( @{ $self->getDocIDs( $page ) } )
{

## Get document ID for hash table assignment
my $docid = $doc->{ 'doc_id' };

## Store download URL and filename in hash table
$docs->{ $docid }->{ 'url' } = $doc->{ 'original' }->{ 'url' };
$docs->{ $docid }->{ 'fn' } = $doc->{ 'original' }->{ 'filename' };
$docs->{ $docid }->{ 'url' } = $doc->{ 'original' }->[0]->{ 'url' };
$docs->{ $docid }->{ 'fn' } = $doc->{ 'original' }->[0]->{ 'filename' };

## Log some ext. debug message
EXT_DEBUG && $log->debug( 'Found document "' . $docs->{ $docid }->{ 'fn' } . '" (Document ID: ' . $docid . ')' );
Expand Down Expand Up @@ -238,15 +249,17 @@ sub getNumberPages
my $docinfo = $self->{ 'api' }->execute_hash
(

method => 'doc.getList',
method => 'doc.search',
user_id => $self->getUserInfo( 'user_id' ),
media => $self->{ 'media' },
tags => $self->{ 'tags' } || undef,
auth_token => $self->{ 'config' }->{ 'IPER_API_AUTHTOKEN' },
per_page => PER_PAGE,

);

## Return number of docs to caller
return $docinfo->{ 'docs' }->{ 'pages' } || undef;
return $docinfo->{ 'docs' }->[0]->{ 'pages' } || undef;

}
# }}}
Expand All @@ -268,7 +281,8 @@ sub getDocIDs
my $docinfo = $self->{ 'api' }->execute_hash
(

method => 'doc.getList',
method => 'doc.search',
user_id => $self->getUserInfo( 'user_id' ),
media => $self->{ 'media' },
auth_token => $self->{ 'config' }->{ 'IPER_API_AUTHTOKEN' },
per_page => PER_PAGE,
Expand All @@ -278,7 +292,7 @@ sub getDocIDs
);

## Return number of docs to caller
return $docinfo->{ 'docs' }->{ 'doc' } || undef;
return $docinfo->{ 'docs' }->[0]->{ 'doc' } || undef;

}
# }}}
Expand Down
10 changes: 9 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ but also you can specify more media types at once. Here are two examples:

-m video
or
--media "video,audio,other"
--media video,audio,other

In case you like to fetch only a documents with a list of specified tags, you can use
the -t (or --tags) parameter. You can provide one or more tags (up to 20). The search
will be more limited, the more tags you provide. Here are two usage examples:

-t 2009
or
--tags 2009,cologne,water


That's all. Have fun using it!
Expand Down

0 comments on commit f453995

Please sign in to comment.