Compare commits

..

No commits in common. "master" and "0.1" have entirely different histories.
master ... 0.1

11 changed files with 28 additions and 124 deletions

2
.gitignore vendored
View File

@ -1,8 +1,6 @@
.idea
venv
extension/build
__pycache__
*.py[cod]

View File

@ -1,24 +1,2 @@
# Unshortener
# Unshorten
Firefox Extension and API server for revealing links behind shortened URLs.
**Supported sites**:
- t.co
- tinyurl.com
## Usage
In order to deal with CORS, Unshorten must send links to a resolver API.
```shell
cd server
make build
docker -d --name unshortener -p 8000:8000 unshortener-api
```
Build the extension and import into Firefox. Right click on a link and choose
"Unshorten Link". The result will be copied to the clipboard.
```shell
cd extension
make zip
```

View File

@ -6,11 +6,9 @@ VERSION := $(shell jq -r '.version' $(SRC_DIR)/manifest.json)
ZIP_FILE := $(EXTENSION_NAME)-$(VERSION).zip
all: zip
zip:
@mkdir -p $(BUILD_DIR)
zip -r $(BUILD_DIR)/$(ZIP_FILE) $(SRC_DIR) -x "*.DS_Store"
clean:
rm -rf $(BUILD_DIR)

View File

@ -1,14 +1,2 @@
# Unshortener Extension
A Firefox extension to reveal links behind URL shorteners like Twitter and TinyURL.
**Supported sites:**
- t.co
- tinyurl.com
## Usage
Build the extension and install it in Firefox. Right-clicking links will reveal a new
"Unshorten link" option which will resolve the link and copy it to the clipboard.
```shell
make zip
```
# Unshorten
A Firefox extension to unshorten links from sites like Twitter.

Binary file not shown.

View File

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

View File

@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Unshortener",
"version": "0.3",
"name": "Unshorten",
"version": "0.1",
"description": "Unshorten links from Twitter.",
@ -18,7 +18,6 @@
"permissions": [
"activeTab",
"clipboardWrite",
"contextMenus"
]
}

View File

@ -1,15 +0,0 @@
IMAGE_NAME=unshortener-api
CONTAINER_NAME=unshortener
CONTAINER_PORT=8000
build:
docker build -t $(IMAGE_NAME) .
run:
docker run --name $(CONTAINER_NAME) -d -p $(CONTAINER_PORT):8000 $(IMAGE_NAME)
stop:
docker stop $(CONTAINER_NAME)
clean:
docker rm $(CONTAINER_NAME)
clean-image:
docker rmi $(IMAGE_NAME)

View File

@ -1,14 +1,2 @@
# Unshortener Resolver API
Simple FastAPI app to resolve links behind shortened URLs.
**Supported sites**:
- t.co
- tinyurl.com
## Usage
Build and run the Docker container:
```shell
make build
docker -d --name unshortener -p 8000:8000 unshortener-api
```
# Unshorten Server
Simple FastAPI app to unshorten URLs.

View File

@ -3,15 +3,12 @@ from typing import Optional
from urllib.parse import urlparse
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from unshorteners import unshorten_twitter, unshorten_tinyurl
from unshorteners import unshorten_twitter
UNSHORTEN = {
't.co': unshorten_twitter,
'tinyurl.com': unshorten_tinyurl
UNSHORTENERS = {
't.co': unshorten_twitter
}
CACHE = {}
app = FastAPI(docs_url=None, redoc_url=None)
app.add_middleware(
CORSMiddleware,
@ -29,15 +26,7 @@ async def receive_url(url: Optional[str] = None):
return {"error": "no url provided"}
domain = urlparse(url).netloc
if domain not in UNSHORTEN:
if domain not in UNSHORTENERS:
return {"error": f"cannot unshorten {domain}"}
if url in CACHE:
return CACHE[url]
result = UNSHORTEN[domain](url)
if result:
CACHE[url] = result
return result
return {"error": "server error"}
return UNSHORTENERS[domain](url)

View File

@ -1,27 +1,12 @@
"""Unshortening functions"""
import re
from typing import Optional
import requests
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]:
def unshorten_twitter(url):
"""Retrieve the actual URL behind a Twitter URL."""
pattern = re.compile(r"<title>(.*?)<\/title>")
try:
response = requests.get(
url=url,
headers={
@ -29,8 +14,6 @@ def unshorten_twitter(url: str) -> Optional[str]:
},
timeout=4
)
except requests.RequestException:
return None
match = pattern.search(response.text)
if match: