Using threads, because it's fun:)
This commit is contained in:
161
pcurse.pm
161
pcurse.pm
@ -1,21 +1,44 @@
|
||||
#!/usr/bin/env perl
|
||||
package pcurse;
|
||||
use JSON;
|
||||
use HTML::HTML5::Parser;
|
||||
use LWP::UserAgent ();
|
||||
use strict;
|
||||
no warnings 'all';
|
||||
use Getopt::Long;
|
||||
use Archive::Extract;
|
||||
use Thread::Pool;
|
||||
use JSON;
|
||||
use LWP::UserAgent;
|
||||
use HTML::HTML5::Parser;
|
||||
use IO::Socket::SSL;
|
||||
use feature ':5.10';
|
||||
|
||||
sub merge_opts {
|
||||
my $opts = shift;
|
||||
my $conf = shift;
|
||||
if(defined($opts->{'debug'})) {
|
||||
print 'We got opts:'."\n";
|
||||
print Dumper $opts;
|
||||
print 'We got conf:'."\n";
|
||||
print Dumper $conf;
|
||||
}
|
||||
foreach my $k(keys %{$opts}) {
|
||||
next unless(defined($opts->{$k}));
|
||||
$conf->{$k} = $opts->{$k};
|
||||
}
|
||||
return $conf;
|
||||
}
|
||||
|
||||
sub parse_arguments {
|
||||
my %toret;
|
||||
GetOptions (
|
||||
"verbose+" => \$toret{'verbose'},
|
||||
"wowpath=s" => \$toret{'wowpath'},
|
||||
"baseuri=s" => \$toret{'baseuri'},
|
||||
"config=s" => \$toret{'config'},
|
||||
"test" => \$toret{'test'},
|
||||
my $toret;
|
||||
Getopt::Long::GetOptions (
|
||||
"verbose" => \$toret->{'verbose'},
|
||||
"wowpath=s" => \$toret->{'wowpath'},
|
||||
"baseuri=s" => \$toret->{'baseuri'},
|
||||
"config=s" => \$toret->{'config'},
|
||||
"test" => \$toret->{'test'},
|
||||
"workers" => \$toret->{'workers'},
|
||||
"debug" => \$toret->{'debug'},
|
||||
);
|
||||
return %toret;
|
||||
return $toret;
|
||||
}
|
||||
|
||||
sub load_config {
|
||||
@ -55,8 +78,9 @@ sub check_config {
|
||||
sub sane_defaults {
|
||||
my $in = shift;
|
||||
$in->{'baseuri'} = 'https://www.curseforge.com' unless(exists($in->{'baseuri'}));
|
||||
$in->{'config'} = $ENV{'HOME'}.'/.pcurse/config.json' unless(exists($in->{'config'}));
|
||||
$in->{'baseuri'} =~ s/^http/https/ unless($in->{'baseuri'} =~ m/^https/);
|
||||
$in->{'addons'} = $ENV{'HOME'}.'/.pcurse/addons.json' unless(exists($in->{'addons'}));
|
||||
$in->{'workers'} = "4" unless(exists($in->{'workers'}));
|
||||
return $in;
|
||||
}
|
||||
|
||||
@ -93,13 +117,21 @@ sub load_addons {
|
||||
|
||||
sub save_config {
|
||||
my $json = JSON->new;
|
||||
$json->convert_blessed;
|
||||
$json->allow_nonref;
|
||||
$json->allow_tags;
|
||||
$json->allow_blessed;
|
||||
my $file = shift;
|
||||
my $json_data = shift;
|
||||
my $text = $json->pretty->encode($json_data);
|
||||
open my $fh, ">", $file or return (0,'Could not open '.$file.' for writing: '.$!);
|
||||
print $fh $text;
|
||||
close $fh;
|
||||
return (1,$file.' saved successfully');
|
||||
if(ref $json_data eq 'ARRAY' or ref $json_data eq 'HASH') {
|
||||
my $text = $json->pretty->encode($json_data);
|
||||
open my $fh, ">", $file or return (0,'Could not open '.$file.' for writing: '.$!);
|
||||
print $fh $text;
|
||||
close $fh;
|
||||
return (1,$file.' saved successfully');
|
||||
} else {
|
||||
say 'Invalid format on passed data (not a HASH or ARRAY ref)';
|
||||
}
|
||||
}
|
||||
|
||||
sub import_json {
|
||||
@ -183,34 +215,44 @@ sub find_in_html {
|
||||
} else {
|
||||
#This means we're on our own = whatever we're getting here is a html document as a string, unparsed.
|
||||
my $parser = HTML::HTML5::Parser->new();
|
||||
my @file = split(/\n/, $html);
|
||||
foreach my $line(@file) {
|
||||
if($line =~ m/$sstring/) {
|
||||
my $parsed = $parser->parse_balanced_chunk($line);
|
||||
my @nodes = $parsed->nonBlankChildNodes();
|
||||
foreach my $node(@nodes) {
|
||||
my @atr = $node->attributes();
|
||||
if($mode eq 'dlstring') {
|
||||
my $href = $node->getAttribute('href');
|
||||
$retstr = (split(/$sstring/, $href,2))[1];
|
||||
} elsif($mode eq 'vstring') {
|
||||
$retstr = $node->getAttribute('data-name');
|
||||
if(defined($html)) {
|
||||
my @file = split(/\n/, $html);
|
||||
foreach my $line(@file) {
|
||||
if($line =~ m/$sstring/) {
|
||||
my $parsed = $parser->parse_balanced_chunk($line);
|
||||
my @nodes = $parsed->nonBlankChildNodes();
|
||||
foreach my $node(@nodes) {
|
||||
my @atr = $node->attributes();
|
||||
if($mode eq 'dlstring') {
|
||||
my $href = $node->getAttribute('href');
|
||||
$retstr = (split(/$sstring/, $href,2))[1];
|
||||
} elsif($mode eq 'vstring') {
|
||||
$retstr = $node->getAttribute('data-name');
|
||||
}
|
||||
return $retstr if($retstr);
|
||||
}
|
||||
return $retstr if($retstr);
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub download_update {
|
||||
my $uri = shift;
|
||||
my $fileid = shift;
|
||||
$uri .= '/download/'.$fileid.'/file';
|
||||
my ($ret,$filename,$file) = pcurse::download($uri);
|
||||
return (1,$filename,$file) if($ret);
|
||||
return (0,undef,undef);
|
||||
}
|
||||
|
||||
sub update {
|
||||
my $uri = shift;
|
||||
my $fileid = shift;
|
||||
my $filename = shift;
|
||||
my $file = shift;
|
||||
my $targetpath = shift;
|
||||
$uri .= '/download/'.$fileid.'/file';
|
||||
my ($filename,$file) = pcurse::download($uri);
|
||||
unless(-e "/tmp/$filename") {
|
||||
open my $fh, '>', "/tmp/$filename" or return 0;
|
||||
print $fh $file;
|
||||
@ -226,9 +268,52 @@ sub update {
|
||||
sub download {
|
||||
my $uri = shift;
|
||||
my $file = pcurse::http_get($uri);
|
||||
my $filename = $file->filename;
|
||||
my $content = $file->decoded_content;
|
||||
return ($filename,$content);
|
||||
if(defined($file)) {
|
||||
my $filename = $file->filename;
|
||||
my $content = $file->decoded_content;
|
||||
return (1,$filename,$content);
|
||||
} else {
|
||||
return (0,$uri,undef);
|
||||
}
|
||||
}
|
||||
|
||||
sub init_pool {
|
||||
my $w = shift;
|
||||
my $p = Thread::Pool->new( {
|
||||
workers => $w,
|
||||
do => sub {
|
||||
my $todo = shift;
|
||||
if($todo eq 'check') {
|
||||
my $addon = shift;
|
||||
my $conf = shift;
|
||||
my $html = pcurse::html_get($conf->{'baseuri'}.$addon->{'uri'});
|
||||
my $fileid = pcurse::get_latest_file_id($html,$addon->{'uri'});
|
||||
if($fileid) {
|
||||
$addon->{'fileid'} = $fileid;
|
||||
my $version = pcurse::get_product_version($html,$addon->{'uri'},$fileid);
|
||||
if($version && ($version ne $addon->{'version'})) {
|
||||
unless($conf->{'test'}) {
|
||||
$addon->{'targetversion'} = $version;
|
||||
return (1,$addon);
|
||||
}
|
||||
} else {
|
||||
return (0,'No need to update');
|
||||
}
|
||||
} else {
|
||||
return (0,'Could not find file id for '.$addon->{'name'});
|
||||
}
|
||||
} elsif($todo eq 'download') {
|
||||
my $uri = shift;
|
||||
my $fileid = shift;
|
||||
my ($ret,$filename,$file) = pcurse::download_update($uri,$fileid);
|
||||
return { retval => $ret, filename => $filename, filecontent => $file } if($ret);
|
||||
return { retval => 0, filename => undef, filecontent => undef };
|
||||
} else {
|
||||
return (0,'Unknown task');
|
||||
}
|
||||
},
|
||||
});
|
||||
return $p;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user