backup de julho
[h2N7SspZmY.git] / data / pages / libraries / appengine.txt
blob5907a19cad7f8d2609128eee87623808429d8126
1 ====== App Engine ======
3 [[http://code.google.com/appengine/|Google App Engine]]: [[wp>Google_App_Engine|Google App Engine]] \\
5 ===== Example =====
7 <code python>
8 from google.appengine.ext import webapp
9 from google.appengine.ext.webapp.util import run_wsgi_app
10 from google.appengine.ext import db
11 import urllib
12 from HTMLParser import HTMLParser
13 import re
15 class MyHTMLParser(HTMLParser):
16         def __init__(self):
17                 HTMLParser.__init__(self)
18                 self.s = ''
20         def handle_starttag(self, tag, attrs):
21                 if tag == 'embed':
22                         for attr in attrs:
23                                 if attr[0] == 'flashvars':
24                                         self.s = attr[1]
26         def feed(self, data):
27                 HTMLParser.feed(self, data)
28                 return self
30         def get_s(self):
31                 return self.s
33 class Level(db.Model):
34         content = db.TextProperty()
36 class Current(db.Model):
37         x = db.IntegerProperty()
38         y = db.IntegerProperty()
39         d = db.IntegerProperty()
40         n = db.IntegerProperty()
41         m = db.IntegerProperty()
42         l = db.IntegerProperty()
44 class Wrong(db.Model):
45         x = db.IntegerProperty()
46         y = db.IntegerProperty()
47         d = db.IntegerProperty()
48         l = db.IntegerProperty()
50 class CrossPage(webapp.RequestHandler):
51         def get(self):
52                 url = 'http://www.hacker.org/cross/index.php?name=kauesilv&password=a1b2c3d4'
53                 regex = r'boardinit = "([^"]*)"'
54                 debug = self.request.get('debug')
55                 s = self.request.get('s')
56                 path = self.request.get('path')
57                 self.response.headers['Content-Type'] = 'text/plain'
58                 if s == '1':
59                         urllib.urlopen(url + '&sol=' + path).read()
60                 s = urllib.urlopen(url).read()
61                 #self.response.out.write(s + '\n')
62                 board = re.compile(regex).search(s).groups()[0].split(',')
63                 self.response.out.write(str(len(board)) + '\n' + str(len(board[0])) + '\n')
64                 for line in board:
65                         self.response.out.write(str(line) + '\n')
66         
67 class MainPage(webapp.RequestHandler):
68         def get(self):
69                 delete = self.request.get('delete')
70                 if delete == '1':
71                         db.delete(Wrong.all().fetch(100))
72                         if Wrong.all().count(1) == 1:
73                                 return
75                 url = 'http://www.hacker.org/coil/index.php?name=kauesilv&password=a1b2c3d4'
76                 regex = r'x=(\d*)&y=(\d*)&board=(.*)'
77                 regexLevel = r'Level: (\d*)'
78                 self.response.headers['Content-Type'] = 'text/plain'
79                 debug = self.request.get('debug')
80                 s = self.request.get('s')
81                 r = self.request.get('r')
82                 x = self.request.get('x')
83                 y = self.request.get('y')
84                 d = self.request.get('d')
85                 w = self.request.get('w')
86                 path = self.request.get('path')
87                 
88                 levels = Level.all()
89                 currents = Current.all()
91                 if s == '1':
92                         urllib.urlopen(url + '&x=' + x + '&y=' + y + '&path=' + path).read()
94                 if w == '1':
95                         wrong = Wrong()
96                         wrong.x = int(x)
97                         wrong.y = int(y)
98                         wrong.d = int(d)
99                         wrong.l = currents.fetch(1)[0].l
100                         wrong.put()
102                 if s == '1' or r == '1':
103                         s = urllib.urlopen(url).read()
104                         x, y, board = re.compile(regex).search(MyHTMLParser().feed(s).get_s()).groups()
105                         lvl, = re.compile(regexLevel).search(s).groups()
106                         db.delete(Level.all().fetch(1000000))
107                         db.delete(Current.all().fetch(1000000))
109                         level = Level()
110                         level.content = x + '\n' + y + '\n' + board + '\n'
111                         level.put()
113                         current = Current()
114                         current.x = current.y = current.d = 0
115                         current.n = int(x)
116                         current.m = int(y)
117                         current.l = int(lvl)
118                         current.put()
120                 cur = currents.fetch(1)[0]
121                 while(True):
122                         cur.d = cur.d + 1
123                         if(cur.d == 4):
124                                 cur.d = 0
125                                 cur.y = cur.y + 1
126                                 if(cur.y == cur.m):
127                                         cur.y = 0
128                                         cur.x = cur.x + 1
129                                         if(cur.x == cur.n):
130                                                 cur.x = 0
131                         cur.put()
132                         wrong = Wrong().gql("WHERE x = :1 AND y = :2 AND d = :3 AND l = :4", cur.x, cur.y, cur.d, cur.l)
133                         if(wrong.count(1) == 0):
134                                 break
135                         
136                 self.response.out.write(str(cur.x) + '\n' + str(cur.y) + '\n' + str(cur.d) + '\n' + str(cur.l) + '\n')
137                 self.response.out.write(levels.fetch(1)[0].content)
139                 if debug == '1':
140                         self.response.out.write('\n\n----DEBUG----\n\nwrongs\n')
141                         wrongs = Wrong.gql("WHERE l = :1", cur.l)
142                         for wrong in wrongs:
143                                 self.response.out.write(str(wrong.x) + ' ' + str(wrong.y) + ' ' + str(wrong.d) + ' ' + str(wrong.l) + '\n')
145                         self.response.out.write('\ncurrents\n')
146                         for current in currents:
147                                 self.response.out.write(str(current.x) + ' ' + str(current.y) + ' ' + str(current.d) + ' ' + str(current.l) + '\n')
149 application = webapp.WSGIApplication([('/', MainPage), ('/cross', CrossPage)], debug=True)
151 def main():
152         run_wsgi_app(application)
154 if __name__ == "__main__":
155         main()
156 </code>