♻️ 修改获取图片逻辑

This commit is contained in:
xtaodada 2022-10-05 23:33:05 +08:00
parent 685e1dc646
commit d3babaea7b
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659

View File

@ -106,40 +106,36 @@ class LofterPost:
async def get_items(self) -> List[Item]: async def get_items(self) -> List[Item]:
datas: List[LofterPost.Item] = [] datas: List[LofterPost.Item] = []
while True: data = await self.get_data()
data = await self.get_data() if data:
if not data:
break
data = data.get("data", {}) data = data.get("data", {})
if not data: if data:
break
self.offset = data.get("offset", 0) self.offset = data.get("offset", 0)
posts = data.get("posts", []) if posts := data.get("posts", []):
if not posts: datas = self.parse_data(posts)
break
datas.extend(self.parse_data(posts))
if self.offset == -1:
break
return datas return datas
async def upload(self, message: Message): async def upload(self, message: Message):
msg = await message.reply_text("正在获取数据...") msg = await message.reply_text("正在上传中...")
try: success, error = 0, 0
items = await self.get_items() while True:
except Exception as e:
await msg.edit_text(f"获取数据失败: {e}")
return
await msg.edit(f"获取到{len(items)}条数据,正在上传...")
success, error, final = 0, 0, len(items)
for item in items:
try: try:
file = await item.init() items = await self.get_items()
await item.upload(file) if not items:
success += 1 break
await sleep(1) for item in items:
try:
file = await item.init()
await item.upload(file)
success += 1
await sleep(1)
except Exception:
error += 1
if (success + error) % 10 == 0:
with contextlib.suppress(Exception):
await msg.edit(f"已成功上传{success}条,失败{error}条,第 {success + error}")
if self.offset == -1:
break
except Exception: except Exception:
error += 1 continue
if (success + error) % 10 == 0:
with contextlib.suppress(Exception):
await msg.edit(f"已成功上传{success}条,失败{error}条,剩余{final - success - error}")
await msg.edit(f"上传完成,成功{success}条,失败{error}") await msg.edit(f"上传完成,成功{success}条,失败{error}")