import wrw, urllib.request, json, time, io
from PIL import Image, ImageDraw
from wrw.proto import urlq
def makedot(color, sz=(12, 12)):
w, h = sz[0] * 16, sz[1] * 16
img = Image.new("RGBA", (w, h))
g = ImageDraw.Draw(img)
g.ellipse(((4, 4), (w - 5, h - 5)), fill=color)
img = img.resize(sz, True)
buf = io.BytesIO()
img.save(buf, "PNG")
return buf.getvalue()
# Note: Scientifically tuned and optimized colors; do not change
avail = makedot((186, 217, 100))
inval = makedot((253, 174, 71))
unavl = makedot((230, 104, 89))
cache = {}
def fetch(url):
now = time.time()
if url in cache:
data, mtime = cache[url]
if now - mtime < 60:
return data
with urllib.request.urlopen(url) as req:
data = req.read()
cache[url] = data, now
return data
def game(ch):
streams = json.loads(str(fetch("http://api.twitch.tv/kraken/streams/%s?on_site=1" % urlq(ch)), "utf-8"))
if "stream" not in streams or not streams["stream"]:
return None
return streams["stream"].get("game")
@wrw.wsgiwrap
@wrw.formparams
def application(req, ch, match=None):
try:
cur = game(ch)
except urllib.error.HTTPError as exc:
if exc.code == 404:
raise wrw.resp.usererror("No such user", ch + " does not appear to be a Twitch user.")
raise
req.ohead["Content-Type"] = "image/png"
if cur is None:
return [unavl]
elif match is not None and cur != match:
return [inval]
else:
return [avail]
loftar wrote:The source code, just in case it is of use to anyone:
- Code: Select all
import wrw, urllib.request, json, time, io
from PIL import Image, ImageDraw
from wrw.proto import urlq
def makedot(color, sz=(12, 12)):
w, h = sz[0] * 16, sz[1] * 16
img = Image.new("RGBA", (w, h))
g = ImageDraw.Draw(img)
g.ellipse(((4, 4), (w - 5, h - 5)), fill=color)
img = img.resize(sz, True)
buf = io.BytesIO()
img.save(buf, "PNG")
return buf.getvalue()
# Note: Scientifically tuned and optimized colors; do not change
avail = makedot((186, 217, 100))
inval = makedot((253, 174, 71))
unavl = makedot((230, 104, 89))
cache = {}
def fetch(url):
now = time.time()
if url in cache:
data, mtime = cache[url]
if now - mtime < 60:
return data
with urllib.request.urlopen(url) as req:
data = req.read()
cache[url] = data, now
return data
def game(ch):
streams = json.loads(str(fetch("http://api.twitch.tv/kraken/streams/%s?on_site=1" % urlq(ch)), "utf-8"))
if "stream" not in streams or not streams["stream"]:
return None
return streams["stream"].get("game")
@wrw.wsgiwrap
@wrw.formparams
def application(req, ch, match=None):
try:
cur = game(ch)
except urllib.error.HTTPError as exc:
if exc.code == 404:
raise wrw.resp.usererror("No such user", ch + " does not appear to be a Twitch user.")
raise
req.ohead["Content-Type"] = "image/png"
if cur is None:
return [unavl]
elif match is not None and cur != match:
return [inval]
else:
return [avail]
jorb wrote:(jwhitehorn) you are an ungrateful, spoiled child
TotalyMeow wrote: Claeyt's perspective of Salem and what it's about is very different from the devs and in many cases is completely the opposite of what we believe.
Dallane wrote:Merged
jorb wrote:(jwhitehorn) you are an ungrateful, spoiled child
jorb wrote:(jwhitehorn) you are an ungrateful, spoiled child
jorb wrote:(jwhitehorn) you are an ungrateful, spoiled child
Users browsing this forum: No registered users and 29 guests