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):
		try:
			WORDS=words.all().order("-time").fetch(limit=WORDS_LIMIT)
			WIKIS=wiki.all().order("-time").fetch(limit=WIKI_LIMIT)
			SWAPS=swap.all().order("-time").fetch(limit=SWAP_LIMIT,offset=0)
			LASTSWAP=swap.all().order("-time").fetch(limit=1)[0].content
			LIS=li.all().order("-time").fetch(limit=LI_LIMIT,offset=0)
			LASTLI=li.all().order("-time").fetch(limit=1)[0].content
		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,
		'LASTSWAP':LASTSWAP,
		'LIS':LIS,
		'LASTLI':LASTLI,
		'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 SiteInfoHandler(webapp.RequestHandler):
    def get(self):
        site = GetSite()
        template_values = {}
        template_values['site'] = site
        path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'site_info.json')
        output = template.render(path, template_values)
        self.response.headers['Content-type'] = 'application/json'
        self.response.out.write(output)

# Nodes
# /api/nodes/all.json
application = webapp.WSGIApplication(
                                     [
									 # ('/', MainPage),

									  ('/api/(.*)', ShowHandler),
									  ],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()