Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/HKCodeCamp/vocably
Browse files Browse the repository at this point in the history
  • Loading branch information
tijptjik committed Oct 13, 2012
2 parents 7bd7302 + d8bfb93 commit 770161e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 20 deletions.
Empty file added utils/__init__.py
Empty file.
11 changes: 10 additions & 1 deletion utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@ def store_user_words(email,words):
for word in words:
query = 'INSERT INTO Vocab VALUES("'
query += email + '","' + word + '");'
cur.execute(query)
try:
cur.execute(query)
except sqlite3.IntegrityError:
pass

def wipe_db():

if not os.path.isfile("vocably.db"):
print "The database is not there to begin with!"
return

msg = "Are you 100% sure you want to wipe the database?\n"
msg += "This will remote all data and is irreversible... (yes/no)\n"
input_ = raw_input(msg).strip().lower()
Expand All @@ -105,6 +113,7 @@ def test():
create_user("[email protected]",0.3726)
set_score("[email protected]",0.9999)
store_user_words("[email protected]",['helicopter','giraffe','heteroskedasticity'])
store_user_words("[email protected]",['computer','giraffe','fantastic'])
print get_list('[email protected]')
print get_score('[email protected]')
print get_score('[email protected]')
Expand Down
56 changes: 56 additions & 0 deletions utils/vocably_oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/python
# -*- coding: utf-8 -*-

# Functions for authenticating with OAuth2 on Google's servers

from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage

import imaplib, json
from urllib import urlopen, urlencode

import score

def initialize_module():
global flow, storage
flow = flow_from_clientsecrets('config/client_secrets.json',
scope='https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/',
redirect_uri='http://localhost:8080/oauth2callback')
storage = Storage('config/credentials')

def authorization_url():
return flow.step1_get_authorize_url()

def authorize(code):
credentials = flow.step2_exchange(code)
storage.put(credentials)
print "Stored credentials"

def fetch_mail():
print "Getting user profile data and email address"
credentials = storage.get()
params = { 'access_token': credentials.access_token }
data = urlopen('https://www.googleapis.com/oauth2/v1/userinfo?%s' % urlencode(params)).read()
data = json.loads(data)
auth_str = "user=%s\1auth=Bearer %s\1\1" % (data['email'], credentials.access_token)

print "Authenticating against GMail IMAP servers"
imap_conn = imaplib.IMAP4_SSL('imap.gmail.com')
imap_conn.authenticate('XOAUTH2', lambda x: auth_str)
imap_conn.select('INBOX') #"[Gmail]/Sent Mail"

# Pull email bodies
resp, data = imap_conn.search(None, 'ALL')
email_text = ""
for num in data[0].split():
resp, body = imap_conn.fetch(num, '(BODY[TEXT])')
email_text += body
print 'Message %s\n%s\n' % (num, body[0][1])
return email_text

def deauthorize():
if imap_conn:
imap_conn.close()
imap_conn.logout()

initialize_module()
33 changes: 14 additions & 19 deletions vocably.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from bottle import route, run, debug, template, request, static_file, redirect
from utils import vocably_oauth as oauth

from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage

import httplib2

# Landing Page
# Landing Page
@route('/')
def home():
output = template('home')
return output

# OAuth
@route('/login')
def login():
print "Logging in a user: redirecting to Google"
redirect(flow.step1_get_authorize_url())
redirect(oauth.authorization_url())

@route('/logout')
def logout():
oauth.deauthorize()

@route('/oauth2callback')
def login_callback():
Expand All @@ -26,16 +25,14 @@ def login_callback():
redirect('/')
else:
print "Successfully acquired an authentication token"
credentials = flow.step2_exchange(request.query.code)
storage.put(credentials)
print "Stored credentials"
redirect('/')
# http = credentials.authorize(httplib2.Http())
oauth.authorize(request.query.code)
redirect('/emails')

flow = flow_from_clientsecrets('config/client_secrets.json',
scope='https://mail.google.com/',
redirect_uri='http://hype.hk/oauth2callback')
storage = Storage('config/credentials')
@route('/emails')
def fetch_mail():
email_text = oauth.fetch_mail()
# Process big string here
redirect('/words')

# Static Files
@route('/css/:path#.+#', name='css')
Expand All @@ -50,6 +47,4 @@ def img(path):
def js(path):
return static_file(path, root='js')



run(host='0.0.0.0', port=8080, debug=True, reloader=True)

0 comments on commit 770161e

Please sign in to comment.