usr bin env python coding utf-8 from urllib import urlopen encoding ut

 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib import urlopen
encoding='utf-8'
try:
import cPickle as pickle
except ImportError:
import pickle
try:
import cStringIO as StringIO
except ImportError:
import StringIO
from os import stat
from time import time
HOURS = 2
cache_file = '.weather_menu_cache'
text = None
try:
seconds_old = time() - stat(cache_file).st_mtime
if seconds_old < HOURS * 3600:
text = StringIO.StringIO()
f = open(cache_file, 'rb')
text.writelines(pickle.load(f))
f.close()
text.seek(0)
except OSError:
#can't open the file
pass
if text == None:
text_ = urlopen('http://cn.ru/ask/weather')
text = StringIO.StringIO()
text.writelines(text_.readlines())
text.seek(0)
f = open(cache_file, 'wb')
pickle.dump(text.readlines(), f, -1)
f.close()
text.seek(0)
i=1
days = 3
print '<openbox_pipe_menu>'
while i and days:
i = unicode(text.readline(), 'cp1251').encode(encoding)
if 'Температура за окном: ' in i:
print '<item label="' + i.replace('<strong>', '').replace('&deg;', '')[53:-17] + '"/>'
elif 'Прогноз погоды на 3 дня' in i:
print '<item label="' + 'Прогноз погоды на 3 дня' + '"/>'
elif 'class="date"' in i:
days -= 1
print '<separator label="' + u' * ', i[i.index('>')+1:-7].capitalize() + '"/>'
jj = 1
while jj:
j = unicode(text.readline(), 'cp1251').encode(encoding)[5:-7]
if j=='вечер': # ночь, утро, день, вечер
jj = 0
j += ':'
print '<item label="' + j.ljust(7),
print unicode(text.readline()[5:-7], 'cp1251').encode(encoding).replace(u'&hellip;', ' ... ')+',',
k = unicode(text.readline(), 'cp1251').encode(encoding)[60:]
k = k[k.index('"')+1:]
print k[:k.index('"')]+',',
k = unicode(text.readline(), 'cp1251').encode(encoding)[60:]
k = k[k.index('"')+1:]
print k[:k.index('"')]+'.',
k = text.readline()
print 'Ветер:', unicode(text.readline(), 'cp1251').encode(encoding)[5:-7].replace('&hellip;', '...'), 'м/с,', unicode(text.readline(), 'cp1251').encode(encoding)[5:-7] + '"/>'
for agdvsfds in xrange(3):
k = text.readline()
print '</openbox_pipe_menu>'