mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
This commit is contained in:
parent
484a61bdc1
commit
d29b18da8c
10 changed files with 53 additions and 16 deletions
|
|
@ -3,6 +3,7 @@ from contextlib import suppress
|
|||
from aiogram.dispatcher.flags import FlagGenerator
|
||||
|
||||
from . import enums, methods, types
|
||||
from .__meta__ import __api_version__, __version__
|
||||
from .client import session
|
||||
from .client.bot import Bot
|
||||
from .dispatcher.dispatcher import Dispatcher
|
||||
|
|
@ -36,6 +37,3 @@ __all__ = (
|
|||
"md",
|
||||
"flags",
|
||||
)
|
||||
|
||||
__version__ = "3.0.0b8"
|
||||
__api_version__ = "6.6"
|
||||
|
|
|
|||
2
aiogram/__meta__.py
Normal file
2
aiogram/__meta__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
__version__ = "3.0.0b8"
|
||||
__api_version__ = "6.6"
|
||||
|
|
@ -18,7 +18,10 @@ from typing import (
|
|||
|
||||
import certifi
|
||||
from aiohttp import BasicAuth, ClientError, ClientSession, FormData, TCPConnector
|
||||
from aiohttp.hdrs import USER_AGENT
|
||||
from aiohttp.http import SERVER_SOFTWARE
|
||||
|
||||
from aiogram.__meta__ import __version__
|
||||
from aiogram.methods import TelegramMethod
|
||||
|
||||
from ...exceptions import TelegramNetworkError
|
||||
|
|
@ -121,7 +124,12 @@ class AiohttpSession(BaseSession):
|
|||
await self.close()
|
||||
|
||||
if self._session is None or self._session.closed:
|
||||
self._session = ClientSession(connector=self._connector_type(**self._connector_init))
|
||||
self._session = ClientSession(
|
||||
connector=self._connector_type(**self._connector_init),
|
||||
headers={
|
||||
USER_AGENT: f"{SERVER_SOFTWARE} aiogram/{__version__}",
|
||||
},
|
||||
)
|
||||
self._should_reset_connector = False
|
||||
|
||||
return self._session
|
||||
|
|
@ -163,11 +171,21 @@ class AiohttpSession(BaseSession):
|
|||
return cast(TelegramType, response.result)
|
||||
|
||||
async def stream_content(
|
||||
self, url: str, timeout: int, chunk_size: int, raise_for_status: bool
|
||||
self,
|
||||
url: str,
|
||||
headers: Optional[Dict[str, Any]] = None,
|
||||
timeout: int = 30,
|
||||
chunk_size: int = 65536,
|
||||
raise_for_status: bool = True,
|
||||
) -> AsyncGenerator[bytes, None]:
|
||||
if headers is None:
|
||||
headers = {}
|
||||
|
||||
session = await self.create_session()
|
||||
|
||||
async with session.get(url, timeout=timeout, raise_for_status=raise_for_status) as resp:
|
||||
async with session.get(
|
||||
url, timeout=timeout, headers=headers, raise_for_status=raise_for_status
|
||||
) as resp:
|
||||
async for chunk in resp.content.iter_chunked(chunk_size):
|
||||
yield chunk
|
||||
|
||||
|
|
|
|||
|
|
@ -158,7 +158,12 @@ class BaseSession(abc.ABC):
|
|||
|
||||
@abc.abstractmethod
|
||||
async def stream_content(
|
||||
self, url: str, timeout: int, chunk_size: int, raise_for_status: bool
|
||||
self,
|
||||
url: str,
|
||||
headers: Optional[Dict[str, Any]] = None,
|
||||
timeout: int = 30,
|
||||
chunk_size: int = 65536,
|
||||
raise_for_status: bool = True,
|
||||
) -> AsyncGenerator[bytes, None]: # pragma: no cover
|
||||
"""
|
||||
Stream reader
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import io
|
|||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import AsyncGenerator, AsyncIterator, Iterator, Optional, Union
|
||||
from typing import Any, AsyncGenerator, AsyncIterator, Dict, Iterator, Optional, Union
|
||||
|
||||
import aiofiles
|
||||
|
||||
|
|
@ -114,6 +114,7 @@ class URLInputFile(InputFile):
|
|||
def __init__(
|
||||
self,
|
||||
url: str,
|
||||
headers: Optional[Dict[str, Any]] = None,
|
||||
filename: Optional[str] = None,
|
||||
chunk_size: int = DEFAULT_CHUNK_SIZE,
|
||||
timeout: int = 30,
|
||||
|
|
@ -122,12 +123,16 @@ class URLInputFile(InputFile):
|
|||
Represents object for streaming files from internet
|
||||
|
||||
:param url: URL in internet
|
||||
:param headers: HTTP Headers
|
||||
:param filename: Filename to be propagated to telegram.
|
||||
:param chunk_size: Uploading chunk size
|
||||
"""
|
||||
super().__init__(filename=filename, chunk_size=chunk_size)
|
||||
if headers is None:
|
||||
headers = {}
|
||||
|
||||
self.url = url
|
||||
self.headers = headers
|
||||
self.timeout = timeout
|
||||
|
||||
async def read(self, chunk_size: int) -> AsyncGenerator[bytes, None]:
|
||||
|
|
@ -136,6 +141,7 @@ class URLInputFile(InputFile):
|
|||
bot = Bot.get_current(no_error=False)
|
||||
stream = bot.session.stream_content(
|
||||
url=self.url,
|
||||
headers=self.headers,
|
||||
timeout=self.timeout,
|
||||
chunk_size=self.chunk_size,
|
||||
raise_for_status=True,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue