diff --git a/lib/GTSRSSApi.pm b/lib/GTSRSSApi.pm index 46d3604..7dfd83f 100644 --- a/lib/GTSRSSApi.pm +++ b/lib/GTSRSSApi.pm @@ -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; } @@ -104,7 +112,7 @@ sub fetch_gts_data { my @offers = $html->find('.gtsPokemonSummary.gtsOffer.pfBoxThin.pfFormGroup') ->each; - my $db = GTSRSSApi::DB->connect; + my $db = GTSRSSApi::DB->connect; $db->do( <<'EOF', undef ); UPDATE offers SET marked_to_check_is_available = true WHERE is_available = true; EOF @@ -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 $@; @@ -320,19 +346,19 @@ EOF SELECT * FROM offers WHERE is_available and marked_to_check_is_available; EOF for my $pokemon (@no_longer_available_pokemon) { - my $species = $pokemon->{species}; - my $nick = $pokemon->{nickname}; - my $offerer = $pokemon->{offerer}; - my $pokeball = $pokemon->{pokeball}; - my $level = $pokemon->{level}; - my $gender = $pokemon->{gender}; - my $nature = $pokemon->{nature}; - my $ability = $pokemon->{ability}; - my $is_shiny = $pokemon->{is_shiny}; - my $has_pokerus = $pokemon->{has_pokerus}; - my $pg_date = $pokemon->{date}; - my $date = DateTime->now().''; - my $held_item = $pokemon->{held_item}; + my $species = $pokemon->{species}; + my $nick = $pokemon->{nickname}; + my $offerer = $pokemon->{offerer}; + my $pokeball = $pokemon->{pokeball}; + my $level = $pokemon->{level}; + my $gender = $pokemon->{gender}; + my $nature = $pokemon->{nature}; + my $ability = $pokemon->{ability}; + my $is_shiny = $pokemon->{is_shiny}; + my $has_pokerus = $pokemon->{has_pokerus}; + my $pg_date = $pokemon->{date}; + my $date = DateTime->now() . ''; + my $held_item = $pokemon->{held_item}; my $wanted_species = $pokemon->{wanted_species}; my $requirements = $pokemon->{wanted_requirements}; @@ -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; diff --git a/lib/GTSRSSApi/Controller/Page.pm b/lib/GTSRSSApi/Controller/Page.pm index 9a70e55..a6fa542 100644 --- a/lib/GTSRSSApi/Controller/Page.pm +++ b/lib/GTSRSSApi/Controller/Page.pm @@ -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}); diff --git a/lib/GTSRSSApi/DB/Migrations.pm b/lib/GTSRSSApi/DB/Migrations.pm index 736ef1e..cc39d2e 100644 --- a/lib/GTSRSSApi/DB/Migrations.pm +++ b/lib/GTSRSSApi/DB/Migrations.pm @@ -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; diff --git a/templates/page/index.html.ep b/templates/page/index.html.ep index 24f12d8..f9970da 100644 --- a/templates/page/index.html.ep +++ b/templates/page/index.html.ep @@ -7,6 +7,9 @@
  • All trades.
  • Only shiny trades.
  • Only pokerus trades.
  • +
  • All trades. (JSON)
  • +
  • Only shiny trades. (JSON)
  • +
  • Only pokerus trades. (JSON)