Browse Source

add limiter #7

tags/1.3
Dryusdan 2 years ago
parent
commit
cb15c9a4b4
4 changed files with 44 additions and 8 deletions
  1. +2
    -1
      README.md
  2. +42
    -7
      bot.py
  3. +0
    -0
      limiter/hour/README.md
  4. +0
    -0
      limiter/minute/README.md

+ 2
- 1
README.md View File

@@ -18,7 +18,8 @@ If you don't want any "spoiler text", just leave the line empty.
| sensitive | Hide picture behind "sensitive content" mask or not | yes | no |
| default_text | Text for --img option | string |
| spoiler_text | Text for every spoiler (or CW) (for --img or --stream option) | string |
| limit | Limit send per minute per person | intg |
| limit | Limit send per minute per person | int |
| limit_hour | Limit send par hour per person | int |

## Use it



+ 42
- 7
bot.py View File

@@ -49,22 +49,24 @@ class BotListener(StreamListener):
if notification['type'] == 'mention':
log.debug("Got a mention")
sender = notification['account']['acct'] # Get sender name
sender_filename = "limiter/" + sender; # Forge file for limiter
if os.path.isfile(sender_filename): # Check if file exist
sender_hour_filename = "limiter/hour" + sender; # Forge file for limiter
sender_minute_filename = "limiter/minute" + sender; # Forge file for limiter
if os.path.isfile(sender_hour_filename): # Check if file exist
log.debug("Sender file exist")
statbuf = os.stat(sender_filename)
statbuf = os.stat(sender_hour_filename)
last_edit = int(statbuf.st_mtime)
ts = int(time.time())
if ts - last_edit > 59: # check if file is modified 1 minute after last edition
log.debug("file is too old")
f = open(sender_filename,'w')
f = open(sender_hour_filename,'w')
f.write(str(1)) # reset counter
f.close()
can_continue = True
else:
log.debug("file is young")
f = open(sender_filename,'r+')
limit = int(get_parameter("limit", config_file))
f = open(sender_hour_filename,'r+')
limit = int(get_parameter("limit_hour", config_file))
number_of_mention = int(f.read())
if number_of_mention < limit: # limit of mention per minute is 4
log.debug("Sender have less of limit requests")
@@ -77,10 +79,43 @@ class BotListener(StreamListener):
f.close()
else: # File not exist, create it and initialise it
log.debug("Sender file not exist")
f = open(sender_filename,"w+")
f = open(sender_hour_filename,"w+")
f.write(str(1))
f.close()
can_continue = True
if can_continue:
if os.path.isfile(sender_minute_filename): # Check if file exist
log.debug("Sender file exist")
statbuf = os.stat(sender_minute_filename)
last_edit = int(statbuf.st_mtime)
ts = int(time.time())
if ts - last_edit > 59: # check if file is modified 1 minute after last edition
log.debug("file is too old")
f = open(sender_minute_filename,'w')
f.write(str(1)) # reset counter
f.close()
can_continue = True
else:
log.debug("file is young")
f = open(sender_minute_filename,'r+')
limit = int(get_parameter("limit", config_file))
number_of_mention = int(f.read())
if number_of_mention < limit: # limit of mention per minute is 4
log.debug("Sender have less of limit requests")
f.seek(0)
f.write(str(number_of_mention + 1))
can_continue = True
else:
log.debug("Sender have more of limit requests")
can_continue = False # if number of mention is for, user can't receive anything
f.close()
else: # File not exist, create it and initialise it
log.debug("Sender file not exist")
f = open(sender_minute_filename,"w+")
f.write(str(1))
f.close()
can_continue = True

if can_continue:
id = notification['status']['id']


+ 0
- 0
limiter/hour/README.md View File


+ 0
- 0
limiter/minute/README.md View File


Loading…
Cancel
Save