extract_kelvin = re.findall(r'[0-9]{4,6}[kK]', tweet["text"], re.I) print "Extracted Kelvin temperatures: %s" % extract_kelvin # Test if we have any Kelvin results, then clean up and convert to RGB: if len(extract_kelvin): extract_kelvin = ''.join(extract_kelvin[0]) # get the first Kelvin extraction, as a list extract_kelvin = extract_kelvin.rstrip('K') # strip trailing K, if any extract_kelvin = extract_kelvin.rstrip('k') # strip trailing k, if any extract_kelvin = extract_kelvin.rstrip(' ') # strip trailing space, if any extract_kelvin = int(''.join(extract_kelvin)) # convert to string then cast to integer kelvin_RGB = kelvin_to_rgb(extract_kelvin) print "Kelvin RGB target: %s" % (kelvin_RGB,) print "Execute Unicorn to Kelvin RGB target" colour_change(kelvin_RGB) # Kelvin input preferred (because Physics, obviously), but fall back to hex if that's present: elif len(extract_hex): # discard all but first match, convert to string before passing to hex_to_rgb hex_RGB = hex_to_rgb(''.join(extract_hex[0])) print "Hex RGB target: %s" % (hex_RGB,) print "Execute Unicorn to Hex RGB target" colour_change(hex_RGB) # Remainder code from Twitter-Stream-Responder example, left as stub for auto-thanks if one should wish to implement that # status = "@%s thanks for the mention" % tweet["user"]["screen_name"] # try: # twitter.statuses.update(status = status) # except Exception, e: # print " - failed (maybe a duplicate?): %s" % e time.sleep(sleep_time)
print "result 4: %s" % result4 def hex_to_rgb(value): value = value.lstrip("#") lv = len(value) return tuple(int(value[i : i + lv // 3], 16) for i in range(0, lv, lv // 3)) def rgb_to_hex(rgb): return "#%02x%02x%02x" % rgb # Need to cast to string before passing to hex_to_rgb. if len(result1): result1 = hex_to_rgb("".join(result1[0])) # discard all but first element print "result 1 RGB: %s" % (result1,) # singleton of tuple, so it prints. Ugh. red = result1[0] print red green = result1[1] print green blue = result1[2] print blue colour_change(red, green, blue) if len(result2): result2 = hex_to_rgb("".join(result2[0])) print "result 2 RGB: %s" % (result2,) colour_change(result2)