From 8c6dc13ceb9c77f0a95a06f97b0c7b5a62a6dfee Mon Sep 17 00:00:00 2001 From: Redume Date: Wed, 8 Mar 2023 13:31:45 +0300 Subject: [PATCH] initial commit --- .gitignore | 1 + LICENSE | 19 +++++++++++++++ main.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..dbc1b68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023 Redume + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..95c0c01 --- /dev/null +++ b/main.py @@ -0,0 +1,71 @@ +import requests +import json +import hashlib + +from aiogram import Bot, types +from aiogram.dispatcher import Dispatcher +from aiogram.utils import executor + + +bot = Bot(token="") +dp = Dispatcher(bot) + + +def get_currency(amount, from_currency, to_currency): + if amount is None: + amount = "1" + + try: + page = requests.get(f"https://duckduckgo.com/js/spice/currency/{amount}/{from_currency}/{to_currency}") + page = page.text.replace('ddg_spice_currency(', "").replace(');', "") + page = json.loads(page) + if page["headers"]["description"].find("ERROR") != -1: + return + + except KeyError: + return print("Чет сломалось") + + return page["conversion"] + + +def isNum(value): + return value.isdecimal() or value.replace('.', '', 1).isdecimal() + + +@dp.inline_handler() +async def currency(query: types.InlineQuery): + text = query.query.split(" ") + result_id: str = hashlib.md5(query.query.encode()).hexdigest() + + if text == ['']: + return + + for i in range(len(text)): + if isNum(text[0]): + continue + + if text[i].find(",") != -1: + text[i] = text[i].replace(",", ".") + + result: str + try: + if isNum(text[0]): + res = get_currency(text[0], text[1], text[2]) + else: + res = get_currency(None, text[0], text[1]) + except Exception: + return + + result = f"{res['from-amount']} {res['from-currency-symbol']} = {res['converted-amount']} {res['to-currency-symbol']}" + + article = [types.InlineQueryResultArticle( + id=result_id, + title="The rate of a certain currency", + input_message_content=types.InputTextMessageContent( + message_text=str(result) + ))] + + await query.answer(article, cache_time=1, is_personal=True) + + +executor.start_polling(dp, skip_updates=True)