Exemple #1
0
def translate(what, src=None, target="en"):
	url = "https://www.googleapis.com/language/translate/v2?key=%s&target=%s" % (urllib.parse.quote(cfg.get("key")), target);
	if src is not None:
		url += "&source=" + urllib.parse.quote(src);
	url += "&q=" + urllib.parse.quote(what)

	f = urllib.request.urlopen(url);
	data = json.loads(f.read().decode("utf-8"))

	if "detectedSourceLanguage" in data["data"]["translations"][0]:
		return data["data"]["translations"][0]["detectedSourceLanguage"], data["data"]["translations"][0]["translatedText"]
	elif src is not None:
		return src, unescapeHtml(
				data["data"]["translations"][0]["translatedText"])
	else:
		return "unk", unescapeHtml(
				data["data"]["translations"][0]["translatedText"])
Exemple #2
0
def showTitle(ctx, url):
	ytMatch = ytRegEx.match(url)
	if ytMatch:
		showYouTube(ctx, ytMatch.group(4))
		return

	twitMatch = twitterRegEx.match(url)
	if twitMatch:
		showTwitter(ctx, twitMatch.group(3))
		return

	
	s = pretty_url(url)

	encoding = None
	
	try:
		u = get_url(url)
		stuff = u.read(READ_SIZE)
		newurl = u.url
		mime = u.info().get_content_type()
		encoding = u.info().get_param("charset")
		u.close()
	except urllib.error.HTTPError as e:
		# Intentionally not adding this to the archive, no point spamming unparsable URLs
		s += " • Failed to get information, HTTP Error: %d." % e.code
		ctx.reply(s, "URL")
		return
	except urllib.error.URLError as e:
		s += " • Failed to get information: URL Error: %s." % e.reason
		ctx.reply(s, "URL")
		return

	if encoding is None:
		encoding = "utf-8"
	
	# Look again after reading it in, to see if it is a shortened youtube url.

	m = ytRegEx.match(newurl)
	if m is not None:
		showYouTube(ctx, m.group(4))
		return

	twitMatch = twitterRegEx.match(newurl)
	if twitMatch:
		showTwitter(ctx, twitMatch.group(3))
		return

	if pretty_url(newurl) != pretty_url(url):
		s += " • Redirects to: %s" % pretty_url(newurl)

	if mime not in titleMimes:
		s += " • MIME type: %s" % mime

	if mime in titleMimes:
		stuff = stuff.decode(encoding)

		titleSearch = titleRegEx.search(stuff)
		if titleSearch is not None:
			title = titleSearch.group(1)
			title = title.replace("\n", "").replace("\r", "").replace("\t", " ")
			title = title.strip()

			s += " • Title: %s" % unescapeHtml(title)
		else:
			s += " • Could not find title."

	addStatusToArchive(ctx, s, "URL")

	ctx.reply(s, "URL")
Exemple #3
0
def showTitle(ctx, url):
    ytMatch = ytRegEx.match(url)
    if ytMatch:
        showYouTube(ctx, ytMatch.group(4))
        return

    twitMatch = twitterRegEx.match(url)
    if twitMatch:
        showTwitter(ctx, twitMatch.group(3))
        return

    appleMatch = appleRegEx.match(url)
    if appleMatch:
        showApple(ctx, appleMatch.group(1))
        return

    s = pretty_url(url)

    encoding = None

    try:
        u = get_url(url)
        stuff = u.read(READ_SIZE)
        newurl = u.url
        mime = u.info().get_content_type()
        encoding = u.info().get_param("charset")
        u.close()
    except urllib.error.HTTPError as e:
        # Intentionally not adding this to the archive, no point spamming unparsable URLs
        s += " • Failed to get information, HTTP Error: %d." % e.code
        ctx.reply(s, "URL")
        return
    except urllib.error.URLError as e:
        s += " • Failed to get information: URL Error: %s." % e.reason
        ctx.reply(s, "URL")
        return

    if encoding is None:
        encoding = "utf-8"

    # Look again after reading it in, to see if it is a shortened youtube url.

    m = ytRegEx.match(newurl)
    if m is not None:
        showYouTube(ctx, m.group(4))
        return

    twitMatch = twitterRegEx.match(newurl)
    if twitMatch:
        showTwitter(ctx, twitMatch.group(3))
        return

    if pretty_url(newurl) != pretty_url(url):
        s += " • Redirects to: %s" % pretty_url(newurl)

    if mime not in titleMimes:
        s += " • MIME type: %s" % mime

    if mime in titleMimes:
        stuff = stuff.decode(encoding)

        titleSearch = titleRegEx.search(stuff)
        if titleSearch is not None:
            title = titleSearch.group(1)
            title = title.replace("\n", "").replace("\r",
                                                    "").replace("\t", " ")
            title = title.strip()

            s += " • Title: %s" % unescapeHtml(title)
        else:
            s += " • Could not find title."

    addStatusToArchive(ctx, s, "URL")

    ctx.reply(s, "URL")