Improving the relevance of promoted content.

This commit is contained in:
sergiotarxz 2023-09-29 14:36:19 +02:00
parent 0a7e75fe9d
commit a475de52f5
6 changed files with 32 additions and 23 deletions

View File

@ -5,6 +5,7 @@
<ogdesc>Mesón - Bar - Cristóbal - Carta y Menú.</ogdesc> <ogdesc>Mesón - Bar - Cristóbal - Carta y Menú.</ogdesc>
<last_modification_date>2023-09-06T11:16+00:00</last_modification_date> <last_modification_date>2023-09-06T11:16+00:00</last_modification_date>
<category>donde-comer</category> <category>donde-comer</category>
<category>index</category>
<slug>bar-cristobal</slug> <slug>bar-cristobal</slug>
<attributes> <attributes>
<attribute>hamburguesas</attribute> <attribute>hamburguesas</attribute>

View File

@ -7,6 +7,7 @@
<img src="/img/beluche.webp" bottom-preview="500"/> <img src="/img/beluche.webp" bottom-preview="500"/>
<pinned>2</pinned> <pinned>2</pinned>
<category>donde-comer</category> <category>donde-comer</category>
<category>index</category>
<attributes> <attributes>
<attribute>hamburguesas</attribute> <attribute>hamburguesas</attribute>
<attribute>reparto-a-domicilio-comida</attribute> <attribute>reparto-a-domicilio-comida</attribute>

View File

@ -8,6 +8,7 @@
contacto.</ogdesc> contacto.</ogdesc>
<img src="/img/yo-to-lo-guiso.webp" /> <img src="/img/yo-to-lo-guiso.webp" />
<category>donde-comer</category> <category>donde-comer</category>
<category>index</category>
<attributes> <attributes>
<attribute>hamburguesas</attribute> <attribute>hamburguesas</attribute>
<attribute>comida-vegana</attribute> <attribute>comida-vegana</attribute>

View File

@ -6,7 +6,7 @@
<img src="/img/farmacia.webp"/> <img src="/img/farmacia.webp"/>
<category>index</category> <category>index</category>
<slug>farmacia-guardia</slug> <slug>farmacia-guardia</slug>
<pinned>1</pinned> <pinned>100</pinned>
<content> <content>
<img alt="Imagen de cruz de Farmacia" src="/img/farmacia.webp"/> <img alt="Imagen de cruz de Farmacia" src="/img/farmacia.webp"/>

View File

@ -6,7 +6,7 @@
<img src="/img/ayuntamiento.webp"/> <img src="/img/ayuntamiento.webp"/>
<category>index</category> <category>index</category>
<slug>telefono-interes-burguillos</slug> <slug>telefono-interes-burguillos</slug>
<pinned>1</pinned> <pinned>100</pinned>
<content> <content>
<img alt="Imagen del ayuntamiento" src="/img/ayuntamiento.webp"/> <img alt="Imagen del ayuntamiento" src="/img/ayuntamiento.webp"/>

View File

@ -32,7 +32,7 @@ sub new {
return bless {}, shift; return bless {}, shift;
} }
sub _ReturnCacheFilter($self, $filters = 1) { sub _ReturnCacheFilter ( $self, $filters = 1 ) {
my %posts_by_category_filtered; my %posts_by_category_filtered;
my %posts_by_slug_filtered; my %posts_by_slug_filtered;
my $iso8601 = DateTime::Format::ISO8601->new; my $iso8601 = DateTime::Format::ISO8601->new;
@ -50,8 +50,8 @@ sub _ReturnCacheFilter($self, $filters = 1) {
} }
} }
$posts_by_slug_filtered{ $post->{slug} } = $post; $posts_by_slug_filtered{ $post->{slug} } = $post;
$posts_by_category_filtered{ $post->{category} } //= []; $posts_by_category_filtered{ $category } //= [];
push @{ $posts_by_category_filtered{ $post->{category} } }, $post; push @{ $posts_by_category_filtered{ $category } }, $post;
} }
} }
return ( \%posts_by_category_filtered, \%posts_by_slug_filtered ); return ( \%posts_by_category_filtered, \%posts_by_slug_filtered );
@ -69,16 +69,19 @@ sub _GeneratePostFromFile ( $self, $post_file ) {
or die "Missing date at $post_file."; or die "Missing date at $post_file.";
my $ogdesc = $dom->at(':root > ogdesc')->text my $ogdesc = $dom->at(':root > ogdesc')->text
or die "Missing ogdesc at $post_file"; or die "Missing ogdesc at $post_file";
my $category = $dom->at(':root > category')->text my @categories = $dom->find(':root > category')->map('text')->each;
or die "Missing category at $post_file."; my $slug = $dom->at(':root > slug')->text
my $slug = $dom->at(':root > slug')->text
or die "Missing slug at $post_file."; or die "Missing slug at $post_file.";
my $content_tag = $dom->at(':root > content') my $content_tag = $dom->at(':root > content')
or die "Missing content at $post_file."; or die "Missing content at $post_file.";
for my $tag ($content_tag->children->each) {
$tag->content($tag->content =~ s/\n//gr); for my $tag ( $content_tag->children->each ) {
$tag->content( $tag->content =~ s/\n//gr );
} }
my $content = $content_tag->content; if ( !scalar @categories ) {
die 'Missing category';
}
my $content = $content_tag->content;
my $pinned_node = $dom->at(':root > pinned'); my $pinned_node = $dom->at(':root > pinned');
my $image_element = $dom->at(':root > img'); my $image_element = $dom->at(':root > img');
my $image; my $image;
@ -106,7 +109,8 @@ sub _GeneratePostFromFile ( $self, $post_file ) {
author => $author, author => $author,
date => $date, date => $date,
ogdesc => $ogdesc, ogdesc => $ogdesc,
category => $category, categories => [@categories],
category => $categories[0],
slug => $slug, slug => $slug,
content => $content, content => $content,
attributes => $attributes, attributes => $attributes,
@ -147,16 +151,18 @@ sub _GeneratePostCache ($self) {
if ( !defined $post ) { if ( !defined $post ) {
next; next;
} }
my $category = $post->{category}; my $categories = $post->{categories};
$cached_posts_by_category->{$category} //= []; for my $category ($post->{categories}->@*) {
my $slug = $post->{slug}; $cached_posts_by_category->{$category} //= [];
my $category_posts = $cached_posts_by_category->{$category}; my $slug = $post->{slug};
$cached_posts_by_slug->{$slug} = $post; my $category_posts = $cached_posts_by_category->{$category};
push @$category_posts, $post; $cached_posts_by_slug->{$slug} = $post;
push @$category_posts, $post;
}
} }
} }
sub Retrieve($self, $filters = 1) { sub Retrieve ( $self, $filters = 1 ) {
if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) { if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) {
return $self->_ReturnCacheFilter($filters); return $self->_ReturnCacheFilter($filters);
} }
@ -189,7 +195,7 @@ sub RetrieveAllPostsForCategory ( $self, $category_name ) {
sub comparePinned ( $self, $a, $b ) { sub comparePinned ( $self, $a, $b ) {
my $cmp = $b->{pinned} <=> $a->{pinned}; my $cmp = $b->{pinned} <=> $a->{pinned};
if ( $cmp != 0 ) { if ( $cmp != 0 ) {
return $cmp; return $cmp;
} }
return int( rand(3) ) - 1; return int( rand(3) ) - 1;
@ -220,14 +226,14 @@ sub RetrieveDirectPostsForCategory ( $self, $category_name ) {
return $self->shufflePostsIfRequired( $category, [@$posts] ); return $self->shufflePostsIfRequired( $category, [@$posts] );
} }
sub PreviewOg($self, $post, $isWhatsApp) { sub PreviewOg ( $self, $post, $isWhatsApp ) {
my $title = $post->{title}; my $title = $post->{title};
my $content = $post->{content}; my $content = $post->{content};
my $image_file = $post->{image}; my $image_file = $post->{image};
my $image_bottom_preview = $post->{image_bottom_preview}; my $image_bottom_preview = $post->{image_bottom_preview};
if ($isWhatsApp) { if ($isWhatsApp) {
return BurguillosInfo::Preview->WhatsappAlternativeGenerate( $title, $content, $image_file, return BurguillosInfo::Preview->WhatsappAlternativeGenerate( $title,
$image_bottom_preview ); $content, $image_file, $image_bottom_preview );
} }
return BurguillosInfo::Preview->Generate( $title, $content, $image_file, return BurguillosInfo::Preview->Generate( $title, $content, $image_file,
$image_bottom_preview ); $image_bottom_preview );