diff --git a/burguillos_info.json.example b/burguillos_info.json.example index 3551544..4516ed5 100644 --- a/burguillos_info.json.example +++ b/burguillos_info.json.example @@ -5,6 +5,7 @@ "database": "example" }, "base_url": "https://burguillos.info", + "search_backend": "http://localhost:3303", "geoip_database": "/usr/share/GeoLite2-City_20230804/GeoLite2-City.mmdb", "onion_base_url": "http://example.onion"; "listen": "https:localhost:3555" diff --git a/lib/BurguillosInfo.pm b/lib/BurguillosInfo.pm index 465a104..98001e5 100644 --- a/lib/BurguillosInfo.pm +++ b/lib/BurguillosInfo.pm @@ -17,6 +17,7 @@ sub startup ($self) { } } ); + push @{ $self->commands->namespaces }, 'BurguillosInfo::Command'; $self->hook( before_render => sub($c, $args) { my $current_route = $c->url_for; diff --git a/lib/BurguillosInfo/Command/index.pm b/lib/BurguillosInfo/Command/index.pm new file mode 100644 index 0000000..091bc8f --- /dev/null +++ b/lib/BurguillosInfo/Command/index.pm @@ -0,0 +1,94 @@ +package BurguillosInfo::Command::index; + +use v5.36.0; + +use strict; +use warnings; +use utf8; + +use feature 'signatures'; + +use Data::Dumper; + +use Mojo::Base 'Mojolicious::Command'; +use Moo; + +use Mojo::UserAgent; + +use BurguillosInfo::Posts; +use BurguillosInfo::Categories; + +sub run ( $self, @args ) { + require BurguillosInfo; + my $app = BurguillosInfo->new; + my $config = $app->config; + my $search_backend = $config->{search_backend}; + my $search_index = $config->{search_index}; + my $ua = Mojo::UserAgent->new; + my $posts = BurguillosInfo::Posts->new->Retrieve; + my $categories = BurguillosInfo::Categories->new->Retrieve; + my $index = []; + $self->_index_posts( $index, $posts ); + $self->_index_categories( $index, $categories ); + $ua->put( $search_backend . '/index/' . $search_index, + {} => json => $index ); +} + +sub _index_categories ( $self, $index, $categories ) { + my @categories_keys = keys %$categories; + for my $category_key (@categories_keys) { + my $category = $categories->{$category_key}; + my $slug = $category->{slug}; + my $url = "/$slug"; + my $content = + Mojo::DOM->new( '' . $category->{description} . '' )->all_text; + my $title = $category->{title}; + my $attributes = $category->{attributes}; + $self->_index_attributes($index, $slug, $attributes); + push @$index, + { + title => $title, + content => $content, + url => $url, + }; + } +} + +sub _index_attributes($self, $index, $category_slug, $attributes) { + my @attributes_keys = keys %$attributes; + for my $attribute_key (@attributes_keys) { + my $attribute = $attributes->{$attribute_key}; + my $slug = $attribute->{identifier}; + my $url = "/$category_slug/atributo/$slug"; + my $title = $attribute->{title}; + my $content = Mojo::DOM->new( '' . $attribute->{description} . '' )->all_text; + push @$index, { + title => $title, + content => $content, + url => $url, + }; + } +} + +sub _index_posts ( $self, $index, $posts ) { + my @posts_keys = keys %$posts; + for my $post_key (@posts_keys) { + my $post = $posts->{$post_key}; + my $slug = $post->{slug}; + my $url = "/posts/$slug"; + my $urlImage = $post->{image}; + my $content = + Mojo::DOM->new( '' . $post->{content} . '' )->all_text; + my $title = $post->{title}; + my $author = $post->{author}; + push @$index, + { + title => $title, + author => $author, + content => $content, + url => $url, + urlImage => $urlImage, + }; + } +} +1;