`

python test

阅读更多
#!/usr/bin/env python
##encoding=utf-8
import MySQLdb
from MySQLdb.constants import FIELD_TYPE
import time,datetime
from xml.dom.minidom import Document
import hashlib
import json
import sys
import urllib 
import urllib2
reload(sys) 
sys.setdefaultencoding('utf8') 
merchant_code=10027
typelist=[80,81,82,83,84,85]
count=1
key="1234567890ABCDEF"
orderlist=[]
lotterydict={80:'601',81:'601',82:'601',83:'601',84:'601',85:'601'}
weekdict={0:1,1:2,2:3,3:4,4:5,5:6,6:7}
tencentplay={80:'60105',81:'60103',82:'60102',83:'60104',84:'60101',85:'60109'}
bifendict={"1":"1:0","2":"2:0","3":"2:1","4":"3:0","5":"3:1","6":"3:2","7":"4:0","8":"4:1","9":"4:2","10":"5:0","11":"5:1","12":"5:2","13":"9:0","14":"0:0","15":"1:1","16":"2:2","17":"3:3","18":"9:9","19":"0:1","20":"0:2","21":"1:2","22":"0:3","23":"1:3","24":"2:3","25":"0:4","26":"1:4","27":"2:4","28":"0:5","29":"1:5","30":"2:5","31":"0:9"}
#bifendict={'1':'1:0','2':'2:0','3':'2:1','4','3:0','5':'3:1','6':'3:2','7':'4:0','8':'4:1','9':'4:2','10':'5:0','11':'5:1','12':'5:2','13':'9:0','14':'0:0','15':'1:1','16':'2:2','17':'3:3','18':'9:9','19':'0:1','20':'0:2','21':'1:2','22':'0:3','23':'1:3','24':'2:3','25':'0:4','26':'1:4','27':'2:4','28':'0:5','29':'1:5','30':'2:5','31':'0:9'}
bqcdict={'1':'3-3','2':'3-1','3':'3-0','4':'1-3','5':'1-1','6':'1-0','7':'0-3','8':'0-1','9':'0-0'}
jqsdict={'1':'0','2':'1','3':'2','4':'3','5':'4','6':'5','7':'6','8':'7'}
url='http://888.qq.com/tws/spnotice/DoTicketStatusNotice'
#url='http://118.26.65.147/test.php'
def md5(str):
    m = hashlib.md5()   
    m.update(str)
    return m.hexdigest()
def connect():
    conn=MySQLdb.connect(host='172.16.3.10',user='lehecai',passwd='123456',db='v_ticket',port=1231)
    conn.autocommit(1) 
    return conn
def gettencentvalue(lotterytype,value):
        if lotterytype=='81':
           return bifendict[value]
        if lotterytype=='83':
           return bqcdict[value]
        if lotterytype=='82':
           return jqsdict[value]
        return value   
        
def create_xml(lotterytype):
    global count
    #count=count+1
    cmd='6103'
    doc = Document()
    bookstore = doc.createElement('message')
    doc.appendChild(bookstore)
    head=doc.createElement('head')
    bookstore.appendChild(head)
    command=doc.createElement('command')
    command_text=doc.createTextNode(cmd)
    command.appendChild(command_text)
    head.appendChild(command)

    agentid=doc.createElement('agentid')
    agentid_text=doc.createTextNode(str(merchant_code))
    agentid.appendChild(agentid_text)
    head.appendChild(agentid)
    
    messageid=doc.createElement('messageid')
    messageid_text=doc.createTextNode(str(count))
    messageid.appendChild(messageid_text)
    head.appendChild(messageid)

    timest=time.strftime('%Y-%m-%d %H:%M:%S')
    timestamp=doc.createElement('timestamp')
    timestamp_text=doc.createTextNode(timest)
    timestamp.appendChild(timestamp_text)
    head.appendChild(timestamp)
    
    body=doc.createElement('body')
    bookstore.appendChild(body)
   
    lottype=doc.createElement('lottype')
    lottype_text=doc.createTextNode(lotterydict[lotterytype])
    lottype.appendChild(lottype_text)
    body.appendChild(lottype)
    datalist=doc.createElement('datalist')
    body.appendChild(datalist)

    


    conn=connect();
    cursor = conn.cursor(MySQLdb.cursors.DictCursor)
    sql="select *  from lottery_order_notice where lottery_type =%s and merchant_code=%s and notice_status=2 limit 50"
    param=(lotterytype,merchant_code)    
    n = cursor.execute(sql,param)

    if (n==0):
      cursor.close()
      conn.close()
      #print "%s is not have bet return"%lotterytype 
      return "No"      
    for row in cursor.fetchall():
        orderid=row['id']
        merchant_no=row['merchant_no']
        status=row['order_status']
        
        ordersql='select * from lottery_order where id=%s'
        orderparam=(orderid,)
        tor=cursor.execute(ordersql,orderparam)
        
        order=cursor.fetchone()
        if not order:
            contine

        dataitem=doc.createElement('dataitem')
        datalist.appendChild(dataitem)
        dataitem.setAttribute('ticketsn',order['merchant_no'])
        dataitem.setAttribute('money',bytes(order['amount']))
        dataitem.setAttribute('maxprizemoney','0')
        dataitem.setAttribute('expand','')
        orderlist.append(orderid)        
        if (status==3):
           dataitem.setAttribute('status','200')
           dataitem.setAttribute('message','成功')
           ticketsql='select * from ticket where order_id =%s'
           ticketparam=(orderid,)
           tn=cursor.execute(ticketsql,ticketparam)
           ticket=cursor.fetchone()
           if not ticket:
               contine
           dataitem.setAttribute('playtype',tencentplay[ticket['lottery_type']])
           
           dataitem.setAttribute('ticketcode',ticket['serial_id'])
           ext=ticket['ext']
           #print ext
           dataitem.setAttribute('rateinfo',ext)
           jsonstr=json.loads(ext)
           peilv=''
           rangqiu=''
           lotteryType=ticket['lottery_type'];         
           for jsonkey in jsonstr.keys():
               if jsonkey==jsonstr[jsonkey]['match_num']:
                  subtimestr=jsonkey[:8]
                  changci=jsonkey[-3:]
                  
                  a=time.strptime(subtimestr,'%Y%m%d');
                                   
                  weekday=weekdict[a.tm_wday]
                  substart=bytes(weekday)+changci
                  
                  peilv=peilv+substart+'→'+'['
                  sp_map=jsonstr[jsonkey]['sp_map'];
                                    
                  for inkey in sp_map: 
                      if inkey.find('$')>0 :
                         ticketType=inkey.split('$')[1] 
                         if bytes(ticketType)=='80':
                            racesql='select * from jczq_race where match_num='+jsonkey
                            jco=cursor.execute(racesql)
                            jczqrace=cursor.fetchone()
                            #print jczqrace['handicap']
                            rangqiu='('+jczqrace['handicap']+')'
                         tkey=inkey.split('$')[0]
                         peilv +=gettencentvalue(bytes(ticketType),bytes(tkey))+'#'+sp_map[inkey]+','
                      else:
                        if bytes(lotteryType)=='80':
                           racesql='select * from jczq_race where match_num='+jsonkey
                           jco=cursor.execute(racesql)
                           jczqrace=cursor.fetchone()
                           #print jczqrace['handicap']
                           rangqiu='('+jczqrace['handicap']+')'
                           
                        peilv=peilv+gettencentvalue(bytes(lotteryType),bytes(inkey))+'#'+sp_map[inkey]+','
                  peilv=peilv[0:len(peilv)-1]+']'+rangqiu.replace('+','')+'/' 
           sp=peilv[0:len(peilv)-1]
           #print ext
           newsp=bytes(sp).strip()
           dataitem.setAttribute('rateinfo',newsp)
           suc=datetime.datetime.strftime(ticket['print_time'], '%Y-%m-%d %H:%M:%S')
           dataitem.setAttribute('suctime',suc)

           
        else:
             dataitem.setAttribute('status','-200')
             dataitem.setAttribute('message','超时未出')
    #bodystr=body.toprettyxml(indent = '')
    bodystr=body.toxml()
    #print bodystr
    md5str=cmd+str(merchant_code)+str(count)+timest+key+bodystr
   # print md5str
    md5num=md5(md5str)
    count=count+1
    keycode=doc.createElement('key')
    keycode_text=doc.createTextNode(md5num.lower())
    keycode.appendChild(keycode_text)
    head.appendChild(keycode)
    
    noticexml=doc.toxml(encoding='utf-8')
    print time.strftime('%Y-%m-%d %H:%M:%S')
    parameters='msg='+noticexml
    print parameters
    #data=urllib.urlencode(parameters)
    request=urllib2.Request(url=url,data=parameters)
    response=urllib2.urlopen(request).read()
    print response
    if response.find('<status>200</status>')>0:
       updatesql='update lottery_order_notice set notice_status=1 where id in %s' 
       updateparam=(orderlist,)
       cursor.execute(updatesql,updateparam)
    cursor.close()
    conn.close()
    return "" #noticexml

if __name__=='__main__':
    while True :
     for lottype in typelist:
      s=create_xml(lottype)
     #print s
    #  break
      time.sleep(10)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics