From 73a80f6d3df706f5094c830f5c2966c211ff5761 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 13 Apr 2024 23:03:50 -0400 Subject: [PATCH] add support for TinyURL --- extension/src/background.js | 3 ++- extension/src/manifest.json | 2 +- server/src/main.py | 17 ++++++++++------- server/src/unshorteners.py | 33 +++++++++++++++++++++++++-------- 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/extension/src/background.js b/extension/src/background.js index 46c92c6..53942a9 100644 --- a/extension/src/background.js +++ b/extension/src/background.js @@ -1,5 +1,6 @@ const shortenerDomains = [ - "t.co" + "t.co", + "tinyurl.com" ]; browser.contextMenus.create({ diff --git a/extension/src/manifest.json b/extension/src/manifest.json index 437b0c5..1e3e3b9 100644 --- a/extension/src/manifest.json +++ b/extension/src/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Unshortener", - "version": "0.2", + "version": "0.3", "description": "Unshorten links from Twitter.", diff --git a/server/src/main.py b/server/src/main.py index 8b65d11..632de62 100644 --- a/server/src/main.py +++ b/server/src/main.py @@ -3,10 +3,11 @@ from typing import Optional from urllib.parse import urlparse from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from unshorteners import unshorten_twitter +from unshorteners import unshorten_twitter, unshorten_tinyurl UNSHORTEN = { - 't.co': unshorten_twitter + 't.co': unshorten_twitter, + 'tinyurl.com': unshorten_tinyurl } CACHE = {} @@ -32,9 +33,11 @@ async def receive_url(url: Optional[str] = None): return {"error": f"cannot unshorten {domain}"} if url in CACHE: - unshortened = CACHE[url] - else: - unshortened = UNSHORTEN[domain](url) - CACHE[url] = unshortened + return CACHE[url] - return unshortened + result = UNSHORTEN[domain](url) + if result: + CACHE[url] = result + return result + + return {"error": f"server error"} diff --git a/server/src/unshorteners.py b/server/src/unshorteners.py index 942884a..c2a8c41 100644 --- a/server/src/unshorteners.py +++ b/server/src/unshorteners.py @@ -1,19 +1,36 @@ """Unshortening functions""" import re import requests +from typing import Optional -def unshorten_twitter(url: str): +def unshorten_tinyurl(url: str) -> Optional[str]: + """Retrieve the actual URL behind a TinyURL.""" + try: + response = requests.get(url, timeout=4, allow_redirects=False) + except requests.RequestException: + return None + + if response.status_code == 301: + return response.headers.get("location", None) + + return None + + +def unshorten_twitter(url: str) -> Optional[str]: """Retrieve the actual URL behind a Twitter URL.""" pattern = re.compile(r"(.*?)<\/title>") - response = requests.get( - url=url, - headers={ - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0" - }, - timeout=4 - ) + try: + response = requests.get( + url=url, + headers={ + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0" + }, + timeout=4 + ) + except requests.RequestException: + return None match = pattern.search(response.text) if match: