Exemple #1
0
/**
 * 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;
}
Exemple #2
0
 public static function getSort()
 {
     if (Cache::has(GlobalVar::$SORT_CACHE)) {
         return Cache::get(GlobalVar::$SORT_CACHE);
     } else {
         $sorts = Sort::all();
         Cache::forever(GlobalVar::$SORT_CACHE, $sorts);
         return $sorts;
     }
 }
Exemple #3
0
 /**
  * 得到首页推荐
  *
  * @return array
  */
 public function getIndex()
 {
     $json = $this->getUrl('http://api.bilibili.cn/index');
     $list = array();
     foreach ($json as $type => $value) {
         $sort = Sort::where('type', '=', $type)->first();
         if ($sort != null) {
             $temp = array();
             $temp['sort'] = $sort;
             $temp['list'] = array();
             foreach ($value as $id => $content) {
                 if (!is_string($content)) {
                     array_push($temp['list'], $content);
                 }
             }
             array_pop($temp['list']);
             array_push($list, $temp);
         }
     }
     return $list;
 }