Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created May 26, 2009 13:56
Show Gist options
  • Save sharifulin/118079 to your computer and use it in GitHub Desktop.
Save sharifulin/118079 to your computer and use it in GitHub Desktop.
Script for parsing JavaScript object and generate plist
#!/usr/bin/perl
use utf8;
use strict;
BEGIN {
open STDERR, '>>', '../log/****.log';
$SIG{'__WARN__'} = sub { CORE::warn sprintf "[%s] %s", scalar localtime, @_ };
};
use lib '/path/my/lib';
use XML::Util ':all';
use JSON;
use LWP::UserAgent;
my $URL = 'http://***.js?id=' . time;
my $PLIST = '../data/***.plist';
my $r = LWP::UserAgent->new(
timeout => 15,
agent => '****'
)->get($URL);
if (my $data = [ $r->content =~ m{ "stat" , \s+ ( .+ ) \); }xs ]->[0]) {
$data =~ s{([A-Za-z]+)\s*: }{"$1":}g;
print plist($PLIST, from_json($data));
} else {
warn $r->status_line;
}
sub plist($$) {
my $file = shift;
my $plist = join('',
PI('xml', {'version' => '1.0', 'encoding' => 'UTF-8'}), "\n",
qq(<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">),
T('plist', {'version' => '1.0'},
T('dict', undef,
map {
my $h = $_;
T('key', undef, $h->{'mainfield'}),
T('dict', undef,
map { ( T('key', undef, $_), T('string', undef, escape($h->{$_})) ) } 'field1', 'field2', 'field3', 'fieldN'
)
}
@{shift->{'groupfield'} || []}
)
)
);
{
open my $fh, '>', "$file~" or die "Can't open file $file~: $!";
print $fh $plist;
}
rename "$file~", $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment