Adding computer readable news.

This commit is contained in:
sergiotarxz 2024-05-06 21:25:53 +02:00
parent 08244ef470
commit 161ca9b759
4 changed files with 124 additions and 21 deletions

View File

@ -7,6 +7,7 @@ use warnings;
use utf8;
use Mojo::Base 'Mojolicious', -signatures;
use Mojo::JSON;
use Mojo::UserAgent;
use Mojo::Util qw/url_unescape url_escape b64_decode/;
@ -40,13 +41,20 @@ sub startup ($self) {
# Router
my $r = $self->routes;
$self->config(
hypnotoad => { proxy => 1, listen => [$self->config('listen') // 'http://localhost:3000'] } );
hypnotoad => {
proxy => 1,
listen => [ $self->config('listen') // 'http://localhost:3000' ]
}
);
# Normal route to controller
$r->get('/')->to('Page#index');
$r->get('/all.rss')->to('Page#all_rss');
$r->get('/shiny.rss')->to('Page#shiny_rss');
$r->get('/pokerus.rss')->to('Page#pokerus_rss');
$r->get('/computer-all.rss')->to('Page#computer_all_rss');
$r->get('/computer-shiny.rss')->to('Page#computer_shiny_rss');
$r->get('/computer-pokerus.rss')->to('Page#computer_pokerus_rss');
$self->start_gts_bot;
}
@ -272,6 +280,7 @@ EOF
}
my $uuid = create_uuid_string;
my $offer_uuid = $uuid;
my $query_create_offer = <<'EOF';
INSERT
INTO offers
@ -309,7 +318,24 @@ EOF
$is_shiny, $has_pokerus,
$line, $held_item,
);
my $query_create_computer_news = <<'EOF';
INSERT
INTO news_computer
(uuid, date, offerer, species, is_shiny, has_pokerus, news_text, held_item)
VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );
EOF
my ($offer) =
$db->selectall_array( 'SELECT * FROM offers WHERE uuid = ?',
{ Slice => {} }, $offer_uuid );
$offer->{is_available} = 1;
$uuid = create_uuid_string;
$db->do(
$query_create_computer_news, undef,
$uuid, $pg_date,
decode( 'utf-8', $offerer ), $species,
$is_shiny, $has_pokerus,
Mojo::JSON::encode_json($offer), $held_item,
);
};
if ($@) {
warn $@;
@ -359,6 +385,13 @@ INTO news
(uuid, date, offerer, species, is_shiny, has_pokerus, news_text, held_item)
VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );
EOF
my $query_create_computer_news = <<'EOF';
INSERT
INTO news_computer
(uuid, date, offerer, species, is_shiny, has_pokerus, news_text, held_item)
VALUES ( ?, ?, ?, ?, ?, ?, ?, ? );
EOF
my $uuid = create_uuid_string;
$db->do(
$query_create_news, undef, $uuid,
@ -366,6 +399,15 @@ EOF
$is_shiny, $has_pokerus, $line,
$held_item,
);
$pokemon->{is_available} = 0;
$uuid = create_uuid_string;
$db->do(
$query_create_computer_news, undef,
$uuid, $pg_date,
decode( 'utf-8', $offerer ), $species,
$is_shiny, $has_pokerus,
Mojo::JSON::encode_json($pokemon), $held_item,
);
}
$db->do( <<'EOF', undef );
UPDATE offers SET is_available = false, marked_to_check_is_available = false = true WHERE is_available and marked_to_check_is_available;

View File

@ -22,7 +22,7 @@ sub index ($self) {
sub all_rss ($self) {
my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT news_text, date FROM news ORDER BY date DESC LIMIT 100;
SELECT species, news_text, date FROM news ORDER BY date DESC LIMIT 100;
EOF
my $dom = $self->_feed_from_news_list(
\@news,
@ -35,10 +35,26 @@ EOF
);
}
sub shiny_rss ($self) {
sub computer_all_rss ($self) {
my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT news_text, date FROM news WHERE is_shiny ORDER BY date DESC LIMIT 100;
SELECT species, news_text, date FROM news_computer ORDER BY date DESC LIMIT 100;
EOF
my $dom = $self->_feed_from_news_list(
\@news,
'All trades GTS',
'https://pkmnclassic.net/gts/'
);
$self->render(
format => 'xml',
text => $dom,
);
}
sub computer_shiny_rss ($self) {
my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT species, news_text, date FROM news_computer WHERE is_shiny ORDER BY date DESC LIMIT 100;
EOF
my $dom = $self->_feed_from_news_list(
\@news,
@ -51,10 +67,42 @@ EOF
);
}
sub shiny_rss ($self) {
my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT species, news_text, date FROM news WHERE is_shiny ORDER BY date DESC LIMIT 100;
EOF
my $dom = $self->_feed_from_news_list(
\@news,
'Shiny trades GTS',
'https://pkmnclassic.net/gts/'
);
$self->render(
format => 'xml',
text => $dom,
);
}
sub computer_pokerus_rss($self) {
my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT species, news_text, date FROM news_computer WHERE has_pokerus ORDER BY date DESC LIMIT 100;
EOF
my $dom = $self->_feed_from_news_list(
\@news,
'Pokerus infected trades GTS',
'https://pkmnclassic.net/gts/'
);
$self->render(
format => 'xml',
text => $dom,
);
}
sub pokerus_rss($self) {
my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT news_text, date FROM news WHERE has_pokerus ORDER BY date DESC LIMIT 100;
SELECT species, news_text, date FROM news WHERE has_pokerus ORDER BY date DESC LIMIT 100;
EOF
my $dom = $self->_feed_from_news_list(
\@news,
@ -86,7 +134,7 @@ sub _feed_from_news_list ( $self, $news, $title, $link ) {
sub _new_to_rss ( $self, $new ) {
my $item_tag = Mojo::DOM->new_tag('item');
my $title_tag = Mojo::DOM->new_tag( 'title', $new->{news_text} );
my $title_tag = Mojo::DOM->new_tag( 'title', $new->{species} );
my $link = Mojo::DOM->new_tag( 'link', 'https://pkmnclassic.net/gts/' );
my $description = Mojo::DOM->new_tag( 'description',
$new->{news_text});

View File

@ -43,6 +43,16 @@ sub MIGRATIONS {
news_text TEXT NOT NULL,
held_item TEXT
);',
'CREATE TABLE news_computer (
uuid UUID PRIMARY KEY,
date timestamp NOT NULL,
species TEXT NOT NULL,
offerer TEXT NOT NULL,
is_shiny boolean NOT NULL,
has_pokerus boolean NOT NULL,
news_text TEXT NOT NULL,
held_item TEXT
);',
);
}
1;

View File

@ -7,6 +7,9 @@
<li><a href="/all.rss">All trades.</a></li>
<li><a href="/shiny.rss">Only shiny trades.</a></li>
<li><a href="/pokerus.rss">Only pokerus trades.</a></li>
<li><a href="/computer-all.rss">All trades. (JSON)</a></li>
<li><a href="/computer-shiny.rss">Only shiny trades. (JSON)</a></li>
<li><a href="/computer-pokerus.rss">Only pokerus trades. (JSON)</a></li>
</ul>
</body>
</html>