Adding json api to see all offers.

This commit is contained in:
Sergiotarxz 2024-05-08 11:59:38 +02:00
parent 8326233d99
commit 098bff90b3
3 changed files with 32 additions and 19 deletions

View File

@ -49,6 +49,7 @@ sub startup ($self) {
# Normal route to controller # Normal route to controller
$r->get('/')->to('Page#index'); $r->get('/')->to('Page#index');
$r->get('/offers.json')->to('Page#active_offers_json');
$r->get('/all.rss')->to('Page#all_rss'); $r->get('/all.rss')->to('Page#all_rss');
$r->get('/shiny.rss')->to('Page#shiny_rss'); $r->get('/shiny.rss')->to('Page#shiny_rss');
$r->get('/pokerus.rss')->to('Page#pokerus_rss'); $r->get('/pokerus.rss')->to('Page#pokerus_rss');

View File

@ -7,11 +7,11 @@ use warnings;
use DateTime::Format::Pg; use DateTime::Format::Pg;
use DateTime::Format::Mail; use DateTime::Format::Mail;
use Mojo::DOM; use Mojo::DOM;
use Mojo::JSON qw/to_json/;
use Mojo::Base 'Mojolicious::Controller', -signatures; use Mojo::Base 'Mojolicious::Controller', -signatures;
use GTSRSSApi::DB; use GTSRSSApi::DB;
# This action will render a template # This action will render a template
sub index ($self) { sub index ($self) {
@ -19,6 +19,17 @@ sub index ($self) {
$self->render; $self->render;
} }
sub active_offers_json($self) {
my $db = GTSRSSApi::DB->connect;
my @offers = $db->selectall_array( <<'EOF', { Slice => {} } );
SELECT * FROM offers WHERE is_available ORDER BY date DESC LIMIT 100;
EOF
@offers =
map { $_->{date} = DateTime::Format::Pg->parse_datetime( $_->{date} ); $_ }
@offers;
return $self->render( json => \@offers );
}
sub all_rss ($self) { sub all_rss ($self) {
my $db = GTSRSSApi::DB->connect; my $db = GTSRSSApi::DB->connect;
my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); my @news = $db->selectall_array( <<'EOF', { Slice => {} } );
@ -125,8 +136,9 @@ sub _feed_from_news_list ( $self, $news, $title, $link ) {
$channel_tag->child_nodes->first->append_content($link_tag); $channel_tag->child_nodes->first->append_content($link_tag);
$channel_tag->child_nodes->first->append_content($description_tag); $channel_tag->child_nodes->first->append_content($description_tag);
for my $new ( @$news ) { for my $new (@$news) {
$channel_tag->child_nodes->first->append_content( $self->_new_to_rss($new) ); $channel_tag->child_nodes->first->append_content(
$self->_new_to_rss($new) );
} }
$dom->child_nodes->first->append_content($channel_tag); $dom->child_nodes->first->append_content($channel_tag);
return $dom; return $dom;
@ -136,8 +148,7 @@ sub _new_to_rss ( $self, $new ) {
my $item_tag = Mojo::DOM->new_tag('item'); my $item_tag = Mojo::DOM->new_tag('item');
my $title_tag = Mojo::DOM->new_tag( 'title', $new->{species} ); my $title_tag = Mojo::DOM->new_tag( 'title', $new->{species} );
my $link = Mojo::DOM->new_tag( 'link', 'https://pkmnclassic.net/gts/' ); my $link = Mojo::DOM->new_tag( 'link', 'https://pkmnclassic.net/gts/' );
my $description = Mojo::DOM->new_tag( 'description', my $description = Mojo::DOM->new_tag( 'description', $new->{news_text} );
$new->{news_text});
my $guid = Mojo::DOM->new_tag( 'guid', $new->{uuid} ); my $guid = Mojo::DOM->new_tag( 'guid', $new->{uuid} );
my $date = Mojo::DOM->new_tag( my $date = Mojo::DOM->new_tag(
'pubDate', 'pubDate',

View File

@ -4,12 +4,13 @@
<body> <body>
<h1>List of RSS Feeds for different kinds of GTS trades in pkmn-classic network.</h1> <h1>List of RSS Feeds for different kinds of GTS trades in pkmn-classic network.</h1>
<ul> <ul>
<li><a href="/all.rss">All trades.</a></li> <li><a href="/all.rss">All trades updates limited to 100.</a></li>
<li><a href="/shiny.rss">Only shiny trades.</a></li> <li><a href="/shiny.rss">Only shiny trades updates limited to 100.</a></li>
<li><a href="/pokerus.rss">Only pokerus trades.</a></li> <li><a href="/pokerus.rss">Only pokerus trades updates limited to 100.</a></li>
<li><a href="/computer-all.rss">All trades. (JSON)</a></li> <li><a href="/offers.json">All available offers in JSON format for computer processing limited to 100.</a></li>
<li><a href="/computer-shiny.rss">Only shiny trades. (JSON)</a></li> <li><a href="/computer-all.rss">All trades updates limited to 100. (JSON)</a></li>
<li><a href="/computer-pokerus.rss">Only pokerus trades. (JSON)</a></li> <li><a href="/computer-shiny.rss">Only shiny trades updates limited to 100. (JSON)</a></li>
<li><a href="/computer-pokerus.rss">Only pokerus trades updates limited to 100. (JSON)</a></li>
</ul> </ul>
</body> </body>
</html> </html>