mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
cleanup; pylint suggestions
This commit is contained in:
@@ -5,7 +5,7 @@ if '__main__' == __name__:
|
|||||||
print('''this is a library file, which is not meant to be executed''')
|
print('''this is a library file, which is not meant to be executed''')
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
|
||||||
import sys, os, time, pickle
|
import sys, time, pickle
|
||||||
from local_config import conf
|
from local_config import conf
|
||||||
|
|
||||||
RATE_GLOBAL = 0x01
|
RATE_GLOBAL = 0x01
|
||||||
|
|||||||
22
plugins.py
22
plugins.py
@@ -125,7 +125,7 @@ def data_parse_other(msg_obj):
|
|||||||
elif 'reply_user' == a:
|
elif 'reply_user' == a:
|
||||||
args['reply_user'] = reply_user
|
args['reply_user'] = reply_user
|
||||||
else:
|
else:
|
||||||
logger('warn', 'unknown required arg for %s: %s' %(p['name'], a))
|
logger('warn', 'unknown required arg for %s: %s' % (p['name'], a))
|
||||||
|
|
||||||
ret = p['func'](args)
|
ret = p['func'](args)
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ def command_help(args):
|
|||||||
if cmd == p['name']:
|
if cmd == p['name']:
|
||||||
logger('plugin', 'sent help for %s' % cmd)
|
logger('plugin', 'sent help for %s' % cmd)
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': help for %s: %s' %(cmd, p['desc'])
|
'msg': args['reply_user'] + ': help for %s: %s' % (cmd, p['desc'])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -281,7 +281,7 @@ def command_dice(args):
|
|||||||
|
|
||||||
dice_char = ['◇', '⚀', '⚁', '⚂', '⚃', '⚄', '⚅']
|
dice_char = ['◇', '⚀', '⚁', '⚂', '⚃', '⚄', '⚅']
|
||||||
return {
|
return {
|
||||||
'msg': 'rolling a dice for %s: %s (%d)' %(args['reply_user'], dice_char[rnd], rnd)
|
'msg': 'rolling a dice for %s: %s (%d)' % (args['reply_user'], dice_char[rnd], rnd)
|
||||||
}
|
}
|
||||||
|
|
||||||
def command_uptime(args):
|
def command_uptime(args):
|
||||||
@@ -305,7 +305,7 @@ def command_uptime(args):
|
|||||||
|
|
||||||
logger('plugin', 'sent statistics')
|
logger('plugin', 'sent statistics')
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' %(u, plural_uptime, conf('request_counter'), plural_request))
|
'msg': args['reply_user'] + (''': happily serving for %d second%s, %d request%s so far.''' % (u, plural_uptime, conf('request_counter'), plural_request))
|
||||||
}
|
}
|
||||||
|
|
||||||
def command_ping(args):
|
def command_ping(args):
|
||||||
@@ -345,7 +345,7 @@ def command_info(args):
|
|||||||
if 'info' in args['data']:
|
if 'info' in args['data']:
|
||||||
logger('plugin', 'sent long info')
|
logger('plugin', 'sent long info')
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + (''': I'm a bot, my job is to extract <title> tags from posted URLs. In case I'm annoying or for further questions, please talk to my master %s. I'm rate limited and shouldn't post more than %d messages per %d seconds. To make me exit immediately, highlight me with 'hangup' in the message (emergency only, please). For other commands, highlight me with 'command'.''' %(conf('bot_owner'), conf('hist_max_count'), conf('hist_max_time')))
|
'msg': args['reply_user'] + (''': I'm a bot, my job is to extract <title> tags from posted URLs. In case I'm annoying or for further questions, please talk to my master %s. I'm rate limited and shouldn't post more than %d messages per %d seconds. To make me exit immediately, highlight me with 'hangup' in the message (emergency only, please). For other commands, highlight me with 'command'.''' % (conf('bot_owner'), conf('hist_max_count'), conf('hist_max_time')))
|
||||||
}
|
}
|
||||||
|
|
||||||
def command_teatimer(args):
|
def command_teatimer(args):
|
||||||
@@ -409,13 +409,13 @@ def command_decode(args):
|
|||||||
try:
|
try:
|
||||||
uni_name = unicodedata.name(char)
|
uni_name = unicodedata.name(char)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger('plugin', 'decode(%s) failed: %s' %(char, str(e)))
|
logger('plugin', 'decode(%s) failed: %s' % (char, str(e)))
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ": can't decode %s (%s): %s" %(char, char_esc, str(e))
|
'msg': args['reply_user'] + ": can't decode %s (%s): %s" % (char, char_esc, str(e))
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': %s (%s) is called "%s"' %(char, char_esc, uni_name)
|
'msg': args['reply_user'] + ': %s (%s) is called "%s"' % (char, char_esc, uni_name)
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
@@ -588,7 +588,7 @@ def data_parse_commands(msg_obj):
|
|||||||
elif 'argv2' == a:
|
elif 'argv2' == a:
|
||||||
args['argv2'] = argv2
|
args['argv2'] = argv2
|
||||||
else:
|
else:
|
||||||
logger('warn', 'unknown required arg for %s: %s' %(p['name'], a))
|
logger('warn', 'unknown required arg for %s: %s' % (p['name'], a))
|
||||||
|
|
||||||
ret = p['func'](args)
|
ret = p['func'](args)
|
||||||
|
|
||||||
@@ -625,7 +625,7 @@ if debug_enabled():
|
|||||||
def _send_reply(a, msg_obj):
|
def _send_reply(a, msg_obj):
|
||||||
logger('send_reply[%s]' % msg_obj, a)
|
logger('send_reply[%s]' % msg_obj, a)
|
||||||
|
|
||||||
def _conf(a):
|
def _conf(ignored):
|
||||||
return 'bot'
|
return 'bot'
|
||||||
|
|
||||||
def _ratelimit_exceeded(ignored=None):
|
def _ratelimit_exceeded(ignored=None):
|
||||||
@@ -672,7 +672,7 @@ def register(func_type, auto=False):
|
|||||||
ret['func'] = f
|
ret['func'] = f
|
||||||
plugins[func_type].append(ret)
|
plugins[func_type].append(ret)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger('warn', 'auto-registering %s failed: %s' %(f, e))
|
logger('warn', 'auto-registering %s failed: %s' % (f, e))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for f in funcs[func_type]:
|
for f in funcs[func_type]:
|
||||||
|
|||||||
18
urlbot.py
18
urlbot.py
@@ -1,14 +1,13 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import sys, os, stat, re, time, pickle, random
|
import sys, re, time, pickle, random
|
||||||
import urllib.request, urllib.parse, urllib.error, html.parser
|
import urllib.request, urllib.parse, urllib.error, html.parser
|
||||||
from common import *
|
from common import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from local_config import conf, set_conf
|
from local_config import conf, set_conf
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import sys
|
|
||||||
sys.stderr.write('''
|
sys.stderr.write('''
|
||||||
%s: E: local_config.py isn't tracked because of included secrets and
|
%s: E: local_config.py isn't tracked because of included secrets and
|
||||||
%s site specific configurations. Rename local_config.py.skel and
|
%s site specific configurations. Rename local_config.py.skel and
|
||||||
@@ -60,7 +59,7 @@ def extract_title(url):
|
|||||||
(code, html_text, headers) = fetch_page(url)
|
(code, html_text, headers) = fetch_page(url)
|
||||||
|
|
||||||
if 1 == code:
|
if 1 == code:
|
||||||
return (3, 'failed: %s for %s' %(html_text, url))
|
return (3, 'failed: %s for %s' % (html_text, url))
|
||||||
|
|
||||||
if html_text:
|
if html_text:
|
||||||
charset = ''
|
charset = ''
|
||||||
@@ -134,7 +133,7 @@ def ratelimit_exceeded(ignored=None): # FIXME: separate counters
|
|||||||
if hist_flag:
|
if hist_flag:
|
||||||
hist_flag = False
|
hist_flag = False
|
||||||
# FIXME: this is very likely broken now
|
# FIXME: this is very likely broken now
|
||||||
send_reply('(rate limited to %d messages in %d seconds, try again at %s)' %(conf('hist_max_count'), conf('hist_max_time'), time.strftime('%T %Z', time.localtime(hist_ts[0] + conf('hist_max_time')))))
|
send_reply('(rate limited to %d messages in %d seconds, try again at %s)' % (conf('hist_max_count'), conf('hist_max_time'), time.strftime('%T %Z', time.localtime(hist_ts[0] + conf('hist_max_time')))))
|
||||||
|
|
||||||
logger('warn', 'rate limiting exceeded: ' + pickle.dumps(hist_ts))
|
logger('warn', 'rate limiting exceeded: ' + pickle.dumps(hist_ts))
|
||||||
return True
|
return True
|
||||||
@@ -183,16 +182,16 @@ def extract_url(data, msg_obj):
|
|||||||
if 0 == status:
|
if 0 == status:
|
||||||
title = title.strip()
|
title = title.strip()
|
||||||
|
|
||||||
message = 'Title: %s: %s' %(title, url)
|
message = 'Title: %s: %s' % (title, url)
|
||||||
elif 1 == status:
|
elif 1 == status:
|
||||||
if conf('image_preview'):
|
if conf('image_preview'):
|
||||||
# of course it's fake, but it looks interesting at least
|
# of course it's fake, but it looks interesting at least
|
||||||
char = """,._-+=\|/*`~"'"""
|
char = """,._-+=\|/*`~"'"""
|
||||||
message = 'No text but %s, 1-bit ASCII art preview: [%c] %s' %(
|
message = 'No text but %s, 1-bit ASCII art preview: [%c] %s' % (
|
||||||
title, random.choice(char), url
|
title, random.choice(char), url
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logger('info', 'no message sent for non-text %s (%s)' %(url, title))
|
logger('info', 'no message sent for non-text %s (%s)' % (url, title))
|
||||||
continue
|
continue
|
||||||
elif 2 == status:
|
elif 2 == status:
|
||||||
message = 'No title: %s' % url
|
message = 'No title: %s' % url
|
||||||
@@ -281,11 +280,10 @@ class bot(ClientXMPP):
|
|||||||
if 'groupchat' == msg_obj['type']:
|
if 'groupchat' == msg_obj['type']:
|
||||||
return
|
return
|
||||||
|
|
||||||
plugins.data_parse_commands(msg_obj)
|
# plugins.data_parse_commands(msg_obj)
|
||||||
plugins.data_parse_other(msg_obj)
|
# plugins.data_parse_other(msg_obj)
|
||||||
|
|
||||||
print('msg from %s: %s' % (msg_obj['from'].bare, msg_obj))
|
print('msg from %s: %s' % (msg_obj['from'].bare, msg_obj))
|
||||||
# msg_obj.reply(body='waaaaah').send()
|
|
||||||
|
|
||||||
if '__main__' == __name__:
|
if '__main__' == __name__:
|
||||||
import plugins
|
import plugins
|
||||||
|
|||||||
Reference in New Issue
Block a user