mirror of
https://github.com/house-of-vanity/conf_bot.git
synced 2025-07-06 14:24:08 +00:00
Code has been formatted.
This commit is contained in:
committed by
Alexandr Bogomyakov
parent
7320a10b51
commit
31ce4d15cf
@ -1,5 +1,6 @@
|
||||
import datetime as dt
|
||||
|
||||
|
||||
class DataBase:
|
||||
def __init__(self, basefile, scheme):
|
||||
import sqlite3
|
||||
@ -76,7 +77,7 @@ class DataBase:
|
||||
self.execute(sql)
|
||||
|
||||
def get_top(self, user_id, conf_id, limit=10):
|
||||
sql= """
|
||||
sql = """
|
||||
SELECT w.word, COUNT(*) as count FROM relations r
|
||||
LEFT JOIN word w ON w.id = r.word_id
|
||||
LEFT JOIN `user` u ON u.id = r.user_id
|
||||
@ -99,7 +100,7 @@ class DataBase:
|
||||
return(result)
|
||||
|
||||
def here(self, user_id, conf_id):
|
||||
sql= """
|
||||
sql = """
|
||||
SELECT DISTINCT(u.username) FROM relations r
|
||||
LEFT JOIN user u
|
||||
ON u.id = r.user_id
|
||||
|
@ -12,4 +12,4 @@ db = DataBase(
|
||||
scheme='assets/main.db.sql')
|
||||
|
||||
global worker
|
||||
worker = MessageWorker(db = db)
|
||||
worker = MessageWorker(db=db)
|
||||
|
@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from http.server import HTTPServer,SimpleHTTPRequestHandler,CGIHTTPRequestHandler
|
||||
from http.server import HTTPServer, SimpleHTTPRequestHandler, CGIHTTPRequestHandler
|
||||
from socketserver import BaseServer
|
||||
import ssl
|
||||
import json
|
||||
@ -10,6 +10,7 @@ import settings
|
||||
# fuckin dirty hack. idk the best way to inherit return func into
|
||||
# RequestHandler class
|
||||
|
||||
|
||||
class RequestHandler(SimpleHTTPRequestHandler):
|
||||
def __init__(self,
|
||||
request,
|
||||
@ -36,17 +37,16 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class WebHook:
|
||||
def __init__(self,
|
||||
certfile,
|
||||
keyfile,
|
||||
address = '0.0.0.0',
|
||||
address='0.0.0.0',
|
||||
port=8443,
|
||||
RequestHandler=RequestHandler):
|
||||
|
||||
self.httpd = HTTPServer((address, port), RequestHandler)
|
||||
self.httpd.socket = ssl.wrap_socket (self.httpd.socket,
|
||||
self.httpd.socket = ssl.wrap_socket(self.httpd.socket,
|
||||
certfile=certfile,
|
||||
keyfile=keyfile,
|
||||
server_side=True)
|
||||
|
20
worker.py
20
worker.py
@ -18,8 +18,9 @@ from pygments.lexers import guess_lexer, get_lexer_by_name
|
||||
from pygments.styles import get_style_by_name
|
||||
from pygments.formatters import ImageFormatter
|
||||
|
||||
|
||||
class MessageWorker:
|
||||
def __init__(self, db, stop_words = 'assets/stop-word.ru', settings = settings):
|
||||
def __init__(self, db, stop_words='assets/stop-word.ru', settings=settings):
|
||||
self.stop_words = stop_words
|
||||
self.db = db
|
||||
self.telegram_key = settings.parser.get('bot', 'telegram_key')
|
||||
@ -38,7 +39,8 @@ class MessageWorker:
|
||||
def handleUpdate(self, msg):
|
||||
try:
|
||||
try:
|
||||
input_message = msg['message']['text'].replace('@' + self.me['result']['username'], '')
|
||||
input_message = msg['message']['text'].replace(
|
||||
'@' + self.me['result']['username'], '')
|
||||
except:
|
||||
input_message = msg['message']['text']
|
||||
if input_message == '/scheme':
|
||||
@ -125,7 +127,7 @@ class MessageWorker:
|
||||
self.db.add_conf(conf_id, chat_title)
|
||||
if len(msg['message']['text'][6:]) < 10000:
|
||||
code = msg['message']['text'][6:]
|
||||
code_tag = code[code.rfind('#')+1:]
|
||||
code_tag = code[code.rfind('#') + 1:]
|
||||
try:
|
||||
lexer = get_lexer_by_name(code_tag)
|
||||
code = code[:code.rfind('#')]
|
||||
@ -175,19 +177,19 @@ class MessageWorker:
|
||||
for word in collection:
|
||||
self.db.add_relation(word=word, user_id=user_id, conf_id=chat_id)
|
||||
|
||||
|
||||
def clean_text(self, s):
|
||||
file = open(self.stop_words, 'rt')
|
||||
sw = file.read().split('\n')
|
||||
file.close()
|
||||
s = re.sub(r'(https?:\/\/)?([\da-z\.-]+)\.([\/\w\.-]*)*\/?\S','',s,flags=re.MULTILINE)
|
||||
s = re.sub(
|
||||
r'(https?:\/\/)?([\da-z\.-]+)\.([\/\w\.-]*)*\/?\S', '', s, flags=re.MULTILINE)
|
||||
print(s)
|
||||
# dirty hack with dat fucking file
|
||||
fh = open("tmp.txt","w")
|
||||
fh = open("tmp.txt", "w")
|
||||
fh.write(s)
|
||||
fh.close()
|
||||
cmd = "./assets/mystem -nlwd < tmp.txt"
|
||||
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
|
||||
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
|
||||
output = ps.communicate()[0]
|
||||
os.remove("tmp.txt")
|
||||
# end of the fuckin' dirty hack
|
||||
@ -196,7 +198,7 @@ class MessageWorker:
|
||||
s = s.split('\n')
|
||||
collection = []
|
||||
for word in s:
|
||||
if len(word) >2:
|
||||
if len(word) > 2:
|
||||
if word not in sw:
|
||||
collection.append(word)
|
||||
else:
|
||||
@ -206,7 +208,7 @@ class MessageWorker:
|
||||
return collection
|
||||
|
||||
def send(self, id, msg):
|
||||
#print(msg)
|
||||
# print(msg)
|
||||
url = self.telegram_api + 'bot' + self.telegram_key + '/sendMessage'
|
||||
post_fields = {
|
||||
'text': msg,
|
||||
|
Reference in New Issue
Block a user