Options

API 101 Running hourly scripts (?)

kansaschuckkansaschuck Registered Users Posts: 50 Big grins
I'm trying to build a process that will check for new pics sent in via the email interface to folder 'email'. I've become farmilar with the API calls and can extract the info I need. I've been doing this by entering the command on the browser line and hitting enter.

So I'm ready to move to the next level (and also ready to show how little I know :D). I'm wanting to automate that process. I've been looking at CGI Perl scripts. I could use some advice and direction. Let's say that every hour I wish to produce a report on how many pics / names / size are in 'email'. And after reviewing some info in the comments area or size stats I wish to do some updates (albumn id, delete,) etc. With those goals does anyone have any recommendations on the underlying systems structure? I guess I still don't have a great understanding of how API are finally implemented. I have a website on Lunar Pages and have access to CGI scripts of different favors. I'm happy to invest the time in research but before for a drift off too far oneway is there anyone with some suggestions?

Are all your APIs website/html driven? Is anyone doing timed scripts? What method are you / or would you recommend using to invoke an hourly API functions to extract data and preform updates? Could I see some basic examples to get going?

Thanks in advance for any help. :thumb

Comments

  • Options
    devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited December 12, 2006
    Hi Chuck,

    Personally, I would choose the Perl route as that's what i am comfortable with. You could also use php, python, even javascript...then just use some mechanism to kick it off..depending on what platform u are using.

    A while back I started writing an API test framework in perl to test XML-RPC, REST, JSON and PHP, but I don't have any code with me right now. However, what you are trying to do should be very straight forward.

    You should be able to use LWP to do the calls you require. Look it up...give me a yell if you need a hand.

    CHeers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • Options
    devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited December 13, 2006
    Chuck,

    Here's some sample code...u will need to install the JSON perl module to run it properly...
    use LWP;
    use JSON;
    
    my $apiURL = 'http://api.smugmug.com/hack/json/';
    my $apiKey = '[COLOR=Yellow][B]insert API key here[/B][/COLOR]';
    my $apiVersion = '1.2.0';
    
    $results = &loginWithPassword('[B][COLOR=Yellow]nickname[/COLOR][/B]', '[B][COLOR=Yellow]password[/COLOR][/B]', $apiKey, $apiURL, $apiVersion);
    exit;
    
    sub loginWithPassword {
      my $userName = $_[0];
      my $password = $_[1];
      my $apiKey = $_[2];
      my $apiURL = $_[3];
      my $apiVersion = $_[4];
      
      my $method = 'smugmug.login.withPassword';
    
      print "[$apiType] $method: ".$userName. " ...\n";
    
      my $function = $apiType."_request";
      my $requestURL = $apiURL.$apiVersion.'/';
      my $hash;
    
      my %inHash = ();
      $inHash {'EmailAddress'} = $userName;
      $inHash {'Password'} = $password;  
      $inHash {'APIKey'} = $apiKey;
    
      $hash = &json_request($requestURL,$method,%inHash);
      $hash = $hash->{'Login'};
    
      print "UserID: ".$hash->{'User'}->{'id'}."\n";
      print "AccountType: ".$hash->{'AccountType'}."\n";
      print "SessionID: ".$hash->{'Session'}->{'id'}."\n";
      print "PasswordHash: ".$hash->{'PasswordHash'}."\n\n";
    
      return $hash;
    }
    
    sub json_request {
      my ($requestURL, $method, %args) = @_;
      $requestURL .= "?method=".$method;
      for my $key ( sort keys %args ) {
        $requestURL .= "&$key=".%args->{$key};
      }
      print $requestURL."\n";
    
      my $browser = LWP::UserAgent->new;
      my $response = $browser->get($requestURL);
    
      my $content = $response->content;
    
      my $json = new JSON;
      $obj = $json->jsonToObj($content);
    
      return $obj;
    }
    

    Cheers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • Options
    kansaschuckkansaschuck Registered Users Posts: 50 Big grins
    edited December 13, 2006
    devbobo wrote:
    Hi Chuck,

    Personally, I would choose the Perl route as that's what i am comfortable with. You could also use php, python, even javascript...then just use some mechanism to kick it off..depending on what platform u are using.

    A while back I started writing an API test framework in perl to test XML-RPC, REST, JSON and PHP, but I don't have any code with me right now. However, what you are trying to do should be very straight forward.

    You should be able to use LWP to do the calls you require. Look it up...give me a yell if you need a hand.

    CHeers,

    David

    Thanks David,

    I spent the day yesterday working with PHP. Can I lean on you for some basics? I was working on my web site and looking at PERL... and then I saw that SMUGMUG was talking up serialized PHP. So I did my research and got PHP working with basic API calls. So now I not sure where PERL would fit into the picture... or LWP. Do PERL and PHP work together? It's really all about understand the basic infrastruture. It's all stuff that everyone here seems to understand without saying. But I'm switching over from MVS operating systems and need just some general pointers. My PHP scripts ended up in my public_html lib with a .php extension and are working ok. I was working with Lunarpages to get some direction on automating them. Can you tell me how PERL would fit in? and LWP? More of a system design 101 overview would be soooooo helpful.

    (of course I don't assume Smugmug and dgrin are here to take me threw computer and servers 101. But I appreciate any help! Feel free to send me to some manual.)

    Thanks! Chuck
  • Options
    devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited December 13, 2006
    ok..here's a few assumptions I am making, can u please confirm them for me...

    1. You plan on running this stuff locally on your own PC.
    2. You want to automate the process to happen every hour
    3. You want to use some scripting language..you don't seem to have a preference...you have mentioned both php and perl.

    so here's some background. SmugMug provide 4 different flavours of the API....XML-RPC, REST, JSON, PHP. Apart from XML-RPC, the request format for the others is identical...and only differ in the structure of the response. Depending on the what language you are using, would dictate what one u use. For example, if you were using php, it would make sense to use the php version of the api...as the response is returned in a structure that is natively accessible from php without additional parsing of the response.

    next it comes down to what you decide to use perl, php, javascript, etc. The choice is up to you...and what you are comfortable with or have an interest learning, I can't see you going wrong with either php or perl. Sounds like you are already under way with php...so go with that, and don't worry about perl.

    Is that the info you are after ? let me know if you need any other sort of overview.

    Cheers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • Options
    kansaschuckkansaschuck Registered Users Posts: 50 Big grins
    edited December 13, 2006
    devbobo wrote:
    ok..here's a few assumptions I am making, can u please confirm them for me...

    1. You plan on running this stuff locally on your own PC.
    2. You want to automate the process to happen every hour
    3. You want to use some scripting language..you don't seem to have a preference...you have mentioned both php and perl.

    so here's some background. SmugMug provide 4 different flavours of the API....XML-RPC, REST, JSON, PHP. Apart from XML-RPC, the request format for the others is identical...and only differ in the structure of the response. Depending on the what language you are using, would dictate what one u use. For example, if you were using php, it would make sense to use the php version of the api...as the response is returned in a structure that is natively accessible from php without additional parsing of the response.

    next it comes down to what you decide to use perl, php, javascript, etc. The choice is up to you...and what you are comfortable with or have an interest learning, I can't see you going wrong with either php or perl. Sounds like you are already under way with php...so go with that, and don't worry about perl.

    Is that the info you are after ? let me know if you need any other sort of overview.

    Cheers,

    David

    Thanks!

    From the asumption list...
    1) I plan to run it from a standalone website running 24/7 to monitor input to the email folder.
    2) correct
    3) I would have considered PERL but I didn't see in metioned on the list of SMUGMUG APIs

    "and need to use REST, JSON, serialized PHP , or XML-RPC to interact with the service. SmugMug *highly* recommends using REST, JSON, or serialized PHP."

    Maybe one of those are PERL by another name?

    Ok.. so for now I'll run with PHP (not to late to switch to PERL if there's some reason).

    Thanks for code example.

    Chuck
  • Options
    devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited December 13, 2006
    Thanks!

    From the asumption list...
    1) I plan to run it from a standalone website running 24/7 to monitor input to the email folder.
    2) correct
    3) I would have considered PERL but I didn't see in metioned on the list of SMUGMUG APIs

    "and need to use REST, JSON, serialized PHP , or XML-RPC to interact with the service. SmugMug *highly* recommends using REST, JSON, or serialized PHP."

    Maybe one of those are PERL by another name?

    Ok.. so for now I'll run with PHP (not to late to switch to PERL if there's some reason).

    Thanks for code example.

    Chuck

    ok, what u need to understand is that XML-RPC, REST, JSON and serialized PHP are just request/response mechanisms to exchange messages. Perl and php could use any of them...but would need different levels processing for responses.

    so, you need to determine the language you want to write your code in. Then once you have decided, choose the appropriate API.

    Hope this helps,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • Options
    kansaschuckkansaschuck Registered Users Posts: 50 Big grins
    edited December 13, 2006
    devbobo wrote:
    ok, what u need to understand is that XML-RPC, REST, JSON and serialized PHP are just request/response mechanisms to exchange messages. Perl and php could use any of them...but would need different levels processing for responses.

    so, you need to determine the language you want to write your code in. Then once you have decided, choose the appropriate API.

    Hope this helps,

    David

    ok..
    (getting smarter)... A PERL script could include PHP? I thought PERL scripts were in .cgi files with special commands that told the systems they were PERL... and .php files were the same.
  • Options
    devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited December 13, 2006
    ok..
    (getting smarter)... A PERL script could include PHP? I thought PERL scripts were in .cgi files with special commands that told the systems they were PERL... and .php files were the same.
    cgi is one way to implement perl, for web apps. But it can also run as a standalone script/app that you run from the command line on your computer.

    I am pretty sure there are some perl modules that allow for translation of php.
    David Parry
    SmugMug API Developer
    My Photos
  • Options
    kansaschuckkansaschuck Registered Users Posts: 50 Big grins
    edited December 14, 2006
    devbobo wrote:
    cgi is one way to implement perl, for web apps. But it can also run as a standalone script/app that you run from the command line on your computer.

    I am pretty sure there are some perl modules that allow for translation of php.


    Ok...Thanks!

    I'm off coding! :D
Sign In or Register to comment.