diff --git a/server/src/main.py b/server/src/main.py
index 91792ba..25eb52a 100644
--- a/server/src/main.py
+++ b/server/src/main.py
@@ -1,16 +1,14 @@
-import re
-import requests
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from typing import Optional
from urllib.parse import urlparse
+from unshorten import unshorten_twitter
-SHORTEN_DOMAINS = [
- 't.co'
-]
+UNSHORTENERS = {
+ 't.co': unshorten_twitter
+}
app = FastAPI(docs_url=None, redoc_url=None)
-
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
@@ -20,21 +18,6 @@ app.add_middleware(
)
-def unshorten_url(url: str):
- 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"}
- )
-
- match = pattern.search(response.text)
- if match:
- return match.group(1)
- else:
- return None
-
-
@app.get('/')
async def receive_url(url: Optional[str] = None):
if url is None:
@@ -42,9 +25,7 @@ async def receive_url(url: Optional[str] = None):
domain = urlparse(url).netloc
- if domain not in SHORTEN_DOMAINS:
- return {"error": f"cannot shorten {url}"}
-
- unshortened = unshorten_url(url)
-
- return {"result": unshortened}
+ if domain not in UNSHORTENERS:
+ return {"error": f"cannot unshorten {domain}"}
+ else:
+ return UNSHORTENERS[domain](url)
diff --git a/server/src/unshorten.py b/server/src/unshorten.py
new file mode 100644
index 0000000..cfe10b7
--- /dev/null
+++ b/server/src/unshorten.py
@@ -0,0 +1,17 @@
+import re
+import requests
+
+
+def unshorten_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"}
+ )
+
+ match = pattern.search(response.text)
+ if match:
+ return match.group(1)
+ else:
+ return None