SpreadRevolutionaryDate/BlueskyLite.pm

filter: Statementstmt 28.9% 44 / 152 Branchbran 1.8% 1 / 54 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 73.3% 11 / 15 Podpod 100% 2 / 2 totaltotal 23.2% 58 / 249 CC 36 SCAR 45.8
CC
36
Cov
20%
_generate_facets
156.0
_fetch_embed_url_card
132.0
create_post
132.0
CRAP 97.4
« SpreadRevolutionaryDate/MsgMaker/Gemini.pm ↑ Summary SpreadRevolutionaryDate/Target/Mastodon.pm »
1 #
2 # This file is part of App-SpreadRevolutionaryDate
3 #
4 # This software is Copyright (c) 2019-2026 by Gérald Sédrati.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU General Public License, Version 3, June 2007
9 #
10 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 package App::SpreadRevolutionaryDate::BlueskyLite;
13 6 $App::SpreadRevolutionaryDate::BlueskyLite::VERSION = '0.54';
14 # ABSTRACT: Simple Class to post status to BlueSky.
15  
16 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use File::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 0 ▶sub _fetch_embed_url_card {
Subroutine _fetch_embed_url_card: not called CC=11 SCAR=48.8
27 0 my $self = shift;
28 0 ▶ my $url = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
29  
30 0 my $card = {uri => $url};
31  
32 0 my $ua = LWP::UserAgent->new(env_proxy => 1, timeout => 10, agent =>'App::SpreadRevolutionaryDate bot');
33 0 my $response = $ua->get($url);
34 0 ▶ return unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
35 0 my $content = $response->content;
36 0 ▶ return unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
37  
38 0 ▶ if ($content =~ /<meta\s+property="og:title"\s+content="([^"]+)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /<meta\s+property="og:title"\s+content="([^"]+)"/u) { } 0 0 0
39 0 my $title = $1;
40 0 ($card->{title}) = decode_utf8($title);
41 } else {
42 0 $card->{title} = '';
43 }
44 0 ▶ if ($content =~ /<meta\s+property="og:description"\s+content="([^"]+)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /<meta\s+property="og:description"\s+content="([^"]+)"/u) { } 0 0 0
45 0 my $description = $1;
46 0 ($card->{description}) = decode_utf8($description);
47 } else {
48 0 $card->{description} = '';
49 }
50 0 ▶ if ($content =~ /<meta\s+property="og:image"\s+content="([^"]+)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /<meta\s+property="og:image"\s+content="([^"]+)"/u) 0 0 0
51 0 my $img_url = $1;
52 0 ▶ unless ($img_url =~ m!://!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($img_url =~ m[://]u) 0 0 0
53 0 ▶ $url = "$url/" unless $url =~ m!/$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[/$]u 0 0 0
54 0 $img_url = $url . $img_url;
55 }
56 0 my $img_response = $ua->get($img_url);
57 0 ▶ return unless $img_response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $img_response->is_success 0 0 0
58  
59 0 my $blob_req = HTTP::Request->new('POST', 'https://bsky.social/xrpc/com.atproto.repo.uploadBlob');
60 0 $blob_req->header('Content-Type' => $img_response->header('Content-Type'));
61 0 $blob_req->content($img_response->content);
62 0 my $blob_response = $self->{ua}->request($blob_req);
63 0 ▶ return unless $blob_response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $blob_response->is_success 0 0 0
64  
65 0 my $blob_content = decode_json($blob_response->decoded_content);
66 0 ($card->{thumb}) = $blob_content->{blob};
67 }
68  
69 0 return $card;
70 }
71  
72 0 ▶sub _lookup_repo {
Subroutine _lookup_repo: not called CC=4 SCAR=30.0
73 0 my $self = shift;
74 0 ▶ my $account = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
75 0 ▶ $account .= '.bsky.social' unless $account =~ /[⋅]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $account =~ /[\x{22c5}]/u 0 0 0
76 0 my $uri = URI->new('https://bsky.social/xrpc/com.atproto.identity.resolveHandle');
77 0 $uri->query_form(handle => $account);
78 0 my $response = $self->{ua}->get($uri);
79 0 ▶ return if !$response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
80  
81 0 my $content = decode_json($response->decoded_content);
82 0 return $content->{did};
83 }
84  
85 0 ▶sub _generate_facets {
Subroutine _generate_facets: not called CC=12 SCAR=50.5
86 0 my $self = shift;
87 0 ▶ my $text = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
88 0 my $facets = [];
89 0 my $embed;
90 0 my $pos = 0;
91 0 foreach my $w (split /\s+/, $text) {
92 0 my ($type, $attrib, $val);
93 0 $w =~ s/[.,:;'"!\?()]+$//;
94 0 $w =~ s/^[.,:;'"!\?()]+//g;
95 0 ▶ if ($w =~ /^https?\:\/\//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w =~ m[^https?\://]u) { } 0 0 0
elsif ($self->{'did'} and $w =~ /^@/u) { } 0 0 0
elsif ($w =~ /^#/u) { } 0 0 0
96 0 $type = 'app.bsky.richtext.facet#link';
97 0 $attrib = 'uri';
98 0 $val = $w;
99 ▶ } elsif ($self->{did} && $w =~ /^@/) {
Condition0%0/3
$self->{"did"} and $w =~ /^@/u
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"did"} and $w =~ /^@/u
$self->{"did"} $w =~ /^@/u
Truth table0%0/3
A and B
A= $self->{"did"}B= $w =~ /^@/u
ABresult
0X0
100
111
100 0 $val = $self->_lookup_repo(substr($w, 1));
101 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
102 0 $type = 'app.bsky.richtext.facet#mention';
103 0 $attrib = 'did';
104 }
105 } elsif ($w =~ /^#/) {
106 0 $val = substr($w, 1);
107 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
108 0 $type = 'app.bsky.richtext.facet#tag';
109 0 $attrib = 'tag';
110 }
111 }
112  
113 0 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 0 0 0
114 0 utf8::encode(my $text_bytes = $text);
115 0 $pos = index($text_bytes, $w, $pos);
116 0 my $end = $pos + length($w);
117  
118 0 push @$facets, {
119 features => [
120 {
121 '$type' => $type,
122 $attrib => $val,
123 },
124 ],
125 index => {
126 byteStart => $pos,
127 byteEnd => $end,
128 },
129 };
130  
131 0 ▶ unless ($embed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($embed) 0 0 0
132 0 my $card = $self->_fetch_embed_url_card($val);
133 0 ▶ if ($card) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card) 0 0 0
134 0 $embed = {
135 '$type' => 'app.bsky.embed.external',
136 'external' => $card,
137 };
138 }
139 }
140  
141 0 $pos = $end;
142 }
143 }
144 0 return ($facets, $embed);
145 }
146  
147  
148 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.5
Pod documented
149 5 my ($class, %args) = @_;
150  
151 5 my %self;
152 5 $self{ua} = LWP::UserAgent->new(env_proxy => 1, timeout => 10, agent =>'App::SpreadRevolutionaryDate bot');
153 5 $self{ua}->default_header('Accept' => 'application/json');
154  
155 my %payload = (
156 identifier => $args{'identifier'},
157 5 password => $args{'password'},
158 );
159 5 my $json = encode_json(\%payload);
160  
161 5 my $req = HTTP::Request->new('POST', 'https://bsky.social/xrpc/com.atproto.server.createSession');
162 5 $req->header('Content-Type' => 'application/json');
163 5 $req->content($json);
164 5 my $response = $self{ua}->request($req);
165  
166 5 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 5 5
167 0 my $content = decode_json($response->decoded_content);
168 0 my $did = $content->{did};
169 0 my $access_jwt = $content->{accessJwt};
170 0 $self{ua}->default_header('Authorization' => 'Bearer ' . $access_jwt);
171 0 $self{did} = $did;
172 }
173  
174 5 my $self = bless(\%self, $class);
175 5 return $self;
176 }
177  
178  
179 0 ▶sub create_post {
Subroutine create_post: not called CC=11 SCAR=48.8
Pod documented
180 0 my ($self, $text, $img, $reply) = @_;
181  
182 0 my ($facets, $embed) = $self->_generate_facets($text);
183 my $payload = {
184 repo => $self->{did},
185 0 ▶ collection => 'app.bsky.feed.post',
Branch: 1 branch
BranchTrueFalseTotal
$embed ? : 0 0 0
186 record => {
187 text => $text,
188 facets => $facets,
189 ($embed ?
190 (embed => $embed)
191 : ()
192 ),
193 createdAt => DateTime->now->iso8601 . 'Z',
194 },
195 };
196  
197 0 ▶ if ($img) {
Branch: 1 branch
BranchTrueFalseTotal
if ($img) 0 0 0
198 0 ▶ $img = {path => $img} unless ref($img) && ref($img) eq 'HASH' && $img->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $img and ref $img eq "HASH" and $img->{"path"} 0 0 0
Condition0%0/6
ref $img and ref $img eq "HASH" and $img->{"path"}
!ll&&!rl&&r
000
ref $img and ref $img eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $img and ref $img eq "HASH" and $img->{"path"}
ref $img ref $img eq "HASH" $img->{"path"}
Truth table0%0/4
A and B and C
A= ref $imgB= ref $img eq "HASH"C= $img->{"path"}
ABCresult
0XX0
10X0
1100
1111
199 0 my $ft = File::Type->new();
200 0 my $mime_type = $ft->mime_type($img->{path});
201  
202 0 ▶ my $img_alt = $img->{alt} // ucfirst(fileparse($img->{path}, qr/\.[^.]*/));
Condition0%0/2
$img->{"alt"} // ucfirst fileparse($img->{"path"}, qr/\.[^.]*/u)
l!l
00
MC/DC0%0/1
$img->{"alt"} // ucfirst fileparse($img->{"path"}, qr/\.[^.]*/u)
$img->{"alt"}
Truth table0%0/2
A
A= $img->{"alt"}
Aresult
11
00
203  
204 0 my $img_bytes;
205 0 ▶ open my $fh, '<', $img->{path} or die "Cannot read $img->{path}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $img->{"path"} or die qq[Cannot read $img->{"path"}: $!\n] 0 0 0
206 {
207 0 local $/;
208 0 $img_bytes = <$fh>;
209 }
210 0 close $fh;
211  
212 0 my $blob_req = HTTP::Request->new('POST', 'https://bsky.social/xrpc/com.atproto.repo.uploadBlob');
213 0 $blob_req->header('Content-Type' => $mime_type);
214 0 $blob_req->content($img_bytes);
215 0 my $blob_response = $self->{ua}->request($blob_req);
216 0 ▶ return unless $blob_response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $blob_response->is_success 0 0 0
217  
218 0 my $blob_content = decode_json($blob_response->decoded_content);
219 $payload->{record}->{embed} = {
220 '$type' => 'app.bsky.embed.images',
221 images => [
222 {
223 alt => $img_alt,
224 image => $blob_content->{blob},
225 },
226 0 ],
227 };
228 }
229  
230 0 ▶ if ($reply) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply) 0 0 0
231 0 $payload->{record}->{reply} = $reply;
232 }
233  
234 0 my $json = encode_json($payload);
235 0 my $req = HTTP::Request->new('POST', 'https://bsky.social/xrpc/com.atproto.repo.createRecord');
236 0 $req->header('Content-Type' => 'application/json');
237 0 $req->content($json);
238 0 my $response = $self->{ua}->request($req);
239 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
240 0 return decode_json($response->decoded_content);
241 } else {
242 0 return $response;
243 }
244 }
245  
246  
247 # A module must return a true value. Traditionally, a module returns 1.
248 # But this module is a revolutionary one, so it discards all old traditions.
249 # Idea borrowed from Jean Forget's DateTime::Calendar::FrenchRevolutionary.
250 6 "Quand le gouvernement viole les droits du peuple,
251 l'insurrection est pour le peuple le plus sacré
252 et le plus indispensable des devoirs";
253  
254 __END__
« SpreadRevolutionaryDate/MsgMaker/Gemini.pm ↑ Summary SpreadRevolutionaryDate/Target/Mastodon.pm »