rendered paste body# -*- coding: utf-8 -*-from xml.parsers import expatfrom urllib import FancyURLopenerimport pickleclass Weather(): weatherBase = list() def showWeather(self, channel, connection, args, db, *params): self.code = self.getCodeOfCity(args[1]) self.city = args[1] if self.code != "": try: self.setOutputText(db) connection.send("PRIVMSG %s %s\r\n" % (channel, self.messagesLines[0])) connection.send("PRIVMSG %s %s\r\n" % (channel, self.messagesLines[1])) connection.send("PRIVMSG %s %s\r\n" % (channel, self.messagesLines[2])) connection.send("PRIVMSG %s %s\r\n" % (channel, self.messagesLines[3])) except: connection.send("PRIVMSG %s Problems with obtaining weather.\r\n" % (channel)) else: connection.send("PRIVMSG %s City not found.\r\n" % (channel)) def setOutputText(self, db): self.messagesLines = [] self.setWeatherBase() pos = 1 str = "" for time in self.weatherBase: if len(self.messagesLines) == 0: self.messagesLines.append(("Погода на " + self.weatherBase[0]["FORECAST"]["day"] + "." + self.weatherBase[0]["FORECAST"]["month"] \ + "." + self.weatherBase[0]["FORECAST"]["year"] + \ " (" + self.city + "), " + db.get("tod" + self.weatherBase[0]["FORECAST"]["tod"]) + ": " + self.weatherBase[0]["TEMPERATURE"]["min"] + \ "..." + self.weatherBase[0]["TEMPERATURE"]["max"] + " °C, " + db.get("cloudiness" + self.weatherBase[0]["PHENOMENA"]["cloudiness"]) + ", " \ + db.get("precipitation" + self.weatherBase[0]["PHENOMENA"]["precipitation"]) + ", Ветер: " + self.weatherBase[0]["WIND"]["min"] + "-" \ + self.weatherBase[0]["WIND"]["max"] + " м/с, " + db.get("direction" + self.weatherBase[0]["WIND"]["direction"]) + ";").decode("utf-8").encode("cp1251")) else: self.messagesLines.append((db.get("tod" + self.weatherBase[pos]["FORECAST"]["tod"]) + ": " + self.weatherBase[pos]["TEMPERATURE"]["min"] + \ "..." + self.weatherBase[pos]["TEMPERATURE"]["max"] + " °C, " + db.get("cloudiness" + self.weatherBase[pos]["PHENOMENA"]["cloudiness"]) + ", " \ + db.get("precipitation" + self.weatherBase[pos]["PHENOMENA"]["precipitation"]) + ", Ветер: " + self.weatherBase[pos]["WIND"]["min"] + "-" \ + self.weatherBase[pos]["WIND"]["max"] + " м/с, " + db.get("direction" + self.weatherBase[pos]["WIND"]["direction"]) + ";").decode("utf-8").encode("cp1251")) pos += 1 def getInformerXml(self): informXml = FancyURLopener().open("http://92.241.171.120:80/xml/" + self.code + "_1.xml") self.str = informXml.read(); informXml.close() def setWeatherBase(self): base = list() baseElem = dict() self.getInformerXml() def start_element(name, attrs): name.strip() if (name != "MMWEATHER") and (name != "REPORT") and (name != "TOWN"): if name == "FORECAST": baseElem[name] = attrs base.append(dict(baseElem)) else: base[len(base) - 1][name] = attrs p = expat.ParserCreate() p.returns_unicode = 0 p.StartElementHandler = start_element p.Parse(self.str) self.weatherBase = list(base) def getCodeOfCity(self, city): codesFile = open("./codes.db", "r") codesDB = pickle.load(codesFile) try: code = codesDB[city][1:] codesFile.close() return code except: codesFile.close() return ""