Skip to content

Instantly share code, notes, and snippets.

View JJ's full-sized avatar
🏠
Working from home

Juan Julián Merelo Guervós JJ

🏠
Working from home
View GitHub Profile
@JJ
JJ / server-post-v3-by-massa.raku
Created November 30, 2022 17:11
Smarter server post
sub format-error(X::Cro::HTTP::Error $_) {
my $status-line = .response.Str.lines.first;
my $resp-body = do { await .response.body-blob }.decode;
my $req-method = .request.method;
my $req-target = .request.target;
my $req-body = do { await .request.body-blob }.decode;
"ERROR $status-line WITH $resp-body FOR $req-method $req-target WITH $req-body"
}
sub server-post($data) {
@JJ
JJ / server-post-v2-by-massa.raku
Created November 30, 2022 17:10
Server post with retries by Humberto Massa
constant NUMBER-OF-RETRIES = 3; # YMMV
constant COOLING-OFF-PERIOD = 2; # this is plenty to stall this thread
sub server-post($data) {
our $cro;
do {
my $r = await $cro.post: "{SERVER-URI}/{DATA-PATH}", body => $data;
my $count = NUMBER-OF-RETRIES;
while $count-- and $r.status == 503|401 {
sleep COOLING-OFF-PERIOD;
@JJ
JJ / server-login-post-by-massa.raku
Created November 30, 2022 17:07
Reading CSV, server-side
sub server-login() {
my Lock \l .= new;
our $cro;
l.protect: {
my $c = Cro::HTTP::Client.new:
base-uri => SERVER-URL,
content-type => JSON,
user-agent => 'honking/2022.2.1',
timeout => %(
connection => 240,
@JJ
JJ / hyperized-csv-reading-by-massa.raku
Created November 30, 2022 17:06
hyperized CSV reading
sub MAIN(Str $input) {
my @r = lazy read-csv $input;
server-login;
react {
whenever supply {
.emit for @r.hyper(:8degree, :16batch)
.map(&csv-to-yaml)
} {
server-post $_
}
@JJ
JJ / read-csv-by-massa.raku
Created November 30, 2022 17:04
Read and convert a CSV file - first version
sub read-csv(IO() $file) {
gather {
my $f = $file.open: :r :!chomp;
with Text::CSV.new {
.header: $f, munge-column-names => { S:g /\W+//.samemark('x').lc };
while my $row = .getline-hr: $f { take $row }
}
}
}
@JJ
JJ / parsing-malware-by-terceranexus6.raku
Created November 28, 2022 07:30
Parsing malware files with Raku
sub MAIN (Str :$fi = '', Str :$fo = '') {
# some genes in the binary
my token gen1 {'InterfaceSpeedTester9Calc'}
my token gen2 {'ScheduledCtrl9UpdateJobERK'}
my token gen3 {'ScanHardwareInfoPSt'}
my regex sparkling2 {
[
<gen1>|<gen2>|^<gen3>$
@JJ
JJ / parsing-malware-1.raku
Last active November 28, 2022 07:27
Parsing malware files by @terceranexus6
my $c = 1;
for "$fo/$fi".IO.lines -> $line {
# If the line contains the gene, print it
if $line ~~ &sparkling_goblin {say "Sparkling Goblin found: "; say $line; say "in line $c"; say "in file $fi"; say " "; }
#if $line ~~ &sparkling2 {say "Sparkling Goblin complex regex found: "; say $line; say "in line $c"; say " "; }
$c++;
}
@JJ
JJ / combine-pds.raku
Created November 26, 2022 17:57
Combine PDs by Tom Browder
use PDF::Lite;
use PDF::Font::Loader;
my @pdfs = <list of pdf files to combine>;
# create a new pdf to hold the entire collection
my $pdf = PDF::Lite.new;
$pdf.media-box = 'Letter';
my $centerx = 4.25*72; # horizontal center of the page
# the cover
my PDF::Lite::Page $page = $pdf.add-page;
@JJ
JJ / using-rakupod-object-by-tbrowder.raku
Created November 26, 2022 17:55
Using Rakupod Object
use Pod::Lite;
use Pod::To::PDF::Lite;
use RakupodObject;
my $pod-object = rakupod2object $pod-doc;
# pod2pdf $pod-object # args ...
@JJ
JJ / raku-advent-2022-markdown-grammar.raku
Created November 25, 2022 14:06
Raku Advent 2022: Markdown Grammar by Tom Browder
# By Tom Browder https://github.com/tbrowder
use Markdown::Grammar:ver<0.4.0>;
my $markdown-doc = "poem.md";
my $pod-doc = "poem.rakudoc";
$pod-doc = from-markdown $markdown-doc, :to("pod");