You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
330 lines
10 KiB
330 lines
10 KiB
# -*- encoding: utf-8 -*- |
|
import json |
|
|
|
import requests |
|
import time |
|
import pymysql |
|
import qrcode |
|
from pymysql.cursors import DictCursor |
|
import base64 |
|
from io import BytesIO |
|
from config import db_settings |
|
|
|
# initial request |
|
session = requests.session() |
|
|
|
# initial db |
|
db_conn = pymysql.Connect(**db_settings) |
|
db_cursor = db_conn.cursor(cursor=DictCursor) |
|
|
|
# default_get_quan_num |
|
NUM = 5 |
|
|
|
|
|
def login(): |
|
login_url = "http://passport-qas.mx.com/login" |
|
header = {'Referer': 'http://passport-qas.mx.com/login?backURL=http://showfilm.qas.cmc.com/', } |
|
data = {'backURL': 'http://showfilm.qas.cmc.com/', |
|
'username': 'wangxiaoli33', |
|
'password': 'F1887D3F9E6EE7A32FE5E76F4AB80D63', |
|
# 'username': 'wangpengfei', |
|
# 'password': 'DC483E80A7A0BD9EF71D8CF973673924', |
|
# 'username': 'admin', |
|
# 'password': 'F1887D3F9E6EE7A32FE5E76F4AB80D63' |
|
} |
|
|
|
r = session.post(login_url, headers=header, data=data) |
|
print('登录') |
|
print(r) |
|
print(r.text) |
|
# print(r.status_code) |
|
# print(r.headers) |
|
if r.status_code == 200: |
|
return True |
|
return False |
|
|
|
|
|
def check_quan(quan_barcode): |
|
quan_url = "http://voucher-info.qas.cmc.com/voucherquery/detail" |
|
|
|
data = {'voucherBarcode': quan_barcode, |
|
'voucherId': ''} |
|
|
|
r = session.post(quan_url, data=data) |
|
if r.status_code == 200: |
|
print(r.text) |
|
j = r.json() |
|
quan_datail = { |
|
'quan_code': j['voucherInfo']['voucherId'], |
|
'quan_barcode': quan_barcode, |
|
'quan_level': j['voucherInfo']['voucherTypeName'], |
|
'quan_status': '', |
|
'expire': j['voucherInfo']['expDate'], |
|
} |
|
if j["voucherInfo"]["voucherState"] == "启用": |
|
if j['exchangeOrderId'] == "": |
|
result = check_quan_bind(quan_datail) |
|
if result: |
|
quan_datail['quan_status'] = result |
|
db_update_quan_by_barcode(quan_datail) |
|
return quan_datail |
|
else: |
|
print(quan_datail['quan_barcode'] + "\t已回兑") |
|
quan_datail['quan_status'] = 'used' |
|
db_update_quan_by_barcode(quan_datail) |
|
return quan_datail |
|
else: |
|
print(quan_datail['quan_barcode'] + "\t" + quan_datail['quan_level'] + " 券类无效") |
|
quan_datail['quan_status'] = 'freeze' |
|
db_update_quan_by_barcode(quan_datail) |
|
return quan_datail |
|
else: |
|
print('券验证') |
|
print(r) |
|
print(r.text) |
|
return False |
|
|
|
|
|
def check_quan_bind(quan_datail): |
|
quan_bind_url = 'http://voucher-trade.qas.cmc.com/relation/searchData' |
|
data = {'voucherNumber': quan_datail['quan_barcode'], |
|
'memberNumber': '', |
|
'voucherState': '0', |
|
'mobile': '', |
|
'backupYear': '', |
|
'pageIndex': 1, |
|
'pageSize': 10, } |
|
r = session.post(quan_bind_url, data=data) |
|
print('check_quan_bind') |
|
print(r) |
|
print(r.text) |
|
if r.status_code == 200: |
|
# print(r.text) |
|
j = r.json() |
|
if j['totalCount'] == 1: |
|
# quan_datail['quan_status'] = 'bound' |
|
# db_update_quan_by_barcode(quan_datail) |
|
print(quan_datail['quan_barcode'] + "\t已绑定") |
|
return 'bound' |
|
else: |
|
# quan_datail['quan_status'] = 'free' |
|
# db_update_quan_by_barcode(quan_datail) |
|
print(quan_datail['quan_barcode']) |
|
return 'free' |
|
return False |
|
|
|
|
|
def get_quan_api(quan_level): |
|
now_datetime = time.strftime('%Y-%m-%d %H:%M:%S') |
|
print(quan_level) |
|
print(now_datetime) |
|
try: |
|
db_cursor.execute( |
|
"SELECT * FROM quan WHERE quan_status = 'free' AND quan_level = %s AND (expire > %s OR expire IS NULL) ORDER BY id;", |
|
(quan_level, now_datetime)) |
|
result = db_cursor.fetchall() |
|
result_length = len(result) |
|
n = NUM |
|
print(f"get_quan_api:") |
|
# print(result) |
|
useful_quan_list = [] |
|
if result_length >= n: |
|
for r in result: |
|
quan_detail = check_quan(r['quan_barcode']) |
|
if quan_detail is not False: |
|
if quan_detail['quan_status'] == 'free': |
|
useful_quan_list.append(quan_detail) |
|
n -= 1 |
|
result_length -= 1 |
|
if n == 0 or result_length == 0: |
|
break |
|
elif result_length > 0: |
|
for r in result: |
|
quan_detail = check_quan(r['quan_barcode']) |
|
if quan_detail is not False: |
|
if quan_detail['quan_status'] == 'free': |
|
useful_quan_list.append(quan_detail) |
|
else: |
|
return False |
|
|
|
if useful_quan_list: |
|
return useful_quan_list |
|
else: |
|
return False |
|
|
|
except Exception as e: |
|
print(e) |
|
return False |
|
|
|
|
|
def get_quan_level_api(): |
|
now_datetime = time.strftime('%Y-%m-%d %H:%M:%S') |
|
try: |
|
db_conn.ping() |
|
print('DB connected') |
|
except Exception as e: |
|
print('DB Disconnect') |
|
print(e) |
|
|
|
try: |
|
db_cursor.execute( |
|
"SELECT quan_type, quan_level FROM quan WHERE quan_level IS NOT NULL AND quan_status = 'free' AND expire > %s GROUP BY quan_level;", |
|
(now_datetime,)) |
|
result = db_cursor.fetchall() |
|
retail_quan = [] |
|
ticket_quan = [] |
|
package_quan = [] |
|
for r in result: |
|
if r['quan_type'] == 'retail': |
|
retail_quan.append(r['quan_level']) |
|
elif r['quan_type'] == 'ticket': |
|
ticket_quan.append(r['quan_level']) |
|
else: |
|
package_quan.append(r['quan_level']) |
|
return {'retail': retail_quan, 'ticket': ticket_quan, 'package': package_quan} |
|
except Exception as e: |
|
print(e) |
|
return False |
|
|
|
|
|
def db_get_quan_barcode(): |
|
now_datetime = time.strftime('%Y-%m-%d %H:%M:%S') |
|
try: |
|
db_cursor.execute("SELECT * FROM quan WHERE quan_status = 'free' AND (expire > %s OR expire IS NULL);", |
|
(now_datetime,)) |
|
r = db_cursor.fetchall() |
|
quan_barcode_list = [] |
|
for q in r: |
|
quan_barcode_list.append(q['quan_barcode']) |
|
return quan_barcode_list |
|
except Exception as e: |
|
print(e) |
|
return False |
|
|
|
|
|
def db_get_quan_barcode_new_insert(): |
|
now_datetime = time.strftime('%Y-%m-%d %H:%M:%S') |
|
try: |
|
db_cursor.execute( |
|
"SELECT * FROM quan WHERE quan_status = 'free' AND (expire > %s OR expire IS NULL) AND quan_level IS NULL;", |
|
(now_datetime,)) |
|
r = db_cursor.fetchall() |
|
quan_barcode_list = [] |
|
for q in r: |
|
quan_barcode_list.append(q['quan_barcode']) |
|
return quan_barcode_list |
|
except Exception as e: |
|
print(e) |
|
return [] |
|
|
|
|
|
def db_check_quan_detail(quan_barcode): |
|
try: |
|
db_cursor.execute("SELECT * FROM quan WHERE quan_barcode = %s;", (quan_barcode,)) |
|
r = db_cursor.fetchone() |
|
if r['quan_code'] is None or r['quan_level'] is None or r['expire'] is None: |
|
return False |
|
return r['quan_status'] |
|
except Exception as e: |
|
print(e) |
|
return False |
|
|
|
|
|
def db_update_quan_by_barcode(quan_data): |
|
result = db_check_quan_detail(quan_data['quan_barcode']) |
|
if result: |
|
if result != quan_data['quan_status']: |
|
try: |
|
db_cursor.execute( |
|
'UPDATE quan SET quan_status = %s WHERE quan_barcode = %s;', |
|
(quan_data['quan_status'], quan_data['quan_barcode'])) |
|
except Exception as e: |
|
print(e) |
|
else: |
|
try: |
|
db_cursor.execute( |
|
'UPDATE quan SET quan_code = %s, quan_level = %s, quan_status = %s, expire = %s WHERE quan_barcode = %s;', |
|
(quan_data['quan_code'], quan_data['quan_level'], quan_data['quan_status'], quan_data['expire'], |
|
quan_data['quan_barcode'])) |
|
except Exception as e: |
|
print(e) |
|
db_conn.commit() |
|
|
|
|
|
def db_insert_quan_barcode(quan_type, _quan_list=None): |
|
n = 0 |
|
if _quan_list is not None: |
|
for quan in _quan_list: |
|
quan = quan.strip() |
|
if quan != '': |
|
db_cursor.execute('SELECT * FROM quan WHERE quan_barcode = %s;', (quan,)) |
|
r = db_cursor.fetchone() |
|
if r: |
|
print('find ' + quan) |
|
else: |
|
result = db_cursor.execute('INSERT INTO quan (quan_barcode, quan_type) VALUES (%s, %s);', |
|
(quan, quan_type)) |
|
print('insert ' + quan + ' successful') |
|
db_conn.commit() |
|
if r != 0: |
|
n += result |
|
else: |
|
with open('quan_id.txt', 'r') as f: |
|
lines = f.readlines() |
|
for line in lines: |
|
quan = line.strip() |
|
if quan != '': |
|
db_cursor.execute('SELECT * FROM quan WHERE quan_barcode = %s;', (quan,)) |
|
r = db_cursor.fetchone() |
|
if r: |
|
print('find ' + quan) |
|
else: |
|
result = db_cursor.execute('INSERT INTO quan (quan_barcode, quan_type) VALUES (%s, %s);', |
|
(quan, quan_type)) |
|
print('insert ' + quan + ' successful') |
|
db_conn.commit() |
|
if r != 0: |
|
n += result |
|
db_close() |
|
return n |
|
|
|
|
|
def db_close(): |
|
db_cursor.close() |
|
db_conn.close() |
|
|
|
|
|
def get_qrcode(barcode): |
|
img = qrcode.make(barcode) |
|
buffered = BytesIO() |
|
img.save(buffered, format="PNG") |
|
img_str = base64.b64encode(buffered.getvalue()) |
|
print(img_str) |
|
return img_str |
|
|
|
|
|
if __name__ == '__main__': |
|
# 插入券数据 |
|
# 卖品券 |
|
# db_insert_quan_barcode('retail') |
|
# 影票券 |
|
# db_insert_quan_barcode('ticket') |
|
|
|
# # 更新券信息 |
|
if login(): |
|
# quan_list = db_get_quan_barcode() |
|
quan_list = db_get_quan_barcode_new_insert() |
|
print(quan_list) |
|
for q in quan_list: |
|
time.sleep(0.5) |
|
check_quan(q) |
|
|
|
# 生成二维码 |
|
# qr_img = get_qrcode('2999971900612152353') |
|
|
|
# 测试 |
|
# if login(): |
|
# print(get_quan_api('Rog卖品兑换券1兑1-2021040601')) |
|
|
|
if __name__ == "__main__": |
|
check_quan('0125049691520602755453')
|
|
|