Skip to content

Instantly share code, notes, and snippets.

@shiweifu
shiweifu / obj2json
Created August 22, 2012 02:12
js_obj2string
function objToString (obj) {
var str = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
str += p + '::' + obj[p] + '\n';
}
}
return str;
}
@shiweifu
shiweifu / add_message.py
Created August 10, 2012 08:07
test curl
#!/usr/bin/env python
import os
import sys
#cmd = """curl http://localhost:8082/apiv1/msgadmin --data \"title=%s&content=content&desc=%s&icon=http://a1.phobos.le/b7/3f/8b/mzi.hztheuez.100x100-75.jpg&type=1&action=add&type_data={\"data\": {\"sort\": \"1\", \"title\": \"Naruto\", \"vip\": \"0\", \"tag\": \"pp_naruto\", \"icon\": \"http://a1.phobos.apple.com/us/r1000/010/Purple/b7/3f/8b/mzi.hztheuez.100x100-75.jpg\"}, \"content_type\": 1}&appname=%s\"}\"}\""""
cmd = """curl http://localhost:8082/apiv1/msgadmin --data \"title=%s&content=content&desc=%s&icon=http://a1.phobos.le/b7/3f/8b/mzi.hztheuez.100x100-75.jpg&type=1&action=add&type_data={\\\"\"data\\\"\": {\\\"\"sort\\\"\": \\\"\"1\\\"\", \\\"\"title\\\"\": \\\"\"Naruto\\\"\", \\\"\"vip\\\"\": \\\"\"0\\\"\", \\\"\"tag\\\"\": \\\"\"pp_naruto\\\"\", \\\"\"icon\\\"\": \\\"\"http://a1.phobos.apple.com/us/r1000/010/Purple/b7/3f/8b/mzi.hztheuez.100x100-75.jpg\\\"\"}, \\\"\"content_type\\\"\": 1}&appname=%s&lang=ja\""""
@shiweifu
shiweifu / upload.lua
Created July 27, 2012 02:40
通过openresty 模块上传文件,并在上传的过程中进行md5计算
local upload = require "resty.upload"
local cjson = require "cjson"
local resty_md5 = require "resty.md5"
local chunk_size = 4096 -- should be set to 4096 or 8192
-- for real-world settings
local form = upload:new(chunk_size)
local data = ""
@shiweifu
shiweifu / lc
Created July 21, 2012 13:53
lc
LANG="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_CTYPE="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_ALL=
-- author: shiweifu
-- mail: [email protected]
cjson = require("cjson")
function load_json_db( db_name )
local f = io.open(db_name)
local s = f:read("*a")
f:close()
return cjson.decode(s)
@shiweifu
shiweifu / mylog.py
Created July 13, 2012 05:41
日志封装
#!/usr/bin/env python
#coding: utf-8
#author: shiweifu
#mail : [email protected]
import logging
@shiweifu
shiweifu / define_ua.py
Created July 12, 2012 07:57
自定义user agent
UA_LIST = [a.split("\n")[0].strip() for a in open("user_agent", "r").readlines()]
random_value = lambda lst: random.randint(0, len(lst))
def down_page(url):
global UA_LIST
headers = { "User-Agent" : random_value(UA_LIST)}
req = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(req)
the_page = response.read()
@shiweifu
shiweifu / user_agent
Created July 12, 2012 02:24
一堆ua
w3m/0.4.1
Emacs-w3m/1.4.4 w3m/0.5.1+cvs-1.946
Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; MSN Companion 2.0; 800x600; Compaq)
DonutP; Windows98SE
MoonBrowser (version 0.41 Beta4)
Moonbrowser - IE based browser (Japan)
Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; MSN Companion 2.0; 800x600; Compaq)
Mozilla/1.1 (compatible; MSPIE 2.0; Windows CE)
Mozilla/1.22 (compatible; MSIE 2.0d; Windows NT)
Mozilla/2.0 (compatible; MSIE 2.1; Mac_PowerPC)
-- trim implementations
function trim1(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- from PiL2 20.4
function trim2(s)
return s:match "^%s*(.-)%s*$"
end
@shiweifu
shiweifu / test.lua
Created July 3, 2012 11:46
lua parser youku video
function main()
regex = [[false"><a title="(.-)"%starget=".-"%shref="(.-)%.html"]]
f = io.open("test.html", "r")
html = f:read("*a")
f:close()
s_idx, e_idx, title, url = string.find(html, regex, 0)