|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from itchat.content import * |
| 4 | +from ProcessInterface import ProcessInterface |
| 5 | +from utilities import * |
| 6 | +from time import time, sleep |
| 7 | +from threading import Timer |
| 8 | +from datetime import datetime |
| 9 | +import itchat |
| 10 | +import re |
| 11 | +import logging |
| 12 | + |
| 13 | +def clearGaNumDict(): |
| 14 | + GaTextHook.gaNumDict = {} |
| 15 | + client[dbName][gaCollName].remove({}, {'multi': True}) |
| 16 | + logging.info('GaNumDict cleared. GaNumDict = {0}.'.format(GaTextHook.gaNumDict)) |
| 17 | + scheduleTimerToClearGaNumDict() |
| 18 | + |
| 19 | +def scheduleTimerToClearGaNumDict(): |
| 20 | + t = datetime.today() |
| 21 | + t2 = t.replace(day=t.day+1, hour=9, minute=0, second=0, microsecond=0) # 0:00 in China |
| 22 | + deltaT = t2 - t |
| 23 | + secs = deltaT.seconds + 1 |
| 24 | + Timer(secs, clearGaNumDict).start() |
| 25 | + |
| 26 | +# The logic is getting more complicated. We make it a seprate processor |
| 27 | +class GaTextHook(ProcessInterface): |
| 28 | + gaNumDict = {} |
| 29 | + def __init__(self, blacklist=[]): |
| 30 | + self.blacklist = blacklist |
| 31 | + self.client = client |
| 32 | + self.gaColl = self.client[dbName][gaCollName] |
| 33 | + GaTextHook.gaNumDict = { x['GroupName']: x['CurrentGaNum'] for x in self.gaColl.find() } |
| 34 | + self.gaNumMax = 100 |
| 35 | + self.triggerText = '鸭哥' |
| 36 | + self.gaText = '嘎?' |
| 37 | + self.forceTriggerText = '鸭哥嘎一个' |
| 38 | + self.forceTriggerNextTimestamp = {} |
| 39 | + self.forceTriggerInterval = 5 * 60 # 5 minutes |
| 40 | + self.forceTriggerGaText = '强力嘎!' |
| 41 | + scheduleTimerToClearGaNumDict() |
| 42 | + |
| 43 | + # Set up the clear timer |
| 44 | + logging.info('GaTextHook initialized.') |
| 45 | + |
| 46 | + def process(self, msg, type): |
| 47 | + if type != TEXT: |
| 48 | + return |
| 49 | + groupName = msg['User']['NickName'] |
| 50 | + toSend = None |
| 51 | + if any([ re.search(x, groupName) is not None for x in self.blacklist ]): |
| 52 | + return |
| 53 | + if re.search(self.forceTriggerText, msg['Content']): |
| 54 | + currentTime = time() |
| 55 | + gaNextTime = self.forceTriggerNextTimestamp.get(groupName, 0) |
| 56 | + if currentTime < gaNextTime: |
| 57 | + logging.info("Don't force Ga because time {0} < NextTime {1} for group {2}.".format(currentTime, gaNextTime, groupName)) |
| 58 | + return; |
| 59 | + self.forceTriggerNextTimestamp[groupName] = currentTime + self.forceTriggerInterval |
| 60 | + toSend = self.forceTriggerGaText |
| 61 | + logging.info('{0} => {1}'.format(msg['Content'], toSend)) |
| 62 | + itchat.send(toSend, msg['FromUserName']) |
| 63 | + return |
| 64 | + if re.search(self.triggerText, msg['Content']): |
| 65 | + # Check the ga time |
| 66 | + if groupName not in GaTextHook.gaNumDict: |
| 67 | + GaTextHook.gaNumDict[groupName] = 0 |
| 68 | + GaTextHook.gaNumDict[groupName] += 1 |
| 69 | + self.gaColl.update({'GroupName': groupName}, {'$set': { 'CurrentGaNum': GaTextHook.gaNumDict[groupName] } }, upsert=True) |
| 70 | + if GaTextHook.gaNumDict[groupName] > self.gaNumMax: |
| 71 | + logging.info("Don't Ga because GaNum {0} exceeds max {1} for group {2}.".format(GaTextHook.gaNumDict[groupName], self.gaNumMax, groupName)) |
| 72 | + return |
| 73 | + toSend = '{0} x{1}'.format(self.gaText, GaTextHook.gaNumDict[groupName]) |
| 74 | + logging.info('{0} => {1}'.format(msg['Content'], toSend)) |
| 75 | + itchat.send(toSend, msg['FromUserName']) |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) |
| 79 | + hook = GaTextHook() |
0 commit comments