Now generate data in results.csv
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 26 May 2012 15:21:28 +0000 (15:21 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 26 May 2012 15:21:28 +0000 (15:21 +0000)
members.py

index 0fbaa79e0193eaf49ced8d843d7749e9ed41724e..2ecefa6244bad55473679e41809dd621a7ee9988 100755 (executable)
@@ -19,9 +19,10 @@ import htmlentities
 """
 csv file format:
 DateTime,Action,Nick1,Grade1,OldField1,NewField1,Nick2,Grade2,OldField2,NewField2,FloodField,FieldLossPercent,Text
-Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD
+Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD, NOOP
 
 Exemples:
+NOOP,,,,,,,,,,,No changes
 JOIN,Nirgal,Recrue,0,571628,,,,,,,Nirgal a rejoint l'alliance avec 571 628 cm² 
 QUIT,oncleblu,Chasseur,9940181,0,,,,,,,oncleblu a quité l'alliance avec 9 940 181 cm² 
 WIN,Nirgal,Chasseur,200000,257900,,,,,,,Nirgal a gagné 57 900 cm² 
@@ -39,6 +40,7 @@ Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1
 #LOGIN
 #PASSWORD
 #BASE_URL
+DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S %Z'
 
 def read_config(config_dir):
     '''
@@ -204,13 +206,15 @@ def members_to_field(members):
     return fields
 
 
-def field_compare(oldmembers, newmembers):
+def field_compare(oldmembers, newmembers, csvfile):
     
     oldfield = members_to_field(oldmembers)
     newfield = members_to_field(newmembers)
 
     changes = {}
     txtchanges = []
+    txtchanges2 = []
+
     for nick in newfield.keys():
         told = oldfield.get(nick, 0)
         tnew = newfield[nick]
@@ -242,6 +246,19 @@ def field_compare(oldmembers, newmembers):
                 continue # not the good one
             percent = float(oldfield[nick2] - newfield[nick2]) / oldfield[nick2] * 100.
             txtchanges.append('%s a pris %s cm² à %s (%.2f%%)' % (nick, number_format(delta), nick2, percent))
+            txtchanges2.append('FLOOD,{nick1},{grade1},{oldfield1},{newfield1},{nick2},{grade2},{oldfield2},{newfield2},{deltafield},{percent},{nick1} a pris {deltafield} cm² à {nick2} ({percent:.2f}%)'.format(
+                nick1=nick,
+                grade1=newmembers[nick].get('grade', ''),
+                oldfield1=oldfield[nick],
+                newfield1=newfield[nick],
+                nick2=nick2,
+                grade2=newmembers[nick2].get('grade', ''),
+                oldfield2=oldfield[nick2],
+                newfield2=newfield[nick2],
+                deltafield=delta,
+                nicedeltafield= number_format(delta),
+                percent=percent,
+                ))
             changes[nick]['delta'] = 0
             changes[nick2]['delta'] = 0
 
@@ -251,17 +268,51 @@ def field_compare(oldmembers, newmembers):
             if nick in newfield:
                 percent = float(-delta) / oldfield[nick] * 100.
                 txtchanges.append('%s a perdu %s cm² (%.2f%%)' % (nick, number_format(-delta), percent))
+                txtchanges2.append('LOSS,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},{percent},{nick1} a perdu {nicedeltafield} cm² ({percent:.2f}%)'.format(
+                    nick1=nick,
+                    grade1=newmembers[nick].get('grade', ''),
+                    oldfield1=oldfield[nick],
+                    newfield1=newfield[nick],
+                    deltafield=-delta,
+                    nicedeltafield=number_format(-delta),
+                    percent=percent,
+                    ))
             else:
                 txtchanges.append("%s a quité l'alliance avec %s cm²" % (nick, number_format(-delta)))
+                txtchanges2.append('QUIT,{nick1},{grade1},{oldfield1},0,,,,,{deltafield},,{nick1} a quité l\'alliance avec {nicedeltafield} cm²'.format(
+                    nick1=nick,
+                    grade1=oldmembers[nick].get('grade', ''),
+                    oldfield1=oldfield[nick],
+                    deltafield=-delta,
+                    nicedeltafield=number_format(-delta),
+                    ))
         elif delta > 0:
             if nick in oldfield:
                 txtchanges.append('%s a gagné %s cm²' % (nick, number_format(delta)))
+                txtchanges2.append('WIN,{nick1},{grade1},{oldfield1},{newfield1},,,,,{deltafield},,{nick1} a gagné {nicedeltafield} cm²'.format(
+                    nick1=nick,
+                    grade1=newmembers[nick].get('grade', ''),
+                    oldfield1=oldfield[nick],
+                    newfield1=newfield[nick],
+                    deltafield=delta,
+                    nicedeltafield=number_format(delta),
+                    percent=percent,
+                    ))
             else:
                 txtchanges.append("%s a rejoint l'alliance avec %s cm²" % (nick, number_format(delta)))
+                txtchanges2.append('JOIN,{nick1},{grade1},0,{newfield1},,,,,{deltafield},,{nick1} a rejoint l\'alliance avec {nicedeltafield} cm²'.format(
+                    nick1=nick,
+                    grade1=newmembers[nick].get('grade', ''),
+                    newfield1=newfield[nick],
+                    deltafield=delta,
+                    nicedeltafield=number_format(delta),
+                    ))
 
     for txtchange in txtchanges:
         logging.info(txtchange)
 
+    for txtchange in txtchanges2:
+        csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
 
 if __name__ == '__main__':
     from optparse import OptionParser
@@ -311,10 +362,10 @@ if __name__ == '__main__':
         BASE_DIRECTORY = os.path.join(HOME, options.directory)
 
     if options.stdout:
-        logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S %Z')
+        logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
     else:
         result_filename = os.path.join(BASE_DIRECTORY, 'results.log')
-        logging.basicConfig(filename=result_filename, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S %Z')
+        logging.basicConfig(filename=result_filename, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
 
     read_config(BASE_DIRECTORY)
 
@@ -339,7 +390,9 @@ if __name__ == '__main__':
         newmembers = members_fetch(options.alliance)
 
     if oldmembers is not None:
-        field_compare(oldmembers, newmembers)
+        csvfilename = os.path.join(BASE_DIRECTORY, 'results.csv')
+        csvfile = open(csvfilename, mode='a', encoding='utf-8')
+        field_compare(oldmembers, newmembers, csvfile)
     
     # Save archive only after processing, just in case it crashes
     if not options.dryrun: