You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromCrypto.CipherimportAESfromCrypto.Util.Paddingimportunpadimportbase64importjsonimportmatplotlib.pyplotaspltimportrequestsimportplatformdefdecrypt_aes_ecb(encrypted_data: str) ->str:
key=encrypted_data[:16].encode('utf-8')
encrypted_data=encrypted_data[16:]
encrypted_data_bytes=base64.b64decode(encrypted_data)
cipher=AES.new(key, AES.MODE_ECB)
decrypted_data=unpad(cipher.decrypt(encrypted_data_bytes), AES.block_size)
returndecrypted_data.decode('utf-8')
idserial=""servicehall=""all_data=dict()
count_data=dict()
if__name__=="__main__":
try:
withopen("config.json", "r", encoding='utf-8') asf:
account=json.load(f)
idserial=account["idserial"]
servicehall=account["servicehall"]
exceptExceptionase:
print("账户信息读取失败,请重新输入")
idserial=input("请输入学号: ")
servicehall=input("请输入服务代码: ")
withopen("config.json", "w", encoding='utf-8') asf:
json.dump({"idserial": idserial, "servicehall": servicehall}, f, indent=4)
url=f"https://card.tsinghua.edu.cn/business/querySelfTradeList?pageNumber=0&pageSize=5000&starttime=2024-01-01&endtime=2024-12-31&idserial={idserial}&tradetype=-1"cookie= {
"servicehall": servicehall,
}
response=requests.post(url, cookies=cookie)
encrypted_string=json.loads(response.text)["data"]
decrypted_string=decrypt_aes_ecb(encrypted_string)
data=json.loads(decrypted_string)
foritemindata["resultData"]["rows"]:
try:
mername=item["mername"]
txamt=item["txamt"]
ifmernameinall_data:
all_data[mername] +=txamtcount_data[mername] +=1else:
all_data[mername] =txamtcount_data[mername] =1exceptExceptionase:
pass# Convert to yuan and round to 2 decimal placesall_data= {k: round(v/100, 2) fork, vinall_data.items()}
# Calculate average spendingavg_data= {k: round(all_data[k] /count_data[k], 2) forkinall_data}
print(f"Total entries processed: {len(all_data)}")
# Sort the data by total amount, number of transactions, and average spendingall_data_sorted=dict(sorted(all_data.items(), key=lambdax: x[1], reverse=True))
count_data_sorted=dict(sorted(count_data.items(), key=lambdax: x[1], reverse=True))
avg_data_sorted=dict(sorted(avg_data.items(), key=lambdax: x[1], reverse=True))
# Set font for Chinese charactersifplatform.system() =="Darwin":
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
elifplatform.system() =="Linux":
plt.rcParams['font.family'] = ['Droid Sans Fallback', 'DejaVu Sans']
else:
plt.rcParams['font.sans-serif'] = ['SimHei']
# Function to create and save a bar chart with data sorted from top (largest) to bottom (smallest)defcreate_bar_chart(data, title, xlabel, filename):
plt.figure(figsize=(12, len(data) /66*18))
# Convert the dictionary items to lists and reverse them for top-to-bottom orderkeys, values=zip(*data.items())
keys=list(keys)[::-1] # Reverse the keys for top-to-bottom ordervalues=list(values)[::-1] # Reverse the values for top-to-bottom orderplt.barh(keys, values)
# Add value labels to the barsforindex, valueinenumerate(values):
plt.text(value+0.01*max(values), # Offset the text slightly to the right of the barindex, # Position the text vertically at the center of each barstr(value),
va='center')
plt.xlim(0, 1.2*max(values)) # Set x-axis limit to accommodate all bars with some paddingplt.title(title)
plt.xlabel(xlabel)
plt.tight_layout() # Adjust layout to fit everything nicely on the canvasplt.savefig(filename) # Save the figure to a fileplt.show() # Display the plot# Plot total spendingcreate_bar_chart(all_data_sorted, "华清大学食堂消费总量情况", "消费金额(元)", "total_spending.png")
# Plot number of transactionscreate_bar_chart(count_data_sorted, "华清大学食堂消费次数情况", "消费次数", "transaction_counts.png")
# Plot average spendingcreate_bar_chart(avg_data_sorted, "华清大学食堂平均消费价格情况", "平均消费金额(元)", "average_spending.png")
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: