Skip to content

Commit

Permalink
update preg match & format code
Browse files Browse the repository at this point in the history
  • Loading branch information
copyrenzhe committed Aug 31, 2017
2 parents d645ef3 + 12ada73 commit 0b2eea8
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ WECHAT_PAYMENT_DEVICE_INFO
WECHAT_PAYMENT_SUB_APP_ID
WECHAT_PAYMENT_SUB_MERCHANT_ID

WECHAT_TYPE
WECHAT_TYPE

BAIDU_PUSH_API
13 changes: 6 additions & 7 deletions app/Console/Commands/SiteMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Console\Commands;

use Cache;
use Carbon\Carbon;
use Event;
use App\Events\MailPostEvent;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -48,15 +46,16 @@ public function handle()
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode === 200) {
Cache::put('siteMonitor', 0, 60);
if($httpcode===200){
\Cache::forget('siteMonitor');
} else {
Cache::increment('siteMonitor');
\Cache::increment('siteMonitor');
}
//连续一个小时访问异常
if (Cache::get('siteMonitor') > 5) {
if(\Cache::get('siteMonitor', 0) > 5){
Event::fire(new MailPostEvent('system', '网站访问异常', array()));
Cache::put('siteMonitor', 0, 60);
\Cache::forget('siteMonitor');
}
return \Cache::get('siteMonitor', 0);
}
}
8 changes: 8 additions & 0 deletions app/Jobs/SnatchInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function handle()
$instance::snatch($novel);
Log::info("小说[$novel->id]:[$novel->name] 已采集完毕");
}

//百度推送
$urls[] = route('book', ['bookId' => $novel->id], true);
$ret = baiduPush($urls);
$retMessage = $ret ? 'success' : 'failed';
Log::info('baidu push '.var_export($urls, true) . $retMessage);
//百度推送END

$dtEnd = microtime_float();
Log::info('expire time '.($dtEnd-$dtStart).' seconds');
Log::info('----- FINISHED THE PROCESS FOR INIT NOVEL FROM LINK:'.$this->link. '-----');
Expand Down
17 changes: 13 additions & 4 deletions app/Repositories/Snatch/Biquge.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public function getNovelList()
public function init($link)
{
$novel_html = $this->send($link, 'GET', false, 'utf-8');
if (!$novel_html) {
return false;
}
if (preg_match('/property="og:novel:book_name" content="(.*?)"/s', $novel_html, $novel_name)) {
preg_match('/property="og:novel:author" content="(.*?)"/s', $novel_html, $novel_author);
preg_match('/property="og:novel:category" content="(.*?)"/s', $novel_html, $category);
Expand Down Expand Up @@ -221,11 +224,14 @@ public function init($link)
/**
* @desc 使用curl_multi 多线程更新章节
* @param Novel $novel
* @return string|void
* @return string|mixed
*/
public function update(Novel $novel)
{
$novel_html = $this->send(self::DOMAIN . $novel->source_link, 'GET', false, 'utf-8');
if (!$novel_html) {
return false;
}

$chapter_list = $this->getChapterList($novel_html);
if (!$chapter_list[1]) {
Expand All @@ -243,8 +249,8 @@ public function update(Novel $novel)
$last_novel = $novel->chapter->last();
if ($last_novel) {
$last_url = $last_novel->source_link;
$urlArr = explode('/', $last_url);
$curr_key = array_search(end($urlArr), $chapter_list[1]);
$urlArr = parse_url($last_url);
$curr_key = array_search($urlArr['path'], $chapter_list[1]);
} else {
$curr_key = -1;
}
Expand Down Expand Up @@ -319,11 +325,14 @@ public function update(Novel $novel)
/**
* 采集小说章节实现方法
* @param Novel $novel
* @return array
* @return array|mixed
*/
public function snatch(Novel $novel)
{
$novel_html = $this->send(self::DOMAIN . $novel->source_link, 'GET', false, 'utf-8');
if (!$novel_html) {
return false;
}
$chapter_list = $this->getChapterList($novel_html);
if (!$chapter_list[1]) {
Log::error('getChapterList failed');
Expand Down
6 changes: 6 additions & 0 deletions app/Repositories/Snatch/Snatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class Snatch
protected $page_size = 200;
protected $source;

public function ping()
{
$ret = $this->send(static::DOMAIN, 'GET', false, 'utf-8');
return $ret;
}

/**
* 单线程模拟请求
* @param $url
Expand Down
2 changes: 2 additions & 0 deletions app/Repositories/Snatch/SnatchInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

Interface SnatchInterface
{
public function ping();

//获取小说列表
public function getNovelList();

Expand Down
37 changes: 33 additions & 4 deletions app/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ function async_get_url($urls, $append_url = '', $page_size = 500, $source_encode
$handle = $info['handle'];
// 读取收到的内容
$content = curl_multi_getcontent($handle);
$recv[] = curl_errno($handle) == 0 ? (($source_encode != 'utf-8') ? mb_convert_encoding($content, 'UTF-8', 'gbk') : $content) : '';
$recv[] = curl_errno($handle) == 0 ? (($source_encode != 'utf-8') ? mb_convert_encoding($content,
'UTF-8', 'gbk') : $content) : '';
// 移除本资源
curl_multi_remove_handle($mh, $handle);
// 关闭资源
Expand Down Expand Up @@ -217,8 +218,10 @@ function getFileSize($url)
$tmp = fgets($fp);
if (trim($tmp) == '') {
break;
} else if (preg_match('/Content-Length:(.*)/si', $tmp, $arr)) {
return trim($arr[1]);
} else {
if (preg_match('/Content-Length:(.*)/si', $tmp, $arr)) {
return trim($arr[1]);
}
}
}
return null;
Expand All @@ -243,12 +246,38 @@ function qidianRank($mod = 'click', $dataType = '1', $chn = '-1', $maxPage = 10)
for ($i = 1; $i <= $maxPage; $i++) {
$urlArr[] = $url_base . $mod . '?dateType=' . $dataType . '&chn=' . $chn . '&page=' . $i;
}
$htmlArr = async_get_url($urlArr, '', $maxPage, '');
$htmlArr = async_get_url($urlArr, '', $maxPage, 'utf-8');
$nameArr = [];
foreach ($htmlArr as $html) {
preg_match_all('/<div class=\"book-mid-info\">.*?<h4><a .*?>(.*?)<\/a><\/h4>.*?<\/div>/s', $html, $matches);
$nameArr = array_merge($nameArr, $matches[1]);
}
return $nameArr;
}
}

/**
* 百度SEO推送
*/
if (!function_exists('baiduPush')) {
function baiduPush($urls)
{
if (config('app.baidu_push_api')) {
$api = config('app.baidu_push_api');
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);;
return $httpCode == 200;
} else {
return true;
}
}
}
2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

'url' => env('APP_URL', 'http:https://localhost'),

'baidu_push_api' => env('BAIDU_PUSH_API', ''),

/*
|--------------------------------------------------------------------------
| Application Timezone
Expand Down

0 comments on commit 0b2eea8

Please sign in to comment.