Meta information and article body cast

This commit is contained in:
2020-08-10 21:23:16 +05:00
parent 51076aca33
commit 8d8e174095
6 changed files with 23 additions and 10 deletions

View File

@@ -32,6 +32,17 @@ class Article extends Model
*/ */
protected $guarded = []; protected $guarded = [];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'body' => 'array',
'meta' => 'array'
];
public function source() public function source()
{ {
return $this->belongsTo(Source::class); return $this->belongsTo(Source::class);

View File

@@ -55,6 +55,9 @@ class ScrapeMihaaruCommand extends Command
"body" => $article["content"], "body" => $article["content"],
"guid" => $article["guid"], "guid" => $article["guid"],
"published_date" => $article["date"], "published_date" => $article["date"],
"meta" => [
"title" => $article["og_title"]
]
]); ]);

View File

@@ -6,6 +6,7 @@ use App\Http\Resources\SourceResource;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Source; use App\Source;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use App\Http\Resources\ArticleResource;
class SourcesController extends Controller class SourcesController extends Controller
{ {
@@ -28,7 +29,7 @@ class SourcesController extends Controller
{ {
return response()->json([ return response()->json([
'source' => new SourceResource($source), 'source' => new SourceResource($source),
'articles' => $source->articles()->paginate(8) 'articles' => ArticleResource::collection($source->articles()->paginate(8))
]); ]);
} }
} }

View File

@@ -21,6 +21,7 @@ class ArticleResource extends JsonResource
"id" => $this->id, "id" => $this->id,
"title" => $this->title, "title" => $this->title,
"author" => $this->author, "author" => $this->author,
"meta" => $this->meta,
"featured_image" => $this->featured_image, "featured_image" => $this->featured_image,
"url" => $this->url, "url" => $this->url,
"body" => $this->body, "body" => $this->body,
@@ -28,8 +29,8 @@ class ArticleResource extends JsonResource
"source" => new SourceResource($this->source), "source" => new SourceResource($this->source),
"topics" => TopicResource::collection($this->topics), "topics" => TopicResource::collection($this->topics),
"link" => url(route('article.show',$this->id)), "link" => url(route('article.show',$this->id)),
"published_date" => $this->published_date, "published_date" => $this->published_date
"meta" => $this->meta
]; ];
} }
} }

View File

@@ -43,11 +43,8 @@ class MihaaruScraper
$this->author = $cleaneddata; $this->author = $cleaneddata;
}); });
$crawler->filter('article')->each(function ($node) { $crawler->filter('article p')->each(function ($node) {
$content = $node->text(); $this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
$input = str_replace("\n", '', $content);
$this->content = $input;
}); });
$crawler->filter('.article-tags')->each(function ($node) { $crawler->filter('.article-tags')->each(function ($node) {

View File

@@ -13,6 +13,6 @@ use Illuminate\Support\Facades\Route;
| |
*/ */
Route::get('/{any?}', function () { Route::get('{path}', function () {
return view('app'); return view('app');
}); })->where('path', '(.*)');