(jsonl_file: str)
| 179 | |
| 180 | |
| 181 | async def download_links(jsonl_file: str) -> None: |
| 182 | async with aiohttp.ClientSession() as session: |
| 183 | with jsonlines.open(jsonl_file) as reader: |
| 184 | tasks = list(reader) |
| 185 | progress_bar = tqdm(total=len(tasks), desc="Downloading files") |
| 186 | task_iter = iter(tasks) |
| 187 | coroutines = [] |
| 188 | while True: |
| 189 | while len(coroutines) < 80: |
| 190 | task = next(task_iter, None) |
| 191 | if task is None: |
| 192 | break |
| 193 | dirname = f"zenodo-pptx/pptx/{task['license']}/{task['created'][:4]}/" |
| 194 | basename = f"{task['checksum'][4:]}-{task['filename']}" |
| 195 | filepath = dirname + basename |
| 196 | try: |
| 197 | open("/tmp/" + basename, "wb").close() |
| 198 | except: |
| 199 | filepath = dirname + basename[:240] + ".pptx" |
| 200 | if os.path.exists(filepath): |
| 201 | progress_bar.update(1) |
| 202 | continue |
| 203 | coroutines.append( |
| 204 | download_file(session, filepath, task["url"], progress_bar) |
| 205 | ) |
| 206 | |
| 207 | start_time = time.time() |
| 208 | results = await asyncio.gather(*coroutines, return_exceptions=True) |
| 209 | for result in results: |
| 210 | if isinstance(result, Exception): |
| 211 | tqdm.write(f"Error {result}") |
| 212 | if len(coroutines) % 80 != 0: |
| 213 | return |
| 214 | coroutines.clear() |
| 215 | elapsed_time = time.time() - start_time |
| 216 | sleep(max(60 - elapsed_time, 0)) |
| 217 | |
| 218 | |
| 219 | async def gather_files(topics: list[str], num_results: int) -> None: |
no test coverage detected