|
| 1 | +#!/usr/bin/python3 |
| 2 | +import json |
| 3 | + |
| 4 | +from gi.repository import CinnamonDesktop, GLib, Gtk, Gio, Pango |
| 5 | + |
| 6 | +from util import utils, trackers, settings |
| 7 | +from baseWindow import BaseWindow |
| 8 | +from floating import Floating |
| 9 | +import requests |
| 10 | + |
| 11 | +MAX_WIDTH = 320 |
| 12 | +MAX_WIDTH_LOW_RES = 200 |
| 13 | + |
| 14 | +WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather" |
| 15 | + |
| 16 | +class WeatherWidget(Floating, BaseWindow): |
| 17 | + """ |
| 18 | + WeatherWidget displays current weather on screen |
| 19 | +
|
| 20 | + It is a child of the Stage's GtkOverlay, and its placement is |
| 21 | + controlled by the overlay's child positioning function. |
| 22 | +
|
| 23 | + When not Awake, it positions itself around all monitors |
| 24 | + using a timer which randomizes its halign and valign properties |
| 25 | + as well as its current monitor. |
| 26 | + """ |
| 27 | + def __init__(self, away_message=None, initial_monitor=0, low_res=False): |
| 28 | + super(WeatherWidget, self).__init__(initial_monitor) |
| 29 | + self.get_style_context().add_class("weather") |
| 30 | + self.set_halign(Gtk.Align.START) |
| 31 | + |
| 32 | + self.set_property("margin", 6) |
| 33 | + |
| 34 | + self.low_res = low_res |
| 35 | + |
| 36 | + if not settings.get_show_weather(): |
| 37 | + return |
| 38 | + |
| 39 | + self.away_message = away_message |
| 40 | + |
| 41 | + box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) |
| 42 | + self.add(box) |
| 43 | + box.show() |
| 44 | + |
| 45 | + self.label = Gtk.Label() |
| 46 | + self.label.show() |
| 47 | + self.label.set_line_wrap(True) |
| 48 | + self.label.set_alignment(0.5, 0.5) |
| 49 | + |
| 50 | + box.pack_start(self.label, True, False, 6) |
| 51 | + |
| 52 | + self.msg_label = Gtk.Label() |
| 53 | + self.msg_label.show() |
| 54 | + self.msg_label.set_line_wrap(True) |
| 55 | + self.msg_label.set_alignment(0.5, 0.5) |
| 56 | + |
| 57 | + if self.low_res: |
| 58 | + self.msg_label.set_max_width_chars(50) |
| 59 | + else: |
| 60 | + self.msg_label.set_max_width_chars(80) |
| 61 | + |
| 62 | + box.pack_start(self.msg_label, True, True, 6) |
| 63 | + |
| 64 | + self.update_weather() |
| 65 | + |
| 66 | + def update_weather(self): |
| 67 | + default_message = GLib.markup_escape_text (settings.get_default_away_message(), -1) |
| 68 | + font_message = Pango.FontDescription.from_string(settings.get_message_font()) |
| 69 | + font_weather = Pango.FontDescription.from_string(settings.get_weather_font()) |
| 70 | + |
| 71 | + response = requests.get(WEATHER_URL, |
| 72 | + { "q": settings.get_weather_location(), |
| 73 | + "units": settings.get_weather_units(), |
| 74 | + "appid": settings.get_weather_api_key()}) |
| 75 | + data = response.json() |
| 76 | + default_message = data["weather"][0]["main"] + " in " + data["name"] |
| 77 | + |
| 78 | + if self.low_res: |
| 79 | + msg_size = font_message.get_size() * .66 |
| 80 | + font_message.set_size(int(msg_size)) |
| 81 | + |
| 82 | + markup = '<b><span font_desc=\"%s\" foreground=\"#CCCCCC\">%s</span></b>\n ' %\ |
| 83 | + (font_message.to_string(), default_message) |
| 84 | + |
| 85 | + self.label.set_markup('<span font_desc=\"%s\">%s°</span>' % (font_weather.to_string(), data['main']['temp'])) |
| 86 | + self.msg_label.set_markup(markup) |
| 87 | + |
| 88 | + def set_message(self, msg=""): |
| 89 | + self.away_message = msg |
| 90 | + self.update_weather() |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def on_destroy(data=None): |
| 94 | + # placeholder |
| 95 | + print("shut down weather widget: " + data) |
0 commit comments