Justin's Words


  • Home

  • About

  • Archives
Justin's Words

运维笔记(二)

Posted on 2014-11-25 | In Linux |

SSH 登陆

1
ssh username@ServerIP
Read more »
Justin's Words

运维笔记(一)

Posted on 2014-11-25 | In Linux |

环境:Ubuntu 14.04

SSH 登陆

使用服务器商给你的公网 IP 进行登陆,然后会要求你输入密码,同样是服务器商提供给你的

1
$ ssh root@ServerIP

还没安装 SSH 得先安装 SSH

1
$ sudo apt-get install openssh-client openssh-server
Read more »
Justin's Words

PHP cURL GET & POST

Posted on 2014-11-23 | In PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function curlCrawler($url, $data=array(), $method='GET', $http_header = "application/x-www-form-urlencoded; charset=utf-8") {
$fields_string = '';
foreach ($data as $key => $value) {
$fields_string .= $key. '=' . $value . '&';
}

$curl_obj = curl_init();
curl_setopt($curl_obj, CURLOPT_HEADER, 0);

if (strtoupper($method) === 'POST') {
curl_setopt($curl_obj, CURLOPT_URL, $url);
curl_setopt($curl_obj, CURLOPT_POST, 1);
curl_setopt($curl_obj, CURLOPT_POSTFIELDS, $fields_string);
} elseif (strtoupper($method) === 'GET') {
curl_setopt($curl_obj, CURLOPT_URL, $url . '?'. $fields_string);
}

curl_setopt($curl_obj, CURLOPT_HTTPHEADER, array($http_header, "Content-Length: " . strlen($fields_string)));
curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_obj, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($curl_obj, CURLOPT_TIMEOUT, 20);

$rtn = curl_exec($curl_obj);

if (!curl_errno($curl_obj)) {
$response = $rtn;
} else {
$response = 'Curl error: ' . curl_error($curl_obj);
}

curl_close($curl_obj);
return $response;
}
Justin's Words

Laravel debug 功能开启

Posted on 2014-11-19 | In PHP |

app/config/app.php

1
2
3
4
5
6
7
return array(
...

'debug' => true,

...
);

备忘:

1
$ sudo chmod -R 777 /path/to/PHPApp/app/storage
Justin's Words

Nginx restore in Ubuntu

Posted on 2014-11-18 | In Linux |

起因是在 Nginx 下打开任何一个 .php 都 403,Laravel 设置 app/storage 为 777 访问 /public 同样 403,于是决定 restore Nginx。

环境

Ubuntu 14.10 Nginx Version: 1.6.2

Read more »
Justin's Words

Python 爬图类整理版

Posted on 2014-11-17 | In Python |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from urllib import request
from urllib.parse import urlparse
import re
import os
import time

class fetchPics(request.FancyURLopener):
"""docstring for fetchPics"""

def __init__(self, config = {}):
super(request.FancyURLopener, self).__init__()

default = {
'url': 'http://www.868aa.com/Se/Se-3-1.html',
'p_title': '<title>(.+?)_',
'p_link': '\/Se\/\d+\.html',
'p_image': '(td|br)\>\<img\ssrc=\"(.+?)\"',
'save_dir': 'img',
'output': True
}
config.update(default)

self.__url = config['url']
self.__p_title = config['p_title']
self.__p_link = config['p_link']
self.__p_image = config['p_image']
self.__save_dir = config['save_dir']
self.__output = config['output']
self.__tday = time.strftime(r"%m_%d_%Y")
version = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'

def isUrl(self):
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
if (regex.match(self.__url)):
return True
else:
return False

def check_dir(self):
if not os.path.exists(self.__save_dir):
os.makedirs(self.__save_dir)
print('Created directory ' + self.__save_dir)

def get_page(self):
if not self.isUrl():
print('Invalid url!')
return False
page = self.open(self.__url)
content = page.read().decode('gb18030')
return content

def get_links(self, save = True):
self.check_dir()
pattern = re.compile(self.__p_link)
links = pattern.findall(self.get_page())
new_links = []
file = open('img/' + self.__tday, 'w')

for link in links:
link = 'http://www.868aa.com' + link
new_links.append(link)
file.write(link + '\n')

file.close()
print('Links saved to img/' + self.__tday)
return new_links

def get_imgs(self):
self.check_dir()
print('Start to download images...')

for l in self.get_links():
page = self.open(l)
content = page.read().decode('gb18030')
p_title = re.compile(self.__p_title)
p_images = re.compile(self.__p_image)
title = p_title.findall(content)
images = p_images.findall(content)

print('Start to download ' + title[0] + '...')

i = 1
for img in images:
fileName, fileExtension = os.path.splitext(img[1])

if not os.path.exists('img/' + title[0]):
os.makedirs('img/' + title[0])
print('Created directory img/' + title[0])

saveName = 'img/' + title[0] + '/' + str(i) + fileExtension

if not os.path.exists(saveName):
self.retrieve(img[1], saveName)
print(saveName + ' downloaded')

i +=1

print(title[0] + 'downloaded')
Justin's Words

SimpleXML 解析 xml 类似 content:encoded 标签

Posted on 2014-11-12 | In PHP |

其实你如果查看我的博客的 rss,会发现有类似 <content:encoded> 或 <atom:link> 这种标签存在,那么怎么用 PHP 获取这种标签的信息呢? 以下是一个带有 <content:encoded> 的 xml 片段: fragment.xml

Read more »
Justin's Words

PHP 爬图脚本

Posted on 2014-11-10 | In PHP |

detect.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class IImage
{
private $url;
private $aContext = array(
'http' => array(
// 'proxy' => 'tcp://192.168.1.105:2516',
'proxy' => 'tcp://127.0.0.1:80',
'request_fulluri' => true,
),
);
private $pattern_title = "/<title>\[.*?\](.*?)\s\s.*?<\/title>/";
private $pattern_images = "/<input\stype=\'image\'\ssrc=\'(.*?)\'.*?>/";

public function __construct($url) {
$this->url = $url;
}

private function getContent() {
$contentArr = array();
$cxContext = NULL;
$content = NULL;
if (is_array($this->url)) {
foreach ($this->url as $key => $url) {
$cxContext = stream_context_create($this->aContext);
$content = file_get_contents($url, False, $cxContext);
$content = iconv('gbk', 'utf-8', $content);
array_push($contentArr, $content);
}
return $contentArr;
} else {
$cxContext = stream_context_create($this->aContext);
$content = file_get_contents($this->url, False, $cxContext);
$content = iconv('gbk', 'utf-8', $content);
return $content;
}
}

public function getTitle() {
$titleArr = array();
$title = NULL;
if (is_array($this->url)) {
foreach ($this->getContent() as $key => $content) {
preg_match_all($this->pattern_title, $content, $title);
array_push($titleArr, $title[1][0]);
}
return $titleArr;
} else {
preg_match_all($this->pattern_title, $this->getContent(), $title);
return $title[1][0];
}
}

private function getImages() {
$imagesArr = array();
$images = NULL;
if (is_array($this->url)) {
foreach ($this->getContent() as $key => $content) {
preg_match_all($this->pattern_images, $content, $images);
array_push($imagesArr, $images[1]);
}
return $imagesArr;
} else {
preg_match_all($this->pattern_images, $this->getContent(), $this->images);
return $this->images[1];
}
}

public function saveImages($dir) {
echo "Starting...\n\r";

if (!is_dir($dir)) {
mkdir($dir);
echo "Create directory ".$dir."\n\r";
}

// Create date directory
$today = getdate();
$toDir = $today['mon'].'-'.$today['mday'].'-'.$today['year'];

if (!is_dir($dir.'/'.$toDir)) {
mkdir($dir.'/'.$toDir);
echo "Create directory ".$dir.'/'.$toDir."\n\r";
}

if (is_array($this->url)) {
foreach ($this->getTitle() as $key => $title) {

if (!$title) {
$image_dir = $dir."/".$toDir.'/noTitle';
} else {
$image_dir = $dir."/".$toDir.'/'.$title;
}
if (!is_dir($image_dir)) {
mkdir($image_dir);
echo "Create directory ".$image_dir."\n\r";
}
echo "Downloading ".$title."\n\r";
foreach (array_unique($this->getImages()[$key]) as $key => $image) {
$image_name = $image_dir."/".$key.".".pathinfo($image, PATHINFO_EXTENSION);
if (!file_exists($image_name) && $image_stream=file_get_contents($image)) {
file_put_contents($image_name, $image_stream);
echo $image_name." downloaded\n\r";
}
}
}
echo "Download complete\n\r";
} else {

if (!$this->getTitle()) {
$image_dir = $dir."/".$toDir.'/noTitle';
} else {
$image_dir = $dir."/".$toDir.'/'.$title;
}

if (!is_dir($image_dir)) {
mkdir($image_dir);
echo "Create directory ".$image_dir."\n\r";
}

echo "Downloading...\n\r";
foreach (array_unique($this->getImages()) as $key => $image) {
$image_name = $image_dir."/".$key.".".pathinfo($image, PATHINFO_EXTENSION);
if (!file_exists($image_name) && $image_stream=file_get_contents($image)) {
file_put_contents($image_name, $image_stream);
echo $image_name." dowloaded\n\r";
}
}
echo "Download complete\n\r";
}
}
}

list.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class IList
{
private $url;
private $aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.1.105:2516',
'request_fulluri' => true,
),
);
private $list_link_pattern = "/<tr.*?class=\"tr3.*?><td><a.*?href=\"(.*?)\"/";
private $list_link_pattern1 = "/<title>.*?<\/title>/";

public function __construct($url) {
$this->url = $url;
}

private function getContent() {
$cxContext = stream_context_create($this->aContext);
$content = file_get_contents($this->url, False, $cxContext);
return $content;
}

private function resolveXMLtoList() {
$xml = simplexml_load_string(trim($this->getContent()));
foreach ($xml->channel->item as $key => $item) {
if ($key === 0) {
continue;
}
array_push($list, (string)$item->link);
}
return $list;
}

public function getList() {

return $list;
}
}

awesome.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
include_once('list.php');
include_once('detect.php');

$rss = "http://t66y.com/rss.php?fid=16";
$url = "http://wo.yao.cl/htm_data/16/1411/1274506.html";

$L = new IList($rss);
// $list = $L->getList();

$I = new IImage($url);
$I->saveImages('../img');

function moveNaked($dir='img') {
echo "Moving...\n\r";
$files = new DirectoryIterator($dir);
$imagePattern = "/(jpg|png|gif)/";
foreach ($files as $fileinfo) {
if ($fileinfo->isFile() && preg_match($imagePattern, $fileinfo->getExtension())) {
$oldname = $fileinfo->getPath()."/".$fileinfo->getFilename();
$newname = realpath("../img/noName\")."/".$fileinfo->getFilename();
rename($oldname, $newname);
echo $fileinfo->getFilename()." has moved to ".$dir."/noName/\n\r";
}
}
echo "Move completed.\n\r";
}
Justin's Words

Laravel 初上手

Posted on 2014-11-08 | In PHP |

Laravel 初上手 11-07-2014

起步

路由

app/routes.php

1
2
3
4
Route::get('users', function()
{
return 'Users!';
});
Read more »
Justin's Words

AngularJS Directive 学习笔记

Posted on 2014-10-16 | In Front-End |

指令要点

大漠穷秋老师的教学节点

  • 解析最简单的指令 hello: 匹配模式 restrict
  • 解析最简单的指令 hello: template、tempmlateUrl、$templateCache
  • 解析最简单的指令 hello: replace 与 transclude
Read more »
1…789…11
Justin Young

Justin Young

You Deserve A Better Life

107 posts
15 categories
54 tags
RSS
Github Twitter
© 2014 - 2019 Justin Young
Powered by Hexo
Theme - NexT.Mist