Compare commits

..

2 Commits

Author SHA1 Message Date
73a80f6d3d add support for TinyURL 2024-04-13 23:03:50 -04:00
e5b701aa55 update console messages 2024-04-13 22:29:15 -04:00
4 changed files with 42 additions and 20 deletions

View File

@ -1,5 +1,6 @@
const shortenerDomains = [ const shortenerDomains = [
"t.co" "t.co",
"tinyurl.com"
]; ];
browser.contextMenus.create({ browser.contextMenus.create({
@ -31,13 +32,14 @@ function unshortenUrl(linkUrl) {
fetch("http://localhost:8000/?url=" + linkUrl) fetch("http://localhost:8000/?url=" + linkUrl)
.then(res => { .then(res => {
if (!res.ok) { if (!res.ok) {
console.log("error fetching result"); console.log("Couldn't unshorten URL: " + res.statusText);
} }
return res.json(); return res.json();
}) })
.then(unshortenedUrl => { .then(unshortenedUrl => {
console.log("unshortened: " + unshortenedUrl) console.log("unshortened: " + unshortenedUrl)
navigator.clipboard.writeText(unshortenedUrl) navigator.clipboard.writeText(unshortenedUrl)
.catch(err => console.error("couldn't copy to clipboard", err)); .catch(err => console.error("Couldn't copy to clipboard: ", err));
}); })
.catch(err => {console.error("Couldn't contact server:", err)});
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Unshortener", "name": "Unshortener",
"version": "0.2", "version": "0.3",
"description": "Unshorten links from Twitter.", "description": "Unshorten links from Twitter.",

View File

@ -3,10 +3,11 @@ from typing import Optional
from urllib.parse import urlparse from urllib.parse import urlparse
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from unshorteners import unshorten_twitter from unshorteners import unshorten_twitter, unshorten_tinyurl
UNSHORTEN = { UNSHORTEN = {
't.co': unshorten_twitter 't.co': unshorten_twitter,
'tinyurl.com': unshorten_tinyurl
} }
CACHE = {} CACHE = {}
@ -32,9 +33,11 @@ async def receive_url(url: Optional[str] = None):
return {"error": f"cannot unshorten {domain}"} return {"error": f"cannot unshorten {domain}"}
if url in CACHE: if url in CACHE:
unshortened = CACHE[url] return CACHE[url]
else:
unshortened = UNSHORTEN[domain](url)
CACHE[url] = unshortened
return unshortened result = UNSHORTEN[domain](url)
if result:
CACHE[url] = result
return result
return {"error": f"server error"}

View File

@ -1,19 +1,36 @@
"""Unshortening functions""" """Unshortening functions"""
import re import re
import requests 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.""" """Retrieve the actual URL behind a Twitter URL."""
pattern = re.compile(r"<title>(.*?)<\/title>") pattern = re.compile(r"<title>(.*?)<\/title>")
response = requests.get( try:
url=url, response = requests.get(
headers={ url=url,
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0" headers={
}, "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0"
timeout=4 },
) timeout=4
)
except requests.RequestException:
return None
match = pattern.search(response.text) match = pattern.search(response.text)
if match: if match: