Skip to content

Commit

Permalink
2024-10-27 12:34:00+04:00
Browse files Browse the repository at this point in the history
  • Loading branch information
nett00n committed Oct 27, 2024
1 parent fec8d63 commit 8ec3159
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# build-and-push.yml

name: Build and Push Docker Image

on:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lint-markdown.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# lint-markdown.yml

---
name: Markdown Lint

Expand Down
2 changes: 2 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# .markdownlint.yaml

---
MD013: false
default: true
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# .pre-commit-config.yaml

---
repos:
- hooks:
Expand Down
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# app/__init__.py
# __init__.py
2 changes: 2 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# api.py

from fastapi import APIRouter, HTTPException, Body
from pydantic import BaseModel
from .url_processing import process_url_request
Expand Down
3 changes: 2 additions & 1 deletion app/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# app/bot.py
# bot.py

import asyncio
import logging
from aiogram import Bot, Dispatcher, types
Expand Down
3 changes: 2 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# app/config.py
# config.py

import os
from dotenv import load_dotenv
from pydantic_settings import BaseSettings # Updated import
Expand Down
3 changes: 2 additions & 1 deletion app/download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# app/download.py
# download.py

import os
import logging
import yt_dlp
Expand Down
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# app/main.py
# main.py

import logging
import asyncio
from fastapi import FastAPI
Expand Down
8 changes: 4 additions & 4 deletions app/url_processing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# app/url_processing.py
# url_processing.py

import os
import logging
from urllib.parse import urlparse, urlunparse
Expand All @@ -16,7 +17,7 @@ async def process_url_request(url: str) -> str:
elif "youtube.com/shorts" in final_url:
sanitized_url = final_url
else:
return "Unsupported URL"
return f"Unsupported URL {final_url}"
video_path = await yt_dlp_download(sanitized_url)
return video_path if video_path else "Download failed"
except Exception as e:
Expand All @@ -33,5 +34,4 @@ def follow_redirects(url: str, timeout=10) -> str:

def transform_tiktok_url(url: str) -> str:
parsed_url = urlparse(url)
video_id = parsed_url.path.split("/")[-1]
return f"https://www.tiktok.com/embed/v2/{video_id}"
return f"{parsed_url.scheme}://{parsed_url.netloc}{parsed_url.path}"
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# docker-compose.yml

---
networks:
traefik_default:
Expand Down
2 changes: 2 additions & 0 deletions nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# default.conf

server {
listen 80;

Expand Down
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# pyproject.toml

[tool.poetry]
name = "url-fairy-bot"
version = "2.0.0"
Expand All @@ -21,6 +23,8 @@ black = "^24.4.2"
flake8 = "^7.0.0"
pytest = "^8.2.0"
yamllint = "^1.35.1"
httpx = "^0.27.2"
pytest-asyncio = "^0.24.0"

[tool.poetry.scripts]
test = "pytest"
Expand Down
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pytest.ini

[pytest]
testpaths = tests
addopts = --ignore=lib/python3.11/site-packages
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#
# __init__.py
12 changes: 12 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# test_api.py

import pytest
from httpx import AsyncClient
from app.main import app

@pytest.mark.asyncio
async def test_process_url():
async with AsyncClient(app=app, base_url="http://test") as ac:
response = await ac.post("/process_url/", json={"url": "https://example.com"})
assert response.status_code == 200
assert response.json()["status"] == "success"
15 changes: 15 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# test_bot.py

import pytest
from aiogram.types import Message
from app.bot import handle_message

@pytest.mark.asyncio
async def test_handle_message():
class MockMessage:
text = "https://example.com"
async def reply(self, text):
assert "Unsupported URL https://example.com/" in text or "success" in text

message = MockMessage()
await handle_message(message)
22 changes: 22 additions & 0 deletions tests/test_url_processing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# test_url_processing.py

import os
import pytest
from unittest.mock import patch, AsyncMock
from app.url_processing import process_url_request, transform_tiktok_url

@pytest.mark.asyncio
async def test_tiktok_url_processing():
test_url = "https://vt.tiktok.com/ZSYFmgDeS/"
expected_file_path = "/tmp/url-fairy-bot-cache/https___www_tiktok_com___video_7371578973689482539.mp4"
transformed_url = transform_tiktok_url("https://www.tiktok.com/video/7371578973689482539")

# Mock follow_redirects to return the final TikTok URL format
with patch("app.url_processing.follow_redirects", return_value="https://www.tiktok.com/video/7371578973689482539"):
# Mock yt_dlp_download to simulate successful download
with patch("app.url_processing.yt_dlp_download", return_value=expected_file_path) as mock_download:
result = await process_url_request(test_url)

# Assertions
mock_download.assert_called_once_with(transformed_url)
assert result == expected_file_path

0 comments on commit 8ec3159

Please sign in to comment.