#!/usr/bin/env python
# encoding:utf-8 import requests
import json
from conf import STORE_DICT_LIST
import time store_list = STORE_DICT_LIST
store_name = 'sandbox_001'
myaccess_token = store_list[store_name]['access_token'] class Authorize():
def __init__(self):
pass
def get_authorize(self, client_id):
"""
:return:返回授权的url + code , code 给get_token 使用
授权成功之后得到一个url: redirect_uri&code=ede9c269-10d3-44ca-88d6-53030311de04
其中,redirect_uri为在wish后台设置好的。
"""
authorize_url = "https://merchant.wish.com/oauth/authorize?client_id=%s" % client_id
req = requests.get(authorize_url)
return req.text
print req.text # def get_product_online(self,name,access_token,refresh_token,client_id,idwish_account,merchant_user_id,client_secret):
# access_token_url = "https://merchant.wish.com/api/v2/oauth/access_token"
# postdict={
# "name":name,
# "access_token":myaccess_token,
# "refresh_token":refresh_token,
# "client_id":client_id,
# "idwish_account":idwish_account,
# "merchant_user_id":merchant_user_id,
# "client_secrect":client_secret
# }
# return 1 def get_token(self, code, client_id, client_secret, redirect_uri=None):
"""
: return : 返回 json
u'{"message":"",
"code":0,
"data":{"expiry_time":1465132728,
"token_type ":"access_token",
"access_token":"84aeb5997b6d4c83a99b1a22ee6423b6",
"expires_in":2591976,"merchant_user_id":"57298cbd15abf85958e9f87a",
"refresh_token":"b0bd8eec162d4aaf87d0e6aaf43729f7"}}'
"""
if not redirect_uri:
redirect_uri = "https://localhost/"
access_token_url = "https://merchant.wish.com/api/v2/oauth/access_token" postdict = {"client_id":client_id,
"client_secret": client_secret,
"code":code,
"grant_type":"authorization_code",
"redirect_uri": redirect_uri}
print postdict
req = requests.post(access_token_url, data = postdict)
token_json = eval(req.text)
print token_json
return token_json def refresh_token(self,client_id, client_secret, refresh_token):
"""
更新token
"""
refresh_url = "https://merchant.wish.com/api/v2/oauth/refresh_token"
#refresh_url = "https://sandbox.merchant.wish.com/api/v2/oauth/refresh_token" postdict = { "client_id": client_id,
"client_secret": client_secret,
"refresh_token": refresh_token,
"grant_type": "refresh_token"
}
req = requests.post(refresh_url, data = postdict)
new_token_json = eval(req.text)
return new_token_json class Wish(object):
# https://china-merchant.wish.com/api/v2/
"""
对wish的接口api做一个整合,主要设计到产品上架相关接口
以及之后需要处理的订单下载接口
"""
def __init__(self, *access_token):
if access_token:
self.access_token = access_token
else:
self.access_token = myaccess_token
self.base_url = "https://china-merchant.wish.com/api/v2/"
# self.base_url = "https://sandbox.merchant.wish.com/api/v2/" def __make_call(self, func, **params):
"""
func: /product/add ,调用的函数 post提交
**params: 参数
"""
urls = self.base_url + func
urls = urls.split("://")
urls = urls[0] + "://" + urls[1].replace("//","/")
print "urls: ", urls
data_params = {}
data_params.update({"access_token":self.access_token})
if params:
data_params.update(**params)
req = requests.post(urls, data = data_params, verify=False)
return req.text def func_call(self, func, **params):
"""
将返回的结果变为字典,如果可以转变成字典的话
"""
result = self.__make_call(func, **params)
try:
result = json.loads(result)
except Exception, e:
# print "result: ", result
print "json loads result Error, %s" % e
result = {}
return result def query_params(self,func, params_list, **params):
""" 检查参数 """
message = ""
for param in params_list:
if not params.get(param):
message = "%s , 参数不全, 没有找到参数: %s " % (str(func), param)
break
return message #-----------------------------------------------------------------
# ---------------------- 订单相关 --------------------------------
def fetch_order_id(self, **params):
"""
func: 根据order_id来获取 订单
:param params:
{
"id": 123456789009876543210164
}
"""
func ="/order"
result = {}
if not params.get("id"):
print "input a id in params, please!"
return result
result = self.func_call(func, **params)
return result def fetch_all_order(self, **params):
"""
func: 获取所有订单
:param params:
{
"start": 0,
"limit": 100,
"since": "2016-4-1"
}
"""
func = "/order/multi-get"
result = {}
start, limit = 0, 15
since = "2016-4-1"
if params.get("start", ""): start = params.get("start")
if params.get("limit", ""): limit = params.get("limit")
if params.get("since", ""): since = params.get("since")
params = {"start": start, "count": limit, "since": since}
tmp_result = self.func_call(func, **params)
result = tmp_result
while len(tmp_result.get("data", [])) >= limit:
start += limit
print "start: ", start
tmp_result = self.func_call(func, **params)
result.get("data").extend(tmp_result.get("data",[]))
print "tmp_result: ", tmp_result
time.sleep(0.5)
return result def get_fulfill_order(self,**params):
"""
func:获取需要处理的订单
:param params:
{ }
"""
func = 'order/get-fulfill'
result ={}
start ,limit = 0,50
since = "2016-4-1"
if params.get("start", ""): start = params.get("start")
if params.get("limit", ""): limit = params.get("limit")
if params.get("since", ""): since = params.get("since")
params = {"start": start, "count": limit, "since": since}
tmp_result = self.func_call(func, **params)
result = tmp_result
print len(result)
while len(tmp_result.get("data", [])) >limit:
params ['start']=start
tmp_result = self.func_call(func, **params)
result.get("data").extend(tmp_result.get("data",[]))
time.sleep(0.5)
start +=limit return result
def cancel_order(self, **params):
"""
func: 退货,取消订单
:param params
{
"id": "", #order_id
"reason_code": 18, #取消原因的代码
"reason_note": "", #原因解释,当选择-1的时候
} :return
{
'code': 0,
'data': {'success': True},
'message': ''
} ------------------------------
-1 其他
18 误下单了
20 配送时间过长
22 商品不合适
23 收到错误的商品
24 商品为假冒伪劣品
25 商品已损坏
26 商品与描述不符
27 商品与清单不符
30 产品被配送至错误的地址
31 用户提供了错误的地址
32 商品退还至发货人
33 Incomplete Order
34 店铺无法履行订单
35 此件显示已妥投,但客户未收到。
1001 Received the wrong color
1002 Item is of poor quality
1004 Product listing is missing information
1005 Item did not meet expectations
1006 Package was empty
"""
result = {}
func = "/order/refund"
if not params.get("id") or not params.get("reason_code"):
print "参数不正确, 确保有参数id, 和reason_code"
return result
result = self.func_call(func, **params)
return result def modify_tracking(self, **params):
"""
func: 修改已装运订单的跟踪
:param params
{
"id":"", #order_id
"tracking_provider": "USPS",
"tracking_number": "12345678" #快递号
"ship_note": "给用户的注意", 可以不填写这个参数
}
"""
result = {}
func = "/order/modify-tracking"
if not params.get("id") or not params.get("tracking_provider") or not params.get("tracking_number"):
print "参数不正确,确保有{'id':'','tracking_provider':'', 'tracking_number':''} ..."
return result
result = self.func_call(func, **params)
return result def fulfile_one_order(self, **params):
"""
func: 标志为已发货
:param params
{
"id" : "57242e8c0000000000000000",
"tracking_provider" : "USPS",
"tracking_number" : "12345678" ,
} return :
{
u'message': u'Your order is being processed right now!',
u'code': 0,
u'data': {u'success': True}
}
"""
func = "/order/fulfill-one"
result = {}
if not params.get("id") or not params.get("tracking_provider") or not params.get("tracking_number"):
print "参数不正确,确保参数含有 {'id':'', 'tracing_provider':'', 'tracking_number':''} "
return result
result = self.func_call(func, **params)
return result #----------------------------------------------------------------
#------------------------- wishAPI产品相关接口 ------------------------------
"""
/product
/product/multi-get
/product/update
/product/enable
/product/disable
/product/add
/product/create-download-job
/product/get-download-job-status
/product/cancel-download-job
""" def retrieve_product(self, **params):
"""
func: 检索在wish平台上存在的产品的详细信息。
:param params
{
"id": "",
"parent_sku": "",
}
@return 返回一个产品实体 字典类型
"""
func = "/product"
result = {}
params_list = ["id", "parent_sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def product_add(self, **params):
"""
func: 添加产品
:param params
{
"name": "产品名称", #必须
"description": "产品描述", #必须
"tags": "关键字", #必须
"sku": "sku", #必须
"color": "颜色", #可选,部分没有
"size": "尺寸", #可选
"inventory": "库存", #必须
"price": "", #必须
"shipping": "配送", #必须
"msrp":"可选的制造商建议零售价",
"shipping_time": "时间",
"main_image": "", #必须
"parent_sku": "",
"brand": "产品厂商或牌子",
"landing_page_url": "登入页码链接",
"upc": "商品条码",
"extra_images": "其他的图片", #用了 '|' 分割
}
"""
func = "/product/add"
result = {}
params_list = ["name", "description", "tags", "sku", "inventory", "price", "main_image", "shipping"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
print result
return result def update_product(self, **params):
"""
func : 更新产品信息, 更新name,description, tags
:param params
{
"id": "", #必须
"parent_sku": "", #必须
"name": "",
"description": "",
"tags": "",
"brand": "",
"landing_page_url": "",
"upc": "",
"main_image": "",
"extra_images": "",
}
"""
func = "/product/update"
result = {}
params_list = ["id", "parent_sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def enable_product(self, **params):
"""
func: 使一个产品可以出售
:param params
{
"id": "",
"parent_sku": ""
}
"""
func = "/product/enable"
result = {}
params_list = ["id", "parent_sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def disable_product(self, **params):
"""
func: 停止一个产品可出售
:param params
{
"id": "",
"parent_sku": "",
}
"""
func = "/product/disable"
params_list = ["id", "parent_sku"]
result = {}
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result
def list_all_products(self, **params):
"""
func: 列出所有产品
:param params
{
"start": "0",
"limit": "30",
"since": "2016-4-1",
}
"""
result = {}
func = "/product/multi-get"
start, limit = 0, 15
since = "2016-4-1"
if params.get("start", ""): start = params.get("start")
if params.get("limit", ""): limit = params.get("limit")
if params.get("since", ""): since = params.get("since")
params = {"start": start, "count": limit, "since": since}
tmp_result = self.func_call(func, **params)
result = tmp_result
while len(tmp_result.get("data", [])) >= limit:
start += limit
print "start: ", start
tmp_result = self.func_call(func, **params)
result.get("data").extend(tmp_result.get("data",[]))
print "tmp_result: ", tmp_result
time.sleep(0.5)
return result def remove_extra_images(self, **params):
"""
func: 移除extra图片
:param params
{
"id":"", #产品id
"parent_sku":"",
}
"""
result = {}
func = "/product/remove-extra-images"
params_list = ["id", "parent_sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result #----------------------------------------------------------------------------
#------------------------------ 创建新产品属性相关 -------------------------- def variant_add(self, **params):
"""
func: 属性添加。比如已经有一个产品有红色属性,但是想添加一个size=12的属性,则用此api
:param params
{
"parent_sku": "产品名称", #必须
"sku": "sku", #必须
"color": "颜色", # 颜色与尺寸,不能同时为空
"size": "尺寸", #
"inventory": "库存", #必须
"price": "", #必须
"shipping": "配送", #必须
"msrp":"可选的制造商建议零售价",
"shipping_time": "时间",
"main_image": "",
}
""" func = "/variant/add"
result = {}
params_list = ["parent_sku", "sku", "inventory", "price", "shipping"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
result = self.func_call(func, **params)
return result def variant_retrieve(self, **params):
"""
func: 想查看一个商品的状态
:param params
{
"sku":"",
}
"""
func = "/variant"
result = {}
params_list = ["sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def enable_variant_product(self, **params):
"""
func: 让一个变体产品可用
:param params
{
"sku" : "",
}
"""
func = "/variant/enable"
result = {}
params_list = ["sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result
def disable_variant_product(self, **params):
"""
func: 变体产品不可用
:param params
{
"sku": ""
}
"""
func = "/variant/disable"
result = {}
params_list = ["sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def change_product_sku(self, **params):
"""
func: 修改sku
:param params
{
"sku":"",
"new_sku": "",
}
"""
result = {}
func = "/variant/change-sku"
params_list = ["sku", "new_sku"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def update_inventory(self, **params):
"""
func: 更新库存
:param params
{
"sku": "",
"inventory": "",
}
"""
result = {}
func = "/variant/update-inventory"
params_list = ["sku", "inventory"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result def list_all_variations(self, **params):
"""
func: 列出所有变化的产品
:param params
{
"start": "",
"limit": "",
}
"""
func = "/variant/multi-get"
result = {}
params_list = ["start", "limit"]
start, limit = 0, 15
if params.get("start", ""): start = params.get("start")
if params.get("limit", ""): limit = params.get("limit")
params = {"start": start, "count": limit}
tmp_result = self.func_call(func, **params)
result = tmp_result
while len(tmp_result.get("data", [])) >= limit:
start += limit
print "start: ", start
tmp_result = self.func_call(func, **params)
result.get("data").extend(tmp_result.get("data",[]))
print "tmp_result: ", tmp_result
time.sleep(0.5)
return result #----------------------------------------------------------------------------
#------------------------------上传一个图片 ----------------------------------
def upload_image(self, **params):
"""
func: 上传图片
:param params
{
"image": "ABCD1234abcd", #Base64编码
}
"""
func = "/image"
result = {}
params_list = ["image"]
message = self.query_params(func, params_list, **params)
if message:
result["message"] = message
return result
result = self.func_call(func, **params)
return result #----------------------------------------------------------------------------
#------------------------------用户认证测试 --------------------------------
def auth_test(self):
func = "/auth_test" #在前面要加 '/'
req = self.func_call(func)
print req def main():
store_list = STORE_DICT_LIST
store_name = 'sandbox_001'
print store_list[store_name]
wish = Wish(store_list[store_name]['access_token']) wish.auth_test()
"""
all_order_result = wish.fetch_all_order()
print "all_order_result: ", all_order_result # 根据id取得订单
params = {'id':'57242e8c0000000000000000'}
one_order_result = wish.fetch_order_id(**params)
print one_order_result #取消订单
params = {'id': '5728230c0000000000000000', 'reason_code': 18}
cancel_result = wish.cancel_order(**params)
print cancel_result #修改已装运订单的跟踪, 此订单为历史记录里面
params = {
"id":"5722dd0c0000000000000000", #order_id
"tracking_provider": "USPS",
"tracking_number": "12345678", #快递号
"ship_note": "给用户的注意", #可以不填写这个参数
}
result = wish.modify_tracking(**params)
print result # 标志为已发货
params = {
"id" : "57242e8c0000000000000000",
"tracking_provider" : "USPS",
"tracking_number" : "12345678" ,
}
fulfile_result = wish.fulfile_one_order(**params)
print fulfile_result
"""
#------------------------------------------------------------- params = {
"name": "myshipping", #必须
"description": "this is a cool shoe, it's very bueatiful!", #必须
"tags": "red, shoe, cool, myship",
"parent_sku": "skudkjasfabcfc", #必须 #父sku,变体的产品必须
"sku": "skudkjasfabcfc-red-22", #必须
"color": "red", #可选,部分没有
"size": "", #可选
"inventory": "", #必须
"price": "", #必须
"shipping": "", #必须
"msrp":"",
"main_image": "https://dfk8hf9p19dtk.cloudfront.net/E/EL61365P6610-1.jpg", #必须
"parent_sku": "parent-sku_manmansku",
"brand": "shipped",
"landing_page_url": "https://dfk8hf9p19dtk.cloudfront.net/E/EL61365P6610-2.jpg",
"upc": "abc123",
"extra_images": "https://dfk8hf9p19dtk.cloudfront.net/E/EL61365P6610-3.jpg|https://dfk8hf9p19dtk.cloudfront.net/E/EL61365P6610-2.jpg", #用了 '|' 分割
}
# result = wish.product_add(**params)
result = wish.variant_add(**params)
print result
if result.get("code") == 0:
if result.get("data").get("Product"):
print "product_id:", result.get("data").get("Product").get("variants")[0].get("Variant").get("product_id")
else:
print "variation id:", result.get("data").get("Variant").get("product_id")
print "current id: ", result.get("data").get("Variant").get("id")
else:
print "message: ", result.get("message")
"""
## 查看产品信息
params = {"id": "572d9ebeae24f05a7962eee2", "parent_sku":"parent-sku"}
result = wish.retrieve_product(**params)
print result ## 更新产品信息
params = {
"id": "572d9ebeae24f05a7962eee2", #必须
"parent_sku": "parent-sku", #必须
"name": "产品名称",
"description": "it's new update description, color: red, size: 200*200",
"tags": "new, tags1, tags2",
"brand": "无牌子",
"landing_page_url": "http://img.alicdn.com/tps/TB1sNzAJFXXXXcDXpXXXXXXXXXX-1920-450.jpg",
"upc": "30",
"main_image": "http://img.alicdn.com/tps/TB1sNzAJFXXXXcDXpXXXXXXXXXX-1920-450.jpg",
"extra_images": "http://img.alicdn.com/tps/TB1sNzAJFXXXXcDXpXXXXXXXXXX-1920-450.jpg",
}
result = wish.update_product(**params) ## 列出所有产品信息
result = wish.list_all_products()
print result
""" ## 列出所有变更过的产品
#result = wish.list_all_variations()
#print result if __name__ == "__main__":
# main()
code ='3ff343daafd847d980de7bbdd4b9bcab'
client_id='5795739b09815726c88a47f0'
client_secret='9e404f3916604fbcbd2f1d29eb078e31'
redirect_uri='https://localhost/'
aa = Authorize()
aa.get_token(code=code, client_id=client_id, client_secret = client_secret, redirect_uri=redirect_uri)

python实战===代码的更多相关文章

  1. python实战:用70行代码写了一个山炮计算器!

    python实战训练:用70行代码写了个山炮计算器! 好了...好了...各位因为我是三年级而发牢骚的各位伙伴们,我第一次为大家插播了python的基础实战训练.这个,我是想给,那些python基础一 ...

  2. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  3. Python实战:Python爬虫学习教程,获取电影排行榜

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  4. python实战--数据结构二叉树

    此文将讲述如何用python实战解决二叉树实验 前面已经讲述了python语言的基本用法,现在让我们实战一下具体明确python的用法 点击我进入python速成笔记 先看一下最终效果图: 首先我们要 ...

  5. 再一波Python实战项目列表

    前言: 近几年Python可谓是大热啊,很多人都纷纷投入Python的学习中,以前我们实验楼总结过多篇Python实战项目列表,不但有用还有趣,最主要的是咱们实验楼不但有详细的开发教程,更有在线开发环 ...

  6. python实战博客

    2018-10-31 更新Logging日志记录以及异常捕获 感谢廖大教程.Python实战 直接在闲置的服务器上开发.阿里云Centos 6.8 64位. 1 搭建开发环境 Python 环境是Py ...

  7. 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例

    centos7.1下 ZooKeeper 集群安装配置+Python实战范例 下载:http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeepe ...

  8. 自然语言处理之中文分词器-jieba分词器详解及python实战

    (转https://blog.csdn.net/gzmfxy/article/details/78994396) 中文分词是中文文本处理的一个基础步骤,也是中文人机自然语言交互的基础模块,在进行中文自 ...

  9. 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程

    点击了解更多Python课程>>> 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程 适用人群: 即将毕业的大学生,工资低工作重的白领,渴望崭露头角的职场新人, ...

随机推荐

  1. 配置bond和vlan

    网卡是光口还是电口的方法ethtool 网卡名字 一看速度二看port是否是firber首先查看需要做bond的物理网卡,如enp130s0f0,enp131s0f0以物理网卡为enp130s0f0, ...

  2. [BZOJ4942] [NOI2017]整数

    题目背景 在人类智慧的山巅,有着一台字长为1048576位(此数字与解题无关)的超级计算机,著名理论计算机科 学家P博士正用它进行各种研究.不幸的是,这天台风切断了电力系统,超级计算机 无法工作,而 ...

  3. 2018牛客多校第四场 J.Hash Function

    题意: 给出一个已知的哈希表.求字典序最小的插入序列,哈希表不合法则输出-1. 题解: 对于哈希表的每一个不为-1的数,假如他的位置是t,令s = a[t]%n.则这个数可以被插入当且仅当第s ~ t ...

  4. [APIO2017]商旅 0/1分数规划

    ---题面--- 题解: upd: 在洛谷上被Hack了...思路应该是对的,代码就别看了 感觉有个地方还是非常妙的,就是因为在x买东西,在y卖出,就相当于直接从x走向了y,因为经过中间的城市反正也不 ...

  5. [六省联考2017]分手是祝愿 期望DP

    表示每次看见期望的题就很懵逼... 但是这题感觉还是值得一做,有可借鉴之处 要是下面这段文字格式不一样的话(虽然好像的确不一样,我也不知道为什么,是直接从代码里面复制出来的,因为我一般都是习惯在代码里 ...

  6. BZOJ2875 & 洛谷2044:[NOI2012]随机数生成器——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2875 https://www.luogu.org/problemnew/show/P2044 栋栋 ...

  7. BZOJ2809:[Apio2012]dispatching——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2809 题面复制于:https://www.luogu.org/problemnew/show/155 ...

  8. HDU.1233 还是畅通工程(Prim)

    HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...

  9. 手动实现一个简易版SpringMvc

    版权声明:本篇博客大部分代码引用于公众号:java团长,我只是在作者基础上稍微修改一些内容,内容仅供学习与参考 前言:目前mvc框架经过大浪淘沙,由最初的struts1到struts2,到目前的主流框 ...

  10. 搭建openresty需要注意到的地方

    openresty的完整包放在百度云盘linux目录下 一键安装openresty ./install.sh 安装好后,修改nginx.conf配置文件 cd /usr/local/openresty ...