Skip to content

Instantly share code, notes, and snippets.

@samebchase

samebchase/gen-gemini.raku Secret

Last active Mar 24, 2021
Embed
What would you like to do?
Quick and dirty way to generate Gemini files of my Twitter archive
#!/usr/bin/env raku
use JSON::Fast;
use Terminal::ANSIColor;
#use Grammar::Tracer;
grammar TwitterDate {
token TOP { <day-of-week> ' '
<month> ' '
<day> ' '
<hh> ':' <mm> ':' <ss> ' '
'+' <tz> ' '
<yyyy> }
token day { \d ** 2 }
token day-of-week { 'Sun' | 'Mon' | 'Tue' |
'Wed' | 'Thu' | 'Fri' |
'Sat' }
token month { 'Jan' | 'Feb' | 'Mar' | 'Apr' |
'May' | 'Jun' | 'Jul' | 'Aug' |
'Sep' | 'Oct' | 'Nov' | 'Dec' }
token tz { \d ** 4 }
token hh { \d ** 2 }
token mm { \d ** 2 }
token ss { \d ** 2 }
token yyyy { \d ** 4 }
}
my %month-to-num = 'Jan' => 1,
'Feb' => 2,
'Mar' => 3,
'Apr' => 4,
'May' => 5,
'Jun' => 6,
'Jul' => 7,
'Aug' => 8,
'Sep' => 9,
'Oct' => 10,
'Nov' => 11,
'Dec' => 12;
sub is-reply(%tweet) {
so %tweet<tweet><in_reply_to_status_id>;
}
sub is-rt(%tweet) {
%tweet<tweet><full_text>.starts-with('RT');
}
sub has-mentions(%tweet) {
%tweet<tweet><entities><user_mentions>.elems > 0
or
%tweet<tweet><full_text>.starts-with('@')
}
sub has-hashtags(%tweet) {
%tweet<tweet><entities><hashtags>.elems > 0
}
sub has-links(%tweet) {
%tweet<tweet><entities><urls>.elems > 0
}
sub has-media(%tweet) {
so %tweet<tweet><entities><media>
}
sub created-at(%tweet) {
my $match = TwitterDate.parse(%tweet<tweet><created_at>);
return DateTime.new(
year => $match<yyyy>.Int,
month => %month-to-num{$match<month>.Str},
day => $match<day>.Int,
hour => $match<hh>.Int,
minute => $match<mm>.Int,
second => $match<ss>.Int,
);
}
sub created-at-asc(%t1, %t2) {
created-at(%t1) <=> created-at(%t2);
}
sub created-at-desc(%t1, %t2) {
created-at(%t2) <=> created-at(%t1);
}
sub gen-tweet-stream($path) {
my $file-contents = slurp($path);
my @json-array = from-json $file-contents;
my $i = 1;
for @json-array.grep({ !is-reply($_) })
.grep({ !is-rt($_) })
.grep({ !has-mentions($_) })
.grep({ !has-hashtags($_) })
.grep({ !has-links($_) })
.grep({ !has-media($_) })
.sort({ created-at-asc $^a, $^b })
.rotor(33)
-> @tweets {
my %first-tweet = @tweets.head;
my $first-tweet-created-at = created-at(%first-tweet);
my $batch-filename = "$i.gmi";
my $file-contents = '';
say colored("$i $batch-filename $first-tweet-created-at:", "bold blue on_black");
for @tweets -> %tweet {
my $created-at = created-at(%tweet);
$file-contents ~= "### {$created-at.yyyy-mm-dd} {$created-at.hh-mm-ss}\n\n" ~
"%tweet<tweet><full_text>\n\n";
}
$file-contents ~= "=> gemini://gemini.samebchase.com/birdsite/{$i.succ}.gmi next\n";
$file-contents ~= "=> gemini://gemini.samebchase.com/birdsite/{$i.pred}.gmi prev";
spurt "generated/$batch-filename", $file-contents;
say "generated file $batch-filename";
$i++;
}
}
sub MAIN() {
gen-tweet-stream('data/tweets.json');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment