bill-ktor/scripts/labels_mongo2mysql.py
2022-11-18 22:10:13 +08:00

48 lines
1.1 KiB
Python

import aiohttp
import asyncio
mongo_url = 'http://www.fadinglight.cn:8088/class'
mysql_url = 'http://localhost:8080/api/v1/label/'
async def getMongoBills():
async with aiohttp.ClientSession() as session:
async with session.get(mongo_url) as response:
data = await response.json()
return data
async def postBillsToMysql(bills):
async with aiohttp.ClientSession() as session:
async with session.post(mysql_url, json=bills) as response:
data = await response.json()
return data
def dealOneBill(bill):
bill.setdefault('type', 0)
return dict(
money=bill['money'],
cls=bill['cls'],
label=bill['label'],
date=bill['date'],
options=bill['options'],
type="consume" if bill['type'] == 0 else "income",
)
async def main():
data = await getMongoBills()
labels = data['data']
consumeLabels = labels['consume']
incomeLabels = labels['income']
# for i in consumeLabels:
# bills = list(map(dealOneBill, bills))
# respData = await postBillsToMysql(bills)
# print(respData)
asyncio.run(main())