async def save(http, deck, url): DIR = f"{deck['name']}/{DIR_IMAGE}" await mkdir(DIR, exist_ok=True) filename = f"{DIR}/{url.split('/')[-1]}.jpeg" async with aopen(filename, "ab") as image: async with http.stream("GET", url) as r: async for chunk in r.aiter_bytes(): await image.write(chunk)
async def save_file(path, url, semaphore): try: async with semaphore, aiohttp.request( 'GET', url ) as r: # request(method, url) http://aiohttp.readthedocs.io/en/stable/client_reference.html data = await r.read() # 使用 aiohttp.request 來"異步"取得目標檔案的資料 async with aopen(path, 'wb') as f: await f.write(data) # 寫入資料 except Exception as e: print("get error on", path, "due to", repr(e)) # 報錯 return path # 報錯後返回失敗的的檔案名稱 else: print(path, "saved.") # 下載成功 return None # 下載成功後返回None
async def fetch(http, compact_id): uuids = await get_uuids(http, compact_id) decks = await gather(*(grab_deck(http, uuid) for uuid in uuids)) for deck in decks: cards = [[get_content(side["concepts"]) for side in card["sides"]] for card in deck["cards"]] await fetch_images(cards, deck) await mkdir(deck["name"], exist_ok=True) async with aopen(f"{deck['name']}/{deck['slug']}.csv", "w", encoding="utf-8", newline="") as csvfile: record = AsyncWriter(csvfile).writerow show = lambda side: " | ".join( (fact.popitem()[1]) for fact in side) for card in cards: await record([show(side) for side in card]) return compact_id
async def write_file(session, url, name): async with session.get(url) as response: print(f'baixando {name}: {url[-6:-3]}') async with aopen(f'{path}/{name}.jpg', 'wb') as f: content = await response.content.read() await f.write(content)
async def check_update( self, uri: str, path: str, save: Optional[str] = None, write: Optional[bool] = True ) -> Tuple[bool, Optional[Union[bytes, str]]]: dirs = '/'.join(path.split('/')[:-1]) dirs = f'{dirs}/' if dirs else '' filename = path.split('/')[-1] key = '.'.join(filename.split('.')[:-1]) extension = filename.split('.')[-1] tags = self.updates.get( path, self.pc_updates.get(path, self.repl_updates.get(path))) save = save or path is_image = (True if path.endswith('.png') or path.endswith('.jpg') or path.endswith('.ico') else False) async def backup(): if os.path.isfile(path) and 'backup' in tags: try: if os.path.isfile(f'{dirs}{key}_old.{extension}'): await aremove(f'{dirs}{key}_old.{extension}') await arename(path, f'{dirs}{key}_old.{extension}') except PermissionError: self.bot.send(self.l('backup_failed', path, default=("'{0}' のバックアップに失敗しました\n" "Failed backup for '{0}'")), add_p=self.bot.time) self.bot.send(self.l('checking_update', path, default=("'{0}' のアップデートを確認中...\n" "Checking update for '{0}'...")), add_p=[self.bot.time, lambda x: f'\n{x}']) retry = 5 for tries in range(retry): if 'db' in tags: if self.bot.isfile(key): existing = await self.bot.aload_json(key) else: existing = {} else: if os.path.isfile(path): if 'raw' in tags: async with aopen(path, 'rb') as f: existing = await f.read() if not is_image and sys.platform == 'win32': existing = existing.replace(b'\r\n', b'\n') else: async with aopen(path, 'r', encoding='utf-8') as f: existing = await f.read() if not is_image and sys.platform == 'win32': existing = existing.replace('\r\n', '\n') else: existing = b'' if 'raw' in tags else '' res = await self.bot.http.session.get(uri + path) if res.status == 404: self.bot.send(self.l('file_not_found', path, default=("'{0}' ファイルが存在しません\n" "File '{0}' doesn't exists")), add_p=self.bot.time) return False, existing if res.status != 200: retry_after = 10 self.bot.send(self.l( 'get_failed', path, retry_after, tries, retry, default= ("'{0}' のデータの取得に失敗しました。{1}秒後に再試行します {2}/{3}\n" "Failed to get data for '{0}'. Retry after {1} seconds {2}/{3}" )), add_p=self.bot.time) await asyncio.sleep(retry_after) continue if path.endswith('.json'): if isinstance(existing, str): existing = json.loads(existing) latest = await res.json(content_type=None) await res.release() if 'diff' in tags: new = self.add_new_key(existing, latest, 'diff' not in tags) if existing != new: if write: self.bot.send(self.l( 'update_detected', path, default=("'{0}' のアップデートを確認しました\n" "Detected update for '{0}'")), add_p=self.bot.time) if 'db' in tags: self.bot.save_json(key, new) else: if dirs: os.makedirs(dirs, exist_ok=True) await backup() self.bot.save_json(f'{dirs}{key}', new, force_file=True) return True, new else: if existing != latest: if write: self.bot.send(self.l( 'update_detected', path, default=("'{0}' のアップデートを確認しました\n" "Detected update for '{0}'")), add_p=self.bot.time) if 'db' in tags: self.bot.save_json(key, latest) else: if dirs: os.makedirs(dirs, exist_ok=True) await backup() self.bot.save_json(f'{dirs}{key}', latest, force_file=True) return True, latest else: if 'raw' in tags: latest = await res.read() if not is_image and sys.platform == 'win32': latest = latest.replace(b'\r\n', b'\n') else: latest = await res.text() if not is_image and sys.platform == 'win32': latest = latest.replace('\r\n', '\n') await res.release() if existing != latest: if write: self.bot.send(self.l( 'update_detected', path, default=("'{0}' のアップデートを確認しました\n" "Detected update for '{0}'")), add_p=self.bot.time) if dirs: os.makedirs(dirs, exist_ok=True) await backup() if 'raw' in tags: async with aopen(path, 'wb') as f: await f.write(latest) else: async with aopen(path, 'w', encoding='utf-8') as f: await f.write(latest) return True, latest return False, latest
async def download(url, path): async with aopen(path, 'wb') as file: content = await get(url) await file.write(content) return len(content)
async def write_file(): async with aopen('teste.txt', 'w') as f: await f.write('bananas')
async def write_file(n: str): async with aopen('teste.txt', 'a') as f: await f.write(f'{n}\n')