解密 -- 7年前我用Python抢P2P债权挣点小钱

发布时间:2024年01月04日

前言

至少7年前,P2P正风靡全国。当时有个地标金融,后来黄了,很多人钱没提出来,坑了不少人。我有幸提前跑了,这是后话。

当时他们有个功能:债权转让,就是有人急用钱着急低价转让,我写了点Python专抢收益最高的且低于我的余额的,收益很高, 19%的样子。抢过一两次,主要是没有多少闲钱,几百的也不值得抢。

虽然代码不能用了,如要抢别的什么思路还是有借鉴意义的。

代码我也都忘了,就不解释了。

代码

#!/usr/bin/python
# -*- coding: gb2312 -*-

#######################################################################
# Change list:
# 2017-04-05 14:33:00 One html tag has no value <input type="hidden" name="signStr" id="signStr"/>
#######################################################################
import urllib2, urllib, cookielib, traceback, zlib
import os, sys, time,datetime,re,ConfigParser

from BeautifulSoup import BeautifulSoup
from utility import getPyLogger,debug,info

from Crypto.Cipher import AES 
import base64,json

import ssl
ssl._create_default_https_context = ssl._create_unverified_context #全局取消证书验证

def getHtmlContent(respInfo):
	htmlContent = ''
	try:
		respHtml = respInfo.read()
		if( ("Content-Encoding" in respInfo.headers) and (respInfo.headers['Content-Encoding'] == "gzip")):
			htmlContent = zlib.decompress(respHtml, 16+zlib.MAX_WBITS);
			#print respHtml
		else:
			htmlContent = respHtml
	except BaseException, e:
		debug(logger, traceback.format_exc())
	return htmlContent

BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]

def encrypt(string):
	key = base64.decodestring("L21YCmrb75wX6JrBca/McQ==");
	
	mode=AES.MODE_ECB 
	encryptor = AES.new(key, mode) 
	enc1 = encryptor.encrypt(pad(string))
	return base64.encodestring(enc1)
	
def decrypt(string):
	key = base64.decodestring("L2YCmrb75wX6JrBca/McQ==");
	mode=AES.MODE_ECB 
	decryptor = AES.new(key, mode)
	string = base64.decodestring(string)
	plain = decryptor.decrypt(pad(string))
	i=0
	for c in plain:
		if(ord(c)>16):
			i+=1
		else:
			break;
	return plain[:i]

global DEBUG_MODE
DEBUG_MODE = True;
global TOKEN_KEY
TOKEN_KEY='YYYYYYYYYYYYYYYYYYYYYYYYYY'
COOKIE='------------------------'
headers = {'Host':'www.dib66.com','Connection':'Keep-Alive','Accept-Encoding': 'gzip','Cookie':COOKIE,'Cookie2':'$Version=1','Content-Type':r'application/x-www-form-urlencoded;charset=utf-8'}
headers_reg = {'Host':'www.dib66.com','Connection':'Keep-Alive','Accept-Encoding': 'gzip','Cookie':COOKIE,'Cookie2':'$Version=1', 'Content-Type':'application/x-www-form-urlencoded;charset=utf-8'}
global data1
data1 = {'token_key':TOKEN_KEY,'call_id':'','format':'json','ie':'utf-8','bd_sig':'%28null%29','proType':'android'}
data1_reg = {'token_key':'','call_id':'','format':'json','ie':'UTF-8','username':encrypt('your phone number')}

global BORROW_TOKEN_KEY
BORROW_TOKEN_KEY=TOKEN_KEY
BORROW_COOKIE=COOKIE
global data_borrow
data_borrow = {'token_key':TOKEN_KEY,'call_id':'','format':'json','ie':'utf-8','borrowId':''}
borrow_headers = {'Host':'www.dib66.com','Connection':'Keep-Alive','Accept-Encoding': 'gzip','Cookie':COOKIE,'Cookie2':'$Version=1','Content-Type':r'application/x-www-form-urlencoded;charset=UTF-8'}


def login(logger):
	req = urllib2.Request('http://www.dib66.com/appCheckRegister.do',headers=headers_reg)
	data1_reg['call_id']=str(int(time.time()*1000))
	data=urllib.urlencode(data1_reg)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo)  #{"resp_code":"12205","resp_msg":"User name already exists"}
	
	req = urllib2.Request('http://www.dib66.com/appLogining.do',headers=headers)
	data1_reg['call_id']=str(int(time.time()*1000))
	data2={'password':encrypt('neusoft20'),'VerifyCode':'','appVersion':encrypt('3.0.4'),'appType':encrypt('3'),
	'deviceId':'MSi7rzeGNh5iwN9Z0kNC6zfSSVSkaKzAJXLp/RBHaxegfIU/qamHgdE48ACGO','deviceName':encrypt('iphone')}
	data3= dict( data1_reg.items() + data2.items() ) 
	data=urllib.urlencode(data3)
	respInfo = urllib2.urlopen(req,data=data)
	#print respInfo
	res = getHtmlContent(respInfo)  

	json_data = json.loads(res)
	token_key=json_data["token_key"]
	global TOKEN_KEY
	TOKEN_KEY = token_key
	global data1
	data1['token_key']=TOKEN_KEY
	data_borrow['token_key']=TOKEN_KEY
	debug(logger,'new TOKEN_KEY:'+TOKEN_KEY)
	
	#write the new token_key to this file.
	thisfile = os.path.basename(__file__);
	f = open(thisfile,'r')
	lines = f.readlines()
	f.close()
	f = open(thisfile,'w')
	for line in lines:
		if(line.startswith("TOKEN_KEY='")):
			line = "TOKEN_KEY='%s'\n"%TOKEN_KEY
		f.write(line)
	f.close()
	
def scoreOfDueDateWeekDay(creItem):
	retVal = 0;
	try:
		nowDatetime = datetime.datetime.now()
		if(nowDatetime.weekday() in [4,5,6] or nowDatetime.weekday()==3 and nowDatetime.hour>16):#Thur night or Fri-Sun, have no more chance.
			return 0;
		nowTime = time.localtime(creItem["lastRepayDate"]["time"]/1000)
		dueDay = datetime.datetime(year=nowTime.tm_year,month=nowTime.tm_mon,day=nowTime.tm_mday)
		weekDay = dueDay.weekday();
		retVal = WEEKDAY_SCORE_MAP[weekDay]
		creItem["due"]="%i%i%i"%(nowTime.tm_year,nowTime.tm_mon,nowTime.tm_mday)
	except BaseException, e:
		print str(e)
	return retVal;

def getCreList(logger,yue):
	req = urllib2.Request('http://www.dib66.com/appCreditorList.do',headers=headers)
	data1['call_id']=str(int(time.time()*1000))
	data2={'page_no':'1','page_size':'10','proType':'3'}
	data3= dict( data1.items() + data2.items() )  
	data=urllib.urlencode(data3)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo) 
	#debug(logger,res)
	json_data = json.loads(res)
	tempMap=json_data[0]
	creList = []
	if(tempMap["resp_msg"]=="Success"):
		creAllList = tempMap["list"]
		for cre in creAllList:
			if(cre["result"]==0): # and 
				creList.append(cre)
			if(cre["result"]==0 and yue<cre["creditDealAmt"]):
				debug(logger,"id=%s amt=%s days=%s rate=%s"%(str(cre["id"]),str(cre["creditDealAmt"]),str(cre["residueDays"]),str(cre["realIncomeRate"])))
	return creList
	
def filterCreListAndSortByScore(logger,creListALL,yue):
	#debug(logger, str(creListALL))
	creList = []
	debug(logger, "===============All==================")
	for cre in creListALL:
		fuJiaShouYi_f = 0.0
		fuJiaShouYi = ''
		total_score = 0.0
		try:
			if(yue>=cre["creditDealAmt"]):
				fuJiaShouYi_score = (float(cre["creditAmt"])-float(cre["creditDealAmt"]))/int(cre["residueDays"])*360/float(cre["creditDealAmt"])
				amt_score = cre["creditDealAmt"]/yue
				total_score = fuJiaShouYi_score+amt_score
				content = "id=%s amt=%s days=%s rate=%s score[%.4f]=(amt=%.4f+fujia=%.4f)"%(str(cre["id"]),str(cre["creditDealAmt"]),str(cre["residueDays"]),str(cre["realIncomeRate"]),total_score,amt_score,fuJiaShouYi_score)
				debug(logger, content)
		except BaseException, e:
			debug(logger, str(e))
		if(yue>=cre["creditDealAmt"] and int(cre['residueDays'])<=20):
			cre["score"]= total_score 
			creList.append(cre)
	creList = sorted(creList,key=lambda item:item["score"],reverse=True)
	try:
		debug(logger,"==============After Sort============")
		for cre in creList:
			debug(logger,"id=%s amt=%s days=%s rate=%s score=%s"%(str(cre["id"]),str(cre["creditDealAmt"]),str(cre["residueDays"]),str(cre["realIncomeRate"]),str(cre["score"])))
	except BaseException, e:
		debug(logger, str(e))
	return creList

def getBorrowList(logger):
	req = urllib2.Request('http://www.dib66.com/appFinanceTendering.do',headers=headers)
	global TOKEN_KEY
	borrow_data = {'proType':'android','versionParamete':'newbie','format':'json','token_key':TOKEN_KEY,'page_no':'1','call_id':str(int(time.time()*1000)),'ie':'UTF-8','page_size':'6'}    
	data=urllib.urlencode(borrow_data)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo) 
	#debug(logger,res)
	json_data = json.loads(res)
	tempMap=json_data[0]
	creList = []

	if(tempMap["resp_msg"]=="Success"):
		tempList = tempMap["list"]
		for cre in tempList:
			if(cre["productTypeId"]=="1" and float(cre["investNum"])>0.0):
				creList.append(cre)
		creList = sorted(creList,key=lambda item:float(item["annualRate"])*100000+1000000-float(item[investNum]),reverse=True)
	return []

ERROR_MAP = {
 "12110":"本笔债权已经不存在,不能被转让"
,"12111":"请不要购买自己出售的债权"
,"12112":"你的账户余额小于债权转让的转让价格,请先充值后"
,"12113":"本笔债权的投资不存在"
,"12114":"本笔债权的借款不存在"
,"12115":"该债权已被锁定,您无法对其进行购买"
,"12116":"该债权暂时没有开放购买,请稍后再试"
,"12117":"该债权已被转让,无法购买"
,"12118":"该债权正在被别人购买,您无法对其进行购买"
,"12119":"该债权已被赎回,无法购买"
,"12120":"债权信息异常,无法购买,请联系客服"
,"12121":"您无法购买债权,详情请咨询客服"
}

def lockCreditorByCreId(logger,creId):
	req = urllib2.Request('http://www.dib66.com/appBuyCreditor.do',headers=headers)
	data1['call_id']=str(int(time.time()*1000))
	data2={'creId':encrypt(str(creId))}
	data3= dict( data1.items() + data2.items() ) 
	data=urllib.urlencode(data3)
	respInfo = urllib2.urlopen(req,data=data) #[{"resp_msg":"You cannot buy the Creditor's rights, please consult customer service for details","resp_code":"12121"}]
	res = getHtmlContent(respInfo) 
	if(DEBUG_MODE):
		debug(logger,'step1 - someone creditor/borrowid details:::::::::::::::::::')
		debug(logger,str(creId)+res)
	
	json_data = json.loads(res)
	resMap=json_data[0]
	
	if (resMap["resp_code"]>="12110"):
		debug(logger, 'Error met:::::::::::::::::::::::')
		debug(logger,str(creId)+ERROR_MAP[resMap["resp_code"]])
		return False
	if(resMap["resp_code"]=="20001"):
		time.sleep(5)
		debug(logger, 'Got 20001 error, wait 5 seconds')
		return False
	debug(logger, 'LOCKed for %s'%str(creId))
	return resMap
	
def invest(logger,creId,lockMap={}):
	#invest
	creId = str(creId)
	if(creId not in lockMap):
		resMap = lockCreditorByCreId(logger,creId)
		if(resMap == False):
			return False
		lockMap[creId] = resMap
	resMap = lockMap[creId]
	time.sleep(3)	       #太快了, 4S就抢到了 "id=50076 amt=139 days=188 rate=19.64", 等等

	if(ONLY_ONE):
		time.sleep(5)
	#try:
	resMap = resMap['creditorinfoMap']
	for key in resMap.keys():
		value = resMap[key]
		if(value.endswith("\n")):
			resMap[key]=decrypt(value)
	del resMap['HFReqURL']
	data=urllib.urlencode(resMap)
	###投资正常标step3 请求huifutianxia
	#header = {'Accept':'text/html, application/xhtml+xml, */*','Referer':'https://www.dib66.com/buyCreditor.do','Accept-Language':'zh-CN','User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)','Content-Type':'application/x-www-form-urlencoded','Host':'lab.chinapnr.com','Content-Length':'767','Cache-Control':'no-cache','Cookie':'JSESSIONID=jKlx4WmeaFQ+LxMd-QSIv-Dn; _ga=GA1.2.280180704.1434126631; CHINAPNRJSESSIONID=iY$uyXf7CCJnVuAdVOlo2c698uYVN$$s'}
	req=urllib2.Request(url='https://lab.chinapnr.com/muser/publicRequests',data=data)
	res=urllib2.urlopen(req) 
	res=res.read() 
	if(DEBUG_MODE):
		debug(logger,'step2 - response from publicRequests:::::::::::::::::::')
		debug(logger,res)
	
	if(ONLY_ONE):
		time.sleep(5)
	time.sleep(4)	       #太快了, 4S就抢到了 "id=5076 amt=139 days=188 rate=19.64", 等等
	
	debug(logger,'creditor step4')
	###投资正常标step4 提交密码
	soup=BeautifulSoup(res)
	##获得用户名和密码的name属性
	input_info=soup.findAll("input", attrs={'type':'hidden'})
	sendHuiFuData = {}
	for input in input_info:
		name=input.get('name')
		value = ''
		try:
			value = input.get('value').encode('utf8')
		except Exception, e: 
			pass
		sendHuiFuData[input.get('name')]=value
	sendHuiFuData["password_fake"]="";
	
	sendHuiFuData["TransPwd"]="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	data=urllib.urlencode(sendHuiFuData)			 
	req=urllib2.Request(url='https://lab.chinapnr.com/muser/publicRequests/bidCaConfirm',data=data)
	res=urllib2.urlopen(req)
	res=res.read() 
	debug(logger,'step5 - bidCaConfirm creditor succ:::::::::::::::')
	if(DEBUG_MODE):
		debug(logger,res)
	
	debug(logger,'creditor step5')
	soup=BeautifulSoup(res)
	##获得用户名和密码的name属性
	input_info=soup.findAll("input", attrs={'type':'hidden'})
	sendHuiFuData = {}
	for input in input_info:
		name=input.get('name')
		value = ''
		try:
			value = input.get('value').encode('utf8')
		except Exception, e: 
			pass
		sendHuiFuData[input.get('name')]=value
	
	data=urllib.urlencode(sendHuiFuData)
					 
	req=urllib2.Request(url='http://www.dib66.com/appBuyCreditorToHfBack.do',data=data,headers=headers)
	res=urllib2.urlopen(req)
	res = getHtmlContent(res) 
	if(DEBUG_MODE):
		debug(logger,'step6 - appBuyCreditorToHfBack:::::::::::::::')
		debug(logger,res)
	#except Exception, e: 
	
	return True

def invest_borrow(logger,creId,amount):
	#invest
	req = urllib2.Request('http://www.dib66.com/appInvestInit.do',headers=borrow_headers)
	data_borrow['call_id']=str(int(time.time()*1000))
	data_borrow['borrowId']=encrypt(str(creId))
	data=urllib.urlencode(data_borrow)
	respInfo = urllib2.urlopen(req,data=data) 
	res = getHtmlContent(respInfo) 
	if(DEBUG_MODE):
		debug(logger,'step1 - borrowid %s details:::::::::::::::::::'%str(creId))
		debug(logger,res)
	
	json_data = json.loads(res)
	resMap=json_data[0]
	
	if (resMap["resp_code"]!="0"):
		debug(logger, 'Error met:::::::::::::::::::::::')
		debug(logger,resMap["resp_msg"])
		return False
	time.sleep(1)	       #太快了, 4S就抢到了 "id=5076 amt=139 days=188 rate=19.64", 等等
	#try:
	
	data_borrow['call_id']=str(int(time.time()*1000))
	data_borrow['borrowId']=encrypt(str(creId))
	data2={'InvestType':encrypt('3'), 'amount':encrypt(str(amount))}
	data3= dict( data_borrow.items() + data2.items() ) 
	data=urllib.urlencode(data3)
	req=urllib2.Request(url='http://www.dib66.com/appInvestBorrow.do',data=data,headers=borrow_headers)
	res=urllib2.urlopen(req) 
	res=res.read() 
	json_data = json.loads(res)
	resMap=json_data[0]
	if(DEBUG_MODE):
		debug(logger,'step2 - invest borrowid %s:::::::::::::::::::'%str(creId))
		debug(logger,res)
		
	resMap = resMap['investMap']
	for key in resMap.keys():
		value = resMap[key]
		if(value.endswith("\n")):
			resMap[key]=decrypt(value)
	#del resMap['HFReqURL']
	data=urllib.urlencode(resMap)
	req=urllib2.Request(url='https://lab.chinapnr.com/muser/publicRequests',data=data)
	res=urllib2.urlopen(req) 
	res=res.read() 
	if(DEBUG_MODE):
		debug(logger,'step3 - response from publicRequests:::::::::::::::::::')
		debug(logger,res)
	
	time.sleep(1)	       #太快了, 4S就抢到了 "id=5076 amt=139 days=188 rate=19.64", 等等
	
	debug(logger,'creditor step4')
	###投资正常标step4 提交密码
	soup=BeautifulSoup(res)
	##获得用户名和密码的name属性
	input_info=soup.findAll("input", attrs={'type':'hidden'})
	sendHuiFuData = {}
	for input in input_info:
		name=input.get('name')
		value=''
		try:
			value = input.get('value').encode('utf8') 
		except Exception, e: 
			pass
		sendHuiFuData[input.get('name')]=value  
	sendHuiFuData["password_fake"]="";
	sendHuiFuData["transPwd"]="xxxxxxxxxxxxxxxxxxxxxxxxx"
	data=urllib.urlencode(sendHuiFuData)		 
	req=urllib2.Request(url='https://lab.chinapnr.com/muser/publicRequests/activeBidConfirm',data=data)
	res=urllib2.urlopen(req)
	res=res.read() 
	debug(logger,'step5 - activeBidConfirm creditor succ:::::::::::::::')
	if(DEBUG_MODE):
		debug(logger,res)
	
	debug(logger,'creditor step5')
	soup=BeautifulSoup(res)
	##获得用户名和密码的name属性
	input_info=soup.findAll("input", attrs={'type':'hidden'})
	sendHuiFuData = {}
	for input in input_info:
		name=input.get('name')
		value=''
		try:
			value = input.get('value').encode('utf8')
		except Exception, e: 
			pass
		sendHuiFuData[input.get('name')]=value
	
	data=urllib.urlencode(sendHuiFuData)
	req=urllib2.Request(url='http://www.dib66.com/appTenderToHFBack.do',data=data,headers=headers)
	res=urllib2.urlopen(req)
	res = getHtmlContent(res) 
	if(DEBUG_MODE):
		debug(logger,'step6 - appTenderToHFBack:::::::::::::::')
		debug(logger,res)
	
	return True

def getHisList(logger):
	summaryList = []
	i=1
	eof = False
	while(i<100):
		
		pagelist = []
		try:
			req = urllib2.Request('http://www.dib66.com/appFinanceTendering.do',headers=headers)
			data1['call_id']=str(int(time.time()*1000)) #proType=android&versionParamete=newbie&format=json&token_key=&page_no=2&ie=UTF-8&page_size=6&call_id=1492593862944
			data2={'page_no':str(i),'page_size':'100','proType':'android','versionParamete':'newbie','format':'json','token_key':'','ie':'UTF-8'}
			data3= dict( data1.items() + data2.items() )  
			data=urllib.urlencode(data3)
			respInfo = urllib2.urlopen(req,data=data)
			res = getHtmlContent(respInfo) 
			#debug(logger,res)
			json_data = json.loads(res)
			pagelist=json_data[0]['list']
			time.sleep(1)
		except Exception, e: 
			debug(logger,"error page: i="+str(i)+str(e))
			break;
		i=i+1
		
		borrowList = []
		for borrow in pagelist:
			borrowList.append(borrow['id'])
		if(len(borrowList)<10):
			eof = True;
		for borrowId in borrowList:
			try:
				req = urllib2.Request('http://www.dib66.com/appLoanContent.do',headers=headers)
				#format=json&token_key=&borrowId=7649&ie=UTF-8&call_id=1492593783653
				data1['call_id']=str(int(time.time()*1000))
				data2={'borrowId':borrowId,'format':'json','token_key':'','ie':'UTF-8'}
				data3= dict( data1.items() + data2.items() )  
				data=urllib.urlencode(data3)
				respInfo = urllib2.urlopen(req,data=data)
				res = getHtmlContent(respInfo) 
				#debug(logger,res)
				json_data = json.loads(res)
				borrowMap=json_data[0]['borrowDetailMap']
				borrowAmount=borrowMap['borrowAmount'].replace(',','')
				borrowerName=borrowMap['borrowerName']
				borrowTitle =borrowMap['borrowTitle']
				newitem = [borrowId,borrowAmount,borrowerName,borrowTitle]
				summaryList.append(newitem)
				debug(logger,','.join(newitem))
				time.sleep(1)
			except Exception, e: 
				debug(logger,"error borrowId="+str(borrowId)+str(e))
		if(eof):break;
	summary_map = {}
	for item in summaryList:
		borrowerName = item[2]
		borrowAmount = item[1]
		if(summary_map.has_key(borrowerName)):
			summary_map[borrowerName]+=float(borrowAmount)
		else:
			summary_map[borrowerName]=float(borrowAmount)
	rankingList = []
	for name in summary_map:
		rankingList.append([name,summary_map[name]])
	rankingList = sorted(rankingList,key=lambda item:item[1],reverse=True)
	for person in rankingList:
		debug(logger,person[0]+','+str(person[1]))
	return summaryList
	
def getHisCreList(logger):
	req = urllib2.Request('http://www.dib66.com/appCreditorList.do',headers=headers)
	data1['call_id']=str(int(time.time()*1000))
	data2={'page_no':'1','page_size':'500','proType':'3'}
	data3= dict( data1.items() + data2.items() )  
	data=urllib.urlencode(data3)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo) 
	#debug(logger,res)
	json_data = json.loads(res)
	tempMap=json_data[0]
	creList = tempMap["list"]
	maxDateCre = max(creList, key=lambda x:x['ordDate'])
	minDateCre = min(creList, key=lambda x:x['ordDate'])
	debug(logger,'Collect Creator from %s to %s, total numbers: %d'%(minDateCre['ordDate'],maxDateCre['ordDate'],len(creList)))

	debug(logger,'==========STATISTICS FROM THIS MONTH===========')
	statisticsFromDate(logger,creList,u'2018-08-01')
	
	
def statisticsFromDate(logger,pCreList,dateFrom):
	#"creditDealAmt":303.94,"buyUserId":7168,"ordDate":"2018-07-20 15:00:13"
	try:
		listThisMonth = filter(lambda x:x['ordDate'][:10]>=dateFrom, pCreList)
		
		totalAMTsum = sum([float(cre["creditDealAmt"]) for cre in listThisMonth])
		debug(logger,"TOTAL AMT FROM %s: %f"%(dateFrom, totalAMTsum))
		debug(logger,"TOTAL number: "+str(len(listThisMonth)))
		mapAmtThisMonth={}
		mapTimeThisMonth={}
		for cre in listThisMonth:
			if(cre["buyUserId"] not in mapAmtThisMonth):
				mapAmtThisMonth[cre["buyUserId"]] = float(cre["creditDealAmt"])
			else:
				mapAmtThisMonth[cre["buyUserId"]] += float(cre["creditDealAmt"])
			if(cre["buyUserId"] not in mapTimeThisMonth):
				mapTimeThisMonth[cre["buyUserId"]] = [1,[cre,]]
			else:
				mapTimeThisMonth[cre["buyUserId"]][0]+=1
				mapTimeThisMonth[cre["buyUserId"]][1].append(cre)
		topAmtList=sorted(mapAmtThisMonth.items(),key=lambda x:x[1],reverse=True)
		topTimesList=sorted(mapTimeThisMonth.items(),key=lambda x:x[1][0],reverse=True)
		mapCreIDUserName = {}
		debug(logger, 'The top users by times of buying creator: ')
		for user_times in topTimesList[:8]:
			creID = mapTimeThisMonth[user_times[0]][1][0]["id"]
			UserName=getUserNameFromCreId(creID)
			if(UserName<>None):
				mapCreIDUserName[creID]=UserName
			else:
				mapCreIDUserName[creID]='DONOT KNOW'
			debug(logger,  '\tUserID:%s\t\tTimes:%s\t\tUserName:%s'%(user_times[0],user_times[1][0],UserName))
			creList = mapTimeThisMonth[user_times[0]][1]
			for cre in creList:
				debug(logger,  '\t\tID=%s\tDATE=%s\tLEFTDAYS=%s\t\tAMT=%s'%(cre['id'],cre['ordDate'],cre['residueDays'],cre['creditDealAmt']))
			minDaysCre=min(creList,key=lambda x:x['residueDays'])
			fastedCre =min(creList,key=lambda x:x['ordDate'][-5:])
			debug(logger,'\t\tmin residueDays:%s, ID:%s'%(minDaysCre['residueDays'],minDaysCre['id']))
			debug(logger,'\t\tfastest cre    :%s, ID:%s'%(fastedCre['ordDate'],fastedCre['id']))
			
		debug(logger, 'The top users by total AMT of buying creator: ')
		for user_amt in topAmtList[:8]:
			creID = mapTimeThisMonth[user_amt[0]][1][0]["id"]
			UserName = None
			if(creID in mapCreIDUserName and mapCreIDUserName[creID]<>'DONOT KNOW'):
				UserName = mapCreIDUserName[creID]
			else:
				UserName=getUserNameFromCreId(creID)
			debug(logger,  '\tUserID:%s\t\tTotalAMT:%s\t\tUserName:%s'%(user_amt[0],user_amt[1],UserName))
			creList = mapTimeThisMonth[user_amt[0]][1]
			if(creID not in mapCreIDUserName):
				for cre in creList:
					debug(logger,  '\t\tID=%s\tDATE=%s\tLEFTDAYS=%s\t\tAMT=%s'%(cre['id'],cre['ordDate'],cre['residueDays'],cre['creditDealAmt']))
		
	except Exception, e:
		print str(e)
		
def getUserNameFromCreId(creId):
	#print "redId=",creId
	try:
		req = urllib2.Request('http://www.dib66.com/creditorBuyDetail.html?creditorId=%s'%(str(creId)))
		respInfo = urllib2.urlopen(req)
		res = getHtmlContent(respInfo) 
		soup=BeautifulSoup(res)
		transition_info=soup.find("tbody", attrs={'class':'zrjl-body'})
		tds=transition_info.findAll("td")
		return tds[2].getText()
	except Exception, e:
		#print str(e)
		None
	return None
	
def getYesterdayCreList(logger):
	req = urllib2.Request('http://www.dib66.com/appCreditorList.do',headers=headers)
	data1['call_id']=str(int(time.time()*1000))
	data2={'page_no':'1','page_size':'50','proType':'3'}
	data3= dict( data1.items() + data2.items() )  
	data=urllib.urlencode(data3)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo) 
	#debug(logger,res)
	json_data = json.loads(res)
	tempMap=json_data[0]
	creList = tempMap["list"]
	amt_sum = 0
	counter = 0
	try:
		for cre in creList:
			#debug(logger,str(cre))
			ordDay = cre['ordDate'][:10]
			ordDay_dt = datetime.datetime(year=int(ordDay[0:4]),month=int(ordDay[5:7]) ,day=int(ordDay[8:10]))
			yesterday = datetime.datetime.now()-datetime.timedelta(days=1)
			if(yesterday.year<>ordDay_dt.year or yesterday.month<>ordDay_dt.month or yesterday.day<>ordDay_dt.day):
				continue;
			amt_sum += float(cre['creditDealAmt'])
			counter += 1;
	except Exception, e:
		print str(e)
	debug(logger,'-----------------yesterday summary-------------------')
	debug(logger,'creator counter: '+str(counter))
	debug(logger,'creator amt    : '+str(amt_sum))
	if(amt_sum>400000):
		content = "Today amt>40W"
	return 
	
def getCouponsList(logger,borrowId):
	req = urllib2.Request('http://www.dib66.com/appGetUneffectiveCoupons.do',headers=headers)
	data1['call_id']=str(int(time.time()*1000))
	data2={'page_no':'1','page_size':'200','ie':'UTF-8','amount':'fwX8wpdfyBMwMDzLHT8Q==','flag':'0','format':'json','token_key':TOKEN_KEY,'borrowId':encrypt(borrowId)} 
	data3= dict( data1.items() + data2.items() )  
	data=urllib.urlencode(data3)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo) 
	#debug(logger,res)
	json_data = json.loads(res)
	tempMap=json_data[0]
	creList = []
	if(tempMap["resp_msg"]=="Success"):
		creList = tempMap['list']
		creList = sorted(creList,key=lambda item:item["endTime"])
	return creList

def fastInvest(logger,borrowId,amount,useCoupon=False):
	newCouponsId=''
	if(useCoupon):
		couponList = getCouponsList(logger,borrowId)
		print str(couponList)
		if(len(couponList)>0):
			firstCoupon = couponList[0]
			couponsId = firstCoupon["id"]
			if(couponsId<>'' and couponsId>0):
				newCouponsId=encrypt(str(couponsId))
				#print 'couponsId='+str(couponsId)
	req = urllib2.Request('http://www.dib66.com/appFastInvest.do',headers=headers)
	data_temp={'investType':'6Zf0yEQSFXNjJd6bIGA==',#3
			'amount':encrypt(str(amount)),
			'format':'json',
			'couponsId':newCouponsId,
			'token_key':TOKEN_KEY,
			'borrowId':encrypt(str(borrowId)),
			'ie':'UTF-8',
			'call_id':str(int(time.time()*1000))} 
	data=urllib.urlencode(data_temp)
	respInfo = urllib2.urlopen(req,data=data)
	res = getHtmlContent(respInfo) 
	debug(logger,res)
	json_data = json.loads(res)
	return json_data["resp_msg"]=="Success"
	
def getHourMinSec():
	localtime = time.localtime(time.time())
	hour = str(localtime.tm_hour)
	min = str(localtime.tm_min)
	sec = str(localtime.tm_sec)
	if(len(hour)==1): hour='0'+hour
	if(len(min)==1): min='0'+min
	if(len(sec)==1): sec='0'+sec
	hms = hour+':'+min+':'+sec
	return hms
	
def getYue(logger):
	yue = 0.0
	loginAgain = True
	tryTimes = 0
	while(loginAgain and tryTimes<5):
		req = urllib2.Request('http://www.dib66.com/appQueryUsrMoneyInfo.do',headers=headers)
		data1['call_id']=str(int(time.time()*1000))
		data=urllib.urlencode(data1)
		respInfo = urllib2.urlopen(req,data=data)
		res = getHtmlContent(respInfo) 
		#debug(logger,res)
		json_data = json.loads(res)
		#print json_data
		try:
			resMap=json_data[0]      #KeyError: 0
			yue=float(decrypt(resMap['usableSum']))
			loginAgain = False
		except KeyError, e: 
			loginAgain = True
			login(logger)
			tryTimes = tryTimes+1
	return yue
	
	
if __name__ == '__main__':
	log_path = os.path.dirname(os.path.realpath(__file__))
	if not os.path.exists(log_path):
		os.makedirs(log_path)
	logger = getPyLogger('invest_dib66_app','debug',os.path.join(log_path,os.path.basename(__file__)+'.log'),'d',1,99999)
	
	yue = getYue(logger)
	debug(logger,"yue="+str(yue))
	
	hms = getHourMinSec()
	while('09:50:58'<=hms<='10:03:40' or '14:50:58'<=hms<='15:03:40' or '19:50:58'<=hms<='20:03:40'):
		
		while('09:59:55'<=hms<='10:00:05' or '14:59:55'<=hms<='15:00:05' or '19:59:55'<=hms<='20:00:05'):
			try:
				debug(logger,'Get cre list')
				creList = getCreList(logger,yue)
				debug(logger,'Get cre list over')
				creList = filterCreListAndSortByScore(logger,creList,yue)
				
				lockMap = {}
				
				ONLY_ONE=False;
				if(len(creList)<=1):ONLY_ONE=True;
				for cre in creList:
					if(yue<cre["creditDealAmt"]):
						continue;
					if(invest(logger,cre["id"]),lockMap):
						yue = yue - cre["creditDealAmt"]
						debug(logger,"invest OK, "+str(cre["id"]))
					else:
						debug(logger,'invest failed, '+str(cre["id"]))
						continue;
					time.sleep(2)
				ONLY_ONE=False;
				hms = getHourMinSec()
			except BaseException, e:
				hms = getHourMinSec()
				print str(e)
		hms = getHourMinSec()
		time.sleep(1)
	exit()
	

文章来源:https://blog.csdn.net/zhaiminlove/article/details/135393797
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。