Skip to content

Commit

Permalink
2024-10-27 17:00:48+04:00
Browse files Browse the repository at this point in the history
  • Loading branch information
nett00n committed Oct 27, 2024
1 parent 1149fcd commit f9a7330
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
35 changes: 22 additions & 13 deletions app/url_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,45 @@
async def process_url_request(url: str) -> str:
try:
final_url = follow_redirects(url)
if "youtube.com/shorts" in final_url:
sanitized_url = final_url
elif "tiktok" in final_url:
sanitized_url = final_url
else:
return f"Unsupported URL {final_url}"

video_path = await yt_dlp_download(sanitized_url)
video_path = await yt_dlp_download(final_url)

if video_path:
return (
response = (
"[Click here to ⏯️ Watch or ⏬ Download]"
+ f"(https://{settings.BASE_URL}{video_path})\n\n"
+ f"[📎 Original]({final_url})\n"
)
else:
rewritten_url = final_url.replace("tiktok.com/", "tfxktok.com/")
return (
rewrite_map = {
"https://tiktok.com/": "https://tfxktok.com/",
"https://twitter.com/": "https://www.fxtwitter.com/",
"https://www.instagram.com/p/": "https://www.ddinstagram.com/p/",
"https://www.instagram.com/reel/": "https://www.ddinstagram.com/reel/",
"https://www.twitter.com/": "https://www.fxtwitter.com/",
"https://x.com/": "https://www.fxtwitter.com/",
}
rewritten_url = final_url
for original, rewrite in rewrite_map.items():
if original in final_url:
rewritten_url = final_url.replace(original, rewrite)
break
response = (
"I failed to download the file by myself"
+ "\n\nHere is the link,"
+ f" which Telegram parses better: [📎]({rewritten_url})"
+ f"\n[📎 Original]({final_url})\n"
)

return {"status": "success", "data": response}

except requests.exceptions.RequestException as req_e:
logger.error(f"Request error processing URL: {req_e}")
return f"Request error: {str(req_e)}"
return {"status": "error", "data": f"Request error: {str(req_e)}"}
except Exception as e:
logger.error(f"Error processing URL: {e}")
return f"An error occurred: {str(e)}"
return {"status": "error", "data": f"An error occurred: {str(e)}"}




def follow_redirects(url: str, timeout=10) -> str:
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ python-dotenv = "^1.0.1"
requests = "^2.32.3"
yt-dlp = "^2024.5.27"
pydantic-settings = "^2.6.0"
pyyaml = "^6.0.2"

[tool.poetry.group.dev.dependencies]
black = "^24.4.2"
Expand Down
51 changes: 51 additions & 0 deletions tests/test_messages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# test_messages.yml

---
- request: "https://vt.tiktok.com/ZSYFmgDeS/"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://vt.tiktok.com/ZSYFmgDeS/"}'
name: Normal Tiktok Video Short URL (CAN EXPIRE!)
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___www_tiktok_com___video_7371578973689482539.mp4)\n\n[📎 Original](https://www.tiktok.com/@/video/7371578973689482539)\n"}'
- request: "https://www.tiktok.com/@annethegnome/video/7318093559800827142"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.tiktok.com/@annethegnome/video/7318093559800827142"}'
name: Normal Tiktok Video Long URL
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___www_tiktok_com__annethegnome_video_7318093559800827142.mp4)\n\n[📎 Original](https://www.tiktok.com/@annethegnome/video/7318093559800827142)\n"}'
- request: "https://www.tiktok.com/@bdccarpenter/video/7315528977559325984"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.tiktok.com/@bdccarpenter/video/7315528977559325984"}'
name: Hard Tiktok Video Long URL
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___www_tiktok_com__bdccarpenter_video_7315528977559325984.mp4)\n\n[📎 Original](https://www.tiktok.com/@bdccarpenter/video/7315528977559325984)\n"}'
- request: "https://www.tiktok.com/@lkahaobr9y4/video/72520015795223"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.tiktok.com/@lkahaobr9y4/video/72520015795223"}'
name: Fail Tiktok Video Long URL
response: '{"status":"success","data":"I failed to download the file by myself\n\nHere is the link, which Telegram parses better: [📎](https://www.tiktok.com/@lkahaobr9y4/video/72520015795223)\n[📎 Original](https://www.tiktok.com/@lkahaobr9y4/video/72520015795223)\n"}'
- request: "https://www.tiktok.com/@vladimirraevskiy7/video/7316250241890635015"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.tiktok.com/@vladimirraevskiy7/video/7316250241890635015"}'
name: Big Tiktok Video Long URL
response: '{"status":"success","data":"I failed to download the file by myself\n\nHere is the link, which Telegram parses better: [📎](https://www.tiktok.com/@vladimirraevskiy7/video/7316250241890635015)\n[📎 Original](https://www.tiktok.com/@vladimirraevskiy7/video/7316250241890635015)\n"}'
- request: "https://twitter.com/leonov_mr/status/1772299904179708228?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://twitter.com/leonov_mr/status/1772299904179708228?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"}'
name: Twitter link with video
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___twitter_com_leonov_mr_status_1772299904179708228.mp4)\n\n[📎 Original](https://twitter.com/leonov_mr/status/1772299904179708228)\n"}'
- request: "https://www.twitter.com/leonov_mr/status/1772299904179708228?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.twitter.com/leonov_mr/status/1772299904179708228?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"}'
name: Twitter link with video 2
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___twitter_com_leonov_mr_status_1772299904179708228.mp4)\n\n[📎 Original](https://twitter.com/leonov_mr/status/1772299904179708228)\n"}'
- request: "https://x.com/leonov_mr/status/1772299904179708228?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://x.com/leonov_mr/status/1772299904179708228?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"}'
name: x.com link
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___x_com_leonov_mr_status_1772299904179708228.mp4)\n\n[📎 Original](https://x.com/leonov_mr/status/1772299904179708228)\n"}'
- request: "https://x.com/nett00n/status/1504718261354704898"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://x.com/nett00n/status/1504718261354704898?s=12&t=zeCYafL-E7Tm4xrhQdmb9Q"}'
name: Twitter link without video
response: '{"status":"success","data":"I failed to download the file by myself\n\nHere is the link, which Telegram parses better: [📎](https://www.fxtwitter.com/nett00n/status/1504718261354704898)\n[📎 Original](https://x.com/nett00n/status/1504718261354704898)\n"}'
- request: "https://www.instagram.com/reel/C3PulgmobBO/?igsh=MXZqc2ljbHRuNTRmMg=="
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.instagram.com/reel/C3PulgmobBO/?igsh=MXZqc2ljbHRuNTRmMg=="}'
name: Instagram reel
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___www_instagram_com_reel_C3PulgmobBO_.mp4)\n\n[📎 Original](https://www.instagram.com/reel/C3PulgmobBO/)\n"}'
- request: "https://www.instagram.com/p/C4GhFgSpYd6/"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.instagram.com/p/C4GhFgSpYd6/"}'
name: Instagram post with video
response: '{"status":"success","data":"[Click here to ⏯️ Watch or ⏬ Download](https://url-fairy-bot-dev.nixos.nett00n.org/tmp/url-fairy-bot-cache/https___www_instagram_com_p_C4GhFgSpYd6_.mp4)\n\n[📎 Original](https://www.instagram.com/p/C4GhFgSpYd6/)\n"}'
- request: "https://www.instagram.com/p/Cii3hCFhrpj/"
# curl -X POST "http://localhost:8000/process_url/" -H "Content-Type: application/json" -d '{"url": "https://www.instagram.com/p/Cii3hCFhrpj/"}'
name: Instagram post with photo
response: '{"status":"success","data":"I failed to download the file by myself\n\nHere is the link, which Telegram parses better: [📎](https://www.ddinstagram.com/p/Cii3hCFhrpj/)\n[📎 Original](https://www.instagram.com/p/Cii3hCFhrpj/)\n"}'

0 comments on commit f9a7330

Please sign in to comment.