package GTSRSSApi::Controller::Page; use v5.34.1; use strict; use warnings; use DateTime::Format::Pg; use DateTime::Format::Mail; use Mojo::DOM; use Mojo::Base 'Mojolicious::Controller', -signatures; use GTSRSSApi::DB; # This action will render a template sub index ($self) { # Render template "example/welcome.html.ep" with message $self->render; } sub all_rss ($self) { my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); SELECT species, news_text, date FROM news 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_all_rss ($self) { my $db = GTSRSSApi::DB->connect; my @news = $db->selectall_array( <<'EOF', { Slice => {} } ); 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, 'Shiny trades GTS', 'https://pkmnclassic.net/gts/' ); $self->render( format => 'xml', text => $dom, ); } 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 species, news_text, date FROM news 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 _feed_from_news_list ( $self, $news, $title, $link ) { my $dom = Mojo::DOM->new_tag( 'rss', version => '2.0', undef ); my $channel_tag = Mojo::DOM->new_tag('channel'); my $title_tag = Mojo::DOM->new_tag( 'title', $title ); my $link_tag = Mojo::DOM->new_tag( 'link', $link ); my $description_tag = Mojo::DOM->new_tag( 'description', '' ); $channel_tag->child_nodes->first->append_content($title_tag); $channel_tag->child_nodes->first->append_content($link_tag); $channel_tag->child_nodes->first->append_content($description_tag); for my $new ( @$news ) { $channel_tag->child_nodes->first->append_content( $self->_new_to_rss($new) ); } $dom->child_nodes->first->append_content($channel_tag); return $dom; } sub _new_to_rss ( $self, $new ) { my $item_tag = Mojo::DOM->new_tag('item'); 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}); my $guid = Mojo::DOM->new_tag( 'guid', $new->{uuid} ); my $date = Mojo::DOM->new_tag( 'pubDate', '' . DateTime::Format::Mail->format_datetime( DateTime::Format::Pg->parse_datetime( $new->{date} ) ) ); $item_tag->child_nodes->first->append_content($title_tag); $item_tag->child_nodes->first->append_content($link); $item_tag->child_nodes->first->append_content($description); $item_tag->child_nodes->first->append_content($guid); $item_tag->child_nodes->first->append_content($date); return $item_tag; } 1;