2 # -*- coding: utf-8 -*-
4 #Client app for zero player game godville.net
5 #Copyright (C) 2009 Apkawa
7 #This program is free software; you can redistribute it and/or
8 #modify it under the terms of the GNU General Public License
9 #as published by the Free Software Foundation; either version 2
10 #of the License, or (at your option) any later version.
12 #This program is distributed in the hope that it will be useful,
13 #but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 #GNU General Public License for more details.
17 #You should have received a copy of the GNU General Public License
18 #along with this program; if not, write to the Free Software
19 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #You can contact author by email apkawa@gmail.com
23 import httplib
, urllib
, urllib2
27 class MyRedirectHandler(urllib2
.HTTPRedirectHandler
):
28 def http_error_302(self
, req
, fp
, code
, msg
, headers
):
30 urllib2_30x
= urllib2
.build_opener( MyRedirectHandler
)
38 'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1) Gecko/20090710 Firefox/3.5 (Swiftfox) GTB5',
42 def auth(self
, username
, password
):
43 url
= 'http://www.godville.net/login/login'
44 __headers
= self
.headers
45 __headers
.update( {'Referer':'http://www.godville.net/login'} )
49 'commit':'Я уже бог!',
52 post_data
= urllib
.urlencode( post_data
)
53 req
= urllib2
.Request( url
, post_data
, __headers
)
55 u
= urllib2_30x
.open( req
)
56 except urllib2
.HTTPError
, e
:
58 self
.cookie
= e
.headers
.get('Set-Cookie')
59 self
.headers
.update( {'Cookie': self
.cookie
} )
61 raise Exception('Auth falled')
63 def get_main_page(self
):
64 #url = 'http://www.godville.net/hero'
65 __headers
= self
.headers
67 'Accept':'text/javascript, text/html, application/xml, text/xml, */*',
68 'X-Requested-With':'XMLHttpRequest',
69 'X-Prototype-Version':'1.6.0.3',
70 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'
72 url
= 'http://www.godville.net/hero/update_details'
73 post
= urllib
.urlencode({'':''})
74 req
= urllib2
.Request(url
, post
, __headers
)
75 u
= urllib2
.urlopen(req
)
77 print unicode(self
.page
)
79 def parse_block(self
, parser
, **attrs
):
80 block
= parser('div',attrs
)[0]
81 for line
in block('div',{'class':'new_line'}):
82 print line
.findAllNext(attrs
={'class':'l_capt'})[0].renderContents()
83 print line
.findAllNext(attrs
={'class':'field_content'})[0].renderContents()
87 with
open('hero.html','r') as f
:
89 init_parser
= html5lib
.HTMLParser(tree
=html5lib
.treebuilders
.getTreeBuilder("beautifulsoup"))
90 parser
= init_parser
.parse( self
.page
, 'utf-8' )
91 #print parser.prettify()
92 self
.parse_block(parser
, id='hero_info')
93 self
.parse_block(parser
, id='hero_stats')
99 'http://wiki.godville.net/index.php/API'
100 api_url
= 'http://www.godville.net/gods/api/%s.json'
103 u
'distance':u
'Столбов от столицы',
104 u
'gold_approx':u
'Золотых',
105 u
'inventory_max_num':'',
106 u
'inventory_num':u
'Инвентарь',
108 u
'health':u
'Здоровье',
110 u
'quest_progress':u
'Прогресс задания',
114 u
'godpower':u
'Прана',
115 u
'arena_fight':u
'Сражение на арене',
116 u
'inventory':u
'Инвентарь',
117 u
'diary_last':u
'Дневник героя',
120 u
'town_name':u
'Город',
121 u
'exp_progress':u
'Прогресс опыта',
122 u
'alignment':u
'Характер'
127 def load(self
, godname
='Godville'):
128 json_url
= self
.api_url
%godname
129 _u
= urllib
.urlopen( json_url
)
131 if not json_src
== 'Not found':
132 self
.json_loads
= json
.loads(json_src
)
134 raise Exception('Not found godname. Please enter correct name')
136 return self
.json_loads
137 def get_names_keys(self
):
138 return self
.names_keys
139 def get_name_key(self
, key
):
140 name
= self
.names_keys
.get(key
)
145 def print_info(self
):
146 for k
,v
in self
.json_loads
.items():
148 print self
.get_name_key( k
)
149 for ik
,iv
in v
.items():
150 print '* ',ik
, ', '.join( [
151 ':'.join((str(i
[0]),str(i
[1])) ) for i
in iv
.items()
154 print k
, self
.get_name_key(k
), ': ',v
158 #http://www.pixelbeat.org/docs/terminal_colours/
160 'level':'\033[1;33m',
166 def __init__(self
, name
):
169 def corored_text(self
, text
, color
):
170 return ''.join( (color
, text
, self
.ENDC
) )
171 def progress(self
, current
, max, segments
=30,full
='=', none
=' ' ):
173 current_segments
= (current
*segments
)/max
175 [full
for i
in xrange( current_segments
-1) ]
177 [ none
for i
in xrange( segments
-current_segments
)]
181 __out
= u
'''Бог: %(godname)s
185 Характер: %(alignment)s
187 Уровень: %(level_progress)s %(level)i
188 Инвентарь: %(inventory_progress)s %(inventory_num)i / %(inventory_max_num)i
189 Здоровье: %(health_progress)s %(health)i / %(max_health)i
190 Задание: %(quest_progress_bar)s %(quest_progress)s
192 Золотых: %(gold_approx)s
194 Прана: %(godpower_progress)s %(godpower)i
195 Дневник героя: %(diary_last)s
199 __info
= self
.get_info()
203 u
'В карманах у героя можно найти:\n%s'%'\n'.join(
204 [ '* '+i
for i
in __info
.get('inventory').keys()]
205 ) if __info
.get('inventory') else u
'В карманах у героя пусто.',
207 u
'Город: %10s'% __info
.get('town_name') if __info
.get('town_name') else u
'Столбов от столицы: %10i шт'%__info
.get('distance'),
209 self
.progress(__info
.get('exp_progress'),100),
210 'inventory_progress':
211 self
.progress(__info
.get('inventory_num'),
212 __info
.get('inventory_max_num')),
214 self
.progress(__info
.get( 'health'),
215 __info
.get('max_health'),),
217 self
.progress(__info
.get('godpower'),100),
218 'quest_progress_bar':
219 self
.progress(__info
.get('quest_progress'),100),
225 self
.lines
= len(_o
.split('\n'))
228 return ''.join( [ '\r' for i
in xrange( self
.lines
)] )
231 def main(self
, delay
=60):
236 print self
.make_out()
241 if __name__
== '__main__':
243 name
= os
.sys
.argv
[1]