bill-ktor/scripts/bills_mongo2mysql.py

49 lines
1.1 KiB
Python
Raw Permalink Normal View History

2022-11-18 17:11:52 +08:00
import aiohttp
import asyncio
mongo_url = 'http://www.fadinglight.cn:8088/list'
mysql_url = 'http://localhost:8080/api/v1/bill/'
2022-11-26 01:18:20 +08:00
2022-11-18 17:11:52 +08:00
async def getMongoBills():
async with aiohttp.ClientSession() as session:
async with session.get(mongo_url) as response:
data = await response.json()
return data
2022-11-26 01:18:20 +08:00
2022-11-18 17:11:52 +08:00
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
2022-11-26 01:18:20 +08:00
2022-11-18 17:11:52 +08:00
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",
)
2022-11-26 01:18:20 +08:00
def transType(bill):
bill['type'] = 0 if bill['type'].lower() == 'income' else 1
return bill
2022-11-18 17:11:52 +08:00
async def main():
data = await getMongoBills()
bills = data['data']
bills = list(map(dealOneBill, bills))
2022-11-26 01:18:20 +08:00
bills = list(map(transType, bills))
2022-11-18 17:11:52 +08:00
respData = await postBillsToMysql(bills)
print(respData)
2022-11-26 01:18:20 +08:00
asyncio.run(main())