SeoAiGenerate.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\command;
  3. use app\common\util\SeoAi;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Option;
  7. use think\console\Output;
  8. use think\Db;
  9. use think\Log;
  10. class SeoAiGenerate extends Command
  11. {
  12. protected function configure()
  13. {
  14. $this->setName('seo:generate')
  15. ->setDescription('Generate AI SEO metadata for content')
  16. ->addOption('mid', null, Option::VALUE_OPTIONAL, '1=vod,2=art', '1')
  17. ->addOption('limit', null, Option::VALUE_OPTIONAL, 'Batch size', '100')
  18. ->addOption('id', null, Option::VALUE_OPTIONAL, 'Generate only one content ID', '0');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $mid = intval($input->getOption('mid'));
  23. $limit = max(1, intval($input->getOption('limit')));
  24. $oneId = intval($input->getOption('id'));
  25. if (!in_array($mid, [1, 2])) {
  26. $output->writeln('invalid mid, use 1 or 2');
  27. return;
  28. }
  29. $rows = [];
  30. if ($oneId > 0) {
  31. $rows[] = ['id' => $oneId];
  32. } else {
  33. if ($mid === 1) {
  34. $rows = Db::name('vod')
  35. ->where(['vod_status' => 1])
  36. ->field('vod_id as id')
  37. ->order('vod_id desc')
  38. ->limit($limit)
  39. ->select();
  40. } else {
  41. $rows = Db::name('art')
  42. ->where(['art_status' => 1])
  43. ->field('art_id as id')
  44. ->order('art_id desc')
  45. ->limit($limit)
  46. ->select();
  47. }
  48. }
  49. $ok = 0;
  50. $fail = 0;
  51. foreach ($rows as $row) {
  52. $id = intval($row['id']);
  53. try {
  54. $res = SeoAi::generateByMidObj($mid, $id);
  55. } catch (\Exception $e) {
  56. Log::error('AI SEO generate failed (mid=' . $mid . ', id=' . $id . '): ' . $e->getMessage());
  57. $fail++;
  58. $output->writeln('fail: ' . $id . ' - ' . $e->getMessage());
  59. continue;
  60. }
  61. if ($res['code'] == 1) {
  62. $ok++;
  63. $output->writeln('ok: ' . $id);
  64. } else {
  65. $fail++;
  66. $output->writeln('fail: ' . $id . ' - ' . $res['msg']);
  67. }
  68. }
  69. $output->writeln('done, success=' . $ok . ', fail=' . $fail);
  70. }
  71. }