Skip to content

Commit

Permalink
Added getting currency data by dollar from api
Browse files Browse the repository at this point in the history
  • Loading branch information
YauheniPo committed Dec 24, 2019
1 parent f1fbce5 commit 2b09633
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 58 deletions.
31 changes: 31 additions & 0 deletions features/currency/currency_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json

import requests
import datetime

from features.currency.сurrency import Currency

dollar_id = 145
date_format = "%Y-%m-%d"


def fetch_currency_list(json_data):
text = json.dumps(json_data, sort_keys=True, indent=4)
print(text)

return [Currency(**d) for d in json_data]


def get_currency_response_json():
end_date = datetime.datetime.now().strftime(date_format)
start_date = (datetime.datetime.now() - datetime.timedelta(days=30)).strftime(date_format)

parameters = {
"startDate": start_date,
"endDate": end_date
}

response = requests.get("http://www.nbrb.by/API/ExRates/Rates/Dynamics/{currency_id}".format(currency_id=dollar_id),
params=parameters)

return response.json()
13 changes: 13 additions & 0 deletions features/currency/сurrency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dataclasses import dataclass
import datetime


@dataclass
class Currency:
Cur_ID: int
Cur_OfficialRate: float

def __init__(self, Cur_ID, Date, Cur_OfficialRate, *args, **kwargs):
self.Cur_ID = Cur_ID
self.Date = datetime.datetime.strptime(Date, '%Y-%m-%dT%H:%M:%S')
self.Cur_OfficialRate = Cur_OfficialRate
15 changes: 8 additions & 7 deletions popobot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-

import logging
import requests
from configparser import ExtendedInterpolation, ConfigParser

import telebot
from telebot import types
from telebot.types import ReplyKeyboardMarkup, KeyboardButton

from features.currency.currency_api import fetch_currency_list, get_currency_response_json
from user import User

logging.basicConfig(filename='log.log', datefmt='%d/%m/%Y %I:%M:%S %p',
Expand All @@ -27,13 +26,15 @@ def init_user(message):


def start(user):
bot.send_message(chat_id=user.get_user_id(),
text='Привет {username}, давай пообщаемся?'.format(username=user.get_username()))
bot.send_message(chat_id=user.user_id,
text='Привет {username}, давай пообщаемся?'.format(username=user.username))


@bot.message_handler(commands=['currency'])
def get_currency(message):
return
user = init_user(message)

bot.send_message(chat_id=user.user_id, text=fetch_currency_list(get_currency_response_json())[-1].Cur_OfficialRate, reply_markup=base_buttons)


command_dict = {'/start': start}
Expand All @@ -58,10 +59,10 @@ def echo_all(message):
# key_no = types.InlineKeyboardButton(text='Нет', callback_data='no')
# keyboard.add(key_no)

bot.send_message(chat_id=user.get_user_id(), text="xx", reply_markup=base_buttons)
bot.send_message(chat_id=user.user_id, text="xx", reply_markup=base_buttons)
# bot.send_message(reply_markup=btn)

# bot.send_message(chat_id=user.get_user_id(), text="Hi")
# bot.send_message(chat_id=user.user_id, text="Hi")


@bot.callback_query_handler(func=lambda call: True) # обработчик кнопок
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
telebot==0.0.3
apiai==1.2.3
pyTelegramBotAPI==3.6.5
requests==2.20.0
pyowm==2.9.0
python-telegram-bot==10.1.0
pyTelegramBotAPI==3.6.6
requests==2.22.0
pyowm==2.10.0
python-telegram-bot==12.2.0
54 changes: 7 additions & 47 deletions user.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,13 @@
class User:

def __init__(self, last_name, first_name, username, user_id, lang, message):
self._last_name = last_name
self._first_name = first_name
self._username = username
self._user_id = user_id
self._lang = lang
self._message_text = message.text
self._message = message

def get_last_name(self):
return self._last_name

def get_first_name(self):
return self._first_name

def get_username(self):
return self._username

def get_user_id(self):
return self._user_id

def get_lang(self):
return self._lang

def get_message(self):
return self._message

def get_message_text(self):
return self._message_text
self.last_name = last_name
self.first_name = first_name
self.username = username
self.user_id = user_id
self.lang = lang
self.message_text = message.text
self.message = message

def __repr__(self):
return str(self.__dict__)


class OnlyOne:
class __OnlyOne:

def __str__(self):
return repr(self)
instance = None

def __init__(self):
if not OnlyOne.instance:
OnlyOne.instance = OnlyOne.__OnlyOne()

def __getattr__(self, name):
return getattr(self.instance, name)

def __setattr__(self, **kwargs):
for key, value in kwargs.items():
setattr(self.instance, key, value)

0 comments on commit 2b09633

Please sign in to comment.