/** * Created by PhpStorm. * User: WhiteBlue * Date: 15/7/8 * Time: 下午10:11 */ function fetch() { $client = new Client(); $request = new Request('GET', 'http://api.bilibili.cn/index'); $response = $client->send($request, ['timeout' => 2]); $json = json_decode($response->getBody()); $count = 0; foreach ($json as $type => $value) { $sort = Sort::where('type', '=', $type)->first(); if ($sort != null) { foreach ($value as $id => $content) { if (is_object($content)) { if (Save::where('aid', '=', $content->aid)->first() == null) { $save = new Save(); $save->aid = $content->aid; $save->title = $content->title; if (strlen($content->description) > 70) { $save->content = mb_substr($content->description, 0, 70, 'utf-8') . '....'; } else { $save->content = $content->description; } $save->href = 'http://www.bilibili.com/video/AV' . $content->aid; $save->img = $content->pic; $sort->saves()->save($save); $count++; $sort->update = date('Y:m:d'); $sort->save(); } } } } } event(new UpdateEvent()); return $count; }
public static function saveNew($json, $aid) { $save = new Save(); $save->aid = $aid; $save->mid = $json['mid']; $save->cid = $json['cid']; $save->typename = $json['typename']; $save->title = $json['title']; $save->play = $json['play']; $save->review = $json['review']; $save->video_review = $json['video_review']; $save->favorites = $json['favorites']; $save->coins = $json['coins']; $save->pages = $json['pages']; $save->author = $json['author']; $save->face = $json['face']; $save->description = $json['description']; $save->tag = $json['tag']; $save->pic = $json['pic']; $save->created_at = date_create($json['created_at']); $save->offsite = $json['offsite']; $save->save(); return $save; }