Spaces:
Runtime error
Runtime error
dopc
commited on
Commit
·
6dd7d1d
1
Parent(s):
d82b112
first trial
Browse files- Dockerfile +27 -0
- etymbot.py +55 -0
Dockerfile
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ARG env=prod
|
2 |
+
|
3 |
+
FROM python:3.10
|
4 |
+
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
COPY ./requirements.txt /app/requirements.txt
|
8 |
+
|
9 |
+
RUN pip3 install --no-cache-dir -r /app/requirements.txt
|
10 |
+
|
11 |
+
# User
|
12 |
+
RUN useradd -m -u 1000 user
|
13 |
+
USER user
|
14 |
+
ENV HOME /home/user
|
15 |
+
ENV PATH $HOME/.local/bin:$PATH
|
16 |
+
|
17 |
+
WORKDIR $HOME
|
18 |
+
RUN mkdir app
|
19 |
+
WORKDIR $HOME/app
|
20 |
+
COPY . $HOME/app
|
21 |
+
|
22 |
+
# mount secrets
|
23 |
+
RUN if [ "$env" != "dev" ] --mount=type=secret,id=BOT_TOKEN,mode=0444,required=true ; then \
|
24 |
+
git init && \
|
25 |
+
git remote add origin $(cat /run/secrets/BOT_TOKEN) ; fi
|
26 |
+
|
27 |
+
CMD python etymbot.py
|
etymbot.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from telegram.ext import Updater
|
2 |
+
from telegram.ext import InlineQueryHandler
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
import requests
|
5 |
+
import logging
|
6 |
+
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
7 |
+
import os
|
8 |
+
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
updater = Updater(token=os.getenv("BOT_TOKEN"))
|
12 |
+
dispatcher = updater.dispatcher
|
13 |
+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
14 |
+
level=logging.INFO)
|
15 |
+
SEARCH_URL = 'https://www.etymonline.com/search?q='
|
16 |
+
|
17 |
+
|
18 |
+
def inline_etym(update, context):
|
19 |
+
query = update.inline_query.query
|
20 |
+
try:
|
21 |
+
logger.info(f"Query: {query}")
|
22 |
+
results = list()
|
23 |
+
text = first_result_return(query)
|
24 |
+
content = f"{query}:\n{text}"
|
25 |
+
results.append(InlineQueryResultArticle(
|
26 |
+
id=query,
|
27 |
+
title=query,
|
28 |
+
input_message_content=InputTextMessageContent(content))
|
29 |
+
)
|
30 |
+
context.bot.answer_inline_query(update.inline_query.id, results)
|
31 |
+
except AttributeError:
|
32 |
+
pass
|
33 |
+
|
34 |
+
|
35 |
+
if __name__ == '__main__':
|
36 |
+
inline_etym_handler = InlineQueryHandler(inline_etym)
|
37 |
+
dispatcher.add_handler(inline_etym_handler)
|
38 |
+
updater.start_polling()
|
39 |
+
|
40 |
+
|
41 |
+
'''
|
42 |
+
functions from https://github.com/tetrismegistus/etym
|
43 |
+
'''
|
44 |
+
|
45 |
+
|
46 |
+
def soup_search(search_term):
|
47 |
+
# post request to search URL, return beautiful soup parsed object
|
48 |
+
url = SEARCH_URL + search_term
|
49 |
+
response = requests.get(url)
|
50 |
+
return BeautifulSoup(response.text, 'html.parser')
|
51 |
+
|
52 |
+
|
53 |
+
def first_result_return(word):
|
54 |
+
search_page = soup_search(word)
|
55 |
+
return search_page.find("section", class_="word__defination--2q7ZH undefined").get_text()
|