Last active
August 29, 2015 14:05
-
-
Save zhwei/d1e77fd3f7441f6e20cf to your computer and use it in GitHub Desktop.
百姓网开放APIv2 Python版SDK
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# 百姓网开放APIv2 Python版SDK | |
# 支持 Python2.7+ | |
# Author: [email protected] | |
# Date: 2014-08-13 | |
# 开发文档:http://api.baixing.com.cn/developer/ | |
import json | |
import hashlib | |
try: | |
from urllib.request import Request, urlopen # Python 3 | |
except ImportError: | |
from urllib2 import Request, urlopen # Python 2 | |
APP_KEY = "YOUR_APP_KEY" | |
APP_SECRET = "YOUR_APP_SECRET" | |
class BaixingAPI(object): | |
""" 百姓网开放APIv2 SDK | |
""" | |
api_base_url = "http://api.baixing.com.cn/v2/" | |
def __init__(self, user_mobile): | |
""" 可选user_mobile参数 | |
""" | |
self.user_mobile = user_mobile | |
def json_dumps(self, s): | |
"""封装json转码,正常显示中文""" | |
return json.dumps(s, ensure_ascii=False) | |
def md5(self, s): | |
"""封装md5方法""" | |
m = hashlib.md5() | |
m.update(s.encode("utf-8")) | |
return m.hexdigest() | |
def run(self, api_name, params): | |
"""接口函数""" | |
api_url = "{0}{1}".format(self.api_base_url, api_name) | |
params['userMobile'] = self.user_mobile | |
json_data = self.json_dumps(params) | |
headers = { | |
"Bapi-App-key": APP_KEY, | |
"Bapi-Hash": self.md5("{0}{1}{2}".format(api_name, | |
json_data, APP_SECRET)), | |
"User-Ip": "", | |
} | |
req = Request(api_url) | |
for head in headers.items(): req.add_header(*head) | |
ret = urlopen(req, json_data.encode("utf-8")) | |
return json.loads(ret.read().decode("utf-8")) | |
def area(self, param): | |
"""获取地域树""" | |
# check item | |
area_id = param.get('id') | |
englishName = param.get('englishName') | |
if not any([area_id, englishName]): | |
return dict(status="fail", error=2004, | |
message=("Parameters: Required parameter "), | |
data=None,) | |
levels = param.get("levels", "1") | |
if levels not in ("0", "1"): | |
return dict(status="fail", error=2005, | |
message=("Parameters: Optional parameter" | |
" [levels] should be 0 or 1."), | |
data=None,) | |
# area_id = param.get("areaId").strip() | |
data = dict(levels=int(levels), englishName=englishName, id=area_id) | |
return self.run("area", data) | |
def category(self, param): | |
""" 获取分类 | |
""" | |
# check item | |
for item in ("categoryId", ): | |
if not param.get(item, None): | |
return dict(status="fail", error=2004, | |
message=("Parameters: Required parameter " | |
"[{0}] is missing or blank.".format(item)), | |
data=None,) | |
levels = param.get("levels", "1") | |
if levels not in ("0", "1"): | |
return dict(status="fail", error=2005, | |
message=("Parameters: Optional parameter" | |
" [levels] should be 0 or 1."), | |
data=None,) | |
else: | |
levels = "1" | |
category_id = param.get("categoryId", None) or param.get("id", None) | |
data = dict(levels=int(levels), id=category_id) | |
return self.run("category", data) | |
def meta(self, param): | |
"""获取元数据""" | |
for item in ("categoryId",): | |
if not param.get(item, None): | |
return dict(status="fail", error=2004, | |
message=("Parameters: Required parameter " | |
"[{0}] is missing or blank.".format(item)), | |
data=None,) | |
data = {} | |
data['categoryId'] = param.get("categoryId", None).strip() | |
for item in ("metaName", "parentValue",): | |
if param.get(item, None): | |
data[item] = param.get(item).strip() | |
if param.get("expand", None) in ("true", "1"): | |
data["expand"] = True | |
return self.run("meta", data) | |
def post(self, param): | |
"""提交AD""" | |
for item in ("cityEnglishName", "地区", "categoryId", | |
"title", "contact"): | |
if not param.get(item, None): | |
return dict(status="fail", error=2004, | |
message=("Parameters: Required parameter " | |
"[{0}] is missing or blank.".format(item)), | |
data=None,) | |
return self.run("post", param) | |
def query(self, param): | |
""" 查询提交过的AD | |
""" | |
detail = param.get("detail", None) | |
detail = False if detail in ("false", 0) else True | |
return self.run("query", param) | |
def delete(self, param): | |
""" 根据ID删除AD | |
""" | |
if not param.get("id", None): | |
return dict(status="fail", error=2004, | |
message=("Parameters: Required parameter " | |
"[id] is missing or blank."), | |
data=None,) | |
return self.run("delete", param) | |
if __name__ == '__main__': | |
# api = BaixingAPI("1831231231") | |
# print(api.area({"areaId":"m33"})) | |
# print(api.category({"categoryId":"fang"})) | |
# print(api.meta({"categoryId":"ershouqiche"})) | |
# data = { | |
# "categoryId":"ershouqiche", | |
# "cityEnglishName":"aba", | |
# "地区":"m4807", | |
# "title":"API Test", | |
# "contact": "1830000000", | |
# "content":"test", | |
# } | |
# print(api.post(data)) | |
# print(api.query({"id":"5f6023fdf237b938b4e3ce36ef1e7b2424c86512"})) | |
# print(api.delete({"id":"5f6023fdf237b938b4e3ce36ef1e7b2424c86512"})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment