#! /usr/bin/perl use strict; my $username=shift; my $rssurl="https://mastodon.social/users/$username.rss"; system("wget $rssurl"); my $fname="$username.rss"; my $dir="gemlog"; my $count=`xmllint --xpath 'count(/rss/channel/item)' $fname`; my %post; for(my $i=1;$i<=$count;$i++) { my $date=`xmllint --xpath "string(/rss/channel/item[$i]/pubDate)" $fname`; my $text=`xmllint --xpath "string(/rss/channel/item[$i]/description)" $fname`; my $media=`xmllint --xpath "string(/rss/channel/item[$i]/*[name()='media:content']/\@url)" $fname`; chop $media; # figure out the date $date=~/ (\d+) (\S+) (\d+) /; my $filedate="$3-$2-$1"; # html to gemtext conversion $text=~s@

@@g; $text=~s@

@@g; $text=~s@
@\n@g; # hashtags are converted to links # $text=~s@#(.*)@ #$1@g; if ($text=~m@()@) { my $a_tag=$1; if($a_tag =~ m@href=\"([^\"]+)\"@) { my $url=$1; substr($text, index($text, $a_tag), length($a_tag))="\n=> $url\n"; } } # convert quoted chars, there are probaly more to do $text=~s@'@'@g; $text=~s@"@"@g; if($media ne "") { system("wget -P $dir/img -nc $media"); $media =~ s@.*/@@; $media="=> img/$media\n"; } $post{$filedate} || ($post{$filedate}="# $filedate\n"); $post{$filedate}.="## $date $text $media "; } foreach my $d (keys(%post)) { print "$d\n"; open(FILE, ">$dir/$d.gmi"); print FILE $post{$d}; close FILE; }