#!/usr/bin/env python3
#
# unshorten - unshorten a short URL by using the unshorten.me service
# curl "https://unshorten.me/s/$1"

# Gets the final path of a gogle redirect link

from urllib import parse
import sys
import pyperclip

if len(sys.argv) not in [1, 2]:
    print(f"Usage: {sys.argv[0]} [url]")

clipboard = False
if len(sys.argv) == 1:
    url: str = pyperclip.paste()  # type: ignore
    if type(url) != str:
        sys.exit(1)
    clipboard = True
else:
    url = sys.argv[1]

parsed = parse.urlparse(url)
qs = parsed.query
qs = parse.parse_qs(qs, strict_parsing=True)
dest = qs["url"][0]
if clipboard:
    print('Copied')
    pyperclip.copy(dest)

print(dest)
