def _obmen():
    url = 'https://obmen.dp.ua/'
    response_obmen = requests.get(url)
    soup = BeautifulSoup(response_obmen.content, 'html.parser')
    currencies = {
        'USD':
        soup.find_all(href=re.compile("usd-uah"))[0].find_all(
            'div', attrs={"class": "currencies__block-num"}),
        'EUR':
        soup.find_all(href=re.compile("eur-uah"))[0].find_all(
            'div', attrs={"class": "currencies__block-num"})
    }
    for i in currencies:
        currency = mch.CURR_USD if i == 'USD' else mch.CURR_EUR
        rate_kwargs = {
            'currency': currency,
            'buy': Decimal(currencies[i][0].contents[0]),
            'sale': Decimal(currencies[i][1].contents[0]),
            'source': mch.SR_OBMEN,
        }
        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency,
                                        source=mch.SR_OBMEN).last()
        if last_rate is None or (new_rate.buy != last_rate.buy
                                 or new_rate.sale != last_rate.sale):
            new_rate.save()
Example #2
0
def _industrialbank():
    url = 'https://industrialbank.ua/ua/'
    response_industrialbank = requests.get(url)
    soup = BeautifulSoup(response_industrialbank.content, 'html.parser')
    items = soup.find_all(
        'ul', attrs={"class": "col-sm-9 col-xs-12 exchange-rate-list"})
    currencies = {'USD': [], 'EUR': []}
    for i in items:
        all_list = i.find_all('span')
        j = 0
        while j < len(all_list) - 2:
            currencies[all_list[j].contents[0]].append(all_list[j +
                                                                1].contents[0])
            j += 2
    for i in currencies:
        currency = mch.CURR_USD if i == 'USD' else mch.CURR_EUR
        rate_kwargs = {
            'currency': currency,
            'buy': Decimal(currencies[i][0]),
            'sale': Decimal(currencies[i][1]),
            'source': mch.SR_INDUSTRIAL,
        }
        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency,
                                        source=mch.SR_INDUSTRIAL).last()
        if last_rate is None or (new_rate.buy != last_rate.buy
                                 or new_rate.sale != last_rate.sale):
            new_rate.save()
Example #3
0
def pumb():
    page = requests.get("https://about.pumb.ua/info/currency_converter")
    soup = BeautifulSoup(page.content, 'html.parser')
    rates = soup.find(class_="exchange-rate")

    curr_rate = rates.find_all('td')
    currencies = []
    usd_buy = curr_rate[1].get_text()
    usd_sell = curr_rate[2].get_text()
    currencies.append({
        'currency': mch.CURR_USD,
        'buy': round(Decimal(usd_buy), 2),
        'sale': round(Decimal(usd_sell), 2),
        'source': mch.SR_PUMB,
    })

    eur_buy = curr_rate[4].get_text()
    eur_sell = curr_rate[5].get_text()
    currencies.append({
        'currency': mch.CURR_EUR,
        'buy': round(Decimal(eur_buy), 2),
        'sale': round(Decimal(eur_sell), 2),
        'source': mch.SR_PUMB,
    })

    for rate_kwargs in currencies:
        Rate.objects.create(**rate_kwargs)
        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=rate_kwargs['currency'], source=rate_kwargs['source']).last()

        if last_rate is None or (last_rate and new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
            new_rate.save()
def _pb():
    url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'
    response = requests.get(url)
    r_json = response.json()
    for rate in r_json:
        if rate['ccy'] in {'USD', 'EUR'}:
            # print(rate['ccy'], rate['buy'], rate['sale'])
            # currency = mch.CURR_USD if rate['ccy'] == 'USD' else mch.CURR_EUR
            currency = {
                'USD': mch.CURR_USD,
                'EUR': mch.CURR_EUR,
            }[rate['ccy']]

            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(rate['buy']),
                'sale': Decimal(rate['sale']),
                'source': mch.SRC_PB,
            }

            # Rate.objects.create(**rate_kwargs)
            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency,
                                            source=mch.SRC_PB).last()

            if last_rate is None or (new_rate.buy != last_rate.buy
                                     or new_rate.sale != last_rate.sale):
                new_rate.save()
Example #5
0
def privat():
    url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'
    response = requests.get(url)
    r_json = response.json()
    print(r_json)

    for rate in r_json:
        if rate['ccy'] in {'USD', 'EUR'}:  # 0(1) if we use list it would be 0(n)
            # print(rate['ccy'], rate['buy'], rate['sale'])
            currency = mch.CURR_USD if rate['ccy'] == 'USD' else mch.CURR_EUR

            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(rate['buy']),
                'sale': Decimal(rate['sale']),
                'source': mch.SR_PRIVAT,
            }

            # Rate.objects.create(**rate_kwargs)
            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency, source=mch.SR_PRIVAT).last()

            # print(Rate.objects.filter(currency=currency, source=mch.SR_PRIVAT).query)
            if last_rate is None or (last_rate and new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
                new_rate.save()
def _mtb():
    url = 'https://mtb.ua/'
    response_mtb = requests.get(url)
    soup = BeautifulSoup(response_mtb.content, 'html.parser')
    items = soup.find_all('div', attrs={"class": "exchange-value_item"})
    currencies = {
        'USD': [],
        'EUR': []
    }
    for i in items:
        curr = i.find_all('div', attrs={"class": "exchange-value_currency"})[0].contents[0].strip()
        value = i.find_all('span', attrs={"class": "exchange-value_num"})[0].contents[0].strip()
        currencies[curr].append(value) if curr in {'USD', 'EUR'} else True
    for i in currencies:
        currency = mch.CURR_USD if i == 'USD' else mch.CURR_EUR
        rate_kwargs = {
            'currency': currency,
            'buy': Decimal(currencies[i][0]),
            'sale': Decimal(currencies[i][1]),
            'source': mch.SR_MTB,
        }
        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency, source=mch.SR_MTB).last()
        if last_rate is None or (new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
            new_rate.save()
def _mono():
    url = 'https://api.monobank.ua/bank/currency'
    response = requests.get(url)
    r_json = response.json()

    for rate in r_json:
        if rate['currencyCodeA']:
            if rate['currencyCodeA'] in {840, 978
                                         } and rate['currencyCodeB'] == 980:
                # print(rate['currencyCodeA'], rate['rateSell'], rate['rateBuy'])

                currency = {
                    840: mch.CURR_USD,
                    978: mch.CURR_EUR,
                }[rate['currencyCodeA']]

                rate_kwargs = {
                    'currency': currency,
                    'buy': round(Decimal(rate['rateBuy']), 2),
                    'sale': round(Decimal(rate['rateSell']), 2),
                    'source': mch.SRC_MB,
                }

                # Rate.objects.create(**rate_kwargs)
                new_rate = Rate(**rate_kwargs)
                last_rate = Rate.objects.filter(currency=currency,
                                                source=mch.SRC_MB).last()

                if last_rate is None or (new_rate.buy != last_rate.buy
                                         or new_rate.sale != last_rate.sale):
                    new_rate.save()
Example #8
0
def save_db(currency, buy, sell, bank):
    buy_dec = Decimal(buy)
    buy = buy_dec.quantize(Decimal("1.00"), ROUND_HALF_DOWN)

    sell_dec = Decimal(sell)
    sell = sell_dec.quantize(Decimal("1.00"), ROUND_HALF_DOWN)

    rate_kwargs = {
        'currency': currency,
        'buy': buy,
        'sale': sell,
        'source': bank,
    }

    # Rate.objects.create(**rate_kwargs)
    new_rate = Rate(**rate_kwargs)
    last_rate = Rate.objects.filter(currency=currency, source=bank).last()

    # from pdb import set_trace
    # set_trace()

    # if last_rate and (new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
    if last_rate is None or (new_rate.buy != last_rate.buy
                             or new_rate.sale != last_rate.sale):
        new_rate.save()
Example #9
0
def _privat():
    url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'
    response = requests.get(url)
    r_json = response.json()
    # print(r_json)

    for rate in r_json:
        if rate['ccy'] in {
                'USD', 'EUR'
        }:  # O(1) if we use list it would be O(n) ('USD', 'EUR')
            currency = mch.CURR_USD if rate['ccy'] == 'USD' else mch.CURR_EUR
            # currency = {
            #     'USD': mch.CURR_USD,
            #     'EUR': mch.CURR_EUR,
            # }[rate['ccy']]
            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(rate['buy']),
                'sale': Decimal(rate['sale']),
                'source': mch.SR_PRIVAT,
            }
            # Rate.objects.create(**rate_kwargs)
            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency,
                                            source=mch.SR_PRIVAT).last()

            # from pdb import set_trace
            # set_trace()

            # if last_rate and (new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
            if last_rate is None or (new_rate.buy != last_rate.buy
                                     or new_rate.sale != last_rate.sale):
                new_rate.save()
Example #10
0
 def create_rate(currency, buy_rate, sale_rate, source):
     r = Rate(currency=currency,
              buy=buy_rate,
              sale=sale_rate,
              source=source)
     r.save()
     return r.id
def _alpha():
    url = 'https://alfabank.ua/currency-exchange'
    r = requests.get(url)

    soup = BeautifulSoup(r.content, 'html.parser')
    html = list(soup.children)[6]
    body = list(html.children)[3]
    currencies = list(
        body.find_all('div', attrs={'class': 'exchange-data-currency'}))

    for curr in currencies:
        if curr.get_text() == 'USD/UAH':
            sale = list(
                body.find_all('span',
                              attrs={
                                  'class': 'rate-number',
                                  'data-currency': 'USD_SALE'
                              }))[0]
            buy = list(
                body.find_all('span',
                              attrs={
                                  'class': 'rate-number',
                                  'data-currency': 'USD_BUY'
                              }))[0]
            rate_kwargs = {
                'currency': mch.CURR_USD,
                'sale': Decimal(sale.get_text().strip()),
                'buy': Decimal(buy.get_text().strip()),
                'source': mch.SRC_ALPHA,
            }
        elif curr.get_text() == 'EUR/UAH':
            sale = list(
                body.find_all('span',
                              attrs={
                                  'class': 'rate-number',
                                  'data-currency': 'EUR_SALE'
                              }))[0]
            buy = list(
                body.find_all('span',
                              attrs={
                                  'class': 'rate-number',
                                  'data-currency': 'EUR_BUY'
                              }))[0]
            rate_kwargs = {
                'currency': mch.CURR_EUR,
                'sale': Decimal(sale.get_text().strip()),
                'buy': Decimal(buy.get_text().strip()),
                'source': mch.SRC_ALPHA,
            }
        else:
            continue

        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=new_rate.currency,
                                        source=mch.SRC_ALPHA).last()

        if last_rate is None or (new_rate.buy != last_rate.buy
                                 or new_rate.sale != last_rate.sale):
            new_rate.save()
Example #12
0
def previous_record_check_and_save(rate_kwargs):
    new_rate = Rate(**rate_kwargs)  # create new instance of currency record

    # get last currency record for source
    last_rate = Rate.objects.filter(currency=new_rate.currency, source=new_rate.source).last()

    # verify and save if records are different
    if last_rate is None or (new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
        new_rate.save()
Example #13
0
def _privat():
    url = 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5'
    response = requests.get(url)
    r_json = response.json()

    for rate in r_json:
        if rate['ccy'] in {'USD', 'EUR', 'RUR'}:
            if rate['ccy'] == 'USD':
                currency = mch.CURR_USD
            elif rate['ccy'] == 'EUR':
                currency = mch.CURR_EUR
            else:
                currency = mch.CURR_RUB

            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(rate['buy'][:6]),
                'sale': Decimal(rate['sale'][:6]),
                'source': mch.SR_PRIVAT
            }

            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency,
                                            source=mch.SR_PRIVAT).last()
            save_rate(last_rate, new_rate)
Example #14
0
def _otp():
    url = 'https://www.otpbank.com.ua/'
    response = requests.get(url)
    soup = bs(response.content, 'html.parser')
    currency_block = soup.find('tbody', class_='currency-list__body')
    currency_block = currency_block.select("tbody tr")

    for tr_tag in currency_block:
        curr = tr_tag.find('td', class_='currency-list__type').text
        if curr in {'USD', 'EUR'}:
            if curr == 'USD':
                currency = mch.CURR_USD
            else:
                currency = mch.CURR_EUR

            values = tr_tag.findAll('td', class_='currency-list__value')

            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(values[0].text),
                'sale': Decimal(values[1].text),
                'source': mch.SR_OTP
            }

            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency,
                                            source=mch.SR_OTP).last()
            save_rate(last_rate, new_rate)
Example #15
0
def _vkurse():
    url = 'http://vkurse.dp.ua/course.json'
    response = requests.get(url)
    r_json = response.json()

    for curr in r_json:
        if curr == 'Dollar':
            currency = mch.CURR_USD
        elif curr == 'Euro':
            currency = mch.CURR_EUR
        else:
            currency = mch.CURR_RUB

        buy = r_json[curr]['buy'].replace(',', '.')
        sale = r_json[curr]['sale'].replace(',', '.')

        rate_kwargs = {
            'currency': currency,
            'buy': Decimal(buy),
            'sale': Decimal(sale),
            'source': mch.SR_VKURSE
        }

        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency,
                                        source=mch.SR_VKURSE).last()
        save_rate(last_rate, new_rate)
Example #16
0
def _mono():
    url = 'https://api.monobank.ua/bank/currency'
    response = requests.get(url)
    r_json = response.json()

    if r_json != {'errorDescription': 'Too many requests'}:
        for rate in r_json:
            if (rate['currencyCodeA']
                    in {840, 978, 643}) and (rate['currencyCodeB'] == 980):
                if rate['currencyCodeA'] == 840:
                    currency = mch.CURR_USD
                elif rate['currencyCodeA'] == 978:
                    currency = mch.CURR_EUR
                elif rate['currencyCodeA'] == 643:
                    currency = mch.CURR_RUB

                rate_kwargs = {
                    'currency': currency,
                    'buy': Decimal(str(round(rate['rateBuy'], 3))),
                    'sale': Decimal(str(round(rate['rateSell'], 3))),
                    'source': mch.SR_MONO
                }

                new_rate = Rate(**rate_kwargs)
                last_rate = Rate.objects.filter(currency=currency,
                                                source=mch.SR_MONO).last()
                save_rate(last_rate, new_rate)
Example #17
0
def _oshchad():
    url = 'https://www.oschadbank.ua/ua'
    response = requests.get(url, verify=False)
    soup = bs(response.content, 'html.parser')
    currs = {'USD', 'EUR', 'RUB'}

    for i in currs:
        if i == 'USD':
            currency = mch.CURR_USD
            buy = soup.find('strong', class_='buy-USD').text.strip()
            sale = soup.find('strong', class_='sell-USD').text.strip()
        elif i == 'EUR':
            currency = mch.CURR_EUR
            buy = soup.find('strong', class_='buy-EUR').text.strip()
            sale = soup.find('strong', class_='sell-EUR').text.strip()
        else:
            currency = mch.CURR_RUB
            buy = soup.find('strong', class_='buy-RUB').text.strip()
            sale = soup.find('strong', class_='sell-RUB').text.strip()

        rate_kwargs = {
            'currency': currency,
            'buy': Decimal(buy),
            'sale': Decimal(sale),
            'source': mch.SR_OSHCHAD
        }

        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency,
                                        source=mch.SR_OSHCHAD).last()

        save_rate(last_rate, new_rate)
Example #18
0
def _pumb():
    url = 'https://www.pumb.ua/'
    response = requests.get(url)
    soup = bs(response.content, 'html.parser')
    currency_block = soup.find('div', class_='exchange-rate')
    currency_block = currency_block.select("table tr")
    target_blocks = [currency_block[1], currency_block[2], currency_block[3]]

    for block in target_blocks:
        if block.text.strip().split('\n')[0] == 'USD':
            currency = mch.CURR_USD
        elif block.text.strip().split('\n')[0] == 'EUR':
            currency = mch.CURR_EUR
        else:
            currency = mch.CURR_RUB

        block = block.find_all('td')
        buy = block[1].text
        sale = block[2].text

        rate_kwargs = {
            'currency': currency,
            'buy': Decimal(buy),
            'sale': Decimal(sale),
            'source': mch.SR_PUMB
        }

        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency,
                                        source=mch.SR_PUMB).last()

        save_rate(last_rate, new_rate)
Example #19
0
 def handle(self, *args, **options):
     for i in range(1, 1460):
         d = datetime.timedelta(days=-i)
         date = datetime.datetime.now() + d
         # date_in_list = str(date).split(' ')[0].split('-')
         date_in_list = datetime.datetime.strftime(date,
                                                   "%d.%m.%Y").split('.')
         url = f'https://api.privatbank.ua/p24api/exchange_rates?json&date=' \
               f'{date_in_list[0]}.{date_in_list[1]}.{date_in_list[2]}'
         response_privat = requests.get(url).json()
         r_json = response_privat['exchangeRate']
         for rate in r_json:
             if 'currency' in rate:
                 if rate['currency'] in {'USD', 'EUR'}:
                     currency = mch.CURR_USD if rate[
                         'currency'] == 'USD' else mch.CURR_EUR
                     date_time_str = response_privat['date']
                     if 'purchaseRate' in rate and 'saleRate' in rate:
                         rate_kwargs = {
                             'currency':
                             currency,
                             'buy':
                             Decimal(rate['purchaseRate']),
                             'sale':
                             Decimal(rate['saleRate']),
                             'source':
                             mch.SR_PRIVAT,
                             'created':
                             datetime.datetime.strptime(
                                 date_time_str, '%d.%m.%Y')
                         }
                     else:
                         rate_kwargs = {
                             'currency':
                             currency,
                             'buy':
                             Decimal(rate['purchaseRateNB']),
                             'sale':
                             Decimal(rate['saleRateNB']),
                             'source':
                             mch.SR_PRIVAT,
                             'created':
                             datetime.datetime.strptime(
                                 date_time_str, '%d.%m.%Y')
                         }
                     new_rate = Rate(**rate_kwargs)
                     new_rate.save()
def _mono():
    url = 'https://api.monobank.ua/bank/currency'
    response_mono = requests.get(url)
    r_json = response_mono.json()
    for rate in r_json:
        if rate['currencyCodeA'] in {840, 978} and rate['currencyCodeB'] == 980:
            currency = mch.CURR_USD if rate['currencyCodeA'] == 840 else mch.CURR_EUR
            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(rate['rateBuy']).quantize(Decimal("1.00")),
                'sale': Decimal(rate['rateSell']).quantize(Decimal("1.00")),
                'source': mch.SR_MONO,
            }
            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency, source=mch.SR_MONO).last()
            if last_rate is None or (new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
                new_rate.save()
Example #21
0
def save_db_date(currency, buy, sell, bank, date=None):
    buy_dec = Decimal(buy)
    buy = buy_dec.quantize(Decimal("1.00"), ROUND_HALF_DOWN)

    sell_dec = Decimal(sell)
    sell = sell_dec.quantize(Decimal("1.00"), ROUND_HALF_DOWN)

    rate_kwargs = {
        'currency': currency,
        'buy': buy,
        'sale': sell,
        'source': bank,
        'created': date,
    }

    new_rate = Rate(**rate_kwargs)
    new_rate.save()
def _vkurse():
    url = 'http://vkurse.dp.ua/course.json'
    response_vkurse = requests.get(url)
    soup = BeautifulSoup(response_vkurse.content, 'html.parser')
    new_dictionary = json.loads(str(soup))
    for i in new_dictionary:
        if i in {'Dollar', 'Euro'}:
            currency = mch.CURR_USD if i == 'Dollar' else mch.CURR_EUR
            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(new_dictionary[i]['buy']),
                'sale': Decimal(new_dictionary[i]['sale']),
                'source': mch.SR_VKURSE,
            }
            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency,
                                            source=mch.SR_VKURSE).last()
            if last_rate is None or (new_rate.buy != last_rate.buy
                                     or new_rate.sale != last_rate.sale):
                new_rate.save()
def _concord():
    url = 'https://concord.ua/'
    r = requests.get(url)

    soup = BeautifulSoup(r.content, 'html.parser')
    html = list(soup.children)[2]
    body = list(html.children)[3]
    rates = body.find_all('p', attrs={'class': 'news-block__course-block'})

    for rate in rates:
        c = list(
            rate.find_all('span', attrs={'class': 'news-block__course-type'
                                         }))[0].get_text().strip()
        currency = {
            'Долар': mch.CURR_USD,
            'Євро': mch.CURR_EUR,
        }.get(c)

        if currency:
            c_rate = list(
                rate.find_all('span',
                              attrs={'class': 'news-block__course-data'
                                     }))[0].get_text().strip().split('/')

            if len(c_rate) == 2:
                rate_kwargs = {
                    'currency': currency,
                    'sale': Decimal(c_rate[1].strip()),
                    'buy': Decimal(c_rate[0].strip()),
                    'source': mch.SRC_CONCORD,
                }

                new_rate = Rate(**rate_kwargs)
                last_rate = Rate.objects.filter(currency=currency,
                                                source=mch.SRC_CONCORD).last()

                if last_rate is None or (new_rate.buy != last_rate.buy
                                         or new_rate.sale != last_rate.sale):
                    new_rate.save()
def _mtb():
    url = 'https://mtb.ua/'
    r = requests.get(url)
    soup = BeautifulSoup(r.content, 'html.parser')
    rates = soup.find_all('div', attrs={"class": "exchange-value_item"})

    currencies = {
        'USD': [],
        'EUR': [],
    }

    for rate in rates:
        c = rate.find_all('div', attrs={"class": "exchange-value_currency"
                                        })[0].contents[0].strip()
        if c in {'USD', 'EUR'}:
            c_rate = rate.find_all('span',
                                   attrs={"class": "exchange-value_num"
                                          })[0].contents[0].strip()
            currencies[c].append(c_rate)

    for key, value in currencies.items():
        currency = {
            'USD': mch.CURR_USD,
            'EUR': mch.CURR_EUR,
        }[key]
        rate_kwargs = {
            'currency': currency,
            'sale': Decimal(value[1]),
            'buy': Decimal(value[0]),
            'source': mch.SRC_MTB,
        }
        new_rate = Rate(**rate_kwargs)
        last_rate = Rate.objects.filter(currency=currency,
                                        source=mch.SRC_MTB).last()

        if last_rate is None or (new_rate.buy != last_rate.buy
                                 or new_rate.sale != last_rate.sale):
            new_rate.save()
def _vkurse():
    url = 'http://vkurse.dp.ua/course.json'
    response = requests.get(url)
    r_json = response.json()

    for rate in r_json:
        if rate in {'Dollar', 'Euro'}:
            currency = {'Dollar': mch.CURR_USD, 'Euro': mch.CURR_EUR}[rate]
            buy = r_json[rate]['buy'][:5].replace(',', '.')
            sale = r_json[rate]['sale'][:5].replace(',', '.')
            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(buy),
                'sale': Decimal(sale),
                'source': mch.SRC_VK,
            }

            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency,
                                            source=mch.SRC_VK).last()

            if last_rate is None or (new_rate.buy != last_rate.buy
                                     or new_rate.sale != last_rate.sale):
                new_rate.save()
Example #26
0
def previous_record_check_and_save(rate_kwargs):
    new_rate = Rate(**rate_kwargs)  # create new instance of currency record

    # get last currency record for date
    last_rate = Rate.objects.filter(currency=new_rate.currency,
                                    source=new_rate.source,
                                    created=new_rate.created).last()

    # verify and save if no record for this date
    if last_rate is None:
        new_rate.save()
        new_rate.created = rate_kwargs['created']
        new_rate.save(update_fields=[
            'created',
        ])
Example #27
0
    def handle(self, *args, **options):
        url = 'https://api.privatbank.ua/p24api/exchange_rates'
        start = date.today()
        end = (start + relativedelta(years=-4))
        td = (start - end).days  # number of days for last 4 years

        for i in range(td):
            d = end + timedelta(days=i)

            api_params = {
                'json': '',
                'date': d.strftime("%d.%m.%Y"),
            }

            r = requests.get(url, params=api_params)
            r_json = r.json()

            for rate in r_json['exchangeRate']:
                if 'currency' in rate:
                    if rate['currency'] in {'EUR', 'USD'}:
                        currency = {
                            'EUR': mch.CURR_EUR,
                            'USD': mch.CURR_USD,
                        }[rate['currency']]

                        if 'purchaseRate' and 'saleRate' in rate:
                            rate_kwargs = {
                                'currency': currency,
                                'buy': round(Decimal(rate['saleRate']), 2),
                                'sale': round(Decimal(rate['purchaseRate']), 2),
                                'source': mch.SRC_PB,
                            }
                        else:
                            rate_kwargs = {
                                'currency': currency,
                                'buy': round(Decimal(rate['saleRateNB']), 2),
                                'sale': round(Decimal(rate['purchaseRateNB']), 2),
                                'source': mch.SRC_PB,
                            }
                        Rate(**rate_kwargs).save()
                        r = Rate.objects.filter(currency=currency, source=mch.SRC_PB).last()
                        r.created = datetime.combine(d, datetime.min.time())
                        r.save()
Example #28
0
def vkurse():
    url = 'http://vkurse.dp.ua/course.json'
    response = requests.get(url)
    r_json = response.json()
    print(r_json)

    for rate in r_json:
        if rate == 'Dollar':
            currency = mch.CURR_USD

            rate_kwargs = {
                'currency': currency,
                'buy': Decimal(r_json['Dollar']['buy']),
                'sale': Decimal(r_json['Dollar']['sale']),
                'source': mch.SR_VKURSE,
            }

            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency, source=mch.SR_VKURSE).last()

            if last_rate is None or (last_rate and new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
                new_rate.save()

        elif rate == 'Euro':
            currency = mch.CURR_EUR

            rate_kwargs = {
                'currency': currency,
                'buy': round(Decimal(r_json['Euro']['buy']), 2),
                'sale': round(Decimal(r_json['Euro']['sale']), 2),
                'source': mch.SR_VKURSE,
            }

            new_rate = Rate(**rate_kwargs)
            last_rate = Rate.objects.filter(currency=currency, source=mch.SR_VKURSE).last()

            if last_rate is None or (last_rate and new_rate.buy != last_rate.buy or new_rate.sale != last_rate.sale):
                new_rate.save()
Example #29
0
    def handle(self, *args, **options):
        """
        This function is run when manage.py update_rates command is issued
        Will connect to openexchangerates.org and fetch the latest exchange rates
        Edit the attributes above to change which currencies you want to have in your database
        """
        fetched_successfully = False
        retries = 0
        curr_model_objects = {}
        for currency in self.currencies_to_get:
            curr_obj, created = Currency.objects.get_or_create(
                code=currency, name=self.curr_names_dict[currency])
            curr_model_objects[currency] = curr_obj

        while not fetched_successfully:
            try:
                response_json = requests.get(
                    self.openexchangerates_url.format(
                        settings.OPENEXCHANGERATES_APP_ID))
                if response_json.status_code == 200:
                    fetched_successfully = True
                    # Calculating the rates for each possible pair of currencies given in self.currencies_to_get
                    for currency_pair in [
                            p
                            for p in itertools.product(self.currencies_to_get,
                                                       repeat=2)
                    ]:
                        try:
                            rate = Rate.objects.get(
                                currency_from=curr_model_objects[
                                    currency_pair[0]],
                                currency_to=curr_model_objects[
                                    currency_pair[1]])
                            rate.rate = Decimal(response_json.json()['rates'][
                                currency_pair[1]]) / Decimal(
                                    response_json.json()['rates'][
                                        currency_pair[0]])
                            rate.save()
                        except Rate.DoesNotExist:
                            rate = Rate(
                                currency_from=curr_model_objects[
                                    currency_pair[0]],
                                currency_to=curr_model_objects[
                                    currency_pair[1]],
                                rate=Decimal(response_json.json()['rates'][
                                    currency_pair[1]]) /
                                Decimal(response_json.json()['rates'][
                                    currency_pair[0]]))
                            rate.save()
            except requests.ConnectionError:
                self.stdout.write(
                    self.style.ERROR('Could not connect to the service.'))
            except requests.Timeout:
                self.stdout.write(
                    self.style.ERROR('Connection to the service timed out.'))
            # in case of connection problems, we will retry to get the rates 10 times,
            # with 10 minute wait time in between each request
            retries += 1
            if retries == 10:
                break

            if fetched_successfully:
                self.stdout.write(
                    self.style.SUCCESS(
                        'All rates were fetched and saved successfully.'))
            else:
                time.sleep(600)