A simple but powerful library for building and testing Google Glass applications in Python using Mirror API.
Glass.py uses Flask, Requests and Rauth.
I started this project for testing development of applications for Glass using Mirror API, but I'm not part of Explorer Program, so this library will soon contain an emulator.
- Post cards to timelines
- Get notification from subscriptions
- [Coming soon] Emulator for testing the application
- Built on Requests (v1.x)
- Built on Flask
Clone this repository :
git clone https://github.com/SamyPesse/glass.py.git
Install dependencies :
pip install -r requirements.txt
Install the library (maybe need to be sudo) :
pip install .
python examples/hello.py
A simple helloworld which display a message when the user connect the application to his Glasses.
import glass
app = glass.Application(
name="hello",
client_id="",
client_secret="")
@app.subscriptions.login
def login(user):
print "user : %s" % user.token
user.timeline.post(text="Hello World!")
if __name__ == '__main__':
app.run(port=8080)
Google Glass mirror API uses oAuth for authorizing an application to connect to the glasses. Go to the Google APIs console and create a new API project. Enable the Google Mirror API for your new project. The API is only available to developers who have Glass as part of the Explorer Program, so if it's not available for you, just pass this step. Specify http://localhost:8080/glass/oauth/callback as callback url.
For authorizing the application to connect to your glasses, access the page : http://localhost:8080/glass/oauth/authorize If you don't have Glass as part of the Explorer Program, use the emulator.
Enable the emulator (which will run at localhost:8080/emulator/index.html), emulator user can be identified by his token : "emulator". Emulator is not working yet and it's based on https://github.com/Scarygami/mirror-api
app.emulator = True
app.run(port=8080)
Post text cards :
user.timeline.post(text="Hello World!")
Post HTML cards :
user.timeline.post(html="Hello <b>World</b>")
Post HTML templates, templates are processed using Jinja2 template engine. For templates you can use the full power of Jinja2 templates. Head over to the official Jinja2 Template Documentation for more information.
user.timeline.post_template("message.html", author="Aaron", content="Hey, How are you ?")
Define the directory for the templates using :
app.template_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
Subscribe to an action "REPLY" :
@app.subscriptions.action("REPLY")
def reply(user):
print "User %s reply" % user.token
user.timeline.post(text="Thank you!")
A new location is available for the current user, At this time, location notifications are sent every 10 minutes :
@app.subscriptions.location
def change_location(user):
print "User %s change location" % user.token
user.timeline.post(text="You move !")
Access the user last known location using :
@app.subscriptions.location
def change_location(user):
# Gte last known location
location = user.location()
# Post card with location infos
user.timeline.post(text="You move to (Lat: %s, Long: %s) (Accuracy: %s meters)" % (
location.get('latitude'),
location.get('longitude'),
location.get('accuracy')
))
Get profile data using :
profile = user.profile()
print "Hello %s" % (profile.get("given_name"))
Get last known user location :
location = user.location()
print "User is at (Lat: %s, Long: %s) (Accuracy: %s meters)" % (
location.get('latitude'),
location.get('longitude'),
location.get('accuracy')
)
You can access the flask applciation for adding views (like index, about pages, ...) using :
@app.web.route("/")
def index():
return "Welcome on my Glass Application website !"