Skip to content

Instantly share code, notes, and snippets.

@SamusAranX
Created June 15, 2016 09:33
Show Gist options
  • Save SamusAranX/b2b9483b1812b5ab87f3fe1c56521c28 to your computer and use it in GitHub Desktop.
Save SamusAranX/b2b9483b1812b5ab87f3fe1c56521c28 to your computer and use it in GitHub Desktop.
wttr.in Today widget for Pythonista 3
def get_colors():
return [(0, 0, 0), (0.9725490196078431, 0.1607843137254902, 0.2235294117647059), (0.23137254901960785, 0.9176470588235294, 0.16470588235294117), (0.984313725490196, 0.7529411764705882, 0.27450980392156865), (0.43529411764705883, 0.3058823529411765, 0.7333333333333333), (0.17647058823529413, 0.27058823529411763, 0.3843137254901961), (0.38823529411764707, 0.796078431372549, 0.8862745098039215), (0.5137254901960784, 0.5490196078431373, 0.5843137254901961), (0.27058823529411763, 0.27450980392156865, 0.25098039215686274), (0.9647058823529412, 0.1607843137254902, 0.2235294117647059), (0.23137254901960785, 0.9137254901960784, 0.16470588235294117), (0.9803921568627451, 0.7529411764705882, 0.27450980392156865), (0.43137254901960786, 0.3058823529411765, 0.7333333333333333), (0.17647058823529413, 0.27058823529411763, 0.3843137254901961), (0.3843137254901961, 0.796078431372549, 0.8862745098039215), (0.5098039215686274, 0.5450980392156862, 0.5803921568627451), (0.0, 0.0, 0.0), (0.0, 0.0, 0.2), (0.0, 0.0, 0.4), (0.0, 0.0, 0.6), (0.0, 0.0, 0.796078431372549), (0.0, 0.0, 0.996078431372549), (0.0, 0.2, 0.0), (0.0, 0.2, 0.2), (0.0, 0.2, 0.4), (0.0, 0.2, 0.596078431372549), (0.0, 0.2, 0.796078431372549), (0.0, 0.2, 0.9921568627450981), (0.0, 0.396078431372549, 0.0), (0.0, 0.396078431372549, 0.2), (0.0, 0.396078431372549, 0.4), (0.0, 0.396078431372549, 0.596078431372549), (0.0, 0.396078431372549, 0.792156862745098), (0.0, 0.396078431372549, 0.9921568627450981), (0.0, 0.596078431372549, 0.0), (0.0, 0.596078431372549, 0.2), (0.0, 0.596078431372549, 0.396078431372549), (0.0, 0.592156862745098, 0.592156862745098), (0.0, 0.592156862745098, 0.788235294117647), (0.0, 0.592156862745098, 0.9882352941176471), (0.0, 0.788235294117647, 0.00392156862745098), (0.0, 0.788235294117647, 0.2), (0.0, 0.788235294117647, 0.396078431372549), (0.0, 0.788235294117647, 0.592156862745098), (0.0, 0.788235294117647, 0.788235294117647), (0.0, 0.788235294117647, 0.984313725490196), (0.0, 0.984313725490196, 0.00392156862745098), (0.0, 0.984313725490196, 0.2), (0.0, 0.984313725490196, 0.396078431372549), (0.0, 0.984313725490196, 0.592156862745098), (0.0, 0.996078431372549, 0.796078431372549), (0.0, 1.0, 1.0), (0.2, 0.0, 0.0), (0.2, 0.0, 0.2), (0.2, 0.0, 0.4), (0.2, 0.0, 0.6), (0.2, 0.0, 0.796078431372549), (0.2, 0.0, 0.996078431372549), (0.2, 0.2, 0.0), (0.2, 0.2, 0.2), (0.2, 0.2, 0.4), (0.2, 0.2, 0.596078431372549), (0.2, 0.2, 0.796078431372549), (0.2, 0.2, 0.9921568627450981), (0.2, 0.396078431372549, 0.0), (0.2, 0.396078431372549, 0.2), (0.2, 0.396078431372549, 0.4), (0.2, 0.396078431372549, 0.596078431372549), (0.2, 0.396078431372549, 0.792156862745098), (0.2, 0.396078431372549, 0.9921568627450981), (0.2, 0.596078431372549, 0.0), (0.2, 0.596078431372549, 0.2), (0.2, 0.596078431372549, 0.396078431372549), (0.19607843137254902, 0.592156862745098, 0.592156862745098), (0.19607843137254902, 0.592156862745098, 0.788235294117647), (0.19607843137254902, 0.592156862745098, 0.9882352941176471), (0.19607843137254902, 0.788235294117647, 0.00392156862745098), (0.19607843137254902, 0.788235294117647, 0.2), (0.19607843137254902, 0.788235294117647, 0.396078431372549), (0.19607843137254902, 0.788235294117647, 0.592156862745098), (0.19607843137254902, 0.788235294117647, 0.788235294117647), (0.19607843137254902, 0.788235294117647, 0.984313725490196), (0.19607843137254902, 0.984313725490196, 0.00392156862745098), (0.2, 0.996078431372549, 0.2), (0.2, 0.996078431372549, 0.4), (0.2, 1.0, 0.6), (0.2, 1.0, 0.8), (0.2, 1.0, 1.0), (0.4, 0.0, 0.0), (0.4, 0.0, 0.2), (0.4, 0.0, 0.4), (0.4, 0.0, 0.6), (0.4, 0.0, 0.796078431372549), (0.4, 0.0, 0.996078431372549), (0.4, 0.2, 0.0), (0.4, 0.2, 0.2), (0.4, 0.2, 0.4), (0.396078431372549, 0.2, 0.596078431372549), (0.396078431372549, 0.2, 0.796078431372549), (0.396078431372549, 0.2, 0.9921568627450981), (0.396078431372549, 0.396078431372549, 0.0), (0.396078431372549, 0.396078431372549, 0.2), (0.396078431372549, 0.396078431372549, 0.4), (0.396078431372549, 0.396078431372549, 0.596078431372549), (0.396078431372549, 0.396078431372549, 0.792156862745098), (0.396078431372549, 0.396078431372549, 0.9921568627450981), (0.396078431372549, 0.596078431372549, 0.0), (0.396078431372549, 0.596078431372549, 0.2), (0.396078431372549, 0.596078431372549, 0.396078431372549), (0.396078431372549, 0.596078431372549, 0.596078431372549), (0.396078431372549, 0.592156862745098, 0.788235294117647), (0.396078431372549, 0.592156862745098, 0.9882352941176471), (0.396078431372549, 0.788235294117647, 0.00392156862745098), (0.396078431372549, 0.788235294117647, 0.2), (0.396078431372549, 0.788235294117647, 0.396078431372549), (0.396078431372549, 0.788235294117647, 0.592156862745098), (0.396078431372549, 0.796078431372549, 0.796078431372549), (0.4, 0.796078431372549, 0.996078431372549), (0.4, 0.996078431372549, 0.0), (0.4, 0.996078431372549, 0.2), (0.4, 0.996078431372549, 0.4), (0.4, 1.0, 0.6), (0.4, 1.0, 0.8), (0.4, 1.0, 1.0), (0.6, 0.0, 0.0), (0.6, 0.0, 0.2), (0.6, 0.0, 0.4), (0.6, 0.0, 0.6), (0.6, 0.0, 0.796078431372549), (0.6, 0.0, 0.996078431372549), (0.6, 0.2, 0.0), (0.6, 0.2, 0.2), (0.6, 0.2, 0.4), (0.596078431372549, 0.2, 0.596078431372549), (0.596078431372549, 0.2, 0.796078431372549), (0.596078431372549, 0.2, 0.9921568627450981), (0.596078431372549, 0.396078431372549, 0.0), (0.596078431372549, 0.396078431372549, 0.2), (0.596078431372549, 0.396078431372549, 0.4), (0.592156862745098, 0.396078431372549, 0.596078431372549), (0.592156862745098, 0.396078431372549, 0.792156862745098), (0.592156862745098, 0.396078431372549, 0.9921568627450981), (0.592156862745098, 0.596078431372549, 0.0), (0.592156862745098, 0.596078431372549, 0.2), (0.592156862745098, 0.596078431372549, 0.396078431372549), (0.592156862745098, 0.596078431372549, 0.596078431372549), (0.592156862745098, 0.592156862745098, 0.788235294117647), (0.592156862745098, 0.592156862745098, 0.9882352941176471), (0.592156862745098, 0.788235294117647, 0.00392156862745098), (0.592156862745098, 0.792156862745098, 0.2), (0.6, 0.796078431372549, 0.4), (0.6, 0.796078431372549, 0.6), (0.6, 0.796078431372549, 0.796078431372549), (0.6, 0.796078431372549, 0.996078431372549), (0.6, 0.996078431372549, 0.0), (0.6, 0.996078431372549, 0.2), (0.6, 0.996078431372549, 0.4), (0.6, 1.0, 0.6), (0.6, 1.0, 0.8), (0.6, 1.0, 1.0), (0.8, 0.0, 0.0), (0.8, 0.0, 0.2), (0.796078431372549, 0.0, 0.4), (0.796078431372549, 0.0, 0.6), (0.796078431372549, 0.0, 0.796078431372549), (0.796078431372549, 0.0, 0.996078431372549), (0.796078431372549, 0.2, 0.0), (0.796078431372549, 0.2, 0.2), (0.796078431372549, 0.2, 0.4), (0.796078431372549, 0.2, 0.596078431372549), (0.796078431372549, 0.2, 0.796078431372549), (0.796078431372549, 0.2, 0.9921568627450981), (0.796078431372549, 0.396078431372549, 0.0), (0.796078431372549, 0.396078431372549, 0.2), (0.796078431372549, 0.396078431372549, 0.4), (0.792156862745098, 0.396078431372549, 0.596078431372549), (0.792156862745098, 0.396078431372549, 0.792156862745098), (0.792156862745098, 0.396078431372549, 0.9921568627450981), (0.792156862745098, 0.596078431372549, 0.0), (0.792156862745098, 0.596078431372549, 0.2), (0.792156862745098, 0.596078431372549, 0.396078431372549), (0.792156862745098, 0.596078431372549, 0.596078431372549), (0.788235294117647, 0.592156862745098, 0.788235294117647), (0.796078431372549, 0.6, 0.996078431372549), (0.796078431372549, 0.796078431372549, 0.0), (0.796078431372549, 0.796078431372549, 0.2), (0.796078431372549, 0.796078431372549, 0.4), (0.796078431372549, 0.796078431372549, 0.6), (0.796078431372549, 0.796078431372549, 0.796078431372549), (0.796078431372549, 0.796078431372549, 0.996078431372549), (0.796078431372549, 0.996078431372549, 0.0), (0.796078431372549, 0.996078431372549, 0.2), (0.796078431372549, 0.996078431372549, 0.4), (0.8, 1.0, 0.6), (0.8, 1.0, 0.8), (0.8, 1.0, 1.0), (1.0, 0.0, 0.0), (1.0, 0.0, 0.2), (0.996078431372549, 0.0, 0.4), (0.996078431372549, 0.0, 0.6), (0.996078431372549, 0.0, 0.796078431372549), (0.996078431372549, 0.0, 0.996078431372549), (0.996078431372549, 0.2, 0.0), (0.996078431372549, 0.2, 0.2), (0.996078431372549, 0.2, 0.4), (0.9921568627450981, 0.2, 0.596078431372549), (0.9921568627450981, 0.2, 0.796078431372549), (0.9921568627450981, 0.2, 0.9921568627450981), (0.9921568627450981, 0.396078431372549, 0.0), (0.9921568627450981, 0.396078431372549, 0.2), (0.9921568627450981, 0.396078431372549, 0.4), (0.9882352941176471, 0.396078431372549, 0.596078431372549), (0.9882352941176471, 0.396078431372549, 0.792156862745098), (0.9882352941176471, 0.396078431372549, 0.9921568627450981), (0.9882352941176471, 0.596078431372549, 0.0), (0.9882352941176471, 0.596078431372549, 0.2), (0.996078431372549, 0.6, 0.4), (0.996078431372549, 0.6, 0.6), (0.996078431372549, 0.6, 0.796078431372549), (0.996078431372549, 0.6, 0.996078431372549), (0.996078431372549, 0.796078431372549, 0.0), (0.996078431372549, 0.796078431372549, 0.2), (0.996078431372549, 0.796078431372549, 0.4), (0.996078431372549, 0.796078431372549, 0.6), (0.996078431372549, 0.796078431372549, 0.796078431372549), (0.996078431372549, 0.796078431372549, 0.996078431372549), (0.996078431372549, 0.996078431372549, 0.0), (0.996078431372549, 0.996078431372549, 0.2), (0.996078431372549, 0.996078431372549, 0.4), (1.0, 1.0, 0.6), (1.0, 1.0, 0.8), (1.0, 1.0, 1.0), (0.0, 0.0, 0.0), (0.043137254901960784, 0.043137254901960784, 0.043137254901960784), (0.08627450980392157, 0.08627450980392157, 0.08627450980392157), (0.12941176470588237, 0.12941176470588237, 0.12941176470588237), (0.17254901960784313, 0.17254901960784313, 0.17254901960784313), (0.21568627450980393, 0.21568627450980393, 0.21568627450980393), (0.2627450980392157, 0.2627450980392157, 0.2627450980392157), (0.3058823529411765, 0.3058823529411765, 0.3058823529411765), (0.34901960784313724, 0.34901960784313724, 0.34901960784313724), (0.38823529411764707, 0.38823529411764707, 0.39215686274509803), (0.43137254901960786, 0.43137254901960786, 0.43137254901960786), (0.4745098039215686, 0.4745098039215686, 0.4745098039215686), (0.5176470588235295, 0.5176470588235295, 0.5176470588235295), (0.5607843137254902, 0.5607843137254902, 0.5607843137254902), (0.6039215686274509, 0.6039215686274509, 0.6039215686274509), (0.6470588235294118, 0.6470588235294118, 0.6470588235294118), (0.6862745098039216, 0.6862745098039216, 0.6862745098039216), (0.7333333333333333, 0.7333333333333333, 0.7333333333333333), (0.7803921568627451, 0.7803921568627451, 0.7803921568627451), (0.8235294117647058, 0.8235294117647058, 0.8235294117647058), (0.8666666666666667, 0.8666666666666667, 0.8666666666666667), (0.9098039215686274, 0.9098039215686274, 0.9098039215686274), (0.9529411764705882, 0.9529411764705882, 0.9529411764705882), (0.996078431372549, 0.996078431372549, 0.996078431372549)]
# wttr.in widget
# by @SamusAranX
import appex
import console
import location
import requests
import re
from termcolors import get_colors
colors = get_colors()
text_color = (1.0, 1.0, 1.0)
appex.set_widget_height(120)
# Get city name and create wttr.in URL
location.start_updates()
loc = location.get_location()
location.stop_updates()
geo = location.reverse_geocode(loc)
addr = "http://wttr.in/" + geo[0]["City"]
# wttr.in will respond with HTML unless pretend being wget
headers = {
"User-Agent": "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)"
}
r = requests.get(addr, headers=headers)
# split text into lines and skip everything but the weather
today = "\n".join(r.text.splitlines()[:7])
# ansi color code regex
ansi = re.compile(r'\x1b[^m]*m')
# split text by color codes
today_split = ansi.split(today)
# get a list of all color codes
today_codes = [x[1:] for x in ansi.findall(today)]
# the list of split text elements will always contain one more element
assert len(today_split)-1 == len(today_codes)
for i in range(0, len(today_split)):
if i > 0: # today_split has an additional element, skip it
color_code = today_codes[i-1]
if len(color_code) == 3: # short color code
if color_code[1] == "0":
# reset everything
console.set_color(*text_color)
elif color_code[1] == "1":
# make bold
pass # ignore
else:
# get color ID from color code
color_index = int(color_code[6:][:3].replace(";", ""))
# set color
color_args = colors[color_index]
console.set_color(*color_args)
# finally, print text element with color
print(today_split[i], end="")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment