Commits

ubershmekel committed 1d65484

Added a throttle for less frequent posts.

Ignore posts that contain "km" or "meter" because they may contain conversions
already.

Sleep a lot (600 vs 30 seconds) as to not annoy reddit.

  • Participants
  • Parent commits 1d7a93c

Comments (0)

Files changed (3)

File metricbot.py

     MPH: KPH,
 }
 
+IGNORE_IF_CONTAINS = 'meter', 'kg', 'cm', 'liter', 'gram', 'm/s'
 
 def nice_number_str(number):
     if abs(number) > 1e20:
         return '{0}'.format(number.normalize())
     
 
+def suspected_converted_already(text ):
+    for word in IGNORE_IF_CONTAINS:
+        if word in text:
+            # found a metric word there, maybe it was preconverted
+            return True
+    
+    return False
+    
+
 def nice_unit_name(unit):
     if unit in (KPH, MPH):
         return unit.name
     
     return conversions
 
+
 def metric_response(text):
     conversions = find_imperials(text)
     response = '\n\n'.join(conversions)
+    if suspected_converted_already(text):
+        return ''
     return response
 
 HOUR = 60 * 60
 MIN_TIME_FOR_REPEAT = 6 * HOUR
+MIN_DT_FOR_POST = 1 * HOUR
 
 class Throttle:
     def __init__(self):
         self.d = {}
+        self.last = None
     def is_ok_to_post(self, text):
         # text shouldn't repeat itself in a few hours
         now = time.time()
+        if self.last is not None and (now - self.last) < MIN_DT_FOR_POST:
+            return False
         
         if text in self.d:
             posted = self.d[text]
             deltat = now - posted
-            if deltat > MIN_TIME_FOR_REPEAT:
-                self.d[text] = now
-                return True
-            else:
+            if deltat <= MIN_TIME_FOR_REPEAT:
                 return False
-        else:
-            self.d[text] = now
-            return True
+
+        self.d[text] = now
+        self.last = now
+        return True
 
 globthrot = Throttle()
 

File redditbot.py

                 # is reddit down?
                 time.sleep(60)
             
-            time.sleep(30)
+            time.sleep(600)
     
     def handle_comment(self, comm):
         pass

File test_metricbot.py

            '-0.1 mph': '0.1 mph = 0.16 km/h',
            '1 feet 1 ft 1foot': '1 feet = 0.3 meters',
            '''I'm 6'3" 223lbs, so I avoid a lot of confrontations''': '''223 lbs = 101.15 kilograms\n\n6\'3" = 1.91 meters''',
+           '20 ft and 1 meter': '',
            }
         
         for text, expected in pairs.items():