mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-06 00:33:59 +03:00
initial commit
This commit is contained in:
commit
8c6dc13ceb
3 changed files with 91 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.idea
|
19
LICENSE
Normal file
19
LICENSE
Normal file
|
@ -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.
|
71
main.py
Normal file
71
main.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue