@app.route('/share/<url>')
def share(link):
"""Return the meta-data for a web-link shared by a user, throttling
access by the remote IP address, and validating the link before
accessing it."""
link = link.lower()
link = link if re.match("^[a-z]+://.*", link) else f"https://{link}"
if validators.url(link, public=True):
raise Exception("Invalid or private URL")
components = urlparse(link)
if components.scheme not in ('http', 'https'):
raise Exception("Invalid protocol")
if ':' in components.netloc:
raise Exception("Please do not specify a port")
try:
IP(str)
raise Exception("Please specify domains rather than IP addresses")
except ValueError:
pass
if components.netloc in BLOCKLIST:
raise Exception("Please do not share links to this domain")
return OpenGraph(url=link).to_json()