MCPcopy Create free account
hub / github.com/VJBots/VJ-File-Store / yield_file

Method yield_file

TechVJ/utils/custom_dl.py:168–226  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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

Callers 1

media_streamerFunction · 0.95

Calls 2

get_locationMethod · 0.95

Tested by

no test coverage detected