#! /usr/bin/perl use strict; use utf8; use warnings; use JSON::PP; use Time::Local; sub gmdate { my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = gmtime($_[0]); return {sec=>$sec, min=>$min, hour=>$hour, day=>$day, mon0=>$mon, mon1=>$mon+1, year=>$year+1900, wday=>$wday, yday=>$yday, isdst=>$isdst, unix=>$_[0]}; } my %posts; binmode STDOUT, ':utf8'; die "Please specify an archive on the command line" unless $ARGV[0]; die "Not a cohost archive" unless -d $ARGV[0] . '/project'; #for my $fn (glob('/Users/chri/Downloads/archives/cohost/export-85359-1210234936947115965/project/felidoptera/posts/published/*/post.json')) { for my $fn (glob($ARGV[0] . '/project/*/posts/published/*/post.json')) { local $/; open my $fh, '<', $fn; my $json = decode_json(<$fh>); close $fh; my $text = "
{postId}\">\n"; if($json->{headline}) { #$text .= "

{postId}\"># $json->{headline}

\n"; $text .= "

$json->{headline}

\n"; } else { #$text .= "

{postId}\"># (post)

\n"; $text .= "

(post)

\n"; } for my $b (@{$json->{blocks}}) { if($b->{type} eq 'attachment' && $b->{attachment}{kind} eq 'image') { $b->{attachment}{fileURL} =~ /[^\/]*$/ or die; my $src = $&; my $alt = $b->{attachment}{altText} =~ s/&/&/gr =~ s/"/"/gr; $text .= "

\"$alt\"

\n"; } elsif($b->{type} eq 'attachment' && $b->{attachment}{kind} eq 'audio') { $b->{attachment}{fileURL} =~ /[^\/]*$/ or die; my $src = $&; $text .= "

(download)

\n"; } elsif($b->{type} eq 'markdown') { my $md = $b->{markdown}; $md = $md->{content} if ref $md; $text .= "

$md

\n"; } else { $text .= "

?????

\n"; warn "Unrecognized block type in $fn"; } } $json->{publishedAt} =~ /^(\d{4})-(\d{1,2})-(\d{1,2})T(\d{1,2}):(\d{1,2}):(\d{1,2})/ or die "Date did not match"; my ($yr,$mo,$da,$hr,$mi,$se) = ($1,$2,$3,$4,$5,$6); my $date = gmdate(timegm($se,$mi,$hr,$da,$mo-1,$yr) - 43200); my $display_date = "$date->{year}-$date->{mon1}-$date->{day}"; if($json->{tags} && @{$json->{tags}}) { $text .= "

#" . join(' #', @{$json->{tags}}) . "

\n"; } $text .= "

{singlePostPageUrl}\">Original posted . {postId}\">Permalink

\n"; $text .= "
\n\n"; $posts{$json->{publishedAt}} = $text; } for my $k (sort {$a cmp $b} keys %posts) { print $posts{$k}; }