-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from captaincolonelfox/dev
Release 1.5.0
- Loading branch information
Showing
8 changed files
with
91 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from aiogram import Bot, Dispatcher | ||
|
||
from bot.api.likee import LikeeAPI | ||
from bot.api.tiktok import TikTokAPI | ||
from settings import API_TOKEN | ||
|
||
|
||
bot = Bot(token=API_TOKEN) | ||
dp = Dispatcher(bot=bot) | ||
dp = Dispatcher(bot=bot) | ||
|
||
|
||
from . import handlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,3 @@ | ||
import asyncio | ||
import re | ||
from abc import ABC, abstractmethod | ||
from typing import Any, List, Optional | ||
|
||
import httpx | ||
import sentry_sdk | ||
from aiogram.types import Message | ||
from bs4 import BeautifulSoup | ||
from httpx import HTTPStatusError | ||
|
||
from bot.data import VideoData | ||
|
||
|
||
class API(ABC): | ||
client = httpx.AsyncClient( | ||
headers={ | ||
"User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' | ||
} | ||
) | ||
|
||
@property | ||
def headers(self) -> dict[str, Any]: | ||
return {} | ||
|
||
@property | ||
@abstractmethod | ||
def links(self) -> List[str]: | ||
return ['platform.com'] | ||
|
||
@property | ||
@abstractmethod | ||
def regexp_key(self) -> str: | ||
return 'key' | ||
|
||
async def handle_message(self, message: Message) -> List[VideoData]: | ||
urls = [] | ||
for e in message.entities: | ||
for link in self.links: | ||
if link in (url := message.text[e.offset:e.offset + e.length]): | ||
urls.append(url) | ||
if not urls: | ||
return [] | ||
try: | ||
return [await self.download_video(url) for url in urls] | ||
except (KeyError, HTTPStatusError) as ex: | ||
sentry_sdk.capture_exception(ex) | ||
return [] | ||
|
||
async def download_video(self, url: str, retries: int = 2) -> VideoData: | ||
for _ in range(retries): | ||
page = await self.client.get(url) | ||
soup = BeautifulSoup(page.content, 'html.parser') | ||
if data := soup(text=re.compile(self.regexp_key)): | ||
for script in data: | ||
if link := self._parse_data(script): | ||
if video := await self.client.get(link, headers=self.headers): | ||
video.raise_for_status() | ||
return VideoData(link, video.content) | ||
await asyncio.sleep(0.5) | ||
return VideoData() | ||
|
||
@abstractmethod | ||
def _parse_data(self, script: str) -> Optional[str]: | ||
pass | ||
from .base import API | ||
from .likee import LikeeAPI | ||
from .tiktok import TikTokAPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import asyncio | ||
import re | ||
from abc import ABC, abstractmethod | ||
from typing import Any, List, Optional | ||
|
||
import httpx | ||
import sentry_sdk | ||
from aiogram.types import Message | ||
from bs4 import BeautifulSoup | ||
from httpcore import TimeoutException | ||
from httpx import HTTPStatusError | ||
from bot.data import VideoData | ||
|
||
|
||
class API(ABC): | ||
|
||
@property | ||
def headers(self) -> dict[str, Any]: | ||
return {} | ||
|
||
@property | ||
@abstractmethod | ||
def links(self) -> List[str]: | ||
return ['platform.com'] | ||
|
||
@property | ||
@abstractmethod | ||
def regexp_key(self) -> str: | ||
return 'key' | ||
|
||
async def handle_message(self, message: Message) -> List[VideoData]: | ||
urls = [] | ||
for e in message.entities: | ||
for link in self.links: | ||
if link in (url := message.text[e.offset:e.offset + e.length]): | ||
urls.append(url if url.startswith('http') else f'https://{url}') | ||
try: | ||
return [await self.download_video(url) for url in urls] | ||
except (KeyError, HTTPStatusError) as ex: | ||
sentry_sdk.capture_exception(ex) | ||
return [] | ||
|
||
async def download_video(self, url: str, retries: int = 2) -> VideoData: | ||
for _ in range(retries): | ||
async with httpx.AsyncClient(headers=self.headers) as client: | ||
try: | ||
page = await client.get(url) | ||
soup = BeautifulSoup(page.content, 'html.parser') | ||
if data := soup(text=re.compile(self.regexp_key)): | ||
for script in data: | ||
if link := self._parse_data(script): | ||
if video := await client.get(link): | ||
video.raise_for_status() | ||
return VideoData(link, video.content) | ||
except TimeoutException: | ||
pass | ||
await asyncio.sleep(0.5) | ||
return VideoData() | ||
|
||
@abstractmethod | ||
def _parse_data(self, script: str) -> Optional[str]: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters