webpower似乎只提供php,java,.net介面,正好要用到python的介面,於是就參照php的介面寫了個webpower的介面
python版:
# -*- coding: utf-8 -*-import os,sys,traceback,timereload(sys)sys.setdefaultencoding('utf-8')from suds.client import Clientclass webpower(object): """docstring for webpower""" def __init__(self, arg): super(webpower, self).__init__() self.arg = arg self.client = None self.url = self.arg.get('url','') self.login = self.arg.get('login',{}) def connection(self): '''建立串連''' self.client = Client(self.url) self.client.set_options(headers={"Content-Type":"text/xml; charset=utf-8"}) def createSenderAddress(self,fromEmail = ''): ''' 建立發送的郵件地址 @param fromEmail @return Dict ''' resultDict = {} try: if not self.client : self.connection() tmpResult = self.client.service.createSenderAddress(self.login,fromEmail) if hasattr(tmpResult, 'statusMsg'): resultDict['statusMsg'] = tmpResult.statusMsg if hasattr(tmpResult, 'id'): resultDict['id'] = tmpResult.id except: resultDict['error'] = traceback.format_exc() return resultDict def createCampaign(self,campaignName = '',defaultSenderName='',defaultSenderAddrID=''): ''' 建立活動 @param campaignName 活動名稱 @param defaultSenderName 寄件者名稱 @param defaultSenderAddrID 發送的ID地址 return Dict ''' resultDict = {} try: if not self.client : self.connection() tmpResult = self.client.service.createCampaign(self.login, 0, #brandID campaignName, #The name of the new campaign defaultSenderName, #default Sender Name defaultSenderAddrID, #default Sender Address Id 0, #default Reply Address 'cn', #lang False, #is overall "", "", "" ) if hasattr(tmpResult, 'status'): resultDict['status'] = tmpResult.status if hasattr(tmpResult, 'id'): resultDict['id'] = tmpResult.id except: resultDict['error'] = traceback.format_exc() return resultDict def createMailing(self,campaignID='',title = '',fromName ='',html =''): ''' 建立活動對應的郵件內容 @param campaignID 活動ID @param title 活動標題/活動唯一標題 @param fromName 來源名稱 @param html 發送的html內容 @return Dict ''' resultDict = {} try: if not self.client : self.connection() mailingName = '%s_%s' % (title, int(time.time())) tmpResult = self.client.service.createMailing(self.login, campaignID, #campaign ID mailingName, #mailing Name 'cn', #lang title, #subject fromName, #fromName 0, #sender ID html #html ) if hasattr(tmpResult, 'status'): resultDict['status'] = tmpResult.status if hasattr(tmpResult, 'id'): resultDict['id'] = tmpResult.id except: resultDict['error'] = traceback.format_exc() return resultDict def getMailingStatsSummary(self,campaignID='',mailingID=''): ''' 通過郵件ID來擷取該郵件的發送情況 @param campaignID 品牌ID @param mailingID 郵件ID @return Dict ''' resultDict = {} try: if not self.client : self.connection() tmpResult = self.client.service.getMailingStatsSummary(self.login, campaignID, #campaign ID mailingID, #mailing ID ) resultDict['result'] = dict(tmpResult) except: resultDict['error'] = traceback.format_exc() return resultDict def addRecipient(self,campaignID ='',groupIDs =[],recipientData = []): ''' 增加一條資料到組 @param campaignID 品牌ID @param groupIDs 組ID @param recipientData 添加的資料集 例: [{'name':'email','value':'xx@shopex.cn'}] return Dict ''' resultDict = {} try: if not self.client : self.connection() #數組結構 arrayofint = self.client.factory.create('ArrayOfIntType') arrayofint.int = groupIDs #NewRecipient 結構 NewRecipientDataType = self.client.factory.create('NewRecipientType') NewRecipientDataType.fields = recipientData tmpResult = self.client.service.addRecipient(self.login, campaignID, #campaign ID arrayofint, #groupIDs arrayofint1 NewRecipientDataType, #recipient Data recipientData True, True ) resultDict['result'] = dict(tmpResult) except: resultDict['error'] = traceback.format_exc() return resultDict def addGroup(self,campaignID ='',name = ''): ''' 新增組 @param campaignID 品牌ID @param name 組名稱 @return Dict ''' resultDict = {} try: if not self.client : self.connection() #NewGroupType NewGroupType = self.client.factory.create('NewGroupType') NewGroupType.name = name NewGroupType.is_test = True NewGroupType.remarks = '' tmpResult = self.client.service.addGroup(self.login, campaignID, #campaign ID NewGroupType, #New Group Type ) resultDict['result'] = dict(tmpResult) except: resultDict['error'] = traceback.format_exc() return resultDict def sendMailing(self,campaignID ='',mailingID='',resultsEmail='',groupIDs=[],callbackUrl=''): ''' 發送郵件 @param campaignID 品牌ID @param mailingID 郵件ID @param isTest 是否是測試 @param resultsEmail 結果email @param groupIDs 組ID @param callbackUrl 返回的URL @return Dict ''' resultDict = {} try: if not self.client : self.connection() #groupIDs ArrayOfIntType = self.client.factory.create('ArrayOfIntType') ArrayOfIntType.int = groupIDs #langs ArrayOfStringType = self.client.factory.create('ArrayOfStringType') ArrayOfStringType.string = ['cn'] tmpResult = self.client.service.sendMailing(self.login, campaignID, #campaign ID mailingID, #mailing ID False, #is test resultsEmail, #results Email ArrayOfIntType, #group ids 0, ArrayOfStringType,#langs '', callbackUrl ) resultDict['result'] = dict(tmpResult) except: resultDict['error'] = traceback.format_exc() return resultDict def sendSingleMailing(self,campaignID='',mailingID='',recipientID=''): ''' 發送單個EMAIL @param campaignID 品牌ID @param mailingID 發送的EMAIL地址 @param recipientID recipient id @return Dict ''' resultDict = {} try: if not self.client : self.connection() tmpResult = self.client.service.sendSingleMailing(self.login, campaignID, #campaign ID mailingID, #mailing ID recipientID , #is test ) resultDict['result'] = {'status':tmpResult} except: resultDict['error'] = traceback.format_exc() return resultDictif __name__ == '__main__': arg = { 'login' : {'username':'******','password':'******'}, 'url' : 'http://******.dmdelivery.com/x/soap-v4/wsdl.php' } #print webpower(arg).createSenderAddress('wangzhaoming@shopex.cn') #print webpower(arg).createCampaign('1sssss') #print webpower(arg).createMailing(237,'subjecttest22','67272740','<html><body>test</body></html>') #print webpower(arg).getMailingStatsSummary(237,1745) #print webpower(arg).addRecipient(237,[81],[{'name':'email','value':'test1@shopex.cn'}]) #print webpower(arg).addGroup(237,'create003') #print webpower(arg).sendMailing(237,1745,'wangzhaoming@shopex.cn',[81],callbackUrl='http://callback.webpower-inc.com/callback.php') #print webpower(arg).sendSingleMailing(237,1745,3) pass