tixcraft_bot/text_server.py

260 lines
40 KiB
Python
Raw Normal View History

2023-06-15 15:45:50 +00:00
#!/usr/bin/env python
#encoding=utf-8
try:
# for Python2
from Tkinter import *
import ttk
import tkMessageBox as messagebox
except ImportError:
# for Python3
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import os
import sys
import platform
2023-06-19 09:25:58 +00:00
import webbrowser
2023-06-15 15:45:50 +00:00
import pyperclip
import base64
2023-06-16 04:04:53 +00:00
import time
2023-06-15 15:45:50 +00:00
import threading
2023-06-16 04:04:53 +00:00
import socket
2023-06-15 15:45:50 +00:00
import asyncio
import tornado
from tornado.web import Application
CONST_APP_VERSION = "MaxBot (2023.09.15)"
2023-07-06 15:29:58 +00:00
2023-06-16 04:04:53 +00:00
CONST_MAXBOT_QUESTION_FILE = "MAXBOT_QUESTION.txt"
2023-06-15 16:31:46 +00:00
2023-06-16 04:04:53 +00:00
CONST_SERVER_PORT_DEFAULT = 8888
CONST_SERVER_PORT = CONST_SERVER_PORT_DEFAULT
2023-06-15 15:45:50 +00:00
def get_ip_address():
default_ip = "127.0.0.1"
ip = default_ip
try:
ip = [l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)),
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
except Exception as exc:
print(exc)
try:
ip = socket.gethostname()
except Exception as exc2:
print(exc2)
ip = default_ip
2023-06-15 15:45:50 +00:00
return ip
2023-06-16 04:04:53 +00:00
def btn_copy_ip_clicked():
2023-06-15 15:45:50 +00:00
local_ip = get_ip_address()
ip_address = "http://%s:%d/" % (local_ip,CONST_SERVER_PORT)
pyperclip.copy(ip_address)
2023-06-16 04:04:53 +00:00
def btn_copy_question_clicked():
global txt_question
question_text = txt_question.get("1.0",END).strip()
2023-06-19 09:25:58 +00:00
if len(question_text) > 0:
pyperclip.copy(question_text)
def btn_query_question_clicked():
global txt_question
question_text = txt_question.get("1.0",END).strip()
if len(question_text) > 0:
webbrowser.open("https://www.google.com/search?q="+question_text)
2023-06-16 04:04:53 +00:00
def btn_paste_answer_by_user():
print("btn_paste_answer_by_user")
2023-06-15 15:45:50 +00:00
def TextInput(root, UI_PADDING_X):
row_count = 0
frame_group_header = Frame(root)
group_row_count = 0
global lbl_ip
lbl_ip = Label(frame_group_header, text="IP")
lbl_ip.grid(column=0, row=group_row_count, sticky = E)
local_ip = get_ip_address()
ip_address = "http://%s:8888/" % (local_ip)
global lbl_ip_address
lbl_ip_address = Label(frame_group_header, text=ip_address)
lbl_ip_address.grid(column=1, row=group_row_count, sticky = W)
icon_copy_filename = "icon_copy_2.gif"
icon_copy_img = PhotoImage(file=icon_copy_filename)
2023-06-16 04:04:53 +00:00
lbl_icon_copy_ip = Label(frame_group_header, image=icon_copy_img, cursor="hand2")
lbl_icon_copy_ip.image = icon_copy_img
lbl_icon_copy_ip.grid(column=2, row=group_row_count, sticky = W+N)
lbl_icon_copy_ip.bind("<Button-1>", lambda e: btn_copy_ip_clicked())
group_row_count += 1
global lbl_question
lbl_question = Label(frame_group_header, text="Question")
lbl_question.grid(column=0, row=group_row_count, sticky = E+N)
global txt_question
2023-06-19 09:25:58 +00:00
txt_question = Text(frame_group_header, width=50, height=22)
2023-06-16 04:04:53 +00:00
txt_question.grid(column=1, row=group_row_count, sticky = W)
txt_question.insert("1.0", "")
lbl_icon_copy_question = Label(frame_group_header, image=icon_copy_img, cursor="hand2")
lbl_icon_copy_question.image = icon_copy_img
2023-06-21 08:29:07 +00:00
#lbl_icon_copy_question.grid(column=2, row=group_row_count, sticky = W+N)
2023-06-16 04:04:53 +00:00
lbl_icon_copy_question.bind("<Button-1>", lambda e: btn_copy_question_clicked())
2023-06-15 15:45:50 +00:00
2023-06-19 09:25:58 +00:00
icon_query_filename = "icon_query_5.gif"
icon_query_img = PhotoImage(file=icon_query_filename)
lbl_icon_query_question = Label(frame_group_header, image=icon_query_img, cursor="hand2")
lbl_icon_query_question.image = icon_query_img
2023-06-21 08:29:07 +00:00
lbl_icon_query_question.grid(column=2, row=group_row_count, sticky = W+N)
2023-06-19 09:25:58 +00:00
lbl_icon_query_question.bind("<Button-1>", lambda e: btn_query_question_clicked())
2023-06-15 15:45:50 +00:00
group_row_count += 1
2023-06-16 04:04:53 +00:00
global lbl_answer
lbl_answer = Label(frame_group_header, text="Answer")
lbl_answer.grid(column=0, row=group_row_count, sticky = E)
2023-06-15 15:45:50 +00:00
global txt_answer
global txt_answer_value
txt_answer_value = StringVar(frame_group_header, value="")
txt_answer = Entry(frame_group_header, width=30, textvariable = txt_answer_value)
txt_answer.grid(column=1, row=group_row_count, sticky = W)
txt_answer.bind('<Control-v>', lambda e: btn_paste_answer_by_user())
2023-06-16 04:04:53 +00:00
2023-06-15 15:45:50 +00:00
frame_group_header.grid(column=0, row=row_count, padx=UI_PADDING_X, pady=15)
def main_ui():
global root
root = Tk()
root.title(CONST_APP_VERSION)
global UI_PADDING_X
UI_PADDING_X = 20
TextInput(root, UI_PADDING_X)
GUI_SIZE_WIDTH = 520
GUI_SIZE_HEIGHT = 420
2023-06-15 15:45:50 +00:00
GUI_SIZE_MACOS = str(GUI_SIZE_WIDTH) + 'x' + str(GUI_SIZE_HEIGHT)
GUI_SIZE_WINDOWS=str(GUI_SIZE_WIDTH-30) + 'x' + str(GUI_SIZE_HEIGHT-35)
2023-06-15 15:45:50 +00:00
GUI_SIZE =GUI_SIZE_MACOS
if platform.system() == 'Windows':
GUI_SIZE = GUI_SIZE_WINDOWS
root.geometry(GUI_SIZE)
# for icon.
icon_filepath = 'tmp.ico'
# icon format.
iconImg = 'AAABAAEAAAAAAAEAIAD4MgAAFgAAAIlQTkcNChoKAAAADUlIRFIAAAEAAAABAAgGAAAAXHKoZgAAAAFzUkdCAK7OHOkAAABQZVhJZk1NACoAAAAIAAIBEgADAAAAAQABAACHaQAEAAAAAQAAACYAAAAAAAOgAQADAAAAAQABAACg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MNMNq9G3yuY4d8zwaWkaqOKP1V7lPjcOHPanHr2GcP6hGCQTLe/693AtX2r2Wo9kOKjpyhpxlp5Zv0VIVU6jNcMAoArrJpMMDInAFbdzSJbiUIfXvktCmYQsPIUf2rIgjEXPiQLxu8UScij2bU68q9fMSef/lrZ10kM3NSMnBdpiCYACaAJjbtaOPBFcxB5/6yXKNKYwRHgGmpCkjwfmRMAAScDr0K9hlFLath5op+0/D3TX2r0meYKa4AVcqyU82TBtoZy4f2CGVTYsWCGFXdhWTAmw2RVftvqTUNT/8rLvyov
tmpIcon = open(icon_filepath, 'wb+')
tmpIcon.write(base64.b64decode(iconImg))
tmpIcon.close()
if platform.system() == 'Windows':
root.iconbitmap(icon_filepath)
if platform.system() == 'Darwin':
#from PIL import Image, ImageTk
#logo = ImageTk.PhotoImage(Image.open(icon_filepath).convert('RGB'))
#root.call('wm', 'iconphoto', root._w, logo)
pass
if platform.system() == 'Linux':
logo = PhotoImage(file=icon_filepath)
root.call('wm', 'iconphoto', root._w, logo)
os.remove(icon_filepath)
root.mainloop()
2023-06-15 15:45:50 +00:00
class MainHandler(tornado.web.RequestHandler):
def format_config_keyword_for_json(self, user_input):
if len(user_input) > 0:
if not ('\"' in user_input):
user_input = '"' + user_input + '"'
return user_input
def compose_as_json(self, user_input):
user_input = self.format_config_keyword_for_json(user_input)
return "{\"data\":[%s]}" % user_input
2023-06-15 15:45:50 +00:00
def get(self):
global txt_answer_value
answer_text = ""
try:
answer_text = txt_answer_value.get().strip()
except Exception as exc:
pass
answer_text_output = self.compose_as_json(answer_text)
#print("answer_text_output:", answer_text_output)
self.write(answer_text_output)
2023-06-15 15:45:50 +00:00
2023-06-16 04:04:53 +00:00
class QuestionHandler(tornado.web.RequestHandler):
def get(self):
global txt_question
txt_question.insert("1.0", "")
2023-06-15 15:45:50 +00:00
async def main_server():
app = Application([
(r"/", MainHandler),
2023-06-16 04:04:53 +00:00
(r"/query", MainHandler),
(r"/question", QuestionHandler),
2023-06-15 15:45:50 +00:00
])
app.listen(CONST_SERVER_PORT)
await asyncio.Event().wait()
def web_server():
asyncio.run(main_server())
2023-06-16 04:04:53 +00:00
def preview_question_text_file():
if os.path.exists(CONST_MAXBOT_QUESTION_FILE):
infile = None
if platform.system() == 'Windows':
infile = open(CONST_MAXBOT_QUESTION_FILE, 'r', encoding='UTF-8')
else:
infile = open(CONST_MAXBOT_QUESTION_FILE, 'r')
if not infile is None:
question_text = infile.readline()
global txt_question
try:
displayed_question_text = txt_question.get("1.0",END).strip()
if displayed_question_text != question_text:
# start to refresh
txt_question.delete("1.0","end")
if len(question_text) > 0:
txt_question.insert("1.0", question_text)
except Exception as exc:
pass
2023-06-16 04:04:53 +00:00
def text_server_timer():
while True:
preview_question_text_file()
time.sleep(0.2)
2023-06-15 15:45:50 +00:00
if __name__ == "__main__":
2023-06-16 04:04:53 +00:00
threading.Thread(target=text_server_timer, daemon=True).start()
2023-06-15 15:45:50 +00:00
threading.Thread(target=web_server, daemon=True).start()
main_ui()