-
Notifications
You must be signed in to change notification settings - Fork 0
/
motd_bot.pl
executable file
·140 lines (114 loc) · 3.26 KB
/
motd_bot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env perl
#
# quaddicted motd bot for twitter
# 10/22/22 - morb
#
use strict;
use warnings;
use utf8;
use lib ".";
use MyLib::ATProto qw( post_media );
use List::Util qw(shuffle);
use HTML::FormatText::Lynx;
use Config::IniFiles;
use Mastodon::Client;
use LWP::UserAgent;
use MIME::Base64;
use XML::Simple;
binmode( STDOUT, ":utf8" );
################################################################
my $mins = $settings->val( 'mastodon', 'ins' );
my $mkey = $settings->val( 'mastodon', 'ckey');
my $msec = $settings->val( 'mastodon', 'csec');
my $mat = $settings->val( 'mastodon', 'at' );
################################################################
my $mt = Mastodon::Client->new(
instance => $mins,
client_id => $mkey,
client_secret => $msec,
access_token => $mat,
coerce_entities => 0,
);
my $xml = XMLin('quaddicted_database.xml');
my $db = $xml->{"file"};
srand;
my @names = ( keys %$db );
my $entry = ( shuffle(@names) );
my $fn = "motd.jpg";
my $desc = HTML::FormatText::Lynx->new(
leftmargin => 0,
rightmargin => 0,
lynx_options => ['-nolist']
)->format( $db->{$entry}->{"description"} );
my $urlbase = "https://www.quaddicted.com/reviews";
my $ssurl = $urlbase . "/screenshots/$entry.jpg";
my $enurl = $urlbase . "/$entry.html"; $enurl =~ (s/\ /+/g);
my $desclen = length $desc;
my $taglen = 0;
my $tagmax = 5;
my $tagout = '';
my $ml = 250;
my $authors = $db->{$entry}->{"author"};
my $authbuf;
if (length $authors >= 50) {
$authbuf = (substr $authors,0,50) . "...";
} else {
$authbuf .= $authors;
}
#dumb tag handler
if ( $db->{$entry}->{'tags'} ) {
my $test = $db->{$entry}->{'tags'}->{'tag'};
if ( ref $test ) {
my @tags = @{ $db->{$entry}->{'tags'}->{'tag'} };
$taglen = scalar @tags;
if ($taglen > $tagmax) { $taglen = $tagmax; }
for ( my $i = 0 ; $i < $taglen ; $i++ ) {
$tags[$i] =~ s/(\ |-)//g;
$tagout .= "#" . $tags[$i] . " ";
}
}
else {
$tagout .= "#" . $test;
}
}
my $output =
"Author(s): "
. $authbuf . "\n"
. "Title: "
. $db->{$entry}->{"title"} . "\n"
. "Date: "
. $db->{$entry}->{"date"} . "\n";
my $outlen = length $output;
my $descbuf = substr $desc, 0, ( $ml - ( $outlen + $taglen ) );
if ($outlen + ( length $descbuf ) <= 220 ) {
$output .= $descbuf . "\n";
}
$outlen = length $output;
$output .= $enurl . "\n";
# bluesky can't handle >300c
if ($outlen + (length $tagout ) + 2 < 300 ) {
$output .= $tagout;
}
print $output . "\n";
print length $output . "\n";
my $status_update = { status => $output };
unless (fetch($ssurl)) {
$fn = "no_screenshot.jpg";
}
$status_update->{media_ids} = &chunklet;
my $mm = $mt->upload_media($fn);
$mt->post_status($output,{ media_ids => [ $mm->{'id'} ]});
my $bluesky = post_media({ text => "$output", account => 'motd', fn => "$fn" });
sub fetch {
my ($file) = @_;
my $ua = LWP::UserAgent->new( agent => "quaddicted_motd_bot/0.1" );
my $req = HTTP::Request->new( GET => $file );
my $resp = $ua->request($req);
if ( $resp->is_success ) {
open( my $FH, ">", $fn );
print $FH $ua->request($req)->content;
close $FH;
} else {
return 0;
}
}