mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Add app:hashtag-related-generate command
This commit is contained in:
parent
aa166ab11a
commit
176b4ed793
3 changed files with 92 additions and 5 deletions
83
app/Console/Commands/HashtagRelatedGenerate.php
Normal file
83
app/Console/Commands/HashtagRelatedGenerate.php
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Hashtag;
|
||||||
|
use App\StatusHashtag;
|
||||||
|
use App\Models\HashtagRelated;
|
||||||
|
use App\Services\HashtagRelatedService;
|
||||||
|
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
||||||
|
use function Laravel\Prompts\multiselect;
|
||||||
|
|
||||||
|
class HashtagRelatedGenerate extends Command implements PromptsForMissingInput
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'app:hashtag-related-generate {tag}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Command description';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prompt for missing input arguments using the returned questions.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function promptForMissingArgumentsUsing()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'tag' => 'Which hashtag should we generate related tags for?',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$tag = $this->argument('tag');
|
||||||
|
$hashtag = Hashtag::whereName($tag)->orWhere('slug', $tag)->first();
|
||||||
|
if(!$hashtag) {
|
||||||
|
$this->error('Hashtag not found, aborting...');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->info('Looking up #' . $tag . '...');
|
||||||
|
|
||||||
|
$tags = StatusHashtag::whereHashtagId($hashtag->id)->count();
|
||||||
|
if(!$tags || $tags < 100) {
|
||||||
|
$this->error('Not enough posts found to generate related hashtags!');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->info('Found ' . $tags . ' posts that use that hashtag');
|
||||||
|
$related = collect(HashtagRelatedService::fetchRelatedTags($tag));
|
||||||
|
|
||||||
|
$selected = multiselect(
|
||||||
|
label: 'Which tags do you want to generate?',
|
||||||
|
options: $related->pluck('name'),
|
||||||
|
required: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
$filtered = $related->filter(fn($i) => in_array($i['name'], $selected))->all();
|
||||||
|
$agg_score = $related->filter(fn($i) => in_array($i['name'], $selected))->sum('related_count');
|
||||||
|
|
||||||
|
HashtagRelated::updateOrCreate([
|
||||||
|
'hashtag_id' => $hashtag->id,
|
||||||
|
], [
|
||||||
|
'related_tags' => array_values($filtered),
|
||||||
|
'agg_score' => $agg_score,
|
||||||
|
'last_calculated_at' => now()
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->info('Finished!');
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,8 @@ class HashtagRelated extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $guarded = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be mutated to dates and other custom formats.
|
* The attributes that should be mutated to dates and other custom formats.
|
||||||
*
|
*
|
||||||
|
|
|
@ -8,10 +8,13 @@ use App\Models\HashtagRelated;
|
||||||
|
|
||||||
class HashtagRelatedService
|
class HashtagRelatedService
|
||||||
{
|
{
|
||||||
|
|
||||||
public static function get($id)
|
public static function get($id)
|
||||||
{
|
{
|
||||||
return HashtagRelated::whereHashtagId($id)->first();
|
$tag = HashtagRelated::whereHashtagId($id)->first();
|
||||||
|
if(!$tag) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return $tag->related_tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fetchRelatedTags($tag)
|
public static function fetchRelatedTags($tag)
|
||||||
|
@ -20,15 +23,14 @@ class HashtagRelatedService
|
||||||
->select('h2.name', DB::raw('COUNT(*) as related_count'))
|
->select('h2.name', DB::raw('COUNT(*) as related_count'))
|
||||||
->join('status_hashtags as hs2', function ($join) {
|
->join('status_hashtags as hs2', function ($join) {
|
||||||
$join->on('status_hashtags.status_id', '=', 'hs2.status_id')
|
$join->on('status_hashtags.status_id', '=', 'hs2.status_id')
|
||||||
->whereRaw('status_hashtags.hashtag_id != hs2.hashtag_id')
|
->whereRaw('status_hashtags.hashtag_id != hs2.hashtag_id');
|
||||||
->where('status_hashtags.created_at', '>', now()->subMonths(3));
|
|
||||||
})
|
})
|
||||||
->join('hashtags as h1', 'status_hashtags.hashtag_id', '=', 'h1.id')
|
->join('hashtags as h1', 'status_hashtags.hashtag_id', '=', 'h1.id')
|
||||||
->join('hashtags as h2', 'hs2.hashtag_id', '=', 'h2.id')
|
->join('hashtags as h2', 'hs2.hashtag_id', '=', 'h2.id')
|
||||||
->where('h1.name', '=', $tag)
|
->where('h1.name', '=', $tag)
|
||||||
->groupBy('h2.name')
|
->groupBy('h2.name')
|
||||||
->orderBy('related_count', 'desc')
|
->orderBy('related_count', 'desc')
|
||||||
->limit(10)
|
->limit(30)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
|
|
Loading…
Reference in a new issue