Skip to content

Instantly share code, notes, and snippets.

@smartm13
Created May 31, 2017 06:15
Show Gist options
  • Save smartm13/7939105d2316994934ada90bb169d75f to your computer and use it in GitHub Desktop.
Save smartm13/7939105d2316994934ada90bb169d75f to your computer and use it in GitHub Desktop.
GCM Web push notification
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START app]
from flask import Flask,request,Flask, request, redirect, url_for, send_from_directory,Response
import json
app = Flask(__name__)#,static_url_path='')
app.config["DEBUG"]=True
@app.route('/')
def hello():return all('index.html')#'Hello World!'#+request.args.get('en')
@app.route('/msg.txt')
def msg(arg={}):
if (not arg) and len(request.args.keys())<2:return all('msg.txt')
arg=request.args
return '{"message":"'+arg.get('msg',"no msg")+'","title":"'+arg.get('title',"no title")+'","icon":"images/'+arg.get('icon',"ic")+'.png"}'
@app.route('/curl')
def curl():
import requests
data = '{"registration_ids":["'+request.args.get('to','NULL')+'"]}'
r=requests.post('http://android.googleapis.com/gcm/send', data=data,headers={"Authorization": "key=AIzaSyC6VG3V4sCJzKQWOkYfTqnCU8IK_QeRuF0","Content-Type": "application/json"})
return str(r.content)
@app.route('/registered')
def reg():
id,name=request.args.get('id',''),request.args.get('name','')
if not (id and name):return "No Uinfo was sent."
old=retrivedb()
if not old:old={}
old[id]=name
return storedb(old)
@app.route('/showdb')
def showdb():return ( retrivedb()[request.args['client']] if retrivedb().has_key(request.args['client']) else 'Registeration Lost.Erase me and reRegister!') if (request.args.has_key('client')) else json.dumps(retrivedb())
@app.route('/cleardb')
def clrdb():return storedb({})
@app.route('/setdb')
def setdb():return storedb(json.loads(request.args.get('db')))
@app.route('/import')
def x():
from pywebpush import WebPusher
return 'succes'
@app.route('/send')
def showsend():
item=retrivedb().items()[int(request.args.get('index'))][0]
msgdata=request.args['msgdata']
import requests
return requests.get('http://smartass.pythonanywhere.com/encryptgcm?client='+item+'&msgdata='+msgdata).content
@app.route('/show')
def showclients():
return json.dumps(retrivedb().items())
@app.route('/<path:path>')
def all(path):
mimetypes = {
"css": "text/css",
"html": "text/html",
"js": "application/javascript",
"json": "application/json",
"png":"image/png",
"ico":"image/ico",
"mp3":"audio/mpeg",
}
# path="static/airhorn2/app/"+path
#return path #or app.send_static_file(path)
with open(path,'rb') as f:return Response(f.read(),mimetype=mimetypes.get(path.split('.')[-1],'text/html'))#,headers={'access-control-allow-origin':'*'})
@app.errorhandler(404)
def page_not_found(e):
return 'Sorry, nothing at this URL.', 404
if __name__ == '__main__':
app.run(host='' and '192.168.1.7', port=8082)
# [END app]
def retrivedb():
from google.appengine.ext import db
class data(db.Model):
dt=db.TextProperty()
try:
olddt = db.GqlQuery("SELECT * FROM data").get()
dt=olddt.dt
except: return ""
return json.loads(dt)
def storedb(given):
from google.appengine.ext import db
given=json.dumps(given)
class data(db.Model):
dt=db.TextProperty()
try:
olddt = db.GqlQuery("SELECT * FROM data").get()
olddt.dt=given
d=olddt
except:d=data(dt=given)
o=d.put()
return 'Success.'
def signup():
return """
<html><title>SignUp for Qoutes</title><body>
</body></html>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment