плагин яндекс погоды

 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
46
47
48
49
# ***
# Plugin file
# ***
#$ neutron_plugin 01
# -*- coding: utf-8 -*-
from libpy.parser.yandex_weather import YandexWeather
def handler_yandex_weather(type, source, place):
weather = YandexWeather().get(place).result
if not weather:
weather = 'Not found'
smsg(type, source, weather)
register_command_handler(handler_yandex_weather, '!yaw', 0, 'Weather by yandex.', '!yaw <place name>', ['!yaw Омск'])
# ***
#yandex_weather file
# ***
from BeautifulSoup import BeautifulSoup
import re
from urllib import quote_plus
from libpy.html import html2text
from libpy.parser.parser2 import Parser2
from libpy.net.grab import Grab
class YandexWeather(Parser2):
def _get(self, place):
place = quote_plus(place.encode('cp1251'))
url = 'http://weather.yandex.ru/search.xml?text=%s' % place
grab = Grab()
grab.setup({'url': url,
'redirect': False,
'nobody': True})
grab.run()
header = grab.headers.get('Location', '')
match = re.search('city=(\d+)', header)
if not match:
raise Exception('Unknown place')
place_id = match.group(1)
data = self.fetch('http://weather.yandex.ru/?city=%s' % place_id)
soup = BeautifulSoup(data)
div = soup.find(attrs={'class': 'b-current-weather'})
return html2text(unicode(div))