Skip to content

Fix/gae runtime #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions gae/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file specifies files that are *not* uploaded to Google Cloud
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg
1 change: 1 addition & 0 deletions gae/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 3.10.0
11 changes: 8 additions & 3 deletions gae/app.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# use --project=pebble-notes for gcloud
runtime: python27
api_version: 1
threadsafe: true
runtime: python310
# api_version: 1
# threadsafe: true
# app_engine_apis: true
entrypoint: gunicorn -b :$PORT main:app

env_variables:
ENVIRONMENT: production

handlers:
- url: /
Expand Down
171 changes: 0 additions & 171 deletions gae/auth.py

This file was deleted.

12 changes: 7 additions & 5 deletions gae/config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os

# get our version id
version = os.environ['CURRENT_VERSION_ID'].split('.')[0]
# version = os.environ['CURRENT_VERSION_ID'].split('.')[0]

# construct hostname for the current version
# (use -dot- instead of . to avoid SSL problems)
# see also: https://developers.google.com/appengine/kb/general#https
host = "https://%s-dot-pebble-notes.appspot.com" % version
environment = os.getenv('ENVIRONMENT', 'local')

# Set the host based on the environment
if environment == 'production':
host = "https://pebble-notes-426618.uc.r.appspot.com"
else:
host = "http://127.0.0.1:5000"
# where user will be redirected after logging in with Google
auth_redir_uri = host+"/auth/result"

Expand Down
115 changes: 115 additions & 0 deletions gae/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from flask import Flask, redirect, request, jsonify
import json
import random
import urllib.request
from urllib.parse import urlencode
import os

try:
from google.appengine.api import memcache
except ImportError:
# Mock memcache for local development
class SimpleCache:
def __init__(self):
self.store = {}

def set(self, key, value, time=0):
self.store[key] = value

def get(self, key):
return self.store.get(key)

memcache = SimpleCache()

from secret import client_id, client_secret
import config

app = Flask(__name__)

WORDS = (
'red green blue white brown violet purple black yellow orange '
'dog cat cow unicorn animal hedgehog chicken '
'task computer phone watch android robot apple '
'rambler rogue warrior king '
'jeans muffin cake bake cookie oven bread '
).split()

def query_json(url, data):
if not isinstance(data, str):
data = urlencode(data).encode()
try:
with urllib.request.urlopen(url, data) as response:
return json.loads(response.read())
except urllib.error.HTTPError as e:
return json.loads(e.read())

@app.route('/auth')
def auth_redirect():
url = 'https://accounts.google.com/o/oauth2/auth?' + urlencode({
'client_id': client_id,
'redirect_uri': config.auth_redir_uri,
'response_type': 'code',
'scope': 'https://www.googleapis.com/auth/tasks',
'state': '',
'access_type': 'offline',
'approval_prompt': 'force',
'include_granted_scopes': 'true',
})
return redirect(url)

@app.route('/auth/check')
def auth_check():
lifetime = 10
passcode = '-'.join(random.sample(WORDS, 4))
passcode2 = passcode.replace('-', '')

memcache.set(passcode, request.args.get('state'), time=lifetime*60)
memcache.set(passcode2, request.args.get('state'), time=lifetime*60)

html = f'''
<style>
tt {{
border: 1px solid #88c;
border-radius: 3px;
background: #ccf;
font-size: 20pt;
}}
</style>
<h3>Passcode: <tt>{passcode}</tt></h3>
<h3>Or: <tt>{passcode2}</tt></h3>
<p><em>It will expire in {lifetime} minutes.</em></p>
<p>Enter it on application settings page</p>
'''
return html

@app.route('/auth/result')
def auth_callback():
code = request.args.get('code')
if not code:
return 'Code parameter missing', 400

result = query_json('https://accounts.google.com/o/oauth2/token', {
'code': code,
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': config.auth_redir_uri,
'grant_type': 'authorization_code'
})
return jsonify(result)

@app.route('/auth/refresh')
def auth_refresh():
refresh_token = request.args.get('refresh_token')
if not refresh_token:
return 'Refresh token parameter missing', 400

result = query_json('https://accounts.google.com/o/oauth2/token', {
'refresh_token': refresh_token,
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'refresh_token'
})
return jsonify(result)

if __name__ == '__main__':
app.run(debug=True)
6 changes: 6 additions & 0 deletions gae/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flask==2.3.2
gunicorn==20.1.0
google-api-python-client==2.92.0
google-auth==2.21.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==1.0.0
4 changes: 2 additions & 2 deletions gae/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1>
GoogleTasks for Pebble <small>(ex. PebbleNotes)</small>
</h1>
<p>This is a <a href="https://mail.google.com/tasks/canvas">Google Tasks</a> client for Pebble smartwatch.</p>
<p>You may discuss it <a href="https://github.com/MarSoft/PebbleNotes">here</a>.</p>
<p>You may discuss it <a href="https://github.com/ngencokamin/PebbleNotes">here</a>.</p>
<p>Available <a href="https://store-beta.rebble.io/app/52b1fc07cd41830cc200001e">in Rebble AppStore</a>.</p>
<hr>
<h2>Authentication</h2>
Expand All @@ -21,7 +21,7 @@ <h2>Authentication</h2>
<p>
Pebble is fading away, but is still alive.
This application is one of the examples.
You can <a href="http://www.maryasin.name/pebble.html"><strong>donate</strong></a> to support it.
You can <a href="https://www.buymeacoffee.com/ngencokamin"><strong>donate</strong></a> to support it.
</p>
<h2 id="privacy-policy">Privacy Policy</h2>
<p>
Expand Down
2 changes: 1 addition & 1 deletion gae/static/notes-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h2>Account</h2>
<input type="hidden" id="refresh_token"/>
<button data-theme="b" id="btn-login">Log in</button>
<div id="div-login">
<p>Please open <tt>https://pebble-notes.appspot.com/</tt> (or <tt>goo.gl/6U5Zxn</tt>) in your browser
<p>Please open <tt>https://pebble-notes-426618.uc.r.appspot.com//</tt> in your browser
and login there to obtain your passcode.</p>
<input type="text" id="passcode" name="passcode" placeholder="Passcode">
<button data-theme="b" id="btn-login-do">OK</button>
Expand Down
2 changes: 1 addition & 1 deletion src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var g_xhr_timeout = 30000;
// Timeout for sending appmessage to Pebble, in milliseconds
var g_msg_timeout = 8000;

var g_server_url = "https://2-dot-pebble-notes.appspot.com";
var g_server_url = "https://pebble-notes-426618.uc.r.appspot.com/";

var g_options = {
"sort_status": false,
Expand Down