Custom generator that yields the bytes of the media file. Modded from Thanks to Eyaadh
(
self,
file_id: FileId,
index: int,
offset: int,
first_part_cut: int,
last_part_cut: int,
part_count: int,
chunk_size: int,
)
| 161 | return location |
| 162 | |
| 163 | async def yield_file( |
| 164 | self, |
| 165 | file_id: FileId, |
| 166 | index: int, |
| 167 | offset: int, |
| 168 | first_part_cut: int, |
| 169 | last_part_cut: int, |
| 170 | part_count: int, |
| 171 | chunk_size: int, |
| 172 | ) -> Union[str, None]: |
| 173 | """ |
| 174 | Custom generator that yields the bytes of the media file. |
| 175 | Modded from <https://github.com/eyaadh/megadlbot_oss/blob/master/mega/telegram/utils/custom_download.py#L20> |
| 176 | Thanks to Eyaadh <https://github.com/eyaadh> |
| 177 | """ |
| 178 | client = self.client |
| 179 | work_loads[index] += 1 |
| 180 | logging.debug(f"Starting to yielding file with client {index}.") |
| 181 | media_session = await self.generate_media_session(client, file_id) |
| 182 | |
| 183 | current_part = 1 |
| 184 | location = await self.get_location(file_id) |
| 185 | |
| 186 | try: |
| 187 | r = await media_session.send( |
| 188 | raw.functions.upload.GetFile( |
| 189 | location=location, offset=offset, limit=chunk_size |
| 190 | ), |
| 191 | ) |
| 192 | if isinstance(r, raw.types.upload.File): |
| 193 | while True: |
| 194 | chunk = r.bytes |
| 195 | if not chunk: |
| 196 | break |
| 197 | elif part_count == 1: |
| 198 | yield chunk[first_part_cut:last_part_cut] |
| 199 | elif current_part == 1: |
| 200 | yield chunk[first_part_cut:] |
| 201 | elif current_part == part_count: |
| 202 | yield chunk[:last_part_cut] |
| 203 | else: |
| 204 | yield chunk |
| 205 | |
| 206 | current_part += 1 |
| 207 | offset += chunk_size |
| 208 | |
| 209 | if current_part > part_count: |
| 210 | break |
| 211 | |
| 212 | r = await media_session.send( |
| 213 | raw.functions.upload.GetFile( |
| 214 | location=location, offset=offset, limit=chunk_size |
| 215 | ), |
| 216 | ) |
| 217 | except (TimeoutError, AttributeError): |
| 218 | pass |
| 219 | finally: |
| 220 | logging.debug("Finished yielding file with {current_part} parts.") |
no test coverage detected