1 <span class="kw1">from</span> google.<span class="me1">appengine</span>.<span class="me1">ext</span> <span class="kw1">import</span> webapp
2 <span class="kw1">from</span> google.<span class="me1">appengine</span>.<span class="me1">ext</span>.<span class="me1">webapp</span>.<span class="me1">util</span> <span class="kw1">import</span> run_wsgi_app
3 <span class="kw1">from</span> google.<span class="me1">appengine</span>.<span class="me1">ext</span> <span class="kw1">import</span> db
4 <span class="kw1">import</span> <span class="kw3">urllib</span>
5 <span class="kw1">from</span> <span class="kw3">HTMLParser</span> <span class="kw1">import</span> <span class="kw3">HTMLParser</span>
6 <span class="kw1">import</span> <span class="kw3">re</span>
8 <span class="kw1">class</span> MyHTMLParser<span class="br0">(</span><span class="kw3">HTMLParser</span><span class="br0">)</span>:
9 <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">(</span><span class="kw2">self</span><span class="br0">)</span>:
10 <span class="kw3">HTMLParser</span>.<span class="kw4">__init__</span><span class="br0">(</span><span class="kw2">self</span><span class="br0">)</span>
11 <span class="kw2">self</span>.<span class="me1">s</span> = <span class="st0">''</span>
13 <span class="kw1">def</span> handle_starttag<span class="br0">(</span><span class="kw2">self</span>, tag, attrs<span class="br0">)</span>:
14 <span class="kw1">if</span> tag == <span class="st0">'embed'</span>:
15 <span class="kw1">for</span> attr <span class="kw1">in</span> attrs:
16 <span class="kw1">if</span> attr<span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span> == <span class="st0">'flashvars'</span>:
17 <span class="kw2">self</span>.<span class="me1">s</span> = attr<span class="br0">[</span>1<span class="br0">]</span>
19 <span class="kw1">def</span> feed<span class="br0">(</span><span class="kw2">self</span>, data<span class="br0">)</span>:
20 <span class="kw3">HTMLParser</span>.<span class="me1">feed</span><span class="br0">(</span><span class="kw2">self</span>, data<span class="br0">)</span>
21 <span class="kw1">return</span> <span class="kw2">self</span>
23 <span class="kw1">def</span> get_s<span class="br0">(</span><span class="kw2">self</span><span class="br0">)</span>:
24 <span class="kw1">return</span> <span class="kw2">self</span>.<span class="me1">s</span>
26 <span class="kw1">class</span> Level<span class="br0">(</span>db.<span class="me1">Model</span><span class="br0">)</span>:
27 content = db.<span class="me1">TextProperty</span><span class="br0">(</span><span class="br0">)</span>
29 <span class="kw1">class</span> Current<span class="br0">(</span>db.<span class="me1">Model</span><span class="br0">)</span>:
30 x = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
31 y = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
32 d = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
33 n = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
34 m = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
35 l = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
37 <span class="kw1">class</span> Wrong<span class="br0">(</span>db.<span class="me1">Model</span><span class="br0">)</span>:
38 x = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
39 y = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
40 d = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
41 l = db.<span class="me1">IntegerProperty</span><span class="br0">(</span><span class="br0">)</span>
43 <span class="kw1">class</span> CrossPage<span class="br0">(</span>webapp.<span class="me1">RequestHandler</span><span class="br0">)</span>:
44 <span class="kw1">def</span> get<span class="br0">(</span><span class="kw2">self</span><span class="br0">)</span>:
45 url = <span class="st0">'http://www.hacker.org/cross/index.php?name=kauesilv&password=a1b2c3d4'</span>
46 regex = r<span class="st0">'boardinit = "([^"]*)"'</span>
47 debug = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'debug'</span><span class="br0">)</span>
48 s = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'s'</span><span class="br0">)</span>
49 path = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'path'</span><span class="br0">)</span>
50 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">headers</span><span class="br0">[</span><span class="st0">'Content-Type'</span><span class="br0">]</span> = <span class="st0">'text/plain'</span>
51 <span class="kw1">if</span> s == <span class="st0">'1'</span>:
52 <span class="kw3">urllib</span>.<span class="me1">urlopen</span><span class="br0">(</span>url + <span class="st0">'&sol='</span> + path<span class="br0">)</span>.<span class="me1">read</span><span class="br0">(</span><span class="br0">)</span>
53 s = <span class="kw3">urllib</span>.<span class="me1">urlopen</span><span class="br0">(</span>url<span class="br0">)</span>.<span class="me1">read</span><span class="br0">(</span><span class="br0">)</span>
54 <span class="co1">#self.response.out.write(s + '\n')</span>
55 board = <span class="kw3">re</span>.<span class="kw2">compile</span><span class="br0">(</span>regex<span class="br0">)</span>.<span class="me1">search</span><span class="br0">(</span>s<span class="br0">)</span>.<span class="me1">groups</span><span class="br0">(</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span>.<span class="me1">split</span><span class="br0">(</span><span class="st0">','</span><span class="br0">)</span>
56 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="kw2">str</span><span class="br0">(</span><span class="kw2">len</span><span class="br0">(</span>board<span class="br0">)</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">(</span><span class="kw2">len</span><span class="br0">(</span>board<span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">)</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">)</span>
57 <span class="kw1">for</span> line <span class="kw1">in</span> board:
58 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="kw2">str</span><span class="br0">(</span>line<span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">)</span>
60 <span class="kw1">class</span> MainPage<span class="br0">(</span>webapp.<span class="me1">RequestHandler</span><span class="br0">)</span>:
61 <span class="kw1">def</span> get<span class="br0">(</span><span class="kw2">self</span><span class="br0">)</span>:
62 delete = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'delete'</span><span class="br0">)</span>
63 <span class="kw1">if</span> delete == <span class="st0">'1'</span>:
64 db.<span class="me1">delete</span><span class="br0">(</span>Wrong.<span class="kw2">all</span><span class="br0">(</span><span class="br0">)</span>.<span class="me1">fetch</span><span class="br0">(</span>100<span class="br0">)</span><span class="br0">)</span>
65 <span class="kw1">if</span> Wrong.<span class="kw2">all</span><span class="br0">(</span><span class="br0">)</span>.<span class="me1">count</span><span class="br0">(</span>1<span class="br0">)</span> == 1:
66 <span class="kw1">return</span>
68 url = <span class="st0">'http://www.hacker.org/coil/index.php?name=kauesilv&password=a1b2c3d4'</span>
69 regex = r<span class="st0">'x=(<span class="es0">\d</span>*)&y=(<span class="es0">\d</span>*)&board=(.*)'</span>
70 regexLevel = r<span class="st0">'Level: (<span class="es0">\d</span>*)'</span>
71 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">headers</span><span class="br0">[</span><span class="st0">'Content-Type'</span><span class="br0">]</span> = <span class="st0">'text/plain'</span>
72 debug = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'debug'</span><span class="br0">)</span>
73 s = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'s'</span><span class="br0">)</span>
74 r = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'r'</span><span class="br0">)</span>
75 x = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'x'</span><span class="br0">)</span>
76 y = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'y'</span><span class="br0">)</span>
77 d = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'d'</span><span class="br0">)</span>
78 w = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'w'</span><span class="br0">)</span>
79 path = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">(</span><span class="st0">'path'</span><span class="br0">)</span>
81 levels = Level.<span class="kw2">all</span><span class="br0">(</span><span class="br0">)</span>
82 currents = Current.<span class="kw2">all</span><span class="br0">(</span><span class="br0">)</span>
84 <span class="kw1">if</span> s == <span class="st0">'1'</span>:
85 <span class="kw3">urllib</span>.<span class="me1">urlopen</span><span class="br0">(</span>url + <span class="st0">'&x='</span> + x + <span class="st0">'&y='</span> + y + <span class="st0">'&path='</span> + path<span class="br0">)</span>.<span class="me1">read</span><span class="br0">(</span><span class="br0">)</span>
87 <span class="kw1">if</span> w == <span class="st0">'1'</span>:
88 wrong = Wrong<span class="br0">(</span><span class="br0">)</span>
89 wrong.<span class="me1">x</span> = <span class="kw2">int</span><span class="br0">(</span>x<span class="br0">)</span>
90 wrong.<span class="me1">y</span> = <span class="kw2">int</span><span class="br0">(</span>y<span class="br0">)</span>
91 wrong.<span class="me1">d</span> = <span class="kw2">int</span><span class="br0">(</span>d<span class="br0">)</span>
92 wrong.<span class="me1">l</span> = currents.<span class="me1">fetch</span><span class="br0">(</span>1<span class="br0">)</span><span class="br0">[</span>0<span class="br0">]</span>.<span class="me1">l</span>
93 wrong.<span class="me1">put</span><span class="br0">(</span><span class="br0">)</span>
95 <span class="kw1">if</span> s == <span class="st0">'1'</span> <span class="kw1">or</span> r == <span class="st0">'1'</span>:
96 s = <span class="kw3">urllib</span>.<span class="me1">urlopen</span><span class="br0">(</span>url<span class="br0">)</span>.<span class="me1">read</span><span class="br0">(</span><span class="br0">)</span>
97 x, y, board = <span class="kw3">re</span>.<span class="kw2">compile</span><span class="br0">(</span>regex<span class="br0">)</span>.<span class="me1">search</span><span class="br0">(</span>MyHTMLParser<span class="br0">(</span><span class="br0">)</span>.<span class="me1">feed</span><span class="br0">(</span>s<span class="br0">)</span>.<span class="me1">get_s</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>.<span class="me1">groups</span><span class="br0">(</span><span class="br0">)</span>
98 lvl, = <span class="kw3">re</span>.<span class="kw2">compile</span><span class="br0">(</span>regexLevel<span class="br0">)</span>.<span class="me1">search</span><span class="br0">(</span>s<span class="br0">)</span>.<span class="me1">groups</span><span class="br0">(</span><span class="br0">)</span>
99 db.<span class="me1">delete</span><span class="br0">(</span>Level.<span class="kw2">all</span><span class="br0">(</span><span class="br0">)</span>.<span class="me1">fetch</span><span class="br0">(</span>1000000<span class="br0">)</span><span class="br0">)</span>
100 db.<span class="me1">delete</span><span class="br0">(</span>Current.<span class="kw2">all</span><span class="br0">(</span><span class="br0">)</span>.<span class="me1">fetch</span><span class="br0">(</span><span class="nu0">1000000</span><span class="br0">)</span><span class="br0">)</span>
102 level = Level<span class="br0">(</span><span class="br0">)</span>
103 level.<span class="me1">content</span> = x + <span class="st0">'<span class="es0">\n</span>'</span> + y + <span class="st0">'<span class="es0">\n</span>'</span> + board + <span class="st0">'<span class="es0">\n</span>'</span>
104 level.<span class="me1">put</span><span class="br0">(</span><span class="br0">)</span>
106 current = Current<span class="br0">(</span><span class="br0">)</span>
107 current.<span class="me1">x</span> = current.<span class="me1">y</span> = current.<span class="me1">d</span> = 0
108 current.<span class="me1">n</span> = <span class="kw2">int</span><span class="br0">(</span>x<span class="br0">)</span>
109 current.<span class="me1">m</span> = <span class="kw2">int</span><span class="br0">(</span>y<span class="br0">)</span>
110 current.<span class="me1">l</span> = <span class="kw2">int</span><span class="br0">(</span>lvl<span class="br0">)</span>
111 current.<span class="me1">put</span><span class="br0">(</span><span class="br0">)</span>
113 cur = currents.<span class="me1">fetch</span><span class="br0">(</span>1<span class="br0">)</span><span class="br0">[</span>0<span class="br0">]</span>
114 <span class="kw1">while</span><span class="br0">(</span><span class="kw2">True</span><span class="br0">)</span>:
115 cur.<span class="me1">d</span> = cur.<span class="me1">d</span> + 1
116 <span class="kw1">if</span><span class="br0">(</span>cur.<span class="me1">d</span> == 4<span class="br0">)</span>:
117 cur.<span class="me1">d</span> = 0
118 cur.<span class="me1">y</span> = cur.<span class="me1">y</span> + 1
119 <span class="kw1">if</span><span class="br0">(</span>cur.<span class="me1">y</span> == cur.<span class="me1">m</span><span class="br0">)</span>:
120 cur.<span class="me1">y</span> = 0
121 cur.<span class="me1">x</span> = cur.<span class="me1">x</span> + 1
122 <span class="kw1">if</span><span class="br0">(</span>cur.<span class="me1">x</span> == cur.<span class="me1">n</span><span class="br0">)</span>:
123 cur.<span class="me1">x</span> = <span class="nu0">0</span>
124 cur.<span class="me1">put</span><span class="br0">(</span><span class="br0">)</span>
125 wrong = Wrong<span class="br0">(</span><span class="br0">)</span>.<span class="me1">gql</span><span class="br0">(</span><span class="st0">"WHERE x = :1 AND y = :2 AND d = :3 AND l = :4"</span>, cur.<span class="me1">x</span>, cur.<span class="me1">y</span>, cur.<span class="me1">d</span>, cur.<span class="me1">l</span><span class="br0">)</span>
126 <span class="kw1">if</span><span class="br0">(</span>wrong.<span class="me1">count</span><span class="br0">(</span>1<span class="br0">)</span> == 0<span class="br0">)</span>:
127 <span class="kw1">break</span>
129 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="kw2">str</span><span class="br0">(</span>cur.<span class="me1">x</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">(</span>cur.<span class="me1">y</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">(</span>cur.<span class="me1">d</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">(</span>cur.<span class="me1">l</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">)</span>
130 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span>levels.<span class="me1">fetch</span><span class="br0">(</span>1<span class="br0">)</span><span class="br0">[</span>0<span class="br0">]</span>.<span class="me1">content</span><span class="br0">)</span>
132 <span class="kw1">if</span> debug == <span class="st0">'1'</span>:
133 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="st0">'<span class="es0">\n</span><span class="es0">\n</span>----DEBUG----<span class="es0">\n</span><span class="es0">\n</span>wrongs<span class="es0">\n</span>'</span><span class="br0">)</span>
134 wrongs = Wrong.<span class="me1">gql</span><span class="br0">(</span><span class="st0">"WHERE l = :1"</span>, cur.<span class="me1">l</span><span class="br0">)</span>
135 <span class="kw1">for</span> wrong <span class="kw1">in</span> wrongs:
136 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="kw2">str</span><span class="br0">(</span>wrong.<span class="me1">x</span><span class="br0">)</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">(</span>wrong.<span class="me1">y</span><span class="br0">)</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">(</span>wrong.<span class="me1">d</span><span class="br0">)</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">(</span>wrong.<span class="me1">l</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">)</span>
138 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="st0">'<span class="es0">\n</span>currents<span class="es0">\n</span>'</span><span class="br0">)</span>
139 <span class="kw1">for</span> current <span class="kw1">in</span> currents:
140 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">(</span><span class="kw2">str</span><span class="br0">(</span>current.<span class="me1">x</span><span class="br0">)</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">(</span>current.<span class="me1">y</span><span class="br0">)</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">(</span>current.<span class="me1">d</span><span class="br0">)</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">(</span>current.<span class="me1">l</span><span class="br0">)</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">)</span>
142 application = webapp.<span class="me1">WSGIApplication</span><span class="br0">(</span><span class="br0">[</span><span class="br0">(</span><span class="st0">'/'</span>, MainPage<span class="br0">)</span>, <span class="br0">(</span><span class="st0">'/cross'</span>, CrossPage<span class="br0">)</span><span class="br0">]</span>, debug=<span class="kw2">True</span><span class="br0">)</span>
144 <span class="kw1">def</span> main<span class="br0">(</span><span class="br0">)</span>:
145 run_wsgi_app<span class="br0">(</span>application<span class="br0">)</span>
147 <span class="kw1">if</span> __name__ == <span class="st0">"__main__"</span>:
148 main<span class="br0">(</span><span class="br0">)</span>