backup de julho
[h2N7SspZmY.git] / data / cache / b / bd566529dedac600e3f3ee99e50915b0.code
bloba3c30387bb4ea69e6b506e9bc37bad5319af2830
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>
7 &nbsp;
8 <span class="kw1">class</span> MyHTMLParser<span class="br0">&#40;</span><span class="kw3">HTMLParser</span><span class="br0">&#41;</span>:
9         <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:
10                 <span class="kw3">HTMLParser</span>.<span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>
11                 <span class="kw2">self</span>.<span class="me1">s</span> = <span class="st0">''</span>
12 &nbsp;
13         <span class="kw1">def</span> handle_starttag<span class="br0">&#40;</span><span class="kw2">self</span>, tag, attrs<span class="br0">&#41;</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">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> == <span class="st0">'flashvars'</span>:
17                                         <span class="kw2">self</span>.<span class="me1">s</span> = attr<span class="br0">&#91;</span>1<span class="br0">&#93;</span>
18 &nbsp;
19         <span class="kw1">def</span> feed<span class="br0">&#40;</span><span class="kw2">self</span>, data<span class="br0">&#41;</span>:
20                 <span class="kw3">HTMLParser</span>.<span class="me1">feed</span><span class="br0">&#40;</span><span class="kw2">self</span>, data<span class="br0">&#41;</span>
21                 <span class="kw1">return</span> <span class="kw2">self</span>
22 &nbsp;
23         <span class="kw1">def</span> get_s<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:
24                 <span class="kw1">return</span> <span class="kw2">self</span>.<span class="me1">s</span>
25 &nbsp;
26 <span class="kw1">class</span> Level<span class="br0">&#40;</span>db.<span class="me1">Model</span><span class="br0">&#41;</span>:
27         content = db.<span class="me1">TextProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
28 &nbsp;
29 <span class="kw1">class</span> Current<span class="br0">&#40;</span>db.<span class="me1">Model</span><span class="br0">&#41;</span>:
30         x = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
31         y = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
32         d = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
33         n = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
34         m = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
35         l = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
36 &nbsp;
37 <span class="kw1">class</span> Wrong<span class="br0">&#40;</span>db.<span class="me1">Model</span><span class="br0">&#41;</span>:
38         x = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
39         y = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
40         d = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
41         l = db.<span class="me1">IntegerProperty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
42 &nbsp;
43 <span class="kw1">class</span> CrossPage<span class="br0">&#40;</span>webapp.<span class="me1">RequestHandler</span><span class="br0">&#41;</span>:
44         <span class="kw1">def</span> get<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:
45                 url = <span class="st0">'http://www.hacker.org/cross/index.php?name=kauesilv&amp;password=a1b2c3d4'</span>
46                 regex = r<span class="st0">'boardinit = &quot;([^&quot;]*)&quot;'</span>
47                 debug = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'debug'</span><span class="br0">&#41;</span>
48                 s = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'s'</span><span class="br0">&#41;</span>
49                 path = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'path'</span><span class="br0">&#41;</span>
50                 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Content-Type'</span><span class="br0">&#93;</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">&#40;</span>url + <span class="st0">'&amp;sol='</span> + path<span class="br0">&#41;</span>.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
53                 s = <span class="kw3">urllib</span>.<span class="me1">urlopen</span><span class="br0">&#40;</span>url<span class="br0">&#41;</span>.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</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">&#40;</span>regex<span class="br0">&#41;</span>.<span class="me1">search</span><span class="br0">&#40;</span>s<span class="br0">&#41;</span>.<span class="me1">groups</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">','</span><span class="br0">&#41;</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">&#40;</span><span class="kw2">str</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>board<span class="br0">&#41;</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>board<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">&#41;</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">&#40;</span><span class="kw2">str</span><span class="br0">&#40;</span>line<span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">&#41;</span>
59 &nbsp;
60 <span class="kw1">class</span> MainPage<span class="br0">&#40;</span>webapp.<span class="me1">RequestHandler</span><span class="br0">&#41;</span>:
61         <span class="kw1">def</span> get<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:
62                 delete = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'delete'</span><span class="br0">&#41;</span>
63                 <span class="kw1">if</span> delete == <span class="st0">'1'</span>:
64                         db.<span class="me1">delete</span><span class="br0">&#40;</span>Wrong.<span class="kw2">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fetch</span><span class="br0">&#40;</span>100<span class="br0">&#41;</span><span class="br0">&#41;</span>
65                         <span class="kw1">if</span> Wrong.<span class="kw2">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">count</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span> == 1:
66                                 <span class="kw1">return</span>
67 &nbsp;
68                 url = <span class="st0">'http://www.hacker.org/coil/index.php?name=kauesilv&amp;password=a1b2c3d4'</span>
69                 regex = r<span class="st0">'x=(<span class="es0">\d</span>*)&amp;y=(<span class="es0">\d</span>*)&amp;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">&#91;</span><span class="st0">'Content-Type'</span><span class="br0">&#93;</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">&#40;</span><span class="st0">'debug'</span><span class="br0">&#41;</span>
73                 s = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'s'</span><span class="br0">&#41;</span>
74                 r = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'r'</span><span class="br0">&#41;</span>
75                 x = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'x'</span><span class="br0">&#41;</span>
76                 y = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'y'</span><span class="br0">&#41;</span>
77                 d = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'d'</span><span class="br0">&#41;</span>
78                 w = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'w'</span><span class="br0">&#41;</span>
79                 path = <span class="kw2">self</span>.<span class="me1">request</span>.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">'path'</span><span class="br0">&#41;</span>
80 &nbsp;
81                 levels = Level.<span class="kw2">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
82                 currents = Current.<span class="kw2">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
83 &nbsp;
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">&#40;</span>url + <span class="st0">'&amp;x='</span> + x + <span class="st0">'&amp;y='</span> + y + <span class="st0">'&amp;path='</span> + path<span class="br0">&#41;</span>.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
86 &nbsp;
87                 <span class="kw1">if</span> w == <span class="st0">'1'</span>:
88                         wrong = Wrong<span class="br0">&#40;</span><span class="br0">&#41;</span>
89                         wrong.<span class="me1">x</span> = <span class="kw2">int</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span>
90                         wrong.<span class="me1">y</span> = <span class="kw2">int</span><span class="br0">&#40;</span>y<span class="br0">&#41;</span>
91                         wrong.<span class="me1">d</span> = <span class="kw2">int</span><span class="br0">&#40;</span>d<span class="br0">&#41;</span>
92                         wrong.<span class="me1">l</span> = currents.<span class="me1">fetch</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span>.<span class="me1">l</span>
93                         wrong.<span class="me1">put</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
94 &nbsp;
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">&#40;</span>url<span class="br0">&#41;</span>.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
97                         x, y, board = <span class="kw3">re</span>.<span class="kw2">compile</span><span class="br0">&#40;</span>regex<span class="br0">&#41;</span>.<span class="me1">search</span><span class="br0">&#40;</span>MyHTMLParser<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">feed</span><span class="br0">&#40;</span>s<span class="br0">&#41;</span>.<span class="me1">get_s</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">groups</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
98                         lvl, = <span class="kw3">re</span>.<span class="kw2">compile</span><span class="br0">&#40;</span>regexLevel<span class="br0">&#41;</span>.<span class="me1">search</span><span class="br0">&#40;</span>s<span class="br0">&#41;</span>.<span class="me1">groups</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
99                         db.<span class="me1">delete</span><span class="br0">&#40;</span>Level.<span class="kw2">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fetch</span><span class="br0">&#40;</span>1000000<span class="br0">&#41;</span><span class="br0">&#41;</span>
100                         db.<span class="me1">delete</span><span class="br0">&#40;</span>Current.<span class="kw2">all</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fetch</span><span class="br0">&#40;</span><span class="nu0">1000000</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
101 &nbsp;
102                         level = Level<span class="br0">&#40;</span><span class="br0">&#41;</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">&#40;</span><span class="br0">&#41;</span>
105 &nbsp;
106                         current = Current<span class="br0">&#40;</span><span class="br0">&#41;</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">&#40;</span>x<span class="br0">&#41;</span>
109                         current.<span class="me1">m</span> = <span class="kw2">int</span><span class="br0">&#40;</span>y<span class="br0">&#41;</span>
110                         current.<span class="me1">l</span> = <span class="kw2">int</span><span class="br0">&#40;</span>lvl<span class="br0">&#41;</span>
111                         current.<span class="me1">put</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
112 &nbsp;
113                 cur = currents.<span class="me1">fetch</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span>
114                 <span class="kw1">while</span><span class="br0">&#40;</span><span class="kw2">True</span><span class="br0">&#41;</span>:
115                         cur.<span class="me1">d</span> = cur.<span class="me1">d</span> + 1
116                         <span class="kw1">if</span><span class="br0">&#40;</span>cur.<span class="me1">d</span> == 4<span class="br0">&#41;</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">&#40;</span>cur.<span class="me1">y</span> == cur.<span class="me1">m</span><span class="br0">&#41;</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">&#40;</span>cur.<span class="me1">x</span> == cur.<span class="me1">n</span><span class="br0">&#41;</span>:
123                                                 cur.<span class="me1">x</span> = <span class="nu0">0</span>
124                         cur.<span class="me1">put</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
125                         wrong = Wrong<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">gql</span><span class="br0">&#40;</span><span class="st0">&quot;WHERE x = :1 AND y = :2 AND d = :3 AND l = :4&quot;</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">&#41;</span>
126                         <span class="kw1">if</span><span class="br0">&#40;</span>wrong.<span class="me1">count</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span> == 0<span class="br0">&#41;</span>:
127                                 <span class="kw1">break</span>
128 &nbsp;
129                 <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">&#40;</span><span class="kw2">str</span><span class="br0">&#40;</span>cur.<span class="me1">x</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">&#40;</span>cur.<span class="me1">y</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">&#40;</span>cur.<span class="me1">d</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span> + <span class="kw2">str</span><span class="br0">&#40;</span>cur.<span class="me1">l</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">&#41;</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">&#40;</span>levels.<span class="me1">fetch</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span>.<span class="me1">content</span><span class="br0">&#41;</span>
131 &nbsp;
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">&#40;</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">&#41;</span>
134                         wrongs = Wrong.<span class="me1">gql</span><span class="br0">&#40;</span><span class="st0">&quot;WHERE l = :1&quot;</span>, cur.<span class="me1">l</span><span class="br0">&#41;</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">&#40;</span><span class="kw2">str</span><span class="br0">&#40;</span>wrong.<span class="me1">x</span><span class="br0">&#41;</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">&#40;</span>wrong.<span class="me1">y</span><span class="br0">&#41;</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">&#40;</span>wrong.<span class="me1">d</span><span class="br0">&#41;</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">&#40;</span>wrong.<span class="me1">l</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">&#41;</span>
137 &nbsp;
138                         <span class="kw2">self</span>.<span class="me1">response</span>.<span class="me1">out</span>.<span class="me1">write</span><span class="br0">&#40;</span><span class="st0">'<span class="es0">\n</span>currents<span class="es0">\n</span>'</span><span class="br0">&#41;</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">&#40;</span><span class="kw2">str</span><span class="br0">&#40;</span>current.<span class="me1">x</span><span class="br0">&#41;</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">&#40;</span>current.<span class="me1">y</span><span class="br0">&#41;</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">&#40;</span>current.<span class="me1">d</span><span class="br0">&#41;</span> + <span class="st0">' '</span> + <span class="kw2">str</span><span class="br0">&#40;</span>current.<span class="me1">l</span><span class="br0">&#41;</span> + <span class="st0">'<span class="es0">\n</span>'</span><span class="br0">&#41;</span>
141 &nbsp;
142 application = webapp.<span class="me1">WSGIApplication</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#40;</span><span class="st0">'/'</span>, MainPage<span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="st0">'/cross'</span>, CrossPage<span class="br0">&#41;</span><span class="br0">&#93;</span>, debug=<span class="kw2">True</span><span class="br0">&#41;</span>
143 &nbsp;
144 <span class="kw1">def</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>:
145         run_wsgi_app<span class="br0">&#40;</span>application<span class="br0">&#41;</span>
146 &nbsp;
147 <span class="kw1">if</span> __name__ == <span class="st0">&quot;__main__&quot;</span>:
148         main<span class="br0">&#40;</span><span class="br0">&#41;</span>