Added hantill id scraping, and output in members.csv
[fourmizzz.git] / members.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Last version is available at svn://svn.nirgal.com/kod/fourmizzz/
5 # Licensed under GPL
6
7 import os
8 import time
9 import re
10 import logging
11 from datetime import datetime
12 import calendar
13 import random
14 import urllib.request
15 from http.cookiejar import CookieJar
16
17 from html_parser import *
18 import htmlentities
19
20 """
21 csv file format:
22 DateTime,Action,Nick1,Grade1,OldField1,NewField1,Nick2,Grade2,OldField2,NewField2,FloodField,Text
23 Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD, NOOP, ACT, ANTHILL, TECHNO, GRADE
24
25 Exemples:
26 NOOP,,,,,,,,,,Pas de changement dans le TdC
27 JOIN,Nirgal,Recrue,0,571628,,,,,,Nirgal a rejoint l'alliance avec 571 628 cm² 
28 QUIT,oncleblu,Chasseur,9940181,0,,,,,,oncleblu a quité l'alliance avec 9 940 181 cm² 
29 WIN,Nirgal,Chasseur,200000,257900,,,,,,Nirgal a gagné 57 900 cm² 
30 LOSS,Nirgal,Passeur 806,553624,442900,,,,,110724,Nirgal a perdu 110 724 cm² (20.00%) 
31 FLOOD,filoux,Passeur 003,55813865,74225093,yosemite,Passeur 004,51142304,32731076,18411228,filoux a pris 18 411 228 cm² à yosemite (36.00%)
32 FLOOD,Nick1,Grade1,100,102,Nick2,Grade2,100,99,1,Nick1 a pris 1 cm² à Nick2 (1.00%)
33 FLOOD,Nick1,Grade1,100,102,Nick3,Grade3,100,99,1,Nick1 a pris 1 cm² à Nick3 (1.00%)
34
35 ACT,Nick1,Grade1,Actif,En vacances,,,,,,Nick1 est maintenant en vacances
36 COLON,Nick1,Grade1,False,True,,,,,,Nick1 s'est fait colonisé
37 COLON,Nick1,Grade1,True,False,,,,,,Nick1 n'est plus colonisé
38
39 ANTHILL,Nick1,Grade1,199,200,,,,,,La fourmilière de Nick1 a atteint le niveau 200
40 TECHNO,Nick1,Grade1,199,200,,,,,,La technologie Nick1 a atteint le niveau 200
41
42 GRADE,Nick1,Recrue,Recrue,Passer 806,,,,,,Nick1 est maintenant Passeur 806
43
44 Note that in last 2 FLOOD lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
45 """
46
47 # globals:
48 #BASE_DIRECTORY
49 #SERVER
50 #LOGIN
51 #PASSWORD
52 #BASE_URL
53 DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
54
55 def read_config(config_dir):
56     '''
57     That function will read config.py in .fourmizzz directory
58     and set up globals SERVER, LOGIN, PASSWORD, and BASE_URL.
59     '''
60     global SERVER, LOGIN, PASSWORD
61     global BASE_URL
62     CONFIG_TEMPLATE='''# Veuillez modifier les lignes ci dessous avec vos paramètres:
63 SERVER = 's1.fourmizzz.fr'
64 LOGIN = 'MonIdentifiant'
65 PASSWORD = 'MonMotDePasse'
66     '''
67
68     sys.path.append(config_dir)
69     try:
70         import config
71     except ImportError:
72         config_filename = os.path.join(config_dir, 'config.py')
73         logging.fatal("No configuration file. Creating %s", config_filename)
74         f = open(config_filename, mode='w+', encoding='utf-8')
75         f.write(CONFIG_TEMPLATE)
76         f.close()
77         logging.fatal("Please update this file with your details.")
78         sys.exit(1)
79     SERVER = config.SERVER
80     LOGIN = config.LOGIN
81     PASSWORD = config.PASSWORD
82     BASE_URL = 'http://%s' % SERVER
83
84 def hide_password_from_log(txt):
85     return re.sub('mot_passe=.*', 'mot_passe=********', txt)
86
87 __opener__ = None
88 def httpopen(url, post_data=None):
89     if post_data:
90         log_post_data = hide_password_from_log(post_data)
91         post_data = post_data.encode('utf-8') # str->bytes
92         logging.debug('HTTP POST %s %s', url, log_post_data)
93     else:
94         logging.debug('HTTP GET %s', url)
95     global __opener__
96     if __opener__ is None:
97         cookiejar = CookieJar()
98         __opener__ = urllib.request.build_opener()
99         __opener__.add_handler(urllib.request.HTTPCookieProcessor(cookiejar))
100     http_response = __opener__.open(url, post_data)
101     return http_response
102
103 def sleep(seconds):
104     logging.debug('Waiting %s seconds', seconds)
105     time.sleep(seconds)
106
107 def timestamp():
108     return calendar.timegm(datetime.now().timetuple())
109
110 def number_format(i):
111     result = ''
112     while i:
113         if result:
114             result = ' ' + result
115         d3 = i % 1000
116         i = i//1000
117         if i: # there will be something else left of that
118             result = ('%03d' % d3) + result
119         else:
120             result = ('%d' % d3) + result
121     return result
122
123
124 def members_fetch(alliance_tag=None):
125     #httpresponse = httpopen(BASE_URL + '/')
126     #html = httpresponse.read().decode('utf-8')
127     #print(html)
128    
129     httpresponse = httpopen(BASE_URL + '/index.php?connexion=1', 'serveur=%s&pseudo=%s&mot_passe=%s' % (SERVER, LOGIN, PASSWORD))
130     #httpresponse = httpopen(BASE_URL + '/index.php?connexion=1', 'serveur=%s&pseudo=%s&mot_passe=%s&connexion=Connexion' % (SERVER, LOGIN, PASSWORD))
131     html = httpresponse.read().decode('utf-8')
132     if "redirectReine" not in html:
133         logging.fatal("Impossible de s'authentifier. Vérifiez vos paramètres dans config.py")
134         sys.exit(1)
135     #print('-'*80)
136     #print(html)
137     
138     #httpresponse = httpopen(BASE_URL + '/alliance.php?Membres')
139     #html = httpresponse.read().decode('utf-8')
140     #print('-'*80)
141     #print(html)
142
143     if not alliance_tag:
144         httpresponse = httpopen(BASE_URL + '/alliance.php?Membres',
145             'xajax=membre&xajaxr='+str(timestamp()))
146         html = httpresponse.read().decode('utf-8')
147         #print('-'*80)
148         #print(html)
149
150         root = html_parse(html)
151         table = get_elem(root, 'table')[0]
152         td = get_elem(table, 'td')[3]
153         table = get_elem(td, 'table')[0]
154         #print('-'*80)
155         #print_idented_tree(table)
156     else:
157         httpresponse = httpopen(BASE_URL + '/classementAlliance.php?alliance=%s' % alliance_tag)
158         html = httpresponse.read().decode('utf-8')
159         
160         root = html_parse(html)
161         table = get_elem(root, 'table')[2]
162         #print('-'*80)
163         #print_idented_tree(table)
164
165     members = {} # NEW
166     for tr in get_elem(table, 'tr'):
167         row = get_elem(tr, 'td')
168         #print('-'*10)
169         #print_idented_tree(tr)
170         if not alliance_tag:
171             if len(row) != 10:
172                 continue
173             grade = get_merged_leaf_content(row[2])
174             nick = get_merged_leaf_content(row[3])
175             field = int(get_merged_leaf_content(row[4]).replace(' ', ''))
176             techno = int(get_merged_leaf_content(row[5]))
177             anthill = int(get_merged_leaf_content(row[6]))
178             active = get_elem(row[7], 'img')[0].attributes['alt']
179             colonized = len(get_elem(row[8], 'img')) == 1
180             #print(nick, grade, field, techno, anthill, active, colonized)
181
182         else:
183             if len(row) != 6:
184                 continue
185             grade = get_merged_leaf_content(row[1])
186             nick = get_merged_leaf_content(row[2])
187             field = int(get_merged_leaf_content(row[3]).replace(' ', ''))
188             techno = int(get_merged_leaf_content(row[4]))
189             anthill = int(get_merged_leaf_content(row[5]))
190             active = None
191             colonized = None
192             #print(nick, grade, field, techno, anthill)
193
194         members[nick] = {
195             'grade': grade,
196             'field': field,
197             'techno': techno,
198             'anthill': anthill,
199             'active': active,
200             'colonized': colonized,
201         }
202         #print(members[nick])
203
204     logging.debug('%s members - total field = %s cm²', len(members), number_format(sum([x['field'] for x in members.values()])))
205     return members
206
207
208 def members_fill_positions(newmembers):
209     for nick, data in newmembers.items():
210         if data['id'] is not None:
211             continue
212
213         httpresponse = httpopen(BASE_URL + '/Membre.php?Pseudo=' + nick)
214         html = httpresponse.read().decode('utf-8')
215
216         f = open("debug.html", mode='w+', encoding='utf-8')
217         f.write(html)
218
219         match = re.match('.*x=([0-9]+) et y=([0-9]+)', html, re.DOTALL|re.MULTILINE)
220         x = int(match.group(1))
221         y = int(match.group(2))
222         newmembers[nick]['x'] = x
223         newmembers[nick]['y'] = y
224
225         match=re.match('.*commerce.php\\?ID=([0-9]+).*', html, re.DOTALL|re.MULTILINE)
226         _id = int(match.group(1))
227         newmembers[nick]['id'] = _id
228
229         logging.debug('%s has position %s %s and ID %s', nick, x, y, _id)
230
231         break # only one at a time (debug)
232
233 def copy_existing_data(oldmembers, newmembers):
234     '''
235     Check if newmembers contains some keys and if missing, copy them from
236     oldmembers:
237     colonized, active: if alliance is forced, these keys are not fetched
238     x, y, id: only fetch them when neded.
239     '''
240     for nick, data in newmembers.items():
241         for key in ['colonized', 'active', 'x', 'y', 'id' ]:
242             if data.get(key, None) is None:
243                 # missing or None
244                 value = oldmembers.get(nick, {}).get(key, None)
245                 newmembers[nick][key] = value
246
247 def members_to_field(members):
248     """
249     Takes a full information dict "members"
250     Returns a simple dict with nick->field
251     """
252     fields  = {}
253     for nick, data in members.items():
254         fields[nick] = data['field']
255     return fields
256
257
258 def publish_line(csvfile, text, **args):
259     csvline = text.format(**args)
260     logging.info(csvline.split(',')[-1])
261     if not options.dryrun:
262         csvfile.write(time.strftime(DATETIME_FORMAT)+','+csvline+'\n')
263
264 def field_compare(oldmembers, newmembers, csvfile):
265     
266     oldfield = members_to_field(oldmembers)
267     newfield = members_to_field(newmembers)
268
269     changes = {}
270
271     for nick in newfield.keys():
272         told = oldfield.get(nick, 0)
273         tnew = newfield[nick]
274         if told == tnew:
275             continue
276         changes[nick] = { 'old': told, 'new': tnew, 'delta': tnew-told }
277     
278     for nick in oldfield.keys():
279         if nick not in newfield:
280             told = oldfield[nick]
281             tnew = 0
282             changes[nick] = { 'old': told, 'new': 0, 'delta': -told }
283     #for nick, change in changes.items():
284     #    print(nick, ' - ', change['old'], ' - ', change['new'], ' - ', change['delta'])
285     #print
286
287     if not len(changes):
288         publish_line(csvfile, 'NOOP,,,,,,,,,,No changes')
289  
290     for nick in changes.keys():
291         delta = changes[nick]['delta']
292         if delta <= 0:
293             continue # 0 is already process, <0 will be processed when nick swaps with nick2
294         for nick2 in changes.keys():
295             if changes[nick2]['delta'] == 0:
296                 continue # already done
297             if changes[nick2]['delta'] != -delta:
298                 continue # not the good one
299             percent = float(oldfield[nick2] - newfield[nick2]) / oldfield[nick2] * 100.
300             publish_line(csvfile, 
301                 'FLOOD,{nick1},{grade1},{oldfield1},{newfield1},{nick2},{grade2},{oldfield2},{newfield2},{deltafield},{nick1} a pris {nicedeltafield} cm² à {nick2} ({percent:.2f}%)',
302                 nick1=nick,
303                 grade1=newmembers[nick].get('grade', ''),
304                 oldfield1=oldfield[nick],
305                 newfield1=newfield[nick],
306                 nick2=nick2,
307                 grade2=newmembers[nick2].get('grade', ''),
308                 oldfield2=oldfield[nick2],
309                 newfield2=newfield[nick2],
310                 deltafield=delta,
311                 nicedeltafield= number_format(delta),
312                 percent=percent,
313                 )
314             changes[nick]['delta'] = 0
315             changes[nick2]['delta'] = 0
316
317     for nick, change in changes.items():
318         delta = change['delta']
319         if delta < 0:
320             if nick in newfield:
321                 percent = float(-delta) / oldfield[nick] * 100.
322                 publish_line(csvfile, 
323                     'LOSS,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{nick1} a perdu {nicedeltafield} cm² ({percent:.2f}%)',
324                     nick1=nick,
325                     grade1=newmembers[nick].get('grade', ''),
326                     oldfield1=oldfield[nick],
327                     newfield1=newfield[nick],
328                     deltafield=-delta,
329                     nicedeltafield=number_format(-delta),
330                     percent=percent,
331                     )
332             else:
333                 publish_line(csvfile, 
334                     'QUIT,{nick1},{grade1},{oldfield1},0,,,,,{deltafield},{nick1} a quité l\'alliance avec {nicedeltafield} cm²',
335                     nick1=nick,
336                     grade1=oldmembers[nick].get('grade', ''),
337                     oldfield1=oldfield[nick],
338                     deltafield=-delta,
339                     nicedeltafield=number_format(-delta),
340                     )
341         elif delta > 0:
342             if nick in oldfield:
343                 publish_line(csvfile, 
344                     'WIN,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{nick1} a gagné {nicedeltafield} cm²',
345                     nick1=nick,
346                     grade1=newmembers[nick].get('grade', ''),
347                     oldfield1=oldfield[nick],
348                     newfield1=newfield[nick],
349                     deltafield=delta,
350                     nicedeltafield=number_format(delta),
351                     )
352             else:
353                 publish_line(csvfile, 
354                     'JOIN,{nick1},{grade1},0,{newfield1},,,,,{deltafield},{nick1} a rejoint l\'alliance avec {nicedeltafield} cm²',
355                     nick1=nick,
356                     grade1=newmembers[nick].get('grade', ''),
357                     newfield1=newfield[nick],
358                     deltafield=delta,
359                     nicedeltafield=number_format(delta),
360                     )
361
362 def activity_compare(oldmembers, newmembers, csvfile):
363     for nick, data in newmembers.items():
364         newa = data.get('active', None)
365         olda = oldmembers.get(nick, {}).get('active', None)
366         if newa and olda and newa != olda:
367             publish_line(csvfile, 
368                 'ACT,{nick1},{grade1},{olddata1},{newdata1},,,,,,{nick1} est maintenant {lowernewdata1}',
369                 nick1=nick,
370                 grade1=newmembers[nick]['grade'],
371                 olddata1=olda,
372                 newdata1=newa,
373                 lowernewdata1=newa.lower(),
374                 )
375
376 def grade_compare(oldmembers, newmembers, csvfile):
377     for nick, data in newmembers.items():
378         newdata = data.get('grade', None)
379         olddata = oldmembers.get(nick, {}).get('grade', None)
380         if newdata is not None and olddata is not None and newdata != olddata:
381             publish_line(csvfile, 
382                 'GRADE,{nick1},{grade1},{olddata1},{newdata1},,,,,,{nick1} est désormais {grade1}',
383                 nick1=nick,
384                 grade1=newmembers[nick]['grade'],
385                 olddata1=olddata,
386                 newdata1=newdata,
387                 )
388
389 def colonized_compare(oldmembers, newmembers, csvfile):
390     for nick, data in newmembers.items():
391         newc = data.get('colonized', None)
392         oldc = oldmembers.get(nick, {}).get('colonized', None)
393         if newc is not None and oldc is not None and newc != oldc:
394             if newc:
395                 txt = "s'est fait colonisé"
396             else:
397                 txt = "n'est plus colonisé"
398             publish_line(csvfile, 
399                 'COLON,{nick1},{grade1},{olddata1},{newdata1},,,,,,{nick1} {txt}',
400                 nick1=nick,
401                 grade1=newmembers[nick]['grade'],
402                 olddata1=str(oldc),
403                 newdata1=str(newc),
404                 txt=txt,
405                 )
406
407 def evolution_compare(oldmembers, newmembers, csvfile):
408     for nick, data in newmembers.items():
409         newdata = data['anthill']
410         olddata = oldmembers.get(nick, {}).get('anthill', None)
411         if newdata is not None and olddata is not None and newdata != olddata:
412             publish_line(csvfile, 
413                 'ANTHILL,{nick1},{grade1},{olddata1},{newdata1},,,,,,La fourmilière de {nick1} a atteint le niveau {newdata1}',
414                 nick1=nick,
415                 grade1=newmembers[nick]['grade'],
416                 olddata1=olddata,
417                 newdata1=newdata,
418                 )
419         newdata = data['techno']
420         olddata = oldmembers.get(nick, {}).get('techno', None)
421         if newdata is not None and olddata is not None and newdata != olddata:
422             publish_line(csvfile, 
423                 'TECHNO,{nick1},{grade1},{olddata1},{newdata1},,,,,,La technologie de {nick1} a atteint le niveau {newdata1}',
424                 nick1=nick,
425                 grade1=newmembers[nick]['grade'],
426                 olddata1=olddata,
427                 newdata1=newdata,
428                 )
429
430 if __name__ == '__main__':
431     from optparse import OptionParser
432     parser = OptionParser()
433     parser.add_option('-d', '-v', '--debug', '--verbose',
434         action='store_true', dest='debug', default=False,
435         help="debug mode")
436     parser.add_option('-q', '--quiet',
437         action='store_true', dest='quiet', default=False,
438         help="quiet mode")
439     parser.add_option('-a', '--alliance',
440         dest='alliance', metavar="TAG",
441         help="Default is to process player own alliance.")
442     parser.add_option('--dir',
443         dest='directory', default='.fourmizzz',
444         help="Working directory, including config.py, archives and results. "
445              "Relative to HOME unless it starts with /. "
446              "Default is '%default'.")
447     parser.add_option('--quick',
448         action='store_true', dest='quick', default=False,
449         help="Don't sleep a random delay before hiting the server. "
450              "The programme usually wait between 0 and 59s, so that "
451              "there is no pick usage on a round minute")
452     parser.add_option('--members-name',
453         dest='archive_filename', metavar='FILENAME', default="archive.py",
454         help="Override filename containing old data. "
455         "Default is '%default' in Working directory. "
456         "See --dir.")
457     parser.add_option('--new-members-name',
458         dest='new_archive_filename', metavar='FILENAME',
459         help="Read new members data from a file. "
460         "Default is to fetch data from antzzz server.")
461     parser.add_option('--dry-run',
462         action='store_true', dest='dryrun', default=False,
463         help="don't store result in archives.")
464     options, args = parser.parse_args()
465
466     if options.debug:
467         loglevel = logging.DEBUG
468     elif options.quiet:
469         loglevel = logging.WARNING
470     else:
471         loglevel = logging.INFO
472     
473     HOME = os.environ['HOME']
474     if options.directory[0] == os.path.sep: # '/' on linux
475         BASE_DIRECTORY = options.directory
476     else:
477         BASE_DIRECTORY = os.path.join(HOME, options.directory)
478     logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
479
480     if not options.quick:
481         delay = random.randint(0,59)
482         sleep(delay)
483
484     read_config(BASE_DIRECTORY)
485
486     members_filename = os.path.join(BASE_DIRECTORY, options.archive_filename)
487     oldmembers = None
488     try:
489         f = open(members_filename, mode='r+', encoding='utf-8')
490     except IOError as err:
491         if err.errno == 2: # No such file or directory
492             logging.warning("No archive file, creating one.")
493             f = open(members_filename, mode='w+', encoding='utf-8')
494         else:
495             raise
496     else:
497         oldmembers = eval(f.read())
498     
499     if options.new_archive_filename:
500         new_archive_filename = os.path.join(BASE_DIRECTORY, options.new_archive_filename)
501         newf = open(new_archive_filename, mode='r', encoding='utf8')
502         newmembers = eval(newf.read())
503     else:
504         newmembers = members_fetch(options.alliance)
505
506     if oldmembers is not None:
507         copy_existing_data(oldmembers, newmembers)
508         if not options.new_archive_filename:
509             members_fill_positions(newmembers)
510         csvfilename = os.path.join(BASE_DIRECTORY, 'results.csv')
511         csvfile = open(csvfilename, mode='a', encoding='utf-8')
512         field_compare(oldmembers, newmembers, csvfile)
513         grade_compare(oldmembers, newmembers, csvfile)
514         activity_compare(oldmembers, newmembers, csvfile)
515         colonized_compare(oldmembers, newmembers, csvfile)
516         evolution_compare(oldmembers, newmembers, csvfile)
517     
518     # Save archive only after processing, just in case it crashes
519     if not options.dryrun:
520         f.seek(0)
521         f.write(repr(newmembers))
522         f.truncate()
523         f.close()
524
525         # We work in members.csv.new file then rename it
526         # In the unlikely event that some script is reading it
527         # while we updated it
528         members_csvfilename = os.path.join(BASE_DIRECTORY, "members.csv")
529         members_csvfilename_new = members_csvfilename + '.new'
530         f = open(members_csvfilename_new, mode='w', encoding='utf-8')
531         f.write('grade,nick,field,techno,anthill,active,colonized,x,y,id\n')
532         for nick, data in newmembers.items():
533             data = [
534                 data['grade'],
535                 nick,
536                 data['field'],
537                 data['techno'],
538                 data['anthill'],
539                 data['active'],
540                 data['colonized'],
541                 data['x'],
542                 data['y'],
543                 data['id'],
544                 ]
545             data = [ str(x) for x in data ]
546             f.write(','.join(data) + '\n')
547         f.close()
548         os.rename(members_csvfilename_new, members_csvfilename)