Files
cm-app-crawlspider/homestead/application/models/CurationModel.php

70 lines
2.3 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class CurationModel extends MY_Model
{
public $thumbnail;
public $title;
public $description;
public $likes;
public $subscribes;
public $updatedAt;
public $category;
public $link;
private $table = 'cm_curation_raw_yt';
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function save($data)
{
$inserted = [];
$updated = [];
foreach ($data->videos as $v) {
if (empty($data->action) || $data->action == 'insert') {
$publishedAt = date('Y-m-d H:i:s', strtotime($v->publishedAt));
$v = (object) array_map(function ($val) {
return is_null($val) ? "" : $val;
}, (array) $v);
$video = [
'Title' => $v->title,
'Thumbnail' => $v->thumbnails->high->url,
'Description' => $v->description,
'Category' => $data->categoryTitle,
'VideoId' => $v->id,
'UpdatedAt' => $publishedAt,
'CreatedAt' => date('Y-m-d H:i:s')
];
}else $video = (array)$v;
if (!$this->db->where('VideoId', $video['VideoId'])->get($this->table)->num_rows()) {
$this->db->insert($this->table, $video);
array_push($inserted, $this->db->insert_id());
} else {
$this->db->where('VideoId', $video['VideoId'])->update($this->table, $video);
array_push($updated, $video['VideoId']);
}
}
return ['inserted' => $inserted, 'updated' => $updated];
}
public function get($data)
{
return $this->db->offset($data->offset)->limit(empty($data->limit) ? 10 : $data->limit)->get($this->table)->result();
}
public function delete($data)
{
$deleted = [];
foreach($data->videos as $video) {
$this->db->where('VideoId', $video->VideoId)->delete($this->table);
array_push($deleted, $video->VideoId);
}
return $deleted;
}
}