Code has been formatted.

This commit is contained in:
Alexandr
2018-08-03 15:15:35 +03:00
committed by Alexandr Bogomyakov
parent 7320a10b51
commit 31ce4d15cf
4 changed files with 74 additions and 71 deletions

View File

@ -1,5 +1,6 @@
import datetime as dt import datetime as dt
class DataBase: class DataBase:
def __init__(self, basefile, scheme): def __init__(self, basefile, scheme):
import sqlite3 import sqlite3
@ -76,7 +77,7 @@ class DataBase:
self.execute(sql) self.execute(sql)
def get_top(self, user_id, conf_id, limit=10): def get_top(self, user_id, conf_id, limit=10):
sql= """ sql = """
SELECT w.word, COUNT(*) as count FROM relations r SELECT w.word, COUNT(*) as count FROM relations r
LEFT JOIN word w ON w.id = r.word_id LEFT JOIN word w ON w.id = r.word_id
LEFT JOIN `user` u ON u.id = r.user_id LEFT JOIN `user` u ON u.id = r.user_id
@ -99,7 +100,7 @@ class DataBase:
return(result) return(result)
def here(self, user_id, conf_id): def here(self, user_id, conf_id):
sql= """ sql = """
SELECT DISTINCT(u.username) FROM relations r SELECT DISTINCT(u.username) FROM relations r
LEFT JOIN user u LEFT JOIN user u
ON u.id = r.user_id ON u.id = r.user_id

View File

@ -12,4 +12,4 @@ db = DataBase(
scheme='assets/main.db.sql') scheme='assets/main.db.sql')
global worker global worker
worker = MessageWorker(db = db) worker = MessageWorker(db=db)

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from http.server import HTTPServer,SimpleHTTPRequestHandler,CGIHTTPRequestHandler from http.server import HTTPServer, SimpleHTTPRequestHandler, CGIHTTPRequestHandler
from socketserver import BaseServer from socketserver import BaseServer
import ssl import ssl
import json import json
@ -10,6 +10,7 @@ import settings
# fuckin dirty hack. idk the best way to inherit return func into # fuckin dirty hack. idk the best way to inherit return func into
# RequestHandler class # RequestHandler class
class RequestHandler(SimpleHTTPRequestHandler): class RequestHandler(SimpleHTTPRequestHandler):
def __init__(self, def __init__(self,
request, request,
@ -36,17 +37,16 @@ class RequestHandler(SimpleHTTPRequestHandler):
pass pass
class WebHook: class WebHook:
def __init__(self, def __init__(self,
certfile, certfile,
keyfile, keyfile,
address = '0.0.0.0', address='0.0.0.0',
port=8443, port=8443,
RequestHandler=RequestHandler): RequestHandler=RequestHandler):
self.httpd = HTTPServer((address, port), 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, certfile=certfile,
keyfile=keyfile, keyfile=keyfile,
server_side=True) server_side=True)

View File

@ -18,8 +18,9 @@ from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.styles import get_style_by_name from pygments.styles import get_style_by_name
from pygments.formatters import ImageFormatter from pygments.formatters import ImageFormatter
class MessageWorker: 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.stop_words = stop_words
self.db = db self.db = db
self.telegram_key = settings.parser.get('bot', 'telegram_key') self.telegram_key = settings.parser.get('bot', 'telegram_key')
@ -38,7 +39,8 @@ class MessageWorker:
def handleUpdate(self, msg): def handleUpdate(self, msg):
try: try:
try: try:
input_message = msg['message']['text'].replace('@' + self.me['result']['username'], '') input_message = msg['message']['text'].replace(
'@' + self.me['result']['username'], '')
except: except:
input_message = msg['message']['text'] input_message = msg['message']['text']
if input_message == '/scheme': if input_message == '/scheme':
@ -125,7 +127,7 @@ class MessageWorker:
self.db.add_conf(conf_id, chat_title) self.db.add_conf(conf_id, chat_title)
if len(msg['message']['text'][6:]) < 10000: if len(msg['message']['text'][6:]) < 10000:
code = msg['message']['text'][6:] code = msg['message']['text'][6:]
code_tag = code[code.rfind('#')+1:] code_tag = code[code.rfind('#') + 1:]
try: try:
lexer = get_lexer_by_name(code_tag) lexer = get_lexer_by_name(code_tag)
code = code[:code.rfind('#')] code = code[:code.rfind('#')]
@ -175,19 +177,19 @@ class MessageWorker:
for word in collection: for word in collection:
self.db.add_relation(word=word, user_id=user_id, conf_id=chat_id) self.db.add_relation(word=word, user_id=user_id, conf_id=chat_id)
def clean_text(self, s): def clean_text(self, s):
file = open(self.stop_words, 'rt') file = open(self.stop_words, 'rt')
sw = file.read().split('\n') sw = file.read().split('\n')
file.close() 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) print(s)
# dirty hack with dat fucking file # dirty hack with dat fucking file
fh = open("tmp.txt","w") fh = open("tmp.txt", "w")
fh.write(s) fh.write(s)
fh.close() fh.close()
cmd = "./assets/mystem -nlwd < tmp.txt" 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] output = ps.communicate()[0]
os.remove("tmp.txt") os.remove("tmp.txt")
# end of the fuckin' dirty hack # end of the fuckin' dirty hack
@ -196,7 +198,7 @@ class MessageWorker:
s = s.split('\n') s = s.split('\n')
collection = [] collection = []
for word in s: for word in s:
if len(word) >2: if len(word) > 2:
if word not in sw: if word not in sw:
collection.append(word) collection.append(word)
else: else:
@ -206,7 +208,7 @@ class MessageWorker:
return collection return collection
def send(self, id, msg): def send(self, id, msg):
#print(msg) # print(msg)
url = self.telegram_api + 'bot' + self.telegram_key + '/sendMessage' url = self.telegram_api + 'bot' + self.telegram_key + '/sendMessage'
post_fields = { post_fields = {
'text': msg, 'text': msg,