Пример #1
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()
Пример #2
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()
Пример #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()
Пример #4
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 _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()
Пример #6
0
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()
Пример #8
0
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()
Пример #9
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()
Пример #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()
Пример #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()
Пример #13
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',
        ])
Пример #14
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()
Пример #15
0
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()
Пример #16
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()
Пример #17
0
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()
Пример #19
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()
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()
Пример #22
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)