mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-04-29 16:57:09 +08:00
Merge pull request #1 from frostming/fix/command
feat: use subclass instead of monkeypatch
This commit is contained in:
commit
274e390458
54
hy_tg.py
54
hy_tg.py
@ -1,30 +1,29 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import random
|
|
||||||
import os
|
|
||||||
import gc
|
import gc
|
||||||
|
import io
|
||||||
|
import random
|
||||||
import subprocess
|
import subprocess
|
||||||
from telebot import TeleBot # type: ignore
|
|
||||||
from telebot.types import Message # type: ignore
|
|
||||||
|
|
||||||
from prettymapp.geo import get_aoi
|
|
||||||
from prettymapp.osm import get_osm_geometries
|
|
||||||
from prettymapp.plotting import Plot
|
|
||||||
from prettymapp.settings import STYLES
|
|
||||||
|
|
||||||
from matplotlib import figure
|
|
||||||
|
|
||||||
from PIL import Image
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import PIL
|
import PIL
|
||||||
import io
|
from matplotlib import figure
|
||||||
|
from PIL import Image
|
||||||
|
from prettymapp.geo import get_aoi
|
||||||
|
from prettymapp.osm import get_osm_geometries
|
||||||
|
from prettymapp.plotting import Plot as PrettyPlot
|
||||||
|
from prettymapp.settings import STYLES
|
||||||
|
from telebot import TeleBot # type: ignore
|
||||||
|
from telebot.types import Message # type: ignore
|
||||||
|
|
||||||
PIL.Image.MAX_IMAGE_PIXELS = 933120000
|
PIL.Image.MAX_IMAGE_PIXELS = 933120000
|
||||||
file_in = "map.jpg"
|
file_in = "map.jpg"
|
||||||
file_out = "map_out.jpg"
|
file_out = "map_out.jpg"
|
||||||
|
|
||||||
|
|
||||||
# monkey patch for Plot thanks @higuoxing https://github.com/higuoxing
|
class Plot(PrettyPlot):
|
||||||
def __post_init__(self):
|
# memory leak fix for Plot. thanks @higuoxing https://github.com/higuoxing
|
||||||
|
# refer to: https://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg11809.html
|
||||||
|
def __post_init__(self):
|
||||||
(
|
(
|
||||||
self.xmin,
|
self.xmin,
|
||||||
self.ymin,
|
self.ymin,
|
||||||
@ -52,9 +51,6 @@ def __post_init__(self):
|
|||||||
self.ax.set_ylim(self.ymin - self.bg_buffer_y, self.ymax + self.bg_buffer_y)
|
self.ax.set_ylim(self.ymin - self.bg_buffer_y, self.ymax + self.bg_buffer_y)
|
||||||
|
|
||||||
|
|
||||||
Plot.__post_init__ = __post_init__
|
|
||||||
|
|
||||||
|
|
||||||
def sizeof_image(image):
|
def sizeof_image(image):
|
||||||
with io.BytesIO() as buff:
|
with io.BytesIO() as buff:
|
||||||
image.save(buff, format="JPEG", quality=95)
|
image.save(buff, format="JPEG", quality=95)
|
||||||
@ -96,14 +92,25 @@ def main():
|
|||||||
|
|
||||||
# Init bot
|
# Init bot
|
||||||
bot = TeleBot(options.tg_token)
|
bot = TeleBot(options.tg_token)
|
||||||
bot_name = bot.get_me().username
|
bot.set_my_commands(
|
||||||
bot.delete_my_commands(scope=None, language_code=None)
|
[
|
||||||
|
{
|
||||||
|
"command": "github",
|
||||||
|
"description": "github poster: /github <github_user_name> [<start>-<end>]",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "map",
|
||||||
|
"description": "pretty map: /map <address>",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
print("Bot init done.")
|
print("Bot init done.")
|
||||||
|
|
||||||
|
@bot.message_handler(commands=["github"])
|
||||||
@bot.message_handler(regexp="^github:")
|
@bot.message_handler(regexp="^github:")
|
||||||
def github_poster_handler(message: Message):
|
def github_poster_handler(message: Message):
|
||||||
reply_message = bot.reply_to(message, "Generating poster please wait:")
|
reply_message = bot.reply_to(message, "Generating poster please wait:")
|
||||||
m = message.text.strip()[7:]
|
m = message.text.strip().split(maxsplit=1)[1].strip()
|
||||||
message_list = m.split(",")
|
message_list = m.split(",")
|
||||||
name = message_list[0].strip()
|
name = message_list[0].strip()
|
||||||
cmd_list = ["github_poster", "github", "--github_user_name", name, "--me", name]
|
cmd_list = ["github_poster", "github", "--github_user_name", name, "--me", name]
|
||||||
@ -112,7 +119,7 @@ def main():
|
|||||||
cmd_list.append("--year")
|
cmd_list.append("--year")
|
||||||
cmd_list.append(years.strip())
|
cmd_list.append(years.strip())
|
||||||
r = subprocess.check_output(cmd_list).decode("utf-8")
|
r = subprocess.check_output(cmd_list).decode("utf-8")
|
||||||
if r.find("done") > 0:
|
if "done" in r:
|
||||||
try:
|
try:
|
||||||
# TODO windows path
|
# TODO windows path
|
||||||
r = subprocess.check_output(
|
r = subprocess.check_output(
|
||||||
@ -127,12 +134,13 @@ def main():
|
|||||||
bot.reply_to(message, "Something wrong please check")
|
bot.reply_to(message, "Something wrong please check")
|
||||||
bot.delete_message(reply_message.chat.id, reply_message.message_id)
|
bot.delete_message(reply_message.chat.id, reply_message.message_id)
|
||||||
|
|
||||||
|
@bot.message_handler(commands=["map"])
|
||||||
@bot.message_handler(regexp="^map:")
|
@bot.message_handler(regexp="^map:")
|
||||||
def map_handler(message: Message):
|
def map_handler(message: Message):
|
||||||
reply_message = bot.reply_to(
|
reply_message = bot.reply_to(
|
||||||
message, "Generating pretty map may take some time please wait:"
|
message, "Generating pretty map may take some time please wait:"
|
||||||
)
|
)
|
||||||
m = message.text.strip()[4:]
|
m = message.text.strip().split(maxsplit=1)[1].strip()
|
||||||
location = m.strip()
|
location = m.strip()
|
||||||
styles_list = list(STYLES.keys())
|
styles_list = list(STYLES.keys())
|
||||||
style = random.choice(styles_list)
|
style = random.choice(styles_list)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user