コード例 #1
0
ファイル: pbm_wordsapi.py プロジェクト: ckx/pyBurlyBot
def word_synonyms(query):
    if not API_KEY:
        raise ConfigException(
            "Require API_KEY for wordsapi. Reload after setting.")

    data = None
    for d in DICT_ORDER:
        r = Request(SEARCH_URL % (d, quote(query.lower().encode("utf-8"))))
        r.add_header("accessKey", API_KEY)
        try:
            f = urlopen(r)
        except HTTPError:
            continue
        data = load(f)
    if not data: return None

    topics = data.get("topics", [])
    syns = []
    for t in topics:
        tid = t.get("topicId")
        if tid:
            r = Request(SAURUS_URL % (DICT_ORDER[0], tid))
            r.add_header("accessKey", API_KEY)
            data = load(urlopen(r))
            for entry in data['entries']:
                syns.append(entry['entryId'])
    return syns
コード例 #2
0
ファイル: pbm_wordsapi.py プロジェクト: ckx/pyBurlyBot
def word_search(query):
    """ word helper. Returns a dictionary entry."""
    if not API_KEY:
        raise ConfigException(
            "Require API_KEY for wordsapi. Reload after setting.")
    for d in DICT_ORDER:
        r = Request(SEARCH_URL % (d, quote(query.lower().encode("utf-8"))))
        r.add_header("accessKey", API_KEY)
        try:
            f = urlopen(r)
        except HTTPError:
            continue
        data = load(f)
        if 'entryContent' in data:
            #print repr(data['entryContent'])
            data = StringIO(data['entryContent'].encode("utf-8"))
            context = iterparse(data, events=("end", "start"))
            # get the root element
            ievent, root = context.next()
            definitions = []  # [[POS, [defs]],]
            usage = None
            defchild = False
            for ievent, elem in context:
                if ievent == "start" and elem.tag == "def":
                    defchild = True
                elif ievent == "end" and elem.tag == "pos":
                    definitions.append([elem.text, []])
                    elem.clear()
                elif ievent == "end" and elem.tag == "def":
                    t = elem.text
                    if not t:
                        e = elem[-1]
                        if e.tag == "x":
                            for ie in e.iter("f"):
                                t = ie.text
                        else:
                            t = elem[-1].tail.strip(
                            )  # get tail (text) of last nested element if there's no main tag text
                    if not t: t = "???"
                    elif t[-2] == ":": t = t[:-2]
                    if usage:
                        t = "(%s) %s" % (usage, t)
                    definitions[-1][-1].append(t)
                    usage = None
                    defchild = False
                    for child in elem:  # clear children
                        for cchild in child:
                            cchild.clear()
                        child.clear()
                    elem.clear()
                elif ievent == "end" and defchild and elem.tag == "usage":
                    usage = elem.text
                    elem.clear()
                elif ievent == "end" and not defchild:
                    elem.clear()
            root.clear()
            return definitions
    else:
        return None
コード例 #3
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google_youtube_check(id):
	""" helper to ask google if youtube ID is valid."""
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	d = {"id" : quote(id.encode("utf-8")), "part" : "id,status", "key" : API_KEY}
	
	f = urlopen(YOUTUBE_INFO_URL % (urlencode(d)))
	ytdata = load(f)
	if not ytdata.get("items"): # if there are no items for the ID search, return False
		return False
	return True
コード例 #4
0
ファイル: pbm_wordsapi.py プロジェクト: ckx/pyBurlyBot
def spell_check(query, skipSearch=False):
    if not API_KEY:
        raise ConfigException(
            "Require API_KEY for wordsapi. Reload after setting.")
    if not skipSearch and word_search(query):
        return None
    else:
        r = Request(
            DIDYOUMEAN_URL %
            (DICT_ORDER[0], urlencode({"q": query.lower().encode("utf-8")})))
        r.add_header("accessKey", API_KEY)
        return load(urlopen(r))['suggestions']
コード例 #5
0
ファイル: pbm_wuapi.py プロジェクト: ckx/pyBurlyBot
def get_weather(lat, lon):
	""" helper to ask WU for current weather. Includes forecast also!"""
	if not API_KEY:
		raise ConfigException("Require API_KEY for wuapi. Reload after setting.")
	f = urlopen(URL % (API_KEY, "conditions/forecast", lat, lon))
	weather_data = load(f)
	if f.getcode() == 200:
		if "current_observation" in weather_data:
			return weather_data
		else:
			return None
	else:
		raise RuntimeError("Error (%s): %s" % (f.getcode(), weather_data.replace("\n", " ")))
コード例 #6
0
def init(bot):
    global CHAT_THREADS  # oh nooooooooooooooooo
    if bot.getOption("enablestate"):
        if bot.network not in CHAT_THREADS:
            CHAT_THREADS[bot.network] = SteamChat(
                bot.container, bot.getOption("commandprefix"),
                bot.getOption(
                    "allowedModules",
                    module="pbm_steamchat"))  # bit silly, but whatever
        else:
            print "WARNING: Already have thread for (%s) network." % bot.network
    else:
        raise ConfigException('steamchat module requires "enablestate" option')
    return True
コード例 #7
0
ファイル: pbm_wuapi.py プロジェクト: ckx/pyBurlyBot
def get_forecast(lat, lon):
	""" helper to ask WU for forecasted weather."""
	if not API_KEY:
		raise ConfigException("Require API_KEY for wuapi. Reload after setting.")
	f = urlopen(URL % (API_KEY, "forecast", lat, lon))
	weather_data = load(f)
	if f.getcode() == 200:
		if "forecast" in weather_data:
			forecast = weather_data["forecast"]
			return forecast
		else:
			return None
	else:
		raise RuntimeError("Error (%s): %s" % (f.getcode(), weather_data.replace("\n", " ")))
コード例 #8
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google_timezone(lat, lon, t):
	""" helper to ask google for timezone information about a location."""
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	d = { "location" : "%s,%s" % (lat, lon), "key" : API_KEY, "timestamp" : int(t) }
	# I've seen this request fail quite often, so we'll add a retry
	try:
		f = urlopen(TIMEZONE_URL % (urlencode(d)), timeout=1)
	except URLError:
		f = urlopen(TIMEZONE_URL % (urlencode(d)), timeout=2)
	gdata = load(f)
	if f.getcode() == 200:
		return gdata["timeZoneId"], gdata["timeZoneName"], gdata["dstOffset"], gdata["rawOffset"]
	else:
		raise RuntimeError("Error (%s): %s" % (f.getcode(), gdata.replace("\n", " ")))
コード例 #9
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google_youtube_details(vidid):
	""" helper to ask google for youtube video details."""
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	# TODO: make module option for safesearch
	d = {"id" : quote(vidid.encode("utf-8")), "part" : "contentDetails,id,snippet,statistics,status", "key" : API_KEY}
	
	f = urlopen(YOUTUBE_INFO_URL % (urlencode(d)))
	ytdata = load(f)
	if f.getcode() == 200:
		if "items" in ytdata:
			results = ytdata["items"]
			if len(results) == 0:
				return None
			return results[0]
	else:
		raise RuntimeError("Error (%s): %s" % (f.getcode(), ytdata.replace("\n", " ")))
コード例 #10
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google_image(query, num_results):
	""" google image search helper. Will return Google images using the provided query up to num_results results."""
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	d = { "q" : query.encode("utf-8"), "key" : API_KEY, "cx" : CSE_ID, "num" : num_results, "searchType" : "image",
		"fields" : "spelling/correctedQuery,items(title,link)"}
		#TODO: consider displaying img stats like file size and resolution?
	f = urlopen(SEARCH_URL % (urlencode(d)))
	gdata = load(f)
	if f.getcode() == 200:
		results = []
		spelling =  gdata.get("spelling")
		if spelling: spelling = spelling["correctedQuery"]
		if "items" in gdata:
			for item in gdata["items"]:
				results.append((item['title'], item['link']))
		return (spelling, results)
	else:
		raise RuntimeError("Error: %s" % (gdata.replace("\n", " ")))
コード例 #11
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google_geocode(query):
	""" helper to ask google for location data. Returns name, lat, lon"""
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	d = {"address" : query.encode("utf-8"), "key" : API_KEY }
	f = urlopen(LOC_URL % (urlencode(d)))
	locdata = load(f)
	if f.getcode() == 200:
		if "results" in locdata:
			item = locdata["results"]
			if len(item) == 0:
				return None
			item = item[0]
			ll = item.get("geometry", {}).get("location") # lol tricky
			if not ll: return None
			return item["formatted_address"], ll["lat"], ll["lng"]
		else:
			return None
	else:
		raise RuntimeError("Error (%s): %s" % (f.getcode(), locdata.replace("\n", " ")))
コード例 #12
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google(query, num_results=1):
	""" google helper. Will return Google search results using the provided query up to num_results results."""
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	d = { "q" : query.encode("utf-8"), "key" : API_KEY, "cx" : CSE_ID, "num" : num_results,
		"fields" : "spelling/correctedQuery,items(title,link,snippet)" }
	
	f = urlopen(SEARCH_URL % (urlencode(d)))
	gdata = load(f)
	if f.getcode() == 200:
		results = []
		spelling =  gdata.get("spelling")
		if spelling: spelling = spelling["correctedQuery"]
		if "items" in gdata:
			for item in gdata["items"]:
				snippet = item["snippet"].replace(" \n", " ")
				results.append((item["title"], snippet, item["link"]))
		return (spelling, results)
	else:
		raise RuntimeError("Error: %s" % (gdata.replace("\n", " ")))
コード例 #13
0
ファイル: pbm_googleapi.py プロジェクト: ckx/pyBurlyBot
def google_youtube_search(query, relatedTo=None):
	""" helper to ask google for youtube search. returns numresults, results[(title, url)]"""
	# TODO: make module option for safesearch
	if not API_KEY:
		raise ConfigException("Require API_KEY for googleapi. Reload after setting.")
	d = {"q" : query.encode("utf-8"), "part" : "snippet", "key" : API_KEY, "safeSearch" : "none",
		"type" : "video,channel"}
	if relatedTo:
		d["relatedToVideoId"] = relatedTo
	f = urlopen(YOUTUBE_URL % (urlencode(d)))
	ytdata = load(f)
	# TODO: handle "badRequest (400)  invalidVideoId"  for relatedTo
	if f.getcode() == 200:
		numresults = ytdata.get("pageInfo", {}).get("totalResults")
		if "items" in ytdata:
			results = ytdata["items"]
			if len(results) == 0:
				return numresults, []
			return numresults, results
		return numresults, []
	else:
		raise RuntimeError("Error (%s): %s" % (f.getcode(), ytdata.replace("\n", " ")))
コード例 #14
0
ファイル: pbm_autojoinstatus.py プロジェクト: ckx/pyBurlyBot
def init(bot):
    if not bot.getOption("enablestate"):
        raise ConfigException(
            'autjoinstatus module requires "enablestate" option')
    return True