def getwikidata(url): """ fetch wiki data """ try: result = geturl(url) except IOError, ex: logging.error("error fetching %s: %s" % (url, str(ex))) return
def handle_weather(bot, ievent): """ show weather using Google's weather API """ userhost = "" loc = "" try: nick = ievent.rest if nick: userhost = getwho(bot, nick) if not userhost: pass else: try: name = bot.users.getname(userhost) if not name: ievent.reply("%s is not known with the bot" % nick) ; return us = UserState(name) loc = us['location'] except KeyError: ievent.reply("%s doesn't have his location set in userstate" % nick) ; return except KeyError: pass if not loc: if ievent.rest: loc = ievent.rest else: ievent.missing('<nick>|<location>') ; return query = urlencode({'weather':loc}) weathertxt = geturl('http://www.google.ca/ig/api?%s' % query) if 'problem_cause' in weathertxt: logging.error('weather - %s' % weathertxt) ievent.reply('an error occured looking up data for %s' % loc) return logging.debug("weather - got reply: %s" % weathertxt) resultstr = "" if weathertxt: gweather = minidom.parseString(weathertxt) gweather = gweather.getElementsByTagName('weather')[0] if ievent.usercmnd == "weather": info = gweather.getElementsByTagName('forecast_information')[0] if info: city = info.getElementsByTagName('city')[0].attributes["data"].value zip = info.getElementsByTagName('postal_code')[0].attributes["data"].value time = info.getElementsByTagName('current_date_time')[0].attributes["data"].value weather = gweather.getElementsByTagName('current_conditions')[0] condition = weather.getElementsByTagName('condition')[0].attributes["data"].value temp_f = weather.getElementsByTagName('temp_f')[0].attributes["data"].value temp_c = weather.getElementsByTagName('temp_c')[0].attributes["data"].value humidity = weather.getElementsByTagName('humidity')[0].attributes["data"].value try: wind = weather.getElementsByTagName('wind_condition')[0].attributes["data"].value except IndexError: wind = "" try: wind_km = round(int(wind[-6:-4]) * 1.609344) except ValueError: wind_km = "" if (not condition == ""): condition = " Oh, and it's " + condition + "." resultstr = "As of %s, %s (%s) has a temperature of %sC/%sF with %s. %s (%s km/h).%s" % (time, city, zip, temp_c, temp_f, humidity, wind, wind_km, condition) elif ievent.usercmnd == "forecast": forecasts = gweather.getElementsByTagName('forecast_conditions') for forecast in forecasts: condition = forecast.getElementsByTagName('condition')[0].attributes["data"].value low_f = forecast.getElementsByTagName('low')[0].attributes["data"].value high_f = forecast.getElementsByTagName('high')[0].attributes["data"].value day = forecast.getElementsByTagName('day_of_week')[0].attributes["data"].value low_c = round((int(low_f) - 32) * 5.0 / 9.0) high_c = round((int(high_f) - 32) * 5.0 / 9.0) resultstr += "[%s: F(%sl/%sh) C(%sl/%sh) %s]" % (day, low_f, high_f, low_c, high_c, condition) if not resultstr: ievent.reply('%s not found!' % loc) ; return else: ievent.reply(resultstr)
def show(self, bugId): assert bugId.isdigit(), "bug id has to be a number" bugxml = geturl(self.show_url_xml(bugId)) bugdom = xml.dom.minidom.parseString(bugxml) try: bugset = bugdom.getElementsByTagName('bug')[0] except: raise BugTrackerNotFound('could not find bug tag') bugget = { 'creation_ts': 'created', 'product': 'product', 'component': 'component', 'version': 'version', 'bug_status': 'status', 'resolution': 'resolution', 'priority': 'priority', 'bug_severity': 'severity', 'reporter': 'reporter', 'short_desc': 'description', 'assigned_to': 'assigned to' } data = {} for tag in bugget.keys(): try: value = bugset.getElementsByTagName( tag)[0].firstChild.nodeValue data[bugget[tag]] = value except: pass return data
def geturl_validate(url): """ validate url """ url = urlvalidate % urllib.urlencode({'uri': url}) try: result = geturl(url) except IOError, ex: try: errno = ex[0] except IndexError: handle_exception() ; return return False
def geturl_validate(url): """ validate url """ url = urlvalidate % urllib.urlencode({'uri': url}) try: result = geturl(url) except IOError, ex: try: errno = ex[0] except IndexError: handle_exception() return return False
def list(self): bugrss = geturl(self.list_url()) bugdom = xml.dom.minidom.parseString(bugrss) bugall = bugdom.getElementsByTagName('entry') bugs = [] if bugall: for entry in bugall: try: bugid = entry.getElementsByTagName('id')[0].firstChild.nodeValue.split(':')[2] bugs.append(bugid) except: pass return bugs
def list(self): bugrss = geturl(self.list_url()) bugdom = xml.dom.minidom.parseString(bugrss) bugall = bugdom.getElementsByTagName('entry') bugs = [] if bugall: for entry in bugall: try: bugid = entry.getElementsByTagName( 'id')[0].firstChild.nodeValue.split(':')[2] bugs.append(bugid) except: pass return bugs
def markovlearnurl(url): """ learn an url """ lines = 0 logging.warn('learning %s' % url) try: f = geturl(url) for line in f.split('\n'): line = striphtml(line) if lines % 10 == 0: time.sleep(0.01) line = line.strip() if not line: continue markovtalk_learn(line) lines += 1 except Exception, e: logging.error(str(e))
def markovlearnurl(url): """ learn an url """ lines = 0 logging.warn('learning %s' % url) try: f = geturl(url) for line in f.split('\n'): line = striphtml(line) if lines % 10 == 0: time.sleep(0.01) line = line.strip() if not line: continue markovtalk_learn(line) lines += 1 except Exception, e: logging.error(str(e)) logging.warn('learning %s done' % url) return lines
def comments(self, bugId): assert bugId.isdigit(), "bug id has to be a number" bugrss = geturl(self.comments_url(bugId)) bugdom = xml.dom.minidom.parseString(bugrss) bugall = bugdom.getElementsByTagName('item') comments = [] if bugall: for item in bugall: title = item.getElementsByTagName('title')[0].firstChild.nodeValue if 'comment added' in title: try: author = item.getElementsByTagName('dc:creator')[0].firstChild.nodeValue except IndexError: author = 'anonymous' comment = item.getElementsByTagName('description')[0].firstChild.nodeValue comment = striphtml(comment.replace('\n', ' ')).strip() while ' ' in comment: comment = comment.replace(' ', ' ') comments.append('%s: %s' % (author, comment)) return comments
def show(self, bugId): assert bugId.isdigit(), "bug id has to be a number" bugxml = geturl(self.show_url_xml(bugId)) bugdom = xml.dom.minidom.parseString(bugxml) try: bugset = bugdom.getElementsByTagName('bug')[0] except: raise BugTrackerNotFound('could not find bug tag') bugget = {'creation_ts': 'created', 'product': 'product', 'component': 'component', 'version': 'version', 'bug_status': 'status', 'resolution': 'resolution', 'priority': 'priority', 'bug_severity': 'severity', 'reporter': 'reporter', 'short_desc': 'description', 'assigned_to': 'assigned to'} data = {} for tag in bugget.keys(): try: value = bugset.getElementsByTagName(tag)[0].firstChild.nodeValue data[bugget[tag]] = value except: pass return data
def getwikidata(url): """ fetch wiki data """ result = geturl(url) if not result: return res = rsslist(result) txt = "" for i in res: try: logging.debug(unicode(i)) txt = i['text'] break except: pass txt = re.sub('\[\[(.*?)\]\]', '<b>\g<1></b>', txt) txt = re.sub('{{(.*?)}}', '<i>\g<1></i>', txt) txt = re.sub('==(.*?)==', '<h3>\g<1></h3>', txt) txt = re.sub('=(.*?)=', '<h2>\g<1></h2>', txt) txt = re.sub('\*(.*?)\n', '<li>\g<1></li>', txt) txt = re.sub('\n\n', '<br><br>', txt) txt = re.sub('\s+', ' ', txt) txt = txt.replace('|', ' - ') return txt
def comments(self, bugId): assert bugId.isdigit(), "bug id has to be a number" bugrss = geturl(self.comments_url(bugId)) bugdom = xml.dom.minidom.parseString(bugrss) bugall = bugdom.getElementsByTagName('item') comments = [] if bugall: for item in bugall: title = item.getElementsByTagName( 'title')[0].firstChild.nodeValue if 'comment added' in title: try: author = item.getElementsByTagName( 'dc:creator')[0].firstChild.nodeValue except IndexError: author = 'anonymous' comment = item.getElementsByTagName( 'description')[0].firstChild.nodeValue comment = striphtml(comment.replace('\n', ' ')).strip() while ' ' in comment: comment = comment.replace(' ', ' ') comments.append('%s: %s' % (author, comment)) return comments
def handle_weather(bot, ievent): """ show weather using Google's weather API """ userhost = "" loc = "" try: nick = ievent.rest if nick: userhost = getwho(bot, nick) if not userhost: pass else: try: name = bot.users.getname(userhost) if not name: ievent.reply("%s is not known with the bot" % nick) return us = UserState(name) loc = us['location'] except KeyError: ievent.reply( "%s doesn't have his location set in userstate" % nick) return except KeyError: pass if not loc: if ievent.rest: loc = ievent.rest else: ievent.missing('<nick>|<location>') return query = urlencode({'weather': loc}) weathertxt = geturl('http://www.google.com/ig/api?%s' % query) if 'problem_cause' in weathertxt: logging.error('weather - %s' % weathertxt) ievent.reply('an error occured looking up data for %s' % loc) return logging.debug("weather - got reply: %s" % weathertxt) resultstr = "" if weathertxt: try: gweather = minidom.parseString(weathertxt) except xml.parsers.expat.ExpatError: ievent.reply("can't parse response") logging.error("failed to parse %s" % weathertxt) return gweather = gweather.getElementsByTagName('weather')[0] if ievent.usercmnd == "weather": info = gweather.getElementsByTagName('forecast_information')[0] if info: city = info.getElementsByTagName( 'city')[0].attributes["data"].value zip = info.getElementsByTagName( 'postal_code')[0].attributes["data"].value weather = gweather.getElementsByTagName( 'current_conditions')[0] condition = weather.getElementsByTagName( 'condition')[0].attributes["data"].value temp_f = weather.getElementsByTagName( 'temp_f')[0].attributes["data"].value temp_c = weather.getElementsByTagName( 'temp_c')[0].attributes["data"].value humidity = weather.getElementsByTagName( 'humidity')[0].attributes["data"].value try: wind = weather.getElementsByTagName( 'wind_condition')[0].attributes["data"].value except IndexError: wind = "" try: wind_km = round(int(wind[-6:-4]) * 1.609344) except ValueError: wind_km = "" if (not condition == ""): condition = " Oh, and it's " + condition + "." resultstr = "%s (%s) has a temperature of %sC/%sF with %s. %s (%s km/h).%s" % ( city, zip, temp_c, temp_f, humidity, wind, wind_km, condition) elif ievent.usercmnd == "forecast": forecasts = gweather.getElementsByTagName('forecast_conditions') for forecast in forecasts: condition = forecast.getElementsByTagName( 'condition')[0].attributes["data"].value low_f = forecast.getElementsByTagName( 'low')[0].attributes["data"].value high_f = forecast.getElementsByTagName( 'high')[0].attributes["data"].value day = forecast.getElementsByTagName( 'day_of_week')[0].attributes["data"].value low_c = round((int(low_f) - 32) * 5.0 / 9.0) high_c = round((int(high_f) - 32) * 5.0 / 9.0) resultstr += "[%s: F(%sl/%sh) C(%sl/%sh) %s]" % ( day, low_f, high_f, low_c, high_c, condition) if not resultstr: ievent.reply('%s not found!' % loc) return else: ievent.reply(resultstr)
def handle_weather(bot, ievent): """ show weather using Google's weather API """ userhost = "" loc = "" try: nick = ievent.rest if nick: userhost = getwho(bot, nick) if not userhost: pass else: try: name = bot.users.getname(userhost) if not name: ievent.reply("%s is not known with the bot" % nick) return us = UserState(name) loc = us["location"] except KeyError: ievent.reply("%s doesn't have his location set in userstate" % nick) return except KeyError: pass if not loc: if ievent.rest: loc = ievent.rest else: ievent.missing("<nick>|<location>") return query = urlencode({"weather": loc}) weathertxt = geturl("http://www.google.com/ig/api?%s" % query) if "problem_cause" in weathertxt: logging.error("weather - %s" % weathertxt) ievent.reply("an error occured looking up data for %s" % loc) return logging.debug("weather - got reply: %s" % weathertxt) resultstr = "" if weathertxt: try: gweather = minidom.parseString(weathertxt) except xml.parsers.expat.ExpatError: ievent.reply("can't parse response") logging.error("failed to parse %s" % weathertxt) return gweather = gweather.getElementsByTagName("weather")[0] if ievent.usercmnd == "weather": info = gweather.getElementsByTagName("forecast_information")[0] if info: city = info.getElementsByTagName("city")[0].attributes["data"].value zip = info.getElementsByTagName("postal_code")[0].attributes["data"].value weather = gweather.getElementsByTagName("current_conditions")[0] condition = weather.getElementsByTagName("condition")[0].attributes["data"].value temp_f = weather.getElementsByTagName("temp_f")[0].attributes["data"].value temp_c = weather.getElementsByTagName("temp_c")[0].attributes["data"].value humidity = weather.getElementsByTagName("humidity")[0].attributes["data"].value try: wind = weather.getElementsByTagName("wind_condition")[0].attributes["data"].value except IndexError: wind = "" try: wind_km = round(int(wind[-6:-4]) * 1.609344) except ValueError: wind_km = "" if not condition == "": condition = " Oh, and it's " + condition + "." resultstr = "%s (%s) has a temperature of %sC/%sF with %s. %s (%s km/h).%s" % ( city, zip, temp_c, temp_f, humidity, wind, wind_km, condition, ) elif ievent.usercmnd == "forecast": forecasts = gweather.getElementsByTagName("forecast_conditions") for forecast in forecasts: condition = forecast.getElementsByTagName("condition")[0].attributes["data"].value low_f = forecast.getElementsByTagName("low")[0].attributes["data"].value high_f = forecast.getElementsByTagName("high")[0].attributes["data"].value day = forecast.getElementsByTagName("day_of_week")[0].attributes["data"].value low_c = round((int(low_f) - 32) * 5.0 / 9.0) high_c = round((int(high_f) - 32) * 5.0 / 9.0) resultstr += "[%s: F(%sl/%sh) C(%sl/%sh) %s]" % (day, low_f, high_f, low_c, high_c, condition) if not resultstr: ievent.reply("%s not found!" % loc) return else: ievent.reply(resultstr)