From 9df923d9dbce3cfef4825d4cff7ff9fae2acdda9 Mon Sep 17 00:00:00 2001 From: diSabler Date: Wed, 13 May 2020 22:00:55 +0300 Subject: chg: migrate bot from python 2 to python 3 --- Dockerfile | 6 +- README.md | 1 + isida.py | 46 +++---- kernel.py | 300 ++++++++++++++++------------------------------ plugins/.domain.py | 2 +- plugins/bot_info.py | 2 +- plugins/calendar.py | 90 +++++++------- plugins/chat.py | 14 +-- plugins/drink.py | 41 +------ plugins/error.py | 4 +- plugins/event.py | 2 +- plugins/execute.py | 16 ++- plugins/friday.py | 4 +- plugins/help.py | 6 +- plugins/horo.py | 8 +- plugins/logger.py | 6 +- plugins/news.py | 7 +- plugins/oboobs.py | 2 +- plugins/poem.py | 2 +- plugins/privatbank.py | 2 +- plugins/service.py | 4 +- plugins/shortlink.py | 5 +- plugins/test.py | 2 +- plugins/todate.py | 7 +- plugins/turn.py | 2 +- plugins/whoami.py | 2 +- plugins/xkcd.py | 2 +- plugins/yandexcurrency.py | 9 +- settings/config.ini.demo | 10 +- 29 files changed, 245 insertions(+), 359 deletions(-) diff --git a/Dockerfile b/Dockerfile index c2b8831..6c3ed43 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ -FROM python:2-alpine +FROM python:3-alpine COPY . /isida WORKDIR /isida -RUN pip2 install -r requirements.txt +RUN pip3 install -r requirements.txt -CMD ["python2", "./isida.py"] +CMD ["python3", "./isida.py"] diff --git a/README.md b/README.md index 7978259..c603ea1 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ ====== Required: +* configparser * chardet * feedparser * requests diff --git a/isida.py b/isida.py index a12fe06..cb30621 100644 --- a/isida.py +++ b/isida.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -47,23 +47,23 @@ RAW_IN = '' mode = '' def readfile(filename): - fp = file(filename) - data = fp.read() + with open(filename) as fp: + data = fp.read() fp.close() return data def writefile(filename, data): - fp = file(filename, 'w') - fp.write(data) + with open(filename, 'w') as fp: + fp.write(data) fp.close() def printlog(text): - print text + print(text) lt = tuple(time.localtime()) fname = slog_folder % 'crash_%04d%02d%02d.txt' % lt[0:3] fbody = '%s|%s\n' % ('%02d%02d%02d' % lt[3:6],text) - fl = open(fname, 'a') - fl.write(fbody.encode('utf-8')) + with open(fname, 'a') as fl: + fl.write(fbody) fl.close() def crashtext(t): @@ -81,9 +81,9 @@ def update(USED_REPO): os.system('git describe --always > %s' % ver_file) revno = str(readfile(ver_file)).replace('\n','').replace('\r','').replace('\t','').replace(' ','') os.system('git log --pretty=format:'' > %s' % ver_file) - writefile(ver_file, unicode(GIT_VER_FORMAT % (os.path.getsize(ver_file)+1,revno,id_append)).encode('utf-8')) + writefile(ver_file, str(GIT_VER_FORMAT % (os.path.getsize(ver_file)+1,revno,id_append))) os.system('git log -1 > %s' % updatelog_file) - writefile(updatelog_file, unicode(readfile(updatelog_file)).replace('\n\n','\n').replace('\r','').replace('\t','')) + writefile(updatelog_file, str(readfile(updatelog_file)).replace('\n\n','\n').replace('\r','').replace('\t','')) elif USED_REPO == 'docker': os.system('echo Update not available inside docker image! > %s' % updatelog_file) else: @@ -107,8 +107,10 @@ if __name__ == "__main__": try: os.getsid(last_pid) crash('Multilaunch detected! Kill pid %s before launch bot again!' % last_pid) - except Exception, SM: - if not str(SM).lower().count('no such process'): crash('Unknown exception!\n%s' % SM) + except: + SM = '\n'.join(str(t) for t in sys.exc_info()) + if 'ProcessLookupError' not in SM: + crash('Unknown exception!\n%s' % SM) writefile(pid_file,str(PID)) @@ -116,23 +118,24 @@ if __name__ == "__main__": if PID == 1: USED_REPO = 'docker' - writefile(ver_file, unicode(DOCKER_VER_FORMAT % (hex(int(os.path.getctime('../')))[2:],id_append)).encode('utf-8')) + writefile(ver_file, DOCKER_VER_FORMAT % (hex(int(os.path.getctime('../')))[2:], id_append)) elif '.git' in dirs: USED_REPO = 'git' os.system('git describe --always > %s' % ver_file) revno = str(readfile(ver_file)).replace('\n','').replace('\r','').replace('\t','').replace(' ','') os.system('git log --pretty=format:'' > %s' % ver_file) - writefile(ver_file, unicode(GIT_VER_FORMAT % (os.path.getsize(ver_file)+1,revno,id_append)).encode('utf-8')) + writefile(ver_file, GIT_VER_FORMAT % (os.path.getsize(ver_file)+1,revno, id_append)) else: USED_REPO = 'unknown' - writefile(ver_file, unicode(TIME_VER_FORMAT % (hex(int(os.path.getctime('../')))[2:],id_append)).encode('utf-8')) + writefile(ver_file, TIME_VER_FORMAT % (hex(int(os.path.getctime('../')))[2:], id_append)) while True: try: - execfile('kernel.py') + print(mode) + exec(open('kernel.py').read()) except KeyboardInterrupt: break - except SystemExit, mode: + except SystemExit as MODE: for t in threading.enumerate(): try: if str(t).startswith('=' ' or l in '\t\r\n'] for l in unicode(t)]) + return ''.join([['?', l][l>=' ' or l in '\t\r\n'] for l in str(t)]) # Send request def send_raw(raw_in, method, dt, fl={}): @@ -511,33 +406,28 @@ def send_document(raw_in, document, custom={}): # Open web page def get_opener(page_name, parameters=None): socket.setdefaulttimeout(www_get_timeout) + headers = { + 'User-Agent': USER_AGENT, + 'Cache-Control': 'no-cache' + } try: - proxy_support = urllib2.ProxyHandler({'http' : \ - 'http://%(user)s:%(password)s@%(host)s:%(port)d' % http_proxy}) - opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler) - urllib2.install_opener(opener) - except: - opener = urllib2.build_opener(urllib2.HTTPHandler) - opener.addheaders = [('User-agent', user_agent)] - if parameters: - page_name += urllib.urlencode(parameters) - try: - data = opener.open(page_name) + data = requests.get(page_name, + data = parameters, + proxies = WEB_PROXIES, + headers = headers).content result = True - except Exception, MSG: - try: - MSG = str(MSG) - except: - MSG = unicode(MSG) + except: + MSG = '\n'.join(str(t) for t in sys.exc_info()) data = 'Error! %s' % MSG.replace('>', '').replace('<', '').capitalize() result = False + print(data) return data, result # Load page with limited size def load_page_size(page_name, page_size, parameters=None): data, result = get_opener(page_name, parameters) if result: - return data.read(page_size) + return data[:page_size].decode() else: return data @@ -545,7 +435,7 @@ def load_page_size(page_name, page_size, parameters=None): def load_page(page_name, parameters=None): data, result = get_opener(page_name, parameters) if result: - return data.read(size_overflow) + return data[:size_overflow].decode() else: return data @@ -564,6 +454,11 @@ def check_updates(): except requests.exceptions.ConnectionError: pprint('*** Connection error on getUpdates. Waiting %s seconds.' % MAX_TIMEOUT, 'red') return False + #except requests.exceptions.InvalidSchema: + # time.sleep(MAX_TIMEOUT) + # raise + # pprint('*** Connection error on getUpdates. Waiting %s seconds.' % MAX_TIMEOUT, 'red') + # return False if not request.status_code == 200: pprint('*** Error code on getUpdates: %s' % request.status_code, 'red') @@ -581,9 +476,9 @@ def check_updates(): OFFSET = msg_in['update_id'] return True try: - if msg_in.has_key('message'): + if 'message' in msg_in: CHAT_ID = msg_in['message']['chat'].get('id', 0) - elif msg_in.has_key('callback_query'): + elif 'callback_query' in msg_in: CHAT_ID = msg_in['callback_query']['message']['chat'].get('id', 0) else: CHAT_ID = 0 @@ -594,10 +489,10 @@ def check_updates(): logger(msg_in) except: pprint(json.dumps(msg_in, indent=2, separators=(',', ': ')), 'red') - if msg_in.has_key('edited_message'): + if 'edited_message' in msg_in: msg_in['message'] = msg_in['edited_message'] pprint('*** Edited message!', 'yellow') - elif msg_in.has_key('callback_query'): + elif 'callback_query' in msg_in: msg_in['message'] = msg_in['callback_query']['message'] msg_in['message']['text'] = msg_in['callback_query']['data'] pprint('*** Callback query!', 'yellow') @@ -605,7 +500,7 @@ def check_updates(): #send_msg(msg_in, 'Edited messages not supported now!') if 'message' not in msg_in or 'text' not in msg_in['message']: - if msg_in['message'].has_key('new_chat_participant'): + if 'new_chat_participant' in msg_in['message']: pprint('New participant|%s' % '|'.join([str(t) for t in [\ msg_in['message']['chat'].get('all_members_are_administrators', ''), \ msg_in['message']['chat'].get('type', ''), \ @@ -616,7 +511,7 @@ def check_updates(): msg_in['message']['new_chat_participant'].get('first_name', ''), \ msg_in['message']['new_chat_participant'].get('last_name', '') ]]), 'cyan') break - elif msg_in['message'].has_key('left_chat_participant'): + elif 'left_chat_participant' in msg_in['message']: pprint('Left participant|%s' % '|'.join([str(t) for t in [\ msg_in['message']['chat'].get('all_members_are_administrators', ''), \ msg_in['message']['chat'].get('type', ''), \ @@ -687,12 +582,12 @@ def check_updates(): if not IS_COMMAND: if (msg_in['message']['text'].lower().startswith('@%s ' % BOT_NAME) and \ - msg_in['message'].has_key('chat') and \ - msg_in['message']['chat'].has_key('type') and \ + 'chat' in msg_in['message'] and \ + 'type' in msg_in['message']['chat'] and \ msg_in['message']['chat'].get('type', '') in ['group', 'supergroup']) or \ - (msg_in['message'].has_key('reply_to_message') and \ - msg_in['message']['reply_to_message'].has_key('from') and \ - msg_in['message']['reply_to_message']['from'].has_key('username') and \ + ('reply_to_message' in msg_in['message'] and \ + 'from' in msg_in['message']['reply_to_message'] and \ + 'username' in msg_in['message']['reply_to_message']['from'] and \ msg_in['message']['reply_to_message']['from'].get('username', '').lower() == BOT_NAME): text = msg_in['message']['text'] if text.lower().startswith('@%s ' % BOT_NAME): @@ -702,8 +597,8 @@ def check_updates(): pprint('<<< Chat: %s' % msg, 'bright_green') #time.sleep(len(msg) / 3.0 + random.randint(0, 3)) send_msg(msg_in, msg) - elif (msg_in['message'].has_key('chat') and \ - msg_in['message']['chat'].has_key('type') and \ + elif ('chat' in msg_in['message'] and \ + 'type' in msg_in['message']['chat'] and \ msg_in['message']['chat'].get('type', '') == 'private'): text = msg_in['message'].get('text').strip() pprint('>>> Chat: %s' % text, 'green') @@ -730,15 +625,14 @@ def shell_execute(cmd): else: tmp_file = '%s.tmp' % int(time.time()) try: - error_answ = os.system('%s > %s 2>&1' % (cmd.encode('utf-8'), tmp_file)) + error_answ = os.system('%s > %s 2>&1' % (cmd, tmp_file)) if not error_answ: try: body = html_escape_soft(readfile(tmp_file)) except: body = '⚠️ Command execution error.' if len(body): - enc = chardet.detect(body)['encoding'] - result = remove_sub_space(unicode(body, enc)) + result = remove_sub_space(str(body)) else: result = 'ok' else: @@ -747,11 +641,8 @@ def shell_execute(cmd): result += '\n' + html_escape_soft(readfile(tmp_file)) except: pass - except Exception, MSG: - try: - MSG = str(MSG) - except: - MSG = unicode(MSG) + except: + MSG = '\n'.join(str(t) for t in sys.exc_info()) result = '⚠️ I can\'t execute it! Error: %s' % MSG try: os.remove(tmp_file) @@ -839,16 +730,13 @@ CONFIG_MAIN = 'main' # Main section name in con CONFIG_DEBUG = 'debug' # Debug section name in config CONFIG_OWNER = 'owner' # Owner section name in config CONFIG_LISTS = 'lists' # White/black lists section name in config -CONFIG_PROXY = 'proxy' # Proxy section name in config +CONFIG_SOCKS_PROXY = 'socks_proxy' # Socks proxy section name in config +CONFIG_WEB = 'web' # Web section name in config botName = 'iSida' # Bot's name -botVersionDef = '6.1' # Bot's version +botVersionDef = '6.2' # Bot's version base_type = 'NoDB' # Bot's base type www_get_timeout = 15 # Timeout for web requests size_overflow = 262144 # Web page limit in bytes -#http_proxy = {'host':'127.0.0.1', 'port':3128, 'user':None, 'password':None} -# # Proxy settings -user_agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' -# # User agent for web requests TIMEOUT = 1 # Timeout between request updates MAX_TIMEOUT = 15 # Maximal timeout after request error CYCLES = 0 # Work cycles @@ -880,7 +768,7 @@ pprint('*** Init enviroment succed', 'white') pprint('*** Loading config', 'white') if not os.path.exists(CONFIG_FILE): Error('Config file not found: %s' % CONFIG_FILE) -CONFIG = ConfigParser.ConfigParser() +CONFIG = configparser.ConfigParser() CONFIG.read(CONFIG_FILE) SECTIONS = CONFIG.sections() if CONFIG_MAIN not in SECTIONS: @@ -891,33 +779,51 @@ if CONFIG_OWNER not in SECTIONS: Error('Owner options not found in %s' % CONFIG_FILE) if CONFIG_LISTS not in SECTIONS: pprint('!!! White/black lists options not found in %s' % CONFIG_FILE, 'red') -if CONFIG_PROXY not in SECTIONS: - pprint('Proxy settings not found in %s' % CONFIG_FILE, 'white') +if CONFIG_SOCKS_PROXY not in SECTIONS: + pprint('Socks proxy settings not found in %s' % CONFIG_FILE, 'white') PROXIES = {} else: - pprint('Read proxy settings...', 'white') + pprint('Read socks proxy settings...', 'white') proxy = {} - proxy['HOST'] = CONFIG.get(CONFIG_PROXY, 'host') - proxy['PORT'] = CONFIG.get(CONFIG_PROXY, 'port') + proxy['HOST'] = CONFIG.get(CONFIG_SOCKS_PROXY, 'host') + proxy['PORT'] = CONFIG.get(CONFIG_SOCKS_PROXY, 'port') proxy_string = '%(HOST)s:%(PORT)s' - try: - itm = CONFIG.get(CONFIG_PROXY, 'user') - proxy['USER'] = itm - except: - pass - try: - itm = CONFIG.get(CONFIG_PROXY, 'pass') - proxy['PASS'] = itm - except: - pass - if proxy.has_key('USER') and proxy.has_key('PASS'): - proxy_string = '%(USER)s:%(PASS)s@' + proxy_string - elif proxy.has_key('USER') and not proxy.has_key('PASS'): - proxy_string = '%(USER)s@' + proxy_string + if CONFIG.has_option(CONFIG_SOCKS_PROXY, 'user'): + proxy['USER'] = CONFIG.get(CONFIG_SOCKS_PROXY, 'user') + if CONFIG.has_option(CONFIG_SOCKS_PROXY, 'pass'): + proxy['PASS'] = ':%s' % CONFIG.get(CONFIG_SOCKS_PROXY, 'pass') + else: + proxy['PASS'] = '' + proxy_string = '%(USER)s%(PASS)s@' + proxy_string proxy_string = 'socks5://' + proxy_string PROXIES = {'http': proxy_string % proxy, 'https': proxy_string % proxy} +WEB_PROXIES = {} +USER_AGENT = '' + +if CONFIG_WEB not in SECTIONS: + pprint('Web settings not found in %s' % CONFIG_FILE, 'white') +else: + if CONFIG.has_option(CONFIG_WEB, 'proxy_host') \ + and CONFIG.has_option(CONFIG_WEB, 'proxy_port'): + pprint('Read web proxy settings...', 'white') + proxy = {} + proxy['HOST'] = CONFIG.get(CONFIG_WEB, 'proxy_host') + proxy['PORT'] = CONFIG.get(CONFIG_WEB, 'proxy_port') + proxy_string = '%(HOST)s:%(PORT)s' + if CONFIG.has_option(CONFIG_WEB, 'proxy_user'): + proxy['USER'] = CONFIG.get(CONFIG_WEB, 'proxy_user') + if CONFIG.has_option(CONFIG_WEB, 'proxy_pass'): + proxy['PASS'] = CONFIG.get(CONFIG_WEB, 'proxy_pass') + else: + proxy['PASS'] = '' + proxy_string = '%(USER)s%(PASS)s@' + proxy_string + WEB_PROXIES = {'http': proxy_string % proxy, + 'https': proxy_string % proxy} + if CONFIG.has_option(CONFIG_WEB, 'user_agent'): + USER_AGENT = CONFIG.get(CONFIG_WEB, 'user_agent') + CONFIG_API_TOKEN = CONFIG.get(CONFIG_MAIN, 'token') BOT_NAME = CONFIG.get(CONFIG_MAIN, 'bot_name').lower() PARANOIA_MODE = get_config_bin(CONFIG, CONFIG_MAIN, 'paranoia_mode') @@ -937,7 +843,7 @@ COMMANDS = [] for plugin in plug_list: commands = [] pprint('Append plugin: %s' % plugin, 'cyan') - execfile(PLUGIN_FOLDER % plugin) + exec(open(PLUGIN_FOLDER % plugin).read()) if commands: for tmp in commands: if len(tmp) == 5: @@ -962,6 +868,7 @@ if mode: pass # --- Main cycle ------------------------------------------------------------- # + while not GAME_OVER: try: if not GAME_OVER: @@ -975,11 +882,8 @@ while not GAME_OVER: pprint('Shutdown by CTRL+C...', 'bright_red') time.sleep(1) sys.exit('exit') - except Exception, MSG: - try: - MSG = str(MSG) - except: - MSG = unicode(MSG) + except: + MSG = '\n'.join(str(t) for t in sys.exc_info()) pprint('*** Error *** %s ***' % MSG, 'red') logging.exception(' [%s] ' % timeadd(datetime.datetime.now())) if HALT_ON_EXCEPTION: diff --git a/plugins/.domain.py b/plugins/.domain.py index b186bf7..096c0f9 100644 --- a/plugins/.domain.py +++ b/plugins/.domain.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/bot_info.py b/plugins/bot_info.py index 5300747..86d5784 100644 --- a/plugins/bot_info.py +++ b/plugins/bot_info.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/calendar.py b/plugins/calendar.py index 5164cfa..889d449 100644 --- a/plugins/calendar.py +++ b/plugins/calendar.py @@ -1,45 +1,45 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# --------------------------------------------------------------------------- # -# # -# iSida bot VI plugin # -# Copyright (C) diSabler # -# Copyright (C) dr.Schmurge # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see . # -# # -# --------------------------------------------------------------------------- # - -def cmd_calendar(raw_in, text): - text = text.split() - try: - month = int(text[0]) - except: - month = tuple(time.localtime())[1] - try: - year = int(text[1]) - except: - year = tuple(time.localtime())[0] - - msg = '📅 %s' % (timeadd(datetime.datetime.now())) - msg += '\n
Mo Tu We Th Fr Sa Su\n'
-	msg += '\n'.join([' '.join([['%2d' % r, '  '][r==0] for r in t]) for t in calendar.monthcalendar(year, month)])
-	msg += '
' - - send_msg(raw_in, msg) - -commands = [['calendar', cmd_calendar, False, 'all', 'Calendar [month][year]']] - -# The end is near! +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# --------------------------------------------------------------------------- # +# # +# iSida bot VI plugin # +# Copyright (C) diSabler # +# Copyright (C) dr.Schmurge # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# --------------------------------------------------------------------------- # + +def cmd_calendar(raw_in, text): + text = text.split() + try: + month = int(text[0]) + except: + month = tuple(time.localtime())[1] + try: + year = int(text[1]) + except: + year = tuple(time.localtime())[0] + + msg = '📅 %s' % (timeadd(datetime.datetime.now())) + msg += '\n
Mo Tu We Th Fr Sa Su\n'
+	msg += '\n'.join([' '.join([['%2d' % r, '  '][r==0] for r in t]) for t in calendar.monthcalendar(year, month)])
+	msg += '
' + + send_msg(raw_in, msg) + +commands = [['calendar', cmd_calendar, False, 'all', 'Calendar [month][year]']] + +# The end is near! diff --git a/plugins/chat.py b/plugins/chat.py index 247a178..e1366dd 100644 --- a/plugins/chat.py +++ b/plugins/chat.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -43,18 +43,18 @@ for t in llist: ANSWER_FILE = chat_folder + 'answer.txt' PHRASES_FILE = chat_folder + 'phrases.txt' - list_of_answers[cur_lang] = readfile(ANSWER_FILE).decode('utf-8').split('\n') - list_of_empty[cur_lang] = readfile(EMPTY_FILE).decode('utf-8').split('\n') + list_of_answers[cur_lang] = readfile(ANSWER_FILE).split('\n') + list_of_empty[cur_lang] = readfile(EMPTY_FILE).split('\n') list_of_phrases_with_highlight[cur_lang] = [] list_of_phrases_no_highlight[cur_lang] = [] for phrase in readfile(PHRASES_FILE).split('\n'): if 'NICK' in phrase: - list_of_phrases_with_highlight[cur_lang].append(phrase.decode('utf-8')) + list_of_phrases_with_highlight[cur_lang].append(phrase) else: - list_of_phrases_no_highlight[cur_lang].append(phrase.decode('utf-8')) + list_of_phrases_no_highlight[cur_lang].append(phrase) dict_of_mind[cur_lang] = {} - for p in readfile(MIND_FILE).decode('utf-8').split('\n'): + for p in readfile(MIND_FILE).split('\n'): if '||' in p: tmp1, tmp2 = p.strip().split('||') dict_of_mind[cur_lang][tmp1] = tmp2.split('|') @@ -92,7 +92,7 @@ def rating(s, text, ID): def getAnswer(raw_in, text): global ANSW_PREV ID = raw_in['message']['from']['id'] - text = text.strip() + text = text.strip() answ = getSmartAnswer(ID, text) ANSW_PREV[ID] = text.upper() return answ diff --git a/plugins/drink.py b/plugins/drink.py index 8d79dcc..df503d7 100644 --- a/plugins/drink.py +++ b/plugins/drink.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -37,7 +37,7 @@ def cmd_to_drink(raw_in, text): drink_lday = ['последний', 'последний', 'последняя', 'последний', 'последняя', 'последняя', 'последнее'] date_file = DATA_FOLDER % 'date.txt' if os.path.isfile(date_file): - ddate = readfile(date_file).decode('UTF') + ddate = readfile(date_file) week1 = '' week2 = '' if not ddate: @@ -87,41 +87,6 @@ def cmd_to_drink(raw_in, text): msg = 'База не найдена.' send_msg(raw_in, msg) -def cmd_calend(raw_in, text): - msg, url, text = '', '', text.strip() - if not text: - url = 'http://www.calend.ru/day/%s-%s/' % tuple(time.localtime())[1:3] - elif re.match('\d+\.\d+$', text): - url = 'http://www.calend.ru/day/%s-%s/' % tuple(text.split('.')[::-1]) - elif len(text) > 1: - url = 'http://www.calend.ru/search/?search_str=' + urllib.quote(text.encode('cp1251')) - if url: - data = html_encode(load_page(url)) - t = get_tag(data,'title') - if t == u'Поиск': - hl = re.findall('(.+?)(?:.|\s)+?/>\s+?(\d+ .+?)\s', data) - if len(hl) == 1: - d = re.search('class="img_small" />\s+?\s+?(.+?\.)\s+?', data, re.S).group(1) - d = unescape(re.sub('\s+', ' ', d.strip())) - msg += '📅 %s (%s) - %s' % (hl[0][0], hl[0][1], hl[0][2], d) - elif hl: - d = '%s' % get_tag(data,'h1') - for a in hl: - msg = '%s:\n🔹%s' % (d, '\n🔹'.join(map(lambda x: '%s (%s)' % x, hl))) - else: - d = '%s' % get_tag(data,'h1') - hl = re.findall('(.+?)', data) - if hl: - #print hl[0] - msg = '%s:\n🔹%s' % (d, '\n🔹'.join(map(lambda x: '%s' % x, hl))) - - else: - msg = 'What?' - if not msg: - msg = 'Holiday: %s not found.' % text - send_msg(raw_in, msg, custom={'disable_web_page_preview': 'true'}) - -commands = [['drink', cmd_to_drink, False, 'all', 'Find holiday [name_holiday/date]'], - ['calend', cmd_calend, False, 'all', 'Find holiday [name_holiday/date]']] +commands = [['drink', cmd_to_drink, False, 'all', 'Find holiday [name_holiday/date]']] # The end is near! diff --git a/plugins/error.py b/plugins/error.py index c85b2d7..ecde35a 100644 --- a/plugins/error.py +++ b/plugins/error.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -29,7 +29,7 @@ def cmd_show_error(raw_in, text): except: cmd = 1 if os.path.isfile(LOG_FILENAME) and text.lower().strip() != 'clear': - log = readfile(LOG_FILENAME).decode('UTF') + log = readfile(LOG_FILENAME) log = log.split('ERROR:') log_len = len(log) if cmd > log_len: diff --git a/plugins/event.py b/plugins/event.py index 2d41909..9cae21d 100644 --- a/plugins/event.py +++ b/plugins/event.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/execute.py b/plugins/execute.py index 2f2db6a..166cf5d 100644 --- a/plugins/execute.py +++ b/plugins/execute.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -25,12 +25,10 @@ calc_last_res = {} def cmd_execute(raw_in, text): try: - msg = html_escape_soft(remove_sub_space(unicode(eval(text)))) - except Exception, SM: - try: - SM = str(SM) - except: - SM = unicode(SM) + msg = html_escape_soft(remove_sub_space(str(eval(text)))) + except: + SM = '\n'.join(str(t) for t in sys.exc_info()) + SM = html_escape_soft(SM) msg = 'I can\'t execute it! Error: %s' % SM send_msg(raw_in, '%s' % msg) @@ -39,7 +37,7 @@ def cmd_calc(raw_in, text): ID = raw_in['message']['from']['id'] if 'ans' in text.lower() and calc_last_res.has_key(ID): text = text.lower().replace('ans', calc_last_res[ID]) - legal = string.digits + string.letters + '*/+-()=^!<>. ' + legal = string.digits + string.ascii_letters + '*/+-()=^!<>. ' ppc = 1 if '**' in text or 'pow' in text or 'factorial' in text: ppc = 0 @@ -98,4 +96,4 @@ commands = [['dpi', cmd_dpi_calc, False, 'less', 'DPI calculator. Width height s ['sh', cmd_shell, True, 'all', 'Execute shell command.'], ['shsilent', cms_shell_silent, True, 'all', 'Silent execute shell command.']] -# The end is near! \ No newline at end of file +# The end is near! diff --git a/plugins/friday.py b/plugins/friday.py index d7c3b90..db37691 100644 --- a/plugins/friday.py +++ b/plugins/friday.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -25,7 +25,7 @@ def cmd_friday(raw_in): day = datetime.datetime.isoweekday(datetime.datetime.now()) date_file = DATA_FOLDER % 'friday.txt' if os.path.isfile(date_file): - frases = readfile(date_file).decode('UTF') + frases = readfile(date_file) frases = map(lambda x: x.split(' || '), frases.split('\n')) week = [u'понедельник', u'вторник', u'среда', u'четверг', u'пятница', u'суббота', u'воскресенье'] if not frases: diff --git a/plugins/help.py b/plugins/help.py index dc9a561..3c5b913 100644 --- a/plugins/help.py +++ b/plugins/help.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -23,7 +23,7 @@ def get_su(t): return ['', '⚠️'][t] - + def cmd_help(raw_in, text): IS_OWNER = raw_in['message']['from'].get('id', '') == OWNER_ID text = text.lower().strip() @@ -72,7 +72,7 @@ def cmd_commands(raw_in): CHAT_ID = 0 rez = [] for cmd in COMMANDS: - if CHAT_ID not in cmd[5].get('black', []) and not (cmd[5].has_key('white') and CHAT_ID not in cmd[5].get('white', [])): + if CHAT_ID not in cmd[5].get('black', []) and not ('white' in cmd[5] and CHAT_ID not in cmd[5].get('white', [])): if IS_OWNER: rez.append((cmd[0], get_su(cmd[2]))) elif not cmd[2]: diff --git a/plugins/horo.py b/plugins/horo.py index 3df0943..49fc579 100644 --- a/plugins/horo.py +++ b/plugins/horo.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -40,12 +40,12 @@ def cmd_horoscope(raw_in, text): if param: if param == 'list': msg = '\n'.join(['%s /horo_%s' % \ - (horoemo[i], t.capitalize()) for i, t in enumerate(horodb)]) + (horoemo[i], t.capitalize()) for i, t in enumerate(horodb)]) elif param == 'date': msg = 'List of dates:\n%s' % '\n'.join(['%s … %s %s' % \ - (horo_dates[i], horoemo[i], t.capitalize()) for i, t in enumerate(horodb)]) + (horo_dates[i], horoemo[i], t.capitalize()) for i, t in enumerate(horodb)]) elif param in horodb: - body = html_encode(load_page('http://horo.mail.ru/prediction/%s/today' % param)) + body = load_page('http://horo.mail.ru/prediction/%s/today' % param) try: msg = '%s %s (%s)\n%s' % (horoemo[horodb.index(param)], \ param.capitalize(), \ diff --git a/plugins/logger.py b/plugins/logger.py index 1657700..a3453c5 100644 --- a/plugins/logger.py +++ b/plugins/logger.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -87,10 +87,10 @@ def logger_self(msg): def replace_items_regexp(t): t = t.group().strip(' @') return '@%s' % (t, t) - + def replace_items(text): return re.sub('@[\w]+', replace_items_regexp, text) - + def logger(raw_in): global CHAT_ID if LOG_DEBUG: diff --git a/plugins/news.py b/plugins/news.py index f79f6f4..b9a12a8 100644 --- a/plugins/news.py +++ b/plugins/news.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -31,17 +31,20 @@ def cmd_news(raw_in, text): limit = 1 else: limit = 1 + if hasattr(ssl, '_create_unverified_context'): + ssl._create_default_https_context = ssl._create_unverified_context feed = feedparser.parse('https://github.com/isida/vi/commits/master.atom') result = [] for data in feed['entries'][:limit]: link = data['links'][0]['href'] - link_name = link.split('/')[-1][:7] + link_name = link.split('/')[-1][:7] msg = '[%04d.%02d.%02d %02d:%02d:%02d]' % data['updated_parsed'][:6] msg += ' ' + data['author'] msg += ' %s' % (link, link_name) msg += '\n' + data['content'][0]['value'] result.append(msg) msg = '\n\n'.join(result) + print(msg) send_msg(raw_in, msg, custom = {'disable_web_page_preview': True}) commands = [['news', cmd_news, False, 'all', 'Show latest bot\'s commit.']] diff --git a/plugins/oboobs.py b/plugins/oboobs.py index 60f0476..63a27c6 100644 --- a/plugins/oboobs.py +++ b/plugins/oboobs.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/poem.py b/plugins/poem.py index 9a47857..b1857a8 100644 --- a/plugins/poem.py +++ b/plugins/poem.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/privatbank.py b/plugins/privatbank.py index 3019216..c0b95dd 100644 --- a/plugins/privatbank.py +++ b/plugins/privatbank.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/service.py b/plugins/service.py index 7a002ab..a24a8eb 100644 --- a/plugins/service.py +++ b/plugins/service.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -31,7 +31,7 @@ def raw_bot_restart(raw_in, msg, status): def cmd_update(raw_in): raw_bot_restart(raw_in, 'Let\'s update!', 'update') - + def cmd_restart(raw_in): raw_bot_restart(raw_in, 'Let\'s restart!', 'restart') diff --git a/plugins/shortlink.py b/plugins/shortlink.py index 65104a4..4f2c378 100644 --- a/plugins/shortlink.py +++ b/plugins/shortlink.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -28,7 +28,8 @@ SHORT_QR = 'http://chart.apis.google.com/chart?cht=qr&chs=350x350&chld=M|2&chl=% def shorter_raw(raw_in, text, url): text = text.strip() if text: - msg = load_page(url % enidna(text).decode('utf-8')) + msg = load_page(url % text) + print(msg) else: msg = 'What?' send_msg(raw_in, msg) diff --git a/plugins/test.py b/plugins/test.py index 6924fbc..ba216da 100644 --- a/plugins/test.py +++ b/plugins/test.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/todate.py b/plugins/todate.py index 4ca1c41..3c0c65e 100644 --- a/plugins/todate.py +++ b/plugins/todate.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -34,7 +34,7 @@ def cmd_todate(raw_in, text): else: year = time.localtime()[0] try: - _ = time.mktime([year, month, day] + [0] * 6) + _ = time.mktime(tuple([year, month, day] + [0] * 6)) days_remain = (datetime.date(year, month, day) - datetime.date.today()).days if days_remain > 0: msg = 'To %02d.%02d.%04d remain %s day(s).' % (day, month, year, days_remain) @@ -43,7 +43,8 @@ def cmd_todate(raw_in, text): else: msg = '%02d.%02d.%04d is now!' % (day, month, year) except: - msg = 'Wrong date.' + raise + msg = 'Wrong date.' else: msg = 'Need date.' send_msg(raw_in, msg) diff --git a/plugins/turn.py b/plugins/turn.py index 1ad8c7e..c6bc6c9 100644 --- a/plugins/turn.py +++ b/plugins/turn.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/whoami.py b/plugins/whoami.py index 6a25ace..bbf37b5 100644 --- a/plugins/whoami.py +++ b/plugins/whoami.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/xkcd.py b/plugins/xkcd.py index 33756e0..65b4163 100644 --- a/plugins/xkcd.py +++ b/plugins/xkcd.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # diff --git a/plugins/yandexcurrency.py b/plugins/yandexcurrency.py index eb8a6ca..3c5fa49 100644 --- a/plugins/yandexcurrency.py +++ b/plugins/yandexcurrency.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # @@ -25,11 +25,16 @@ def cmd_yandex_currency(raw_in): data = requests.get('https://yandex.ru').content regexp = '''(.+?)') + data = str(data).split('') r = [] + repl = [ + ['\\xe2\\x88\\x92', '-'] + ] for t in data: cur = t.split('<', 1)[0] stat = re.findall(regexp, t)[0].split('<', 1)[0] + for rep in repl: + stat = stat.replace(rep[0], rep[1]) r.append([cur, stat]) r = r[-3:] msg = 'Yandex rates
'
diff --git a/settings/config.ini.demo b/settings/config.ini.demo
index a5d54a8..aafe862 100644
--- a/settings/config.ini.demo
+++ b/settings/config.ini.demo
@@ -21,12 +21,20 @@ botinfo: white 3456789 98765432
 ; Install requests with socks support via:
 ; pip install -U requests[socks]
 ; Then uncomment settings bellow:
-; [proxy]
+; [socks_proxy]
 ; user: proxy_username_if_needed
 ; pass: proxy_password_if_nedded
 ; host: proxy_host
 ; port: proxy_port
 
+;--- Web settings
+[web]
+;proxy_user: proxy_username_if_needed
+;proxy_pass: proxy_password_if_nedded
+;proxy_host: proxy_host
+;proxy_port: proxy_port
+user_agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
+
 [log]
 url: http://your-mega-site.com/logs
 debug: false
-- 
cgit v1.2.3