Update (post): only show table of contents if there are at least 3 toc items

This commit is contained in:
2023-09-25 19:54:51 +08:00
parent 8faf131f13
commit e3fe8bf877
8 changed files with 82 additions and 94 deletions

View File

@@ -39,7 +39,6 @@ public static function readFile($storage_driver, $relative_directory, $filename)
return null; return null;
} }
public static function uploadJson($storage_driver, $relative_directory, $filename, $jsonData) public static function uploadJson($storage_driver, $relative_directory, $filename, $jsonData)
{ {
$jsonString = json_encode($jsonData, JSON_PRETTY_PRINT); $jsonString = json_encode($jsonData, JSON_PRETTY_PRINT);

View File

@@ -57,7 +57,6 @@ public static function createNewArticleTitle($current_title, $supporting_data)
return in following json format {\"main_keyword\":\"(Main Keyword)\",\"title\":\"(Title in 90-130 letters)\",\"short_title\":\"(Short Title in 30-40 letters)\",\"article_type\":\"(How-tos|Guides|Interview|Review|Commentary|Feature|News|Editorial|Report|Research|Case-study|Overview|Tutorial|Update|Spotlight|Insights)\",\"description\":\"(SEO description based on main keyword)\",\"photo_keywords\":[\"photo keyword 1\",\"photo keyword 2\"]}"; return in following json format {\"main_keyword\":\"(Main Keyword)\",\"title\":\"(Title in 90-130 letters)\",\"short_title\":\"(Short Title in 30-40 letters)\",\"article_type\":\"(How-tos|Guides|Interview|Review|Commentary|Feature|News|Editorial|Report|Research|Case-study|Overview|Tutorial|Update|Spotlight|Insights)\",\"description\":\"(SEO description based on main keyword)\",\"photo_keywords\":[\"photo keyword 1\",\"photo keyword 2\"]}";
$user_prompt = "Article Title: {$current_title}\n Article Description: {$supporting_data}\n"; $user_prompt = "Article Title: {$current_title}\n Article Description: {$supporting_data}\n";
$reply = self::chatCompletion($system_prompt, $user_prompt, 'gpt-3.5-turbo'); $reply = self::chatCompletion($system_prompt, $user_prompt, 'gpt-3.5-turbo');

View File

@@ -141,9 +141,16 @@ private function injectTableOfContents($html)
{ {
$crawler = new Crawler($html); $crawler = new Crawler($html);
$h2Elements = $crawler->filter('h2');
if ($h2Elements->count() < 3) {
// Return the original HTML if there are fewer than 3 h2 tags
return $html;
}
// Create the Table of Contents // Create the Table of Contents
$toc = '<div class="p-3 rounded-3 bg-light mb-3"><ol>'; $toc = '<div class="p-3 rounded-3 bg-light mb-3"><ol>';
$crawler->filter('h2')->each(function (Crawler $node, $i) use (&$toc) { $h2Elements->each(function (Crawler $node, $i) use (&$toc) {
$content = $node->text(); $content = $node->text();
$id = 'link-'.$i; // Creating a simple id based on the index $id = 'link-'.$i; // Creating a simple id based on the index
$node->getNode(0)->setAttribute('id', $id); // Set the id to the h2 tag $node->getNode(0)->setAttribute('id', $id); // Set the id to the h2 tag
@@ -162,7 +169,6 @@ private function injectTableOfContents($html)
$updatedHtml = $crawler->filter('body')->html(); $updatedHtml = $crawler->filter('body')->html();
return $updatedHtml; return $updatedHtml;
} }
private function injectFeaturedImage($post, $content) private function injectFeaturedImage($post, $content)

View File

@@ -41,12 +41,11 @@ public static function handle(SerpUrl $serp_url)
$readability_content = ScrapeUrlBodyTask::handle($serp_url->url); $readability_content = ScrapeUrlBodyTask::handle($serp_url->url);
if (is_null($readability_content)) if (is_null($readability_content)) {
{
return self::saveAndReturnSerpProcessStatus($serp_url, -7); return self::saveAndReturnSerpProcessStatus($serp_url, -7);
} }
$markdown = OpenAI::writeArticle($ai_suggestion->title, $readability_content, $ai_suggestion->article_type ,500, 800); $markdown = OpenAI::writeArticle($ai_suggestion->title, $readability_content, $ai_suggestion->article_type, 500, 800);
if (is_empty($markdown)) { if (is_empty($markdown)) {
return self::saveAndReturnSerpProcessStatus($serp_url, -4); return self::saveAndReturnSerpProcessStatus($serp_url, -4);

View File

@@ -2,15 +2,12 @@
namespace App\Jobs\Tasks; namespace App\Jobs\Tasks;
use App\Helpers\FirstParty\OSSUploader\OSSUploader;
use \Illuminate\Support\Facades\Http;
use Carbon\Carbon;
use Storage;
use Exception;
use andreskrey\Readability\Readability;
use andreskrey\Readability\Configuration; use andreskrey\Readability\Configuration;
use andreskrey\Readability\ParseException; use andreskrey\Readability\ParseException;
use andreskrey\Readability\Readability;
use App\Helpers\FirstParty\OSSUploader\OSSUploader;
use Exception;
use Illuminate\Support\Facades\Http;
class ScrapeUrlBodyTask class ScrapeUrlBodyTask
{ {
@@ -18,24 +15,21 @@ public static function handle(string $url)
{ {
$slug = str_slug($url); $slug = str_slug($url);
$disk_url = '/scraped/' . $slug . '.html'; $disk_url = '/scraped/'.$slug.'.html';
$html_content = null; $html_content = null;
try { try {
$html_content = OSSUploader::readFile('r2','/scraped/',$slug.'.html'); $html_content = OSSUploader::readFile('r2', '/scraped/', $slug.'.html');
if (is_null($disk_url)) if (is_null($disk_url)) {
{
throw Exception('Not stored.'); throw Exception('Not stored.');
} }
} } catch (Exception $e) {
catch (Exception $e) {
$html_content = null; $html_content = null;
} }
if (is_null($html_content)) if (is_null($html_content)) {
{
$proxy = 'gate.smartproxy.com:10000'; $proxy = 'gate.smartproxy.com:10000';
$user = 'sp5bbkzj7e'; $user = 'sp5bbkzj7e';
$psw = 'yTtk2cc5kg23kIkSSr'; $psw = 'yTtk2cc5kg23kIkSSr';
@@ -47,7 +41,7 @@ public static function handle(string $url)
if ($response->successful()) { if ($response->successful()) {
$html_content = $response->body(); $html_content = $response->body();
OSSUploader::uploadFile('r2','/scraped/',$slug.'.html', $html_content); OSSUploader::uploadFile('r2', '/scraped/', $slug.'.html', $html_content);
} }
} }
@@ -55,7 +49,6 @@ public static function handle(string $url)
$readability = new Readability(new Configuration()); $readability = new Readability(new Configuration());
try { try {
$readability->parse($html_content); $readability->parse($html_content);

View File

@@ -15,5 +15,5 @@
// exclude route name for exclude from minify // exclude route name for exclude from minify
'exclude_route' => [ 'exclude_route' => [
// 'routeName' // 'routeName'
] ],
]; ];

View File

@@ -5,7 +5,6 @@
return [ return [
'fb_app_id' => '1259730771382460', 'fb_app_id' => '1259730771382460',
'meta' => [ 'meta' => [

View File

@@ -33,15 +33,13 @@
Route::get('/step-2', function (Request $request) { Route::get('/step-2', function (Request $request) {
$news_serp_result = NewsSerpResult::find($request->input('id', null)); $news_serp_result = NewsSerpResult::find($request->input('id', null));
if (is_null($news_serp_result)) if (is_null($news_serp_result)) {
{
abort(404); abort(404);
} }
$task = ParseNewsSerpDomainsTask::handle($news_serp_result); $task = ParseNewsSerpDomainsTask::handle($news_serp_result);
if ($task) if ($task) {
{
$serp_url = SerpUrl::latest()->first(); $serp_url = SerpUrl::latest()->first();
dd($serp_url->id); dd($serp_url->id);
@@ -51,8 +49,7 @@
Route::get('/step-3', function (Request $request) { Route::get('/step-3', function (Request $request) {
$serp_url = SerpUrl::find($request->input('id', null)); $serp_url = SerpUrl::find($request->input('id', null));
if (is_null($serp_url)) if (is_null($serp_url)) {
{
abort(404); abort(404);
} }
@@ -72,8 +69,7 @@
Route::get('/step-5', function (Request $request) { Route::get('/step-5', function (Request $request) {
$post = Post::find($request->input('id')); $post = Post::find($request->input('id'));
if (is_null($post)) if (is_null($post)) {
{
return abort(404); return abort(404);
} }
@@ -81,7 +77,6 @@
dd($post->save()); dd($post->save());
}); });
// Route::get('/suggest_titles', function () { // Route::get('/suggest_titles', function () {
// $results = OpenAI::suggestArticleTitles("It's 2019s Electric: How Fisker Is Reinventing The Automotive Industry And \nExpanding Its Business", "Fisker's approach to building electric vehicles is deeply intertwined with \nits overall business philosophy: use less, use better,...s", 1); // $results = OpenAI::suggestArticleTitles("It's 2019s Electric: How Fisker Is Reinventing The Automotive Industry And \nExpanding Its Business", "Fisker's approach to building electric vehicles is deeply intertwined with \nits overall business philosophy: use less, use better,...s", 1);
// dd($results); // dd($results);
@@ -92,9 +87,7 @@
// dd($results); // dd($results);
// }); // });
Route::get('proxy_test', function () {
Route::get('proxy_test', function() {
$url = 'https://www.cnbc.com/2023/09/24/this-southern-city-is-the-no-1-place-to-start-your-own-business.html'; $url = 'https://www.cnbc.com/2023/09/24/this-southern-city-is-the-no-1-place-to-start-your-own-business.html';
$task = ScrapeUrlBodyTask::handle($url); $task = ScrapeUrlBodyTask::handle($url);