Compare commits
	
		
			No commits in common. "73a80f6d3df706f5094c830f5c2966c211ff5761" and "9afc7b1298f4a7b2eaa712e5393e9d56024095b4" have entirely different histories.
		
	
	
		
			73a80f6d3d
			...
			9afc7b1298
		
	
		
@ -1,6 +1,5 @@
 | 
			
		||||
const shortenerDomains = [
 | 
			
		||||
    "t.co",
 | 
			
		||||
    "tinyurl.com"
 | 
			
		||||
    "t.co"
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
browser.contextMenus.create({
 | 
			
		||||
@ -32,14 +31,13 @@ function unshortenUrl(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)});
 | 
			
		||||
        .catch(err => console.error("couldn't copy to clipboard", err));
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
  "manifest_version": 2,
 | 
			
		||||
  "name": "Unshortener",
 | 
			
		||||
  "version": "0.3",
 | 
			
		||||
  "version": "0.2",
 | 
			
		||||
 | 
			
		||||
  "description": "Unshorten links from Twitter.",
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -3,11 +3,10 @@ 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
 | 
			
		||||
    't.co': unshorten_twitter
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CACHE = {}
 | 
			
		||||
@ -33,11 +32,9 @@ async def receive_url(url: Optional[str] = None):
 | 
			
		||||
        return {"error": f"cannot unshorten {domain}"}
 | 
			
		||||
 | 
			
		||||
    if url in CACHE:
 | 
			
		||||
        return CACHE[url]
 | 
			
		||||
        unshortened = CACHE[url]
 | 
			
		||||
    else:
 | 
			
		||||
        unshortened = UNSHORTEN[domain](url)
 | 
			
		||||
        CACHE[url] = unshortened
 | 
			
		||||
 | 
			
		||||
    result = UNSHORTEN[domain](url)
 | 
			
		||||
    if result:
 | 
			
		||||
        CACHE[url] = result
 | 
			
		||||
        return result
 | 
			
		||||
 | 
			
		||||
    return {"error": f"server error"}
 | 
			
		||||
    return unshortened
 | 
			
		||||
 | 
			
		||||
@ -1,27 +1,12 @@
 | 
			
		||||
"""Unshortening functions"""
 | 
			
		||||
import re
 | 
			
		||||
import requests
 | 
			
		||||
from typing import Optional
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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: str):
 | 
			
		||||
    """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:
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user