From 704b6571d789c508643e49ac3c048294cd7929ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E5=AD=9F=E6=8D=B7?= <718335940@qq.com> Date: Mon, 20 Mar 2023 00:01:23 +0800 Subject: [PATCH] fix --- update_url.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/update_url.py b/update_url.py index 9e5583a..a925a74 100644 --- a/update_url.py +++ b/update_url.py @@ -67,8 +67,8 @@ if DDAL_EMAIL and DDAL_PASSWORD: raise Exception(f"{r['msg']} (alias = {repr(alias)}, url = {repr(url)})") return r['short'] - def update(self, id, url) -> str: - while True: + def update(self, id, alias, url) -> str: + for _ in range(10): with self.__token_lock: bs = BeautifulSoup(self.__session.get(f'https://dd.al/user/edit/{id}').text, 'html.parser') token = bs.find('input', {'name': 'token'}) @@ -82,17 +82,18 @@ if DDAL_EMAIL and DDAL_PASSWORD: loc = r.headers.get('Location') if not (loc and urlsplit(loc).path != '/user'): raise Exception(f'loc = {repr(loc)}') - short = next((item['short'] for item in self.search(url) if item['id'] == id), None) - if short: + item = next((item for item in self.search(alias) if item['id'] == id), None) + if item and item['original'] == url: break - print(f'更新失败,正在重试... (id = {repr(id)}, url = {repr(url)})') - return short + else: + raise Exception(f'尝试 10 次更新 url 均失败 (id = {repr(id)}, url = {repr(url)})') + return item['short'] def upsert(self, alias, url) -> str: self.raise_for_alias(alias) id = next((item['id'] for item in self.search(alias) if urlsplit(item['short']).path[1:] == alias), None) if id: - return self.update(id, url) + return self.update(id, alias, url) else: return self.insert(alias, url)