Last active
August 29, 2015 13:56
-
-
Save Akkiesoft/9203410 to your computer and use it in GitHub Desktop.
wearable-demo.py :: MentionEject + FavEject + Nokia5110LCD demo for OSC2014 Tokyo/Spring.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # favEject | |
| # 2014 Akkiesoft / Eject-Command-Users-Group | |
| # Inspire From: | |
| # http://peter-hoffmann.com/2012/simple-twitter-streaming-api-access-with-python-and-oauth.html | |
| # THIS DEMO is FavEject + Mention + Nokia5110 | |
| import os | |
| import sys | |
| import time | |
| import json | |
| import tweepy | |
| import pcd8544.lcd as lcd | |
| if not os.geteuid() == 0: | |
| sys.exit('Script must be run as root') | |
| ON, OFF = [1, 0] | |
| consumer_key = "" | |
| consumer_secret = "" | |
| access_key = "" | |
| access_secret = "" | |
| def showlogo(base_y): | |
| logo = [ | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe0, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0xf8, 0x84, 0xf8, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, | |
| 0x60, 0xe0, 0xe0, 0xc0, 0x00, 0x00, 0x80, 0xc0, 0xf0, 0x98, 0x8c, 0x84, 0x8c, 0x88, 0xb8, 0xe0, | |
| 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0xc0, 0x60, 0xe0, 0xe0, 0xc0, 0x00, 0x00, 0x20, 0xe0, 0x80, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x84, 0xf8, 0x80, 0x80, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x08, 0x37, 0xc0, 0x86, 0xbc, 0x8b, 0xd4, 0x3b, 0x0e, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x78, 0xc8, 0x88, 0x88, | |
| 0x18, 0xb8, 0xec, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x37, 0xc0, 0x86, 0xbc, 0x8b, | |
| 0xd4, 0x3b, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, | |
| 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x14, 0x14, 0x15, 0x15, 0x15, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0e, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
| 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
| ] | |
| lcd.gotoxy(0, base_y) | |
| for x in logo: | |
| lcd.lcd_data(x) | |
| def clearline(base_y, count): | |
| y = 0 | |
| while y < count: | |
| putstr(" ", 0, base_y + y) | |
| y += 1 | |
| def putstr(str, x, y): | |
| lcd.gotorc(y,x) | |
| lcd.text(str) | |
| class CustomStreamListener(tweepy.StreamListener): | |
| def on_data(self, data): | |
| jdata = json.loads(data) | |
| try: | |
| if jdata['event'] == "favorite": | |
| if jdata['source']['id'] != me.id: | |
| ### Eject. | |
| lcd.backlight(ON) | |
| putstr("@" + jdata['source']['screen_name'], 0, 4) | |
| putstr("favorited!! ", 0, 5) | |
| showlogo(1) | |
| os.system('eject -T /dev/sr0') | |
| # os.system('eject -T /dev/sr0') | |
| time.sleep(5) | |
| clearline(1, 5) | |
| lcd.backlight(OFF) | |
| except KeyError: | |
| pass | |
| try: | |
| if (-1 < jdata['text'].find("@eject_ug")): | |
| lcd.backlight(ON) | |
| showlogo(1) | |
| putstr("@" + jdata['user']['screen_name'], 0, 4) | |
| putstr("mentioned!! ", 0, 5) | |
| os.system('eject -T /dev/sr0') | |
| # os.system('eject -T /dev/sr0') | |
| time.sleep(5) | |
| if (-1 < jdata['text'].find("IP")): | |
| putstr(str(os.popen("hostname -I").read()), 0, 5) | |
| time.sleep(5) | |
| clearline(1, 5) | |
| lcd.backlight(OFF) | |
| except KeyError: | |
| pass | |
| def on_error(self, status_code): | |
| print >> sys.stderr, 'Encountered error with status code:', status_code | |
| putstr("Error: " + str(status_code), 0, 5) | |
| return True # Don't kill the stream | |
| def on_timeout(self): | |
| print >> sys.stderr, 'Timeout...' | |
| putstr("Error: Timeout", 0, 5) | |
| return True # Don't kill the stream | |
| try: | |
| lcd.init() | |
| lcd.cls() | |
| lcd.backlight(ON) | |
| putstr("Wearable Eject", 0, 0) | |
| putstr("Starting", 0, 5) | |
| auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
| auth.set_access_token(access_key, access_secret) | |
| api = tweepy.API(auth) | |
| putstr("Connect twtr.", 0, 5) | |
| me = api.me() | |
| putstr("Get profile. ", 0, 5) | |
| sapi = tweepy.streaming.Stream(auth, CustomStreamListener()) | |
| putstr("Start stream.", 0, 5) | |
| lcd.backlight(OFF) | |
| sapi.userstream() | |
| except KeyboardInterrupt: | |
| pass | |
| finally: | |
| lcd.cls() | |
| lcd.backlight(OFF) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment