# -*- coding: utf-8 -*-
import math
import urllib
import urllib2
import xlrd
from xlutils.copy import copy
import requests
import re
import time
import hmac
import re
import email.utils
import hashlib
import json
app_secret = 'xx'
app_id = 'xxx'
server = 'xxxx'
#server = 'xxxx'
class MyLibrary():
def init(self):
"""初始化函数"""
pass
def remove_space(self,old_string):
"""去掉空格,old_string为需要去除空格的字符串"""
try:
p=re.compile('\s+')
new_string=re.sub(p,'',old_string)
print new_string
return new_string
except:
return False
def read_xls(self,path, name, nrows, ncols):
"""读取excel(path路径, name表名)表并且得到表里面(nrows, ncols)的值"""
try:
excel = xlrd.open_workbook(path)
sd={}
for s in excel.sheets():
sd[s.name]=s
sheet=sd[name]
number=sheet.cell(nrows,ncols).value
return number
except:
return False
def sheet_cols(self,path, name, nrows):
"""2列Excel表格时,通过excel表的路径以及名字,行数,得到excel的内容"""
try:
input_output={}
for i in range(0,nrows):
temp1=self.read_xls(path,name,i,0)
input=self.remove_space(temp1)
temp2=self.read_xls(path,name,i,1)
output=temp2
input_output[input]=output
return input_output
except:
return False
def shipping_fee(self,lists, country, weight):
"""通过传入的Excel表格,以及国家,商品重量,得到邮费价格"""
try:
m=0
n=0
nrow=len(lists)
ncol=len(lists[len(lists)-1])
for i in range(0,nrow):
if weight==lists[i][0]:
m=i
for j in range(0,ncol):
code=[]
code=lists[0][j]
if country in code:
n=j
price=lists[m][n]
print
return price
except:
return False
def sheet(self,path, name, nrow, ncol):
"""通过Excel表格的路径,表名,以及行和列,将Excel表内容已二维数组格式返回"""
try:
lists = [[]*ncol for i in range(nrow)]
for i in range(0,nrow):
for j in range(0,ncol):
output=self.read_xls(path,name,i,j)
lists[i].append(output)
return lists
except:
return False
def slug_weight(self,slug):
"""通过商品slug得到商品重量"""
try:
weight=self.sheet_cols("E:\param\shipping.xls","Product weight",321)
print weight[slug]
return weight[slug]
except:
return False
def country_code(self, country):
"""通过国家全称得到国家简码code"""
try:
country_code=self.sheet_cols("E:\param\shipping.xls","countrycode",38)
code=country_code[country]
code=self.remove_space(code)
print code
return code
except:
return False
def slug_price(self,slug,country):
"""通过商品slug,以及国家全称,得到邮费价格"""
try:
lists=self.sheet("E:\param\shipping.xls","shipping fee",94,18)
weight=self.slug_weight(slug)
weight=weight/1000
weight=round(weight,1)
if weight>1:
integer=round(weight,0)
m=weight-integer
if m>0.5:
weight=integer+1
elif m<=0.5:
weight=integer+0.5
elif weight<1:
if weight-0.5<=0:
weight=0.5
elif weight-0.5>0:
weight=1
else:
weight=weight
price=self.shipping_fee(lists,country,weight)
price=round(price,2)
print price
return price
except:
return False
def time_rfc822(self):
"""得到当前rfc822格式的时间,类似Mon, 28 Mar 2016 00:56:52 +0800"""
try:
t = email.utils.formatdate(usegmt=True)
strinfo = re.compile('GMT')
timestamp = strinfo.sub('+0000', t)
print timestamp
return timestamp
except:
return False
def paramsstring(self, params):
try:
params_string = [] #params_string=['uuid=BB','slug=ii','id=']
for key, value in params.iteritems():
params[key] = value
a = '='.join([key,value])
params_string.append(a)
params_string = sorted(params_string)
# 将参数按照 ASCII 表排序
print params_string
return params_string
except:
return False
def signature(self, http_verb, api_path, params,timestamp):
try:
params_string = self.paramsstring(params)
to_enc = '\n'.join([app_id, http_verb, api_path, timestamp, params_string])
# repr(to_enc )#去掉换行空格等
enc_res = hmac.new(app_secret, to_enc, hashlib.md5).hexdigest()
return enc_res
except:
return False
def api(self, http_verb, api_path, param,lang='en',country='cn'):
api_path # 当前 API 路径(不包含 ? 及以后的部分)
try:
if param=="null":
params={}
else:
params=param
timestamp = self.time_rfc822()
sign = self.signature(http_verb, api_path,params,timestamp)
headers = {'Accept': 'application/vnd.dji-v4', 'Content-Type': 'application/json', 'X-APP-ID': 'mobile', 'X-DJI-Sign': sign, 'X-Timestamp': timestamp,'X-Language':lang,'X-Country':country}
url = ''.join([server, api_path])
req_func = {
'GET': requests.get,
'POST': requests.post,
'PUT': requests.put,
}.get(http_verb)
req = req_func(url, headers=headers, params=params)
status_code=req.status_code
text = req.text
print '==================================================================================='
print "url=",url
print "status_code=",status_code
print "text=" ,text
print '==================================================================================='
return text
except:
return False
def addWord(self,key,value):
""" 将传入的参数key和value,组成字典,#d.setdefault(word, []).append(pagenumber)#存在就在基础上加入列表,不存在就新建个字典key"""
try:
param={}
param.setdefault(key,str(value))
print param
return param
except:
return False
def Merge_dic(self,d1,d2):
""" 将传入的字典1,字典合并"""
try:
d1.update(d2)
print d1
return d1
except:
return False
def Traversal_Web(self,path,sites):
""" 获取网页内容,并遍历网页里面href里面的link,path是报存输出结果的xls路径,sites是要遍历的link的网址
#pqyload = {name:source}
try:
i=0
r = requests.get(sites)
data = r.text
# 利用正则查找所有连接
link_list =re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')" ,data)
for url in link_list:
i=i+1
if url.startswith("http://") or url.startswith("https://"):
r = requests.get(url)
url=r.url
status=r.status_code
elif url.startswith("/"):
url=''.join([sites,url])
r = requests.get(url)
url=r.url
status=r.status_code
else:
url = url
status='xxx'
print url
print status
self.write_xls(path,url,i,0)
self.write_xls(path,status,i,1)
except:
return False
def write_xls(self,path,text,nrow,ncol):
""" 写入相关xls表,path是报存输出结果的xls路径,,text是写入内容,nrow写入行数,ncol写入列数"""
try:
rb=xlrd.open_workbook(path)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)
ws.write(nrow, ncol,text)
wb.save(path)
except:
return False
def Get_Json_Return_Values(self,param,id):
import json
s = json.loads('{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}')
print s
print s.keys()
print s["name"]
print s["type"]["name"]
print s["type"]["parameter"][1]"""
try:
if param.startswith("{\"data\":[") :
data = json.loads(param)
for data in data["data"]:
for key,value in data.iteritems():
if key==id:
print id,"=",data[key]
return data[key]
elif param.startswith("{\"data\":{"):
data=''.join([param])
s=json.loads(data)
print id,"=",s["data"][id]
return s["data"][id]
else:
return param
except:
return False
def banner_link(self,site,param):
try:
if param.startswith("{\"data\":[") :
data = json.loads(param)
for data in data["data"]:
for key,value in data.iteritems():
if key=="title":
print "title" ,"=",data[key]
print "link" ,"=",data["link"]
print "link" ,"=",data["description"]
url=''.join([site,data["link"]])
r = requests.get(url)
print "status_code" ,"=",r.status_code
print "================================================================================================"
except:
return False
My Library
最新推荐文章于 2021-02-05 00:30:36 发布