/stuff/2013/title.cgi

#!/usr/bin/env python3
# Copyright 2013, Sean B. Palmer
# Source: http://inamidst.com/saxo/

import cgitb
cgitb.enable()

import os
import re
import sys
import urllib.parse

sys.path.append("/var/www/inamidst.com/saxo/src")

import web

regex_link = re.compile(r"^(http[s]?://[^<> \"\x01]+)[,.]?$")
regex_title = re.compile(r"(?ims)<title>(.*?)</title>")
regex_tag = re.compile(r"<[^>]+>")

def longest(input, sep):
    longest = 0
    result = ""
    for part in input.split(sep):
        if len(part) > longest:
           longest = len(part)
           result = part
    return result

blacklist = (
    "github.com",
    "swhack.com",
    "translate.google.com",
    "tumbolia.appspot.com",
    "twitter.com",
    "wikia.com",
    "wikipedia.org"
)

def title(text):
    search = regex_link.match(text)
    if not search:
        return

    arg = search.group(1)
    if "#" in arg:
        arg = arg.split("#", 1)[0]

    for blacklisted in blacklist:
        if blacklisted in arg:
            return

    page = web.request(arg, limit=262144)
    search = regex_title.search(page["text"])
    if search:
        title = search.group(1)
        title = regex_tag.sub("", title)
        title = title.replace("\r", "")
        title = title.replace("\n", "")

        title = longest(title, " : ")
        title = longest(title, " | ")
        title = longest(title, "| ")
        title = longest(title, " — ")
        if "youtube.com" not in arg:
            title = longest(title, " - ")
        elif title.endswith(" - YouTube"):
            title = title[:-10]
        title = title.replace('"', "'")
        sys.stdout.buffer.write(title.strip().encode("utf-8") + b"\n")

if __name__ == "__main__":
    uri = os.environ.get("REQUEST_URI", "")
    sys.stdout.buffer.write(b"Content-Type: text/plain; charset=utf-8\r\n")
    sys.stdout.buffer.write(b"\r\n")
    title(urllib.parse.unquote(uri.split("=").pop()))