Last active
August 29, 2015 14:05
-
-
Save zhangxigithub/f5fd6c6a3f7edfede844 to your computer and use it in GitHub Desktop.
change url to apiary.io demo:http://docs.zxtest1.apiary.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
#!/usr/bin/python | |
import getopt,sys,urlparse,urllib2,json | |
def getAPI(theURL): | |
jsonString=urllib2.urlopen(theURL).read() | |
page = json.dumps(json.loads(jsonString),indent=4) | |
page = " "+page.replace("\n","\n ") | |
url = urlparse.urlparse(theURL) | |
i = len(url.path) - 1 | |
while i > 0: | |
if url.path[i] == '/': | |
break | |
i = i - 1 | |
filename = url.path[i+1:len(url.path)] | |
params = dict([(k,v[0]) for k,v in urlparse.parse_qs(url.query).items()]) | |
result = '\n\n' | |
# 1.api params | |
if len(params.keys())>0: | |
# 1.api name | |
result += "## "+filename+" ["+url.path | |
for key in params.keys(): | |
result += "{?"+key+"}" | |
result += "]\n\n" | |
else: | |
result += "## "+filename+" ["+url.path+ "]\n\n" | |
# 2.api get | |
result += "### "+filename+" [GET]\n\n" | |
# 3. + Parameters | |
if len(params.keys()) > 0: | |
result += "+ Parameters\n\n" | |
for key,value in params.items(): | |
result += " + "+key+" (`"+value+"`) ... "+key+"\n" | |
result += "\n" | |
# 4.response | |
if page : | |
result += "+ Response 200 (application/json)\n\n" | |
result += page | |
return result | |
################################################################# | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "f:u:", ["url=","file="]) | |
except getopt.GetoptError: | |
sys.exit() | |
if len(opts) == 0: | |
print "使用说明:\n" | |
print "单个url:" | |
print "api -u 'http://www.zhangxi.me/api?params=aaa' \n" | |
print "多个url,存入文件按行分隔,例如:" | |
print "http://....." | |
print "http://....." | |
print "http://....." | |
print "api -f filename.txt\n" | |
print "生成的文件保存到同目录下的api.txt文件" | |
sys.exit() | |
for o, a in opts: | |
if o in ("-f", "--file"): | |
urlFile = open(a ,"r") | |
apiText = "" | |
for line in urlFile: | |
apiText += getAPI(line.replace("\n","")) | |
apiText += "\n\n\n\n\n" | |
apiFile = open("api.txt" ,"w") | |
apiFile.write(apiText) | |
if o in ("-u", "--url"): | |
apiFile = open("api.txt" ,"w") | |
apiFile.write(getAPI(a)) | |
print "end" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
输入: | |
python api.py -u "http://www.zhangxi.me/api/index.php?name=zhangxi" | |
输出: | |
## index.php [/api/index.php{?name}] | |
### index.php [GET] | |
+ Parameters | |
+ name (`zhangxi`) ... name | |
+ Response 200 (application/json) | |
{ | |
"male": true, | |
"name": "http://zhangxi.me" | |
} | |
在拷贝到http://apiary.io 上即可 | |
demo: | |
http://docs.zxtest1.apiary.io |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment