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.
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
Full examples available at master/examples. Complete example of a Foursquare application available at master/examples/foursquare.
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):
# Get 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')
)
Inserts a new contact for the authenticated user :
user.contacts.insert(displayName="John Doe", id="johndoe", imageUrls=["http://.....png"])
Retrieves a list of contacts for the authenticated user :
contacts = user.contacts.list()
for contact in contacts:
print "%s : %s" % (contact.get("id"), contact.get("displayName"))
Gets a single contact item by ID.
card = user.contacts.get("id_of_the_contact")
print "%s : %s" % (contact.get("id"), contact.get("displayName"))
Updates a contact in place. This method supports patch semantics.
contact = user.contacts.patch("id_of_the_contact", text="Hello World (2)!")
print "%s : %s" % (contact.get("id"), contact.get("displayName"))
Deletes a contact.
user.contacts.delete("id_of_the_contact")
Retrieves a list of timeline items for the authenticated user.
cards = user.timeline.list()
for card in cards:
print "%s :" % (card.get("id")), card
Gets a single timeline item by ID.
card = user.timeline.get("id_of_the_card")
print "%s :" % (card.get("id")), card
Updates a timeline item in place. This method supports patch semantics.
card = user.timeline.patch("id_of_the_card", text="Hello World (2)!")
print "%s :" % (card.get("id")), card
Deletes a timeline item.
user.timeline.delete("id_of_the_card")
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 !"
If you are application need to store glass user credentials for use in the future :
tokens = user.tokens
# tokens is dict with "access_token" and "refersh_token" to store in your user object in the database
Later, initialize a glass user from these stored tokens :
# get the tokens dict from your database
user = glass.User(app=app, tokens=tokens)
glass.py let you manage in a simple way the offline access and refresh tokens, when an glass.exceptions.RefreshTokenException is raised :
try:
# Try to get user profile
profile = user.profile()
except glass.exceptions.RefreshTokenException, e:
# Access token is no longer valid : refresh token
new_tokens = user.refresh_token()
# And Store in the database the new acess token (new_tokens["access_token"])