• Stars
    star
    276
  • Rank 149,319 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 11 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create python web applications for Google Glass

glass.py

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.

Features

  • Post cards to timelines
  • Get notification from subscriptions
  • [Coming soon] Emulator for testing the application
  • Built on Requests (v1.x)
  • Built on Flask

Installation

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 .

Test an Hello World with the emulator

python examples/hello.py

Example Usage

Full examples available at master/examples. Complete example of a Foursquare application available at master/examples/foursquare.

Get started

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)

oAuth

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.

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)

Insert Cards in timeline

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 actions

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 user informations

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')
    )

Managing user contacts

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")

Advanced timeline gestion

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")

Accessing the Flask web server

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 !"

Store users in a database

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)

Handle offline access and refresh 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"])

More Repositories

1

How-to-Make-a-Computer-Operating-System

How to Make a Computer Operating System in C++
C
20,993
star
2

tv.js

Apple TV for Torrent Streaming in JS (Node/Chrome)
JavaScript
1,370
star
3

betty

Google Voice with Receptionist abilities, built on top of Twilio
JavaScript
874
star
4

gitkit-js

Pure javascript implementation of Git (Node.js and Browser)
JavaScript
604
star
5

devos

(Very) basic UNIX-like operating system built when I was younger
C
544
star
6

draft-js-prism

Code highlighting for DraftJS using Prism
JavaScript
274
star
7

draft-js-code

Collection of utilities to make code blocks edition easy in DraftJS
JavaScript
107
star
8

react-mathjax

React component to display math formulas
JavaScript
93
star
9

draft-js-table

Utilities to edit tables in DraftJS
JavaScript
69
star
10

gumroad-api

Node client for the Gumroad API
JavaScript
35
star
11

code2stl

The Revolution is coming, 3D code like youโ€™ve never seen it before!
JavaScript
33
star
12

draft-js-multidecorators

Combine multiple decorators for DraftJS
JavaScript
32
star
13

issues

GitHub Issues manager for Desktop & Browser
JavaScript
31
star
14

octocat.js

Javascript library to access the GitHub API
JavaScript
30
star
15

codemirror-widgets

Utility to write powerful and interactive widgets for CodeMirror
JavaScript
27
star
16

react-combo-keys

React declarative API to bind keyboard shortcuts
TypeScript
25
star
17

react-nps-input

React component to prompt the NPS score
JavaScript
23
star
18

react-electron-menu

React API to controls electron's menu
JavaScript
20
star
19

xml-schema

Node.js library to generate XML from JavaScript schemas
JavaScript
12
star
20

tpl-emails

Generate HTML emails using templating engines
JavaScript
12
star
21

equery

Query builder for elasticsearch (Node.js / Javascript)
JavaScript
11
star
22

blini

Modern ORM for MongoDB based on immutable data structure and promises.
JavaScript
11
star
23

svg-timegraph

Easy to use Node.JS/Browserify library to render time graph into SVG
JavaScript
11
star
24

react-responsive-hoc

Wrapper around react-responsive to work with SSR
JavaScript
11
star
25

nunjucks-i18n

Nunjucks i18n extension
JavaScript
10
star
26

book-heroku-node

Learn how to build and deploy Node.JS applications on Heroku
10
star
27

pmarca-notes

Notes from Marc Andreessen tweetstorms
10
star
28

react-walk-tree

Asynchronously walk a React element tree
JavaScript
8
star
29

passport-gumroad

Gumroad authentication strategy for Passport.
JavaScript
7
star
30

git-push-server

Make it easy to build an HTTP stateless git-push server
JavaScript
7
star
31

nunjucks-do

do block tag for nunjucks
JavaScript
7
star
32

parse-changelog

Javascript ChangeLog parser
JavaScript
6
star
33

github-slugid

Slugify like GitHub
JavaScript
6
star
34

analytics.js-integration-hotjar

Hotjar Analytics.js integration
JavaScript
5
star
35

should-html

HTML testing assertion for should.js
JavaScript
5
star
36

browserify-rfr

Require From project Root tool for browserify
JavaScript
4
star
37

express-statuspage

Get statuspage.io last incidents into express (node.js)
JavaScript
4
star
38

fswrite-stream

Write a stream to a file
JavaScript
4
star
39

segment.js

Segment.com client for the browser and node.js
JavaScript
4
star
40

html2jsx.js

Pure javascript HTML to Jsx parser (works in webworker)
JavaScript
4
star
41

jobworker

WebWorker background jobs worker
JavaScript
3
star
42

aphrodite-to-jss

Aphrodite compatible API on top of JSS.
TypeScript
3
star
43

node-corvisa

Client for the Corvisa REST API
JavaScript
3
star
44

i18n-t

Easy to use i18n utility for node
JavaScript
3
star
45

nunjucks-autoescape

Nunjucks extension that brings a "autoescape" tag
JavaScript
2
star
46

nunjucks-filter

Nunjucks extension that brings a "filter" tag
JavaScript
2
star
47

diff-linetoword

Convert line-diff to word-diff
JavaScript
2
star
48

range-utils

Javascript utilities to work with ranges (offset, length)
JavaScript
2
star
49

suspense-cache

Cache utility to create resources for React suspense
TypeScript
2
star
50

markdown-it-renderer

markdown-it renderer to output markdown
JavaScript
1
star
51

stream-res

Pipe a stream to an HTTP response
JavaScript
1
star
52

algolia-faq

Algolia FAQ as a GitBook
CSS
1
star
53

SamyPesse

1
star