protected function downloadFileInWindowsNT(TFFile $file) { $client = new Client(); try { $response = $client->get($file->source, ['verify' => false]); } catch (\Exception $e) { $error = 'TFDownloader: Downloading file failed, url is %s, msg is %s.'; $error = sprintf($error, $file->source, $e->getMessage()); Log::error($error); $file->state = 'error'; $file->error = $error; $file->save(); return; } if ($response->getStatusCode() == 200) { $location = 'file/' . $file->name; Storage::put($location, $response->getBody()); $file->state = 'downloaded'; $file->location = $location; $file->save(); return; } else { $error = 'TFDownloader: Bad HTTP response code in downloading videos, url is %s, status code is %d.'; $error = sprintf($error, $file->source, $response->getStatusCode()); Log::error($error); $file->state = 'error'; $file->error = $error; $file->save(); return; } }
protected function sniffVideoOfDom(Dom $dom) { foreach ($dom->find('iframe') as $videoFrame) { // 获得iframe的地址 $videoFrameSrc = $videoFrame->getAttribute('src'); // 如果iframe的地址不存在,或者不含有特定字符串(用来判别是否是视频),废弃 if (empty($videoFrameSrc)) { continue; } if (!strstr($videoFrameSrc, 'https://www.tumblr.com/video/')) { continue; } try { // 如果已经检测了此视频,返回 $parentId = $videoFrame->getParent()->getAttribute('id'); $name = explode('_', $parentId)[3] . '.mp4'; if (TFFile::where('name', '=', $name)->count() != 0) { continue; } // 准备video的DOM $videoDom = $this->requestDom($videoFrameSrc); // 获取对应节点 $videoElem = $videoDom->find('video')[0]; $videoSource = $videoElem->find('source')[0]; } catch (\Exception $e) { $error = 'TFSniffer: Failed to parse video element, url is %s, msg is %s.'; $error = sprintf($error, $videoFrameSrc, $e->getMessage()); Log::error($error); continue; // pass this } // 开始读取数据 $video = new TFFile(); $video->type = 'video'; $video->state = 'sniffed'; $video->error = ''; $video->name = $name; $video->source = $videoSource->getAttribute('src'); $video->save(); } }