(.*?)<\/a> <\/h4>.*?(.*?)<\/p>/';
}
- $rs=preg_match_all($pattern, $content, $matches);
- if($rs){
+ $rs = preg_match_all($pattern, $content, $matches);
+ if ($rs) {
mylog('crawl finished.start parsing');
- $data=array();
+ $data = array();
foreach ($matches[1] as $key => $val) {
- if(false !== strpos($matches[1][$key], 'job') || false !== strpos($matches[1][$key], 'amazon')){ // get rid of job and book recommendations
+ if (false !== strpos($matches[1][$key], 'job') || false !== strpos($matches[1][$key], 'amazon')) {
+ // get rid of job and book recommendations
continue;
}
- $item=[
- 'title'=>strip_tags($matches[2][$key]),
- 'desc'=>$matches[3][$key],
- 'href'=>$val,
- 'number'=>$number,
- 'hash'=>md5($val)
+ $item = [
+ 'title' => strip_tags($matches[2][$key]),
+ 'desc' => $matches[3][$key],
+ 'href' => $val,
+ 'number' => $number,
+ 'hash' => md5($val),
];
//加入数据库
- $id=$this->add_cache($item);
- $item['id']=$id;
+ $id = $this->add_cache($item);
+ $item['id'] = $id;
mylog("item {$id} added to cache");
// 加入数组
- $data[]=$item;
+ $data[] = $item;
}
return $data;
}
@@ -304,48 +319,48 @@ function crawl($number){
/**
* add item to cache
*/
- function add_cache($item){
- $sql=$this->arr2sql('cache',$item);
+ function add_cache($item) {
+ $sql = $this->arr2sql('cache', $item);
$this->pdo->exec($sql);
- $id=$this->pdo->lastInsertId();
+ $id = $this->pdo->lastInsertId();
return $id;
}
/**
* translate array to sql
*/
- function arr2sql($dbname,$arr,$updateid=null){
-
- if($updateid){
+ function arr2sql($dbname, $arr, $updateid = null) {
+
+ if ($updateid) {
// return "update {$dbname} set "
unset($arr['ctime']);
unset($arr['addtime']);
- $sql="update {$dbname} set ";
+ $sql = "update {$dbname} set ";
foreach ($arr as $key => $val) {
- $sql.="`{$key}`='{$val}',";
+ $sql .= "`{$key}`='{$val}',";
}
- $sql.='ctime='.time();
- $sql.=" where id={$updateid}";
+ $sql .= 'ctime=' . time();
+ $sql .= " where id={$updateid}";
return $sql;
- }else{
- $fields=[];
- $values=[];
+ } else {
+ $fields = [];
+ $values = [];
foreach ($arr as $key => $v) {
- $fields[]="`{$key}`";
- $values[]="'{$v}'";
+ $fields[] = "`{$key}`";
+ $values[] = "'{$v}'";
}
- return "insert into {$dbname} (".implode(',', $fields).") values(".implode(',', $values).")";
+ return "insert into {$dbname} (" . implode(',', $fields) . ") values(" . implode(',', $values) . ")";
}
}
/**
* get categories
*/
- function get_cate(){
- $arr=[];
- $rs=$this->pdo->query("select distinct category from issue order by category")->fetchAll(PDO::FETCH_ASSOC);
+ function get_cate() {
+ $arr = [];
+ $rs = $this->pdo->query("select distinct category from issue order by category")->fetchAll(PDO::FETCH_ASSOC);
foreach ($rs as $val) {
- $arr[]=$val['category'];
+ $arr[] = $val['category'];
}
return $arr;
}
@@ -353,16 +368,19 @@ function get_cate(){
/**
* add an item to db or update it if exists
*/
- function add($data){
- $sql = $this->arr2sql('issue',$data);
- $rs=$this->pdo->exec($sql);
- if($rs){
+ function add($data) {
+ $sql = $this->arr2sql('issue', $data);
+ $rs = $this->pdo->exec($sql);
+ if ($rs) {
return $this->pdo->lastInsertId();
}
- $rs=$this->pdo->query("select id from issue where hash='{$data['hash']}'")->fetch(PDO::FETCH_ASSOC);
- if(isset($rs['id'])){
- $update=$this->pdo->exec($this->arr2sql('issue',$data,$rs['id']));
- if($update) return $rs['id'];
+ $rs = $this->pdo->query("select id from issue where hash='{$data['hash']}'")->fetch(PDO::FETCH_ASSOC);
+ if (isset($rs['id'])) {
+ $update = $this->pdo->exec($this->arr2sql('issue', $data, $rs['id']));
+ if ($update) {
+ return $rs['id'];
+ }
+
}
return false;
}
@@ -370,12 +388,12 @@ function add($data){
/**
* fetch all recorded data
*/
- function get_all(){
+ function get_all() {
return $this->pdo->query('select * from issue order by category,addtime')->fetchAll(PDO::FETCH_ASSOC);
}
- function current_number(){
- $rs=$this->pdo->query('select max(number) as max from issue')->fetch(PDO::FETCH_ASSOC);
+ function current_number() {
+ $rs = $this->pdo->query('select max(number) as max from issue')->fetch(PDO::FETCH_ASSOC);
return $rs['max'];
}
}
diff --git a/talks.md b/talks.md
index 991af64..57de18c 100644
--- a/talks.md
+++ b/talks.md
@@ -379,3 +379,17 @@
[我的编程生涯里启发我的 15 本书](http://weekly.manong.io/bounce?url=https%3A%2F%2Fwww.phodal.com%2Fblog%2Fmost-important-15-books-of-phodal-programmer-life%2F&aid=5687&nid=111)
[写在离开百度后的第一天](http://weekly.manong.io/bounce?url=http%3A%2F%2Fweibo.com%2Fp%2F1001603956574254141761+&aid=5686&nid=111)
[重构的七宗罪](http://weekly.manong.io/bounce?url=http%3A%2F%2Finsights.thoughtworkers.org%2Frefactoring%2F&aid=5694&nid=111)
+[我的编程之路](http://weekly.manong.io/bounce?url=https%3A%2F%2Fgithub.com%2Ficepy%2F_posts%2Fissues%2F32&aid=5760&nid=112)
+[Google 的绩效管理](http://weekly.manong.io/bounce?url=http%3A%2F%2Fwww.ruanyifeng.com%2Fblog%2F2016%2F03%2Fperformance-management.html&aid=5761&nid=112)
+[让 BAT 的 Offer 不再难拿](http://weekly.manong.io/bounce?url=http%3A%2F%2Fwww.jianshu.com%2Fp%2Fee15c1cf9c16&aid=5759&nid=112)
+[GitHub 中国区前 100 名到底是什么样的人?](http://weekly.manong.io/bounce?url=http%3A%2F%2Fdiycode.cc%2Ftopics%2F23&aid=5811&nid=113)
+[我的日常一天](http://weekly.manong.io/bounce?url=http%3A%2F%2Fgulu-dev.com%2Fpost%2F2016-04-09-a-working-day&aid=5809&nid=113)
+[程序员成长攻略](http://weekly.manong.io/bounce?url=http%3A%2F%2Fwww.21cto.com%2Farticle%2F46&aid=5810&nid=113)
+[[译] NPM 与 left-pad 事件:我们是不是早已忘记该如何好好地编程?](http://weekly.manong.io/bounce?url=http%3A%2F%2Fqianduan.guru%2F2016%2F04%2F04%2Fnpm-left-pad-have-we-forgotten-how-to-program%2F&aid=5839&nid=113)
+[腾讯广州后台开发一面面经](http://weekly.manong.io/bounce?url=http%3A%2F%2Fwww.nowcoder.com%2Fdiscuss%2F4631%3Fsource%3Drss&aid=5842&nid=113)
+[程序员也需要了解期望值管理](http://weekly.manong.io/bounce?url=http%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F20046507&aid=5848&nid=113)
+[阿里巴巴、美团等各大互联网公司的 Java 类校招对本科生有什么要求?](http://weekly.manong.io/bounce?url=https%3A%2F%2Fwww.zhihu.com%2Fquestion%2F26350691%2Fanswer%2F42692522&aid=5884&nid=114)
+[从学校到公司](http://weekly.manong.io/bounce?url=https%3A%2F%2Fwww.noterhub.com%2F2016%2F04%2F15%2F%25E4%25BB%258E%25E5%25AD%25A6%25E6%25A0%25A1%25E5%2588%25B0%25E5%2585%25AC%25E5%258F%25B8%2F&aid=5898&nid=114)
+[程序员的黄金时代](http://weekly.manong.io/bounce?url=http%3A%2F%2Fblog.coderzh.com%2F2016%2F04%2F17%2Fgold-time-of-programmer%2F&aid=5899&nid=114)
+[《程序员健康指南》读书笔记](http://weekly.manong.io/bounce?url=http%3A%2F%2Fwww.jianshu.com%2Fp%2Fb30bb43bdfdf&aid=5900&nid=114)
+[我是怎么一个月将应用弄上 App Store 首页推荐的?](http://weekly.manong.io/bounce?url=http%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F20742241&aid=5923&nid=114)