Advertisement
stuppid_bot

Untitled

Jul 15th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. # -*- coding: u8 -*-
  2. from account import *
  3. from vkrobot import *
  4. import xml.etree.ElementTree as ET
  5. import datetime
  6. import urllib
  7.  
  8. rb = VKRobot(login, password, delay_time=1)
  9.  
  10. def get_weather():
  11.     u = 'http://informer.gismeteo.ru/xml/34116_1.xml'
  12.     xml = utils.get_page(u)
  13.     root = ET.fromstring(xml)
  14.     town = root.find('REPORT/TOWN').get('sname')
  15.     town = town.replace('+', ' ')
  16.     town = urllib.unquote(town).decode('1251')
  17.  
  18.     dt = datetime.datetime.now()
  19.  
  20.     for forecast in root.findall('REPORT/TOWN/FORECAST'):
  21.         # for k in forecast.keys():
  22.        #     print k, '=', forecast.get(k)
  23.        h = forecast.get('hour')
  24.         if dt.hour < int(h):
  25.             break
  26.     tod = [
  27.         u'ночь',
  28.         u'утро',
  29.         u'день',
  30.         u'вечер'
  31.     ][int(forecast.get('tod'))]
  32.     status = town + ' | ' + tod.title() + ' | '
  33.     phenomena = forecast.find('PHENOMENA')
  34.     # облачность
  35.    status += [
  36.         u'☀',
  37.         u'⛅',
  38.         u'☁',
  39.         '',
  40.     ][ int( phenomena.get('cloudiness') ) ]
  41.     # тип осадков: 4 - дождь, 5 - ливень, 6,7 – снег, 8 - гроза, 9 - нет данных, 10 - без осадков
  42.    status += {
  43.         4: u'💦',
  44.         5: u'☔',
  45.         6: u'❄',
  46.         7: u'❄',
  47.         8: u'⚡',
  48.         9: 'n/a',
  49.         10: ''
  50.     }[ int( phenomena.get('precipitation') ) ]
  51.     format_temperature = lambda t: re.sub(r'^(\d)', '+\\1', t)
  52.     status += ' ' + format_temperature(  forecast.find('TEMPERATURE').get('min') )
  53.     status += '..' + format_temperature(  forecast.find('TEMPERATURE').get('max') ) + u'°C'
  54.     status += u' | 💧 ' + forecast.find('RELWET').get('min') + '-' + forecast.find('RELWET').get('max') + '%'
  55.     status += u' | Ветер: ' + forecast.find('WIND').get('min') + '-' + forecast.find('WIND').get('max') + u' м/с '
  56.     return status
  57.  
  58. def count_messages():
  59.     s = u'✉ Сообщения | 📥 Входящие: '
  60.     r = rb.messages_get(count=0)
  61.     s += str(r['count'])
  62.     r = rb.messages_get(count=0, out=1)
  63.     s += u' | 📤 Исходящие: '
  64.     s += str(r['count'])
  65.     return s
  66.  
  67. def get_rate():
  68.     L = []
  69.     u = 'http://www.cbr.ru/scripts/XML_daily.asp'
  70.     d = utils.get_page(u)
  71.     r = ET.fromstring(d)
  72.     for e in r.findall('Valute'):
  73.         cod = e.find('CharCode').text
  74.         if  cod in ['USD', 'EUR', 'CNY']:
  75.             L.append( cod + ' ' + e.find('Nominal').text + ':' + e.find('Value').text )
  76.     return u'💵 Курс к RUB | ' + ' | '.join(L)
  77.  
  78. # def get_quote():
  79. #     j = utils.get_page('http://forismatic.com/api/1.0/', 'method=getQuote&format=json&param=ms&lang=ru')
  80. #     j = json.loads(j)
  81. #     return j['quoteText'] + u' © ' + j['quoteAuthor']
  82.  
  83. informers = [
  84.     get_rate,
  85.     get_weather,
  86.     count_messages,
  87. ]
  88.  
  89. i = 0
  90. while True:
  91.     informer = informers[i]
  92.     rb.status_set( informer() )
  93.     rb.delay(95)
  94.     i += 1
  95.     if i == len(informers):
  96.         i = 0
  97. print 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement