AsinSeedApi
不写注释的程序员-加密
将JAVA API接口 改写成 Python
JAVA
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.security.MessageDigest;
public class AsinSeedApiTest {
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final String PARTNER_CODE = "xxx";
private static final String PARTNER_SECRET_KEY = "xxx-xxx-xxx-xxx";
private static String byteArrayToHexStringbyte[] byteArray) {
StringBuffer resultSb = new StringBuffer);
for int i = 0; i < byteArray.length; i++) {
int n = byteArray[i];
n = n < 0 ? n + 256 : n;
resultSb.appendHEX_CHAR[n / 16]).appendHEX_CHAR[n % 16]);
}
return resultSb.toString);
}
private static String encodeString origin) {
String output = null;
try {
MessageDigest md = MessageDigest.getInstance"MD5");
output = byteArrayToHexStringmd.digestorigin.getBytes"UTF-8")));
} catch Throwable e) {
e.printStackTrace);
}
return null != output ? output.substring0, 12) : "";
}
public static void mainString[] args) {
String marketplace = "US";
String asin = "B017H39S5U";
String partner = PARTNER_CODE;
String qid = marketplace + "_" + asin + "_" + PARTNER_SECRET_KEY;
qid = encodeqid);
String urlStr = "https://www.asinseed.com/api/" + marketplace + "/" + asin + "/" + partner + "/" + qid;
HttpClient httpClient = new DefaultHttpClient);
HttpGet httpGet = new HttpGeturlStr);
try {
HttpResponse response = httpClient.executehttpGet);
HttpEntity responseEntity = response.getEntity);
if null != responseEntity) {
String responseJsonStr = EntityUtils.toStringresponseEntity, "UTF-8");
JSONObject result = JSON.parseObjectresponseJsonStr);
String code = result.getString"code");
if "ok".equalsIgnoreCasecode)) {
JSONObject data = result.getJSONObject"data");
JSONArray keywords = data.getJSONArray"keywords");
//TODO your custom business logic
System.out.printlnkeywords.toJSONString));
} else {
String errMsg = result.get"message").toString);
//TODO handle the error msg from api
System.out.println"errorMs=>" + errMsg);
}
}
} catch IOException e) {
e.printStackTrace);
}
}
}
Python
class AsinSeedApiTest:
HEX_CHAR = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
PARTNER_CODE = PARTNER_CODE
PARTNER_SECRET_KEY = PARTNER_SECRET_KEY
@classmethod
def byteArrayToHexStringcls, byteArray):
resultSb = ''
for n in byteArray:
n = n + 256 if n < 0 else n
resultSb += cls.HEX_CHAR[n // 16])
resultSb += cls.HEX_CHAR[n % 16])
return resultSb
@classmethod
def encodecls, origin):
m = hashlib.md5)
m.updateorigin.encode'utf-8'))
output = m.hexdigest)
return output[0:12] if output else ""
@classmethod
def maincls, marketplace, asin):
marketplace = marketplace
asin = asin
partner = cls.PARTNER_CODE
qid = marketplace + "_" + asin.upper) + "_" + cls.PARTNER_SECRET_KEY # ASIN
qid = cls.encodeqid)
urlStr = "https://www.asinseed.com/api/" + marketplace + "/" + asin + "/" + partner + "/" + qid # ASIN url
keyWord_urlStr = "https://www.asinseed.com/api/" + marketplace + "/" + partner + "/" + qid + '?keyword=' + asin # 关键词 url
return urlStr, keyWord_urlStr