-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherMan.py
More file actions
123 lines (101 loc) · 3.64 KB
/
WeatherMan.py
File metadata and controls
123 lines (101 loc) · 3.64 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import sublime
import sublime_plugin
import urllib
import xml.etree.ElementTree as ET
WEATHERMAN_SETTING_FILE = 'WeatherMan.sublime-settings'
class WeatherMan(sublime_plugin.EventListener):
# https://tech.yandex.ru/weather/doc/dg/concepts/translations-docpage/
# Weather icons
_icons = {
"clear": "🌞",
"mostly-clear": "🌤",
"partly-cloudy": "🌤",
"overcast": "🌥",
"partly-cloudy-and-light-rain": "🌦",
"partly-cloudy-and-rain": "🌧",
"overcast-and-rain": "🌧",
"overcast-thunderstorms-with-rain": "⛈",
"cloudy": "🌤",
"cloudy-and-light-rain": "🌦",
"overcast-and-light-rain": "🌦",
"cloudy-and-rain": "🌧",
"overcast-and-wet-snow": "🌨",
"partly-cloudy-and-light-snow": "🌨",
"partly-cloudy-and-snow": "🌨",
"overcast-and-snow": "🌨",
"cloudy-and-light-snow": "🌨",
"overcast-and-light-snow": "🌨",
"cloudy-and-snow": "🌨"
}
# Traffic level icons
_ticons = {'green': '🍏', 'yellow': '🍋', 'red': '🍅'}
# Settings: update interval
_updateInterval = None
# Settings: output template
_template = None
# Cache of statusbar string
_status = None
# Was plugin started
_activated = False
# Current view (Sublime's object)
_view = None
def on_activated_async(self, view):
self._run(view)
# Run for given window (view)
def _run(self, view):
self._view = view
if not self._activated:
settings = sublime.load_settings(WEATHERMAN_SETTING_FILE)
self._updateInterval = settings.get('update_interval', 600)
self._template = settings.get('template', None)
self._updateData()
self._startTimer()
self._activated = True
self._showStatus()
# Timer loop
def _startTimer(self):
if self._updateData():
self._showStatus()
timeout = self._updateInterval
else:
# if failed retry in minite
timeout = 60
sublime.set_timeout_async(lambda: self._startTimer(), timeout * 1e3)
# Get current weather and traffic level
def _getData(self):
try:
url = "https://export.yandex.ru/bar/reginfo.xml"
content = urllib.request.urlopen(url).read()
return ET.fromstring(content)
except (IOError, ET.ParseError):
return None
# Get weather icon from XML element
def _getStatus(self, el):
day_part = el.findall('day_part')[0]
return self._icons.get(
day_part.find('weather_code').text,
day_part.find('weather_type').text
)
# Get traffic level icon from XML element
def _getTrafficIcon(self, el):
return self._ticons[el.find('icon').text]
# Update statusbar string cache
def _updateData(self):
xml = self._getData()
if xml is not None:
weather = xml.find('weather').find('day')
title = weather.find('title').text
status = self._getStatus(weather)
temp = weather.findall('day_part')[0].find('temperature').text
traffic = xml.find('traffic').find('region')
tlevel = traffic.find('level').text
ticon = self._getTrafficIcon(traffic)
self._status = self._template % vars()
return True
else:
self._status = None
return False
# Print cached status in current view
def _showStatus(self):
if self._status is not None:
self._view.set_status('YandexPogoda', self._status)