[置顶][原创]在线视频下载(Using Python / Bash / C / Reguar Expressions)
posted @ 2012-01-30 14:00 Wind__Fantasy 阅读(1629) 评论(3) 编辑
置顶随笔 #
posted @ 2012-01-30 14:00 Wind__Fantasy 阅读(1629) 评论(3) 编辑
posted @ 2011-11-23 16:23 Wind__Fantasy 阅读(37) 评论(0) 编辑
2012年2月21日 #
google翻译api要收费了, 所以想法写了个免费版本的google翻译:
<?php
/**
* translate [short] text from source language to target language using google translate proxy
* be careful if google data type or url has been modified
*/
class GoogleTranslater
{
protected $text, $source, $target, $url, $holder;
const REMOTE_TRANSLATE_URL='http://translate.google.com/translate_a/t?client=t&text=%TEXT%&hl=de&sl=%SL%&tl=%TL%&multires=1&otf=2&ssel=3&tsel=3&sc=1';
public function __construct($text, $source='en', $target='de')
{
$this->source=$source;
$this->target=$target;
$this->text=$text;
$this->holder=array();
$this->url=str_replace(array('%TEXT%', '%SL%', '%TL%'), array($text, $source, $target), self::REMOTE_TRANSLATE_URL);
}
public function getTranslateResult()
{
$ch=curl_init($this->url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_REFERER, 'http://translate.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode(preg_replace('/,{2,}/', ',', $response), true);
return ($json && isset($json[0]) && isset($json[0][0]) && isset($json[0][0][0])) ? $json[0][0][0] : null;
}
}
$gt = new GoogleTranslater('你好, 世界!', 'zh', 'en');
$r = $gt->getTranslateResult();
echo "$r\n";
?>
输出为: Hello , world !
替换'你好, 世界!'为其他的中文运行试试看
posted @ 2012-02-21 15:38 Wind__Fantasy 阅读(113) 评论(2) 编辑
2012年1月30日 #
posted @ 2012-01-30 14:00 Wind__Fantasy 阅读(1629) 评论(3) 编辑
2011年12月8日 #
(1) ping -c 1 $(hostname) | grep -m 1 -oP '(\d+\.)+\d+' # ping selfhost and catch the first matched ip
(2) host $(hostname) | grep -oP '(\d+\.)+\d+' # catch matched ip from selfhost's hostname and ip
posted @ 2011-12-08 10:07 Wind__Fantasy 阅读(25) 评论(0) 编辑
2011年11月23日 #
所有的对内置对象扩展的方法都是以x(stands for 'extend')开头, 非扩展内置对象的方法按英文原意命名, 然后命名空间为wf(stands for wind fantasy)
Object.prototype.xattach = function (obj, nooverwrite) {
for (var prop in obj) {
// if the property exists and disallow the property to be overwritten, just skip
if (typeof this[prop] != 'undefined' && nooverwrite) {
continue;
}
this[prop] = obj[prop];
}
return this;
}
String.xattach({
xrepeat: function (repeat, times) {
return new Array(times+1).join(repeat);
}
});
String.prototype.xattach( {
xrepeat: function (times) {
return String.xrepeat(this, times);
}
});
Array.xattach({
'xrange': function (start, stop, step) { // python-like range
var value, range = [];
if (arguments.length == 1) {
stop = start;
start = 0;
}
if (isNaN(stop) || isNaN(start)) {
return null;
}
if (isNaN(step) || step == 0)
step = start < stop ? 1 : -1;
if (step > 0) {
for (value = start; value < stop; value += step)
range.push(value);
} else {
for (value = start; value > stop; value += step)
range.push(value);
}
return range;
}
});
Array.prototype.xattach({
// toString: function () { // better toString
// return ' [' + this.join(',') + '] ';
// },
xcopy: function () { // shallow copy
return this.slice();
},
// [1,2,3,4,5].xgroup(2) => [ [1,2], [2,3], [3,4], [4,5] ]
xgroup: function (count) { // group all subsequences of which the length is count from this
var i, targetlen = this.length - count + 1, target = [];
for (i = 0; i < targetlen; ++i)
target[i] = this.slice(i, i+count);
return target;
},
// ['a', 'b', 'c']; xmap(null, parseInt, true, 16) => [10, 11, 12]
// if you don't want to change array itsself and wanna get the return value, you
// can do like this: arr.xcopy().xmap(null, parseInt, true, 16) // just change its (shallow) copy
xmap: function (thisobj, func, changeself, extraargs) { // map this with thisobj.fuc
var i = 0, len = this.length;
if (! (extraargs instanceof Array)) {
extraargs = Array.prototype.slice.call(arguments, 3);
}
if (changeself) {
for (i=0; i < len; ++i) {
this[i] = func.apply(thisobj, [ this[i] ].concat(extraargs));
}
} else {
for (i=0; i < len; ++i) {
func.apply(thisobj, [ this[i] ].concat(extraargs));
}
}
return this;
}
});
posted @ 2011-11-23 16:23 Wind__Fantasy 阅读(37) 评论(0) 编辑