coding utf-8 import re import urllib2 from BotProcessor import BotProc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
import re
import urllib2
from BotProcessor import BotProcessor
from BeautifulSoup import BeautifulStoneSoup
class WeatherProcessor(BotProcessor):
answerPub = 1 #1 default
protocol = '' #protocol name
lowLevel = 0 # 1 if lowlevel for protocol
url = "http://informer.gismeteo.ru/rss/%s.xml"
def __init__(self):
pass
def process(self, text, connection, frm):
if text[0] in ['$','!', '#']:
if text[1:1+len('weather')] == 'weather':
tmp = text.split(' ')
if len(tmp) == 1:
return ('','')
city = tmp[1]
try:
res = urllib2.urlopen(self.url % city).read()
rss = BeautifulStoneSoup(res)
pattern_title = re.compile('<title>(.*): (.*),(.*)</title>', re.S)
pattern_descr = re.compile('<description>(.*)</description>', re.S)
items = rss.findAll("item")
city_name = pattern_title.findall(unicode(items[0].title))
string = "*%s*\r\n" % city_name[0][0]
for item in items:
title = pattern_title.findall(unicode(item.title))
descr = pattern_descr.findall(unicode(item.description))
string += "*%s, %s*\r\n" % (title[0][1], title[0][2])
string += "%s\r\n" % descr[0]
return (frm,string)
except:
return (frm,"/me говорит всем человекам: этот город уже уничтожен. Ха-ха-ха, ха-ха-ха, ха-ха-ха! (дьявольский смех)")
return (frm,'')