Source

knowledgeCenter / api.py

# -*- coding: utf-8 -*-
#post to "/(.*)/new name:"content""
#import urllib2,urllib
#
#liValue=uillib.uelencode({"content" : liData})
#urllib2.urlopen("http://fy.gongda.tk/li/new",liValue)
#swapValue=uillib.uelencode({"content" : swapData})
#urllib2.urlopen("http://fy.gongda.tk/swap/new",swapValue)

#get json source
#swap=urllib2.urlopen("http://fy.gongda.tk/api/swap.json").read().replace('\n',r'/n/').replace('\r',r'/r/').replace('\t',r'/t/').replace('\0',r'/0/').replace('\\n',r'///n/').replace('\\r',r'///r/').replace('\\t',r'///t/').replace('\\0',r'///0/')
#
#import json
#json.loads(swap)
#li=urllib2.urlopen("http://fy.gongda.tk/api/li.json").read()
#json.loads(li)

from settings import  *
import datetime
import os
import time
from datetime import  timedelta, date

def getCNnow():
	return datetime.datetime.utcnow() + timedelta(hours=+8)

NowDay=getCNnow().isoweekday()


from main import words,swap,li,wiki
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
import cgi
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

# class kcb(db.Model):
	# term = db.ReferenceProperty(kcbterm)
	# name = db.StringProperty(multiline=True)
	# day = db.IntegerProperty()
	# time = db.IntegerProperty()
	# week = db.ListProperty(long)
	# place = db.StringProperty(multiline=True)
	# starttime = db.TimeProperty(auto_now=True)
	# msg=db.ListProperty(long)
class ShowHandler(webapp.RequestHandler):
  def get(self,flag=None):
  	global WORDS_LIMIT,WIKI_LIMIT,SWAP_LIMIT,LI_LIMIT
	LIMIT=self.request.get('limit')
	OFFSET=self.request.get('offset')
	syncFlag=self.request.get('syncFlag')
	if LIMIT:
		LIMIT=int(LIMIT)
		WORDS_LIMIT,WIKI_LIMIT,SWAP_LIMIT,LI_LIMIT=(LIMIT,LIMIT,LIMIT,LIMIT)
	if not OFFSET:
		OFFSET=0
	else:
		OFFSET=int(OFFSET)
	try:
		if syncFlag!="true":
			WORDS=words.all().order("-time").fetch(limit=WORDS_LIMIT,offset=OFFSET)
			WIKIS=wiki.all().order("-time").fetch(limit=WIKI_LIMIT,offset=OFFSET)
			SWAPS=swap.all().order("-time").fetch(limit=SWAP_LIMIT,offset=OFFSET)
			LIS=li.all().order("-time").fetch(limit=LI_LIMIT,offset=OFFSET)
		else:
			WORDS=words.all().filter("syncFlag =",syncFlag).order("-time").fetch(limit=WORDS_LIMIT,offset=OFFSET)
			WIKIS=wiki.all().filter("syncFlag =",syncFlag).order("-time").fetch(limit=WIKI_LIMIT,offset=OFFSET)
			SWAPS=swap.all().filter("syncFlag =",syncFlag).order("-time").fetch(limit=SWAP_LIMIT,offset=OFFSET)
			LIS=li.all().filter("syncFlag =",syncFlag).order("-time").fetch(limit=LI_LIMIT,offset=OFFSET)
#			LIS=db.GqlQuery("SELECT * FROM li WHERE syncFlag = :1 ORDER BY time DESC","0").fetch(limit=LI_LIMIT,offset=OFFSET)
	except IndexError:
		WORDS=None
		WIKIS=None
		SWAPS=None
		LASTSWAP=None
		LIS=None
		LASTLI=None
	callback=self.request.get("callback")
	template_values = {
	'WORDSS':WORDS,
	'WIKIS':WIKIS,
	'SWAPS':SWAPS,
	'LIS':LIS,
	'callback':callback,
	}
	
	if flag.endswith('.json'):
		if callback:
			self.response.headers['Content-type'] = 'text/javascript; charset=UTF-8'
		else:
			self.response.headers['Content-type'] = 'application/json; charset=UTF-8'
		path = os.path.join(os.path.dirname(__file__), 'tpl','api',flag)
	# else:
		# path = os.path.join(os.path.dirname(__file__), 'tpl','bingo.html')
	self.response.out.write(template.render(path, template_values))


class SetUnixTimeHandler(webapp.RequestHandler):
    def get(self,key):
    	import time,calendar,datetime
    	OBJ=db.get(key)
    	OBJ.unixTime=str(int(time.mktime(OBJ.time.timetuple())))
    	OBJ.put()
    	self.response.out.write(OBJ.unixTime)

class SetSyncFlagHandler(webapp.RequestHandler):
    def get(self,key,flag):
    	import time,calendar,datetime
    	OBJ=db.get(key)
    	OBJ.syncFlag=flag
    	OBJ.put()
    	self.response.out.write(OBJ.syncFlag)
# Nodes
# /api/nodes/all.json
application = webapp.WSGIApplication(
                                     [
									 # ('/', MainPage),
									  ('/api/setUnixTime/(.*)', SetUnixTimeHandler),
									  ('/api/setSyncFlag/(.*)/(.*)', SetSyncFlagHandler),
									  ('/api/(.*)', ShowHandler),

									  ],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.