bill-ktor/scripts/labels_mongo2mysql.py

55 lines
1.3 KiB
Python
Raw Normal View History

2022-11-18 17:11:52 +08:00
import aiohttp
import asyncio
mongo_url = 'http://www.fadinglight.cn:8088/class'
mysql_url = 'http://localhost:8080/api/v1/label/'
2022-11-18 22:10:13 +08:00
2022-11-26 01:18:20 +08:00
async def getMongoLabels():
2022-11-18 17:11:52 +08:00
async with aiohttp.ClientSession() as session:
async with session.get(mongo_url) as response:
data = await response.json()
return data
2022-11-18 22:10:13 +08:00
2022-11-26 01:18:20 +08:00
async def postLabelsToMysql(label):
2022-11-18 17:11:52 +08:00
async with aiohttp.ClientSession() as session:
2022-11-26 01:18:20 +08:00
async with session.post(mysql_url, json=label) as response:
2022-11-18 17:11:52 +08:00
data = await response.json()
return data
2022-11-18 22:10:13 +08:00
2022-11-26 01:18:20 +08:00
def newLabel(name, type=0, relativeId=None):
2022-11-18 17:11:52 +08:00
return dict(
2022-11-26 01:18:20 +08:00
name=name,
type=type,
relativeId=relativeId,
2022-11-18 17:11:52 +08:00
)
2022-11-18 22:10:13 +08:00
2022-11-18 17:11:52 +08:00
async def main():
2022-11-26 01:18:20 +08:00
data = await getMongoLabels()
2022-11-18 17:11:52 +08:00
labels = data['data']
2022-11-18 22:10:13 +08:00
consumeLabels = labels['consume']
incomeLabels = labels['income']
2022-11-26 01:18:20 +08:00
print(consumeLabels)
2022-11-18 22:10:13 +08:00
2022-11-26 01:18:20 +08:00
for i in incomeLabels:
label = newLabel(i, type=2)
# print(label)
await postLabelsToMysql(label)
for i in consumeLabels:
subLabels = consumeLabels[i]
label = newLabel(i, type=0)
resp = await postLabelsToMysql(label)
id = resp['data']
print(id)
for j in subLabels:
sub_label = newLabel(j, type=1, relativeId=id)
await postLabelsToMysql(sub_label)
2022-11-18 22:10:13 +08:00
2022-11-18 17:11:52 +08:00
2022-11-18 22:10:13 +08:00
asyncio.run(main())