Anyway, in case anyone else would want to mirror a log or a few as well, here it is:
#! /usr/bin/env pike
constant address = "http://www.advogato.org/XMLRPC";
int main( int argc, array(string) argv )
{
Protocols.XMLRPC.Client client = Protocols.XMLRPC.Client( address );
for( int i=1; i<argc; i++ )
mirror( argv[i], client );
}
int mirror( string user, Protocols.XMLRPC.Client client )
{
Calendar.TimeRange ctime, mtime;
int entries, i;
string html, filename;
[entries] = client["diary.len"]( user );
for( i=0; i<entries; i++ )
{
[[ctime, mtime]] = client["diary.getDates"]( user, i );
[html] = client["diary.get"]( user, i);
string filename = user + "." + i + ".txt";
Stdio.write_file( filename, html + "\n" + ctime->format_time() );
System.utime( filename, ctime->unix_time(), mtime->unix_time() );
}
return entries;
}
out of curiosity, why are you doing variable assignments in []?
ReplyDelete[entries] = ...
[[ctime, mtime]] = ...
[html] = ...
[] are only used/needed for assignment to multiple variablkes at once:
entries = ...
[ctime, mtime] = ...
html = ...
greetings, eMBee.
Because the XMLRPC.Client class wrapped things up in levels of arrays everywhere and I preferred this syntax over indexing out lots of things. Less magic numeric constants and more focus on the code itself, where it's just positional parameters mapping towards corresponding positional parameters in Advogato's XMLRPC API anyway.
ReplyDeleteI would have ended up with lots of temporary variables and cruft just to read out the data from returned results, had I done it otherwise.
oh, johan, this is your blog.
ReplyDeletei didn't recognize you by that half cut of face in the upper left corner :-)))
(got to watch this more closely now :-)
with regards to accessing things wrapped in arrays, this only works if you can be sure if the array never changes size.
as for the number constants, you'd just append [0] to every expression, and not get any additional temporary variables.
wether [a] = arr; is more readable than
a = arr[0]; i am not sure. the latter is more fault tolerant.
i don't know why XMLRPC.Client class wraps ecerything in arrays, but it maybe to allow the return of multiple values at some point, because if not, i can see no reason to return arrays in the first place. (unless they are playing games with referencing basic types)
greetings, eMBee.
That's actually the beauty with the XMLRPC API:s; you do know the layout of what you get back, and don't need to fuss around like that. I'm not suggesting it's good for your everyday programming, but it improved the code in this piece IMO.
ReplyDeleteAnd regarding XMLRPC.Client, I believe the design makes some sense, to make it wrap everything in an identical generic way; if I recall correctly, I did read through the code and the specs at the time since it didn't make sense to me, before having done that. (Then I immediately forgot all the details -- so I'm not going to pretend this description settles the design issue. :-)
(I also don't see any good way of doing [[ctime, mtime]] = ... without using this assignment type, and still avoid using any temporary variables.)
I determined to transmigrate my antediluvian Advogato weblog to my WordPress installing tonight. I utilise Frontier - Client and XML RPC to have it off. It was surprisingly painless and simply took me about 25 transactions.
ReplyDelete