Now also monitor active/inactive status change
[fourmizzz.git] / members.py
index 8933084fef2ac6cb539151653d7a52a1fcc7eaa4..7192c28907cae1cdc9ebe169770fc6cea0a97268 100755 (executable)
@@ -10,6 +10,7 @@ import re
 import logging
 from datetime import datetime
 import calendar
+import random
 import urllib.request
 from http.cookiejar import CookieJar
 
@@ -19,7 +20,7 @@ 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, NOOP
+Action is one of: JOIN, QUIT, WIN, LOSS, FLOOD, NOOP, ACT
 
 Exemples:
 NOOP,,,,,,,,,,,No changes
@@ -31,6 +32,8 @@ FLOOD,filoux,Passeur 003,55813865,74225093,yosemite,Passeur 004,51142304,3273107
 FLOOD,Nick1,Grade1,100,102,Nick2,Grade2,100,99,1,1.00,Nick1 a pris 1 cm² à Nick2 (1.00%)
 FLOOD,Nick1,Grade1,100,102,Nick3,Grade3,100,99,1,1.00,Nick1 a pris 1 cm² à Nick3 (1.00%)
 
+ACT,Nick1,Grade1,100,100,,,,,,,Nick1 est en vacances
+
 Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
 """
 
@@ -296,7 +299,6 @@ def field_compare(oldmembers, newmembers, csvfile):
                     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)))
@@ -314,6 +316,24 @@ def field_compare(oldmembers, newmembers, csvfile):
     for txtchange in txtchanges2:
         csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
 
+def activity_compare(oldmembers, newmembers, csvfile):
+    txtchanges2 = []
+    for nick, data in newmembers.items():
+        newa = data.get('active', None)
+        olda = oldmembers.get(nick, {}).get('active', None)
+        if newa and olda and newa != olda:
+            txtchanges2.append('ACT,{nick1},{grade1},{olddata1},{newdata1},,,,,,,{nick1} est maintenant {lowernewdata1}'.format(
+                nick1=nick,
+                grade1=newmembers[nick]['grade'],
+                olddata1=olda,
+                newdata1=newa,
+                lowernewdata1=newa.lower(),
+                ))
+
+    for txtchange in txtchanges2:
+        logging.info(txtchange.split(',')[-1])
+        csvfile.write(time.strftime(DATETIME_FORMAT)+','+txtchange+'\n')
+
 if __name__ == '__main__':
     from optparse import OptionParser
     parser = OptionParser()
@@ -331,6 +351,11 @@ if __name__ == '__main__':
         help="Working directory, including config.py, archives and results. "
              "Relative to HOME unless it starts with /. "
              "Default is '%default'.")
+    parser.add_option('--quick',
+        action='store_true', dest='quick', default=False,
+        help="Don't sleep a random delay before hiting the server. "
+             "The programme usually wait between 0 and 59s, so that "
+             "there is no pick usage on a round minute")
     parser.add_option('--members-name',
         dest='archive_filename', metavar='FILENAME', default="archive.py",
         help="Override filename containing old data. "
@@ -359,6 +384,10 @@ if __name__ == '__main__':
         BASE_DIRECTORY = os.path.join(HOME, options.directory)
     logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt=DATETIME_FORMAT)
 
+    if not options.quick:
+        delay = random.randint(0,59)
+        sleep(delay)
+
     read_config(BASE_DIRECTORY)
 
     members_filename = os.path.join(BASE_DIRECTORY, options.archive_filename)
@@ -385,6 +414,7 @@ if __name__ == '__main__':
         csvfilename = os.path.join(BASE_DIRECTORY, 'results.csv')
         csvfile = open(csvfilename, mode='a', encoding='utf-8')
         field_compare(oldmembers, newmembers, csvfile)
+        activity_compare(oldmembers, newmembers, csvfile)
     
     # Save archive only after processing, just in case it crashes
     if not options.dryrun: