예제 #1
0
def parse_alfabank():
    from currency.models import Rate, Bank

    bank = Bank.objects.get(code_name=consts.CODE_NAME_ALFABANK)
    url = 'https://alfabank.ua/'
    # parameter to prevent blocking
    headers = {
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit 537.36'
        '(KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
    }

    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.findAll('div', class_='currency-block', limit=3)

    currencies = []

    for curr in items:
        currencies.append({
            'c_type':
            curr.find('div', class_='title').get_text(strip=True),
            'buy':
            curr.findAll('span')[1].get_text(strip=True),
            'sale':
            curr.findAll('span')[3].get_text(strip=True),
        })
    available_currency_type = {
        'USD': choices.RATE_TYPE_USD,
        'EUR': choices.RATE_TYPE_EUR,
    }

    # source = 'alfabank'

    for curr in currencies:
        currency_type = curr['c_type']
        if currency_type in available_currency_type:
            currency_type = available_currency_type[curr['c_type']]
            buy = to_decimal(curr['buy'])
            sale = to_decimal(curr['sale'])

            previous_rate = Rate.objects.filter(
                bank=bank, type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    type=currency_type,
                    sale=sale,
                    buy=buy,
                    # source=source,
                    bank=bank,
                )
            else:
                print(f'Rate already exists: {sale} {buy}')  # noqa
예제 #2
0
def parse_oschadbank():
    from currency.models import Source, Rate

    bank = Source.objects.get(code_name=consts.CODE_NAME_OSCHADBANK)

    # parameter to prevent blocking
    headers = {
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit 537.36'
        '(KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
    }

    response = requests.get(bank.url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.findAll('div',
                         class_='paragraph paragraph--type--exchange-rates '
                         'paragraph--view-mode--default currency-item',
                         limit=3)

    currencies = []

    for curr in items:
        currencies.append({
            'c_type':
            curr.find('span', class_='currency-sign').get_text(strip=True),
            # look for a "strong" tag, select by element number, get the "data-buy" value
            'buy':
            curr.findAll('strong')[0].get_text(strip=True),
            'sale':
            curr.findAll('strong')[1].get_text(strip=True),
        })

    available_currency_type = {
        'USD': choices.RATE_TYPE_USD,
        'EUR': choices.RATE_TYPE_EUR,
    }

    for curr in currencies:
        currency_type = curr['c_type']
        if currency_type in available_currency_type:
            currency_type = available_currency_type[curr['c_type']]
            buy = to_decimal(curr['buy'])
            sale = to_decimal(curr['sale'])

            previous_rate = Rate.objects.filter(
                bank=bank, cur_type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    cur_type=currency_type,
                    sale=sale,
                    buy=buy,
                    bank=bank,
                )
예제 #3
0
    def handle(self, *args, **options):
        date = datetime.date.today()
        date_start = date - datetime.timedelta(days=1)
        date_stop = datetime.date(1992, 3, 19)

        available_currency_type = {
            'USD': choices.RATE_TYPE_USD,
            'EUR': choices.RATE_TYPE_EUR,
        }

        bank = Bank.objects.get(code_name=consts.CODE_NAME_PRIVATBANK)

        while True:
            date_parse = {'json&date': f'{get_date_str_start(date_start)}'}
            response = requests.get(
                'https://api.privatbank.ua/p24api/exchange_rates',
                params=date_parse)
            response.raise_for_status()

            currency_list = response.json()['exchangeRate']
            currency_date = response.json()['date']

            for curr in currency_list:
                if 'currency' in curr:
                    currency_type = curr['currency']
                    if currency_type in available_currency_type:
                        currency_type = available_currency_type[
                            curr['currency']]
                        buy = to_decimal(curr['purchaseRate'])
                        sale = to_decimal(curr['saleRate'])

                        previous_rate = Rate.objects.filter(
                            bank=bank,
                            type=currency_type).order_by('created').last()

                        if (previous_rate is None or previous_rate.sale != sale
                                or previous_rate.buy != buy):
                            Rate.objects.create(
                                type=currency_type,
                                sale=sale,
                                buy=buy,
                                bank=bank,
                            )

                            rate = Rate.objects.last()
                            Rate.objects.filter(id=rate.id).update(
                                created=get_convert_date(currency_date))

            if date_start == date_stop:
                break
            date_start -= datetime.timedelta(days=1)
            sleep(10)
예제 #4
0
def parse_privatbank():
    from currency.models import Rate, Bank

    bank = Bank.objects.get(code_name=consts.CODE_NAME_PRIVATBANK)
    currencies = _get_privatbank_currencies(bank.url)

    # available_currencies = frozenset(('USD', 'EUR'))
    available_currency_types = {
        'USD': choices.RATE_TYPE_USD,
        'EUR': choices.RATE_TYPE_EUR,
    }

    clear_cache = False
    # source = 'privatbank'

    for curr in currencies:
        currency_type = curr['ccy']
        if currency_type in available_currency_types:
            currency_type = available_currency_types[curr['ccy']]

            buy = to_decimal(curr['buy'])
            sale = to_decimal(curr['sale'])

            previous_rate = Rate.objects.filter(
                bank=bank, type=currency_type).order_by('created').last()
            # check if new rate should be created
            if (previous_rate is None
                    or  # rate does not exist, create the first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy !=
                    buy  # check if buy was changed after last check
                ):
                print(f'New rate was created: {sale} {buy}')  # noqa
                Rate.objects.create(
                    type=currency_type,
                    sale=sale,
                    buy=buy,
                    # source=source,
                    bank=bank,
                )
                clear_cache = True
                # cache.delete(consts.CACHE_KEY_LATEST_RATES)
            else:
                print(f'Rate already exists: {sale} {buy}')  # noqa

    if clear_cache:
        cache.delete(consts.CACHE_KEY_LATEST_RATES)
예제 #5
0
def parse_raiffeisen():
    from currency.models import Source, Rate

    bank = Source.objects.get(code_name=consts.CODE_NAME_RAIFFEISEN)

    #
    headers = {
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit 537.36'
        '(KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
    }

    response = requests.get(bank.url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    items = soup.findAll('div', class_="bank-info__wrap")
    items = items[0].find('currency-table')[':currencies']
    currencies = json.loads(items)

    available_currency_type = {
        'USD': choices.RATE_TYPE_USD,
        'EUR': choices.RATE_TYPE_EUR,
    }

    for curr in currencies:
        currency_type = curr['currency']
        if currency_type in available_currency_type:
            currency_type = available_currency_type[curr['currency']]
            buy = to_decimal(curr['rate_buy'])
            sale = to_decimal(curr['rate_sell'])

            previous_rate = Rate.objects.filter(
                bank=bank, cur_type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    cur_type=currency_type,
                    sale=sale,
                    buy=buy,
                    bank=bank,
                )
예제 #6
0
def parse_monobank():
    from currency.models import Rate, Bank

    bank = Bank.objects.get(code_name=consts.CODE_NAME_MONOBANK)
    currencies = _get_monobank_currencies(bank.url)

    # available_currencies = frozenset(('USD' -- 840, 'EUR' -- 978))
    available_currency_types = {
        840: choices.RATE_TYPE_USD,
        978: choices.RATE_TYPE_EUR
    }
    main_currency_type = (980, )
    # source = 'monobank'

    for curr in currencies:
        currency_type = curr['currencyCodeA']
        main_type = curr['currencyCodeB']
        if (currency_type in available_currency_types
                and main_type in main_currency_type):
            currency_type = available_currency_types[curr['currencyCodeA']]
            buy = to_decimal(curr['rateBuy'])
            sale = to_decimal(curr['rateSell'])

            previous_rate = Rate.objects.filter(
                bank=bank, type=currency_type).order_by('created').last()
            # check if new rate should be created
            if (previous_rate is None
                    or  # rate does not exist, create the first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy !=
                    buy  # check if buy was changed after last check
                ):
                print(f'New rate was created: {sale} {buy}')  # noqa
                Rate.objects.create(
                    type=currency_type,
                    sale=sale,
                    buy=buy,
                    # source=source,
                    bank=bank,
                )
            else:
                print(f'Rate already exists: {sale} {buy}')  # noqa
예제 #7
0
def parse_iboxbank():
    from currency.models import Rate, Bank

    bank = Bank.objects.get(code_name=consts.CODE_NAME_IBOXBANK)
    currencies = _get_iboxbunk_currencies(bank.url)

    available_currencies_type = {
        'USD': choices.RATE_TYPE_USD,
        'EUR': choices.RATE_TYPE_EUR,
    }

    # source = 'iboxbank'

    for curr in currencies:
        currency_type = curr['currency']
        if currency_type in available_currencies_type:
            currency_type = available_currencies_type[curr['currency']]
            buy = to_decimal(curr['buyValue'])
            sale = to_decimal(curr['saleValue'])

            previous_rate = Rate.objects.filter(
                bank=bank, type=currency_type).order_by('created').last()
            # check if new rate should be created
            if (previous_rate is None
                    or  # rate does not exist, create the first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy !=
                    buy  # check if buy was changed after last check
                ):
                print(f'New rate was created: {sale} {buy}')  # noqa
                Rate.objects.create(
                    type=currency_type,
                    sale=sale,
                    buy=buy,
                    # source=source,
                    bank=bank,
                )
            else:
                print(f'Rate already exists: {sale} {buy}')  # noqa
예제 #8
0
def parse_privatbank():
    from currency.models import Source, Rate

    bank = Source.objects.get(code_name=consts.CODE_NAME_PRIVATBANK)

    currencies = _get_source_currencies(bank.url)

    available_currency_type = {
        'USD': choices.RATE_TYPE_USD,
        'EUR': choices.RATE_TYPE_EUR,
    }

    clear_cache = False

    for curr in currencies:
        currency_type = curr['ccy']
        if currency_type in available_currency_type:
            currency_type = available_currency_type[curr['ccy']]
            buy = to_decimal(curr['buy'])
            sale = to_decimal(curr['sale'])

            previous_rate = Rate.objects.filter(
                bank=bank, cur_type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    cur_type=currency_type,
                    sale=sale,
                    buy=buy,
                    bank=bank,
                )
                clear_cache = True

    # cleaned cache key if rates updated
    if clear_cache:
        cache.delete(consts.CACHE_KEY_LATEST_RATES)
예제 #9
0
def parse_monobank():
    from currency.models import Source, Rate

    bank = Source.objects.get(code_name=consts.CODE_NAME_MONOBANK)

    currencies = _get_source_currencies(bank.url)

    available_currency_type = {
        840: choices.RATE_TYPE_USD,
        978: choices.RATE_TYPE_EUR,
    }
    basic_currency_type = (980, )

    for curr in currencies:
        currency_type = curr['currencyCodeA']
        basic_type = curr['currencyCodeB']
        if (currency_type in available_currency_type
                and basic_type in basic_currency_type):
            currency_type = available_currency_type[curr['currencyCodeA']]
            buy = to_decimal(curr['rateBuy'])
            sale = to_decimal(curr['rateSell'])

            # in the selection by the cur_type field, a function for reverse conversion of the currency type
            # has been added, in accordance with the Rate model
            previous_rate = Rate.objects.filter(
                bank=bank, cur_type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    cur_type=currency_type,
                    sale=sale,
                    buy=buy,
                    bank=bank,
                    # source=source,
                )
예제 #10
0
def parse_vkurse():
    from currency.models import Rate, Bank

    bank = Bank.objects.get(code_name=consts.CODE_NAME_VKURSE)
    currencies = _get_vkurse_currencies(bank.url)

    available_currency_type = {
        'Dollar': choices.RATE_TYPE_USD,
        'Euro': choices.RATE_TYPE_EUR,
    }

    # source = 'vkurse'

    for currency_type, val in currencies.items():
        if currency_type in available_currency_type:
            currency_type = available_currency_type[currency_type]
            buy = to_decimal(val['buy'])
            sale = to_decimal(val['sale'])

            # in the selection by the cur_type field, a function for reverse conversion of the currency type
            # has been added, in accordance with the Rate model
            previous_rate = Rate.objects.filter(
                bank=bank, type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    type=currency_type,
                    sale=sale,
                    buy=buy,
                    # source=source,
                    bank=bank,
                )
            else:
                print(f'Rate already exists: {sale} {buy}')  # noqa
예제 #11
0
def parse_vkurse_dp_ua():
    from currency.models import Source, Rate

    bank = Source.objects.get(code_name=consts.CODE_NAME_VKURSE)
    currencies = _get_source_currencies(bank.url)

    available_currency_type = {
        'Dollar': choices.RATE_TYPE_USD,
        'Euro': choices.RATE_TYPE_EUR,
    }

    # source = 'vkurse.dp.ua'

    for key, val in currencies.items():
        # currency_type = key
        if key in available_currency_type:
            currency_type = available_currency_type[key]
            buy = to_decimal(val['buy'])
            sale = to_decimal(val['sale'])

            # in the selection by the cur_type field, a function for reverse conversion of the currency type
            # has been added, in accordance with the Rate model
            previous_rate = Rate.objects.filter(
                bank=bank, cur_type=currency_type).order_by('created').last()
            # check if new rate should be create
            if (previous_rate is None
                    or  # rate does not exists, create first one
                    previous_rate.sale != sale
                    or  # check if sale was changed after last check
                    previous_rate.buy != buy):
                Rate.objects.create(
                    cur_type=currency_type,
                    sale=sale,
                    buy=buy,
                    bank=bank,
                )
예제 #12
0
    def handle(self, *args, **options):
        date = datetime.date.today()
        date_start = date - datetime.timedelta(days=1)
        # date_start = latest_archive_rates()
        # date_start = datetime.date(2014, 12, 1)
        date_stop = datetime.date(1992, 3, 19)
        # date_stop =  dt.now().date()

        available_currency_type = {
            'USD': choices.RATE_TYPE_USD,
            'EUR': choices.RATE_TYPE_EUR,
        }

        while True:
            date_parse = f'json&date={date_start_to_str(date_start)}'
            url = 'https://api.privatbank.ua/p24api/exchange_rates'
            response = requests.get(url, params=date_parse)
            response.raise_for_status()

            currency_list = response.json()['exchangeRate']
            currency_date = response.json()['date']

            bank = Source.objects.get(code_name=consts.CODE_NAME_PRIVATBANK)

            for curr in currency_list:
                if 'currency' in curr:
                    currency_type = curr['currency']
                    if currency_type in available_currency_type:
                        currency_type = available_currency_type[
                            curr['currency']]
                        buy = to_decimal(curr['purchaseRate'])
                        sale = to_decimal(curr['saleRate'])

                        previous_rate = Rate.objects.filter(
                            # created=convert_created_db_date(currency_date),
                            bank=bank,
                            cur_type=currency_type).order_by('created').last()

                        # check if new rate should be create
                        if (previous_rate is None
                                or  # rate does not exists, create first one
                                previous_rate.sale != sale
                                or  # check if sale was changed after last check
                                previous_rate.buy != buy):
                            # create Rate object
                            Rate.objects.create(
                                cur_type=currency_type,
                                sale=sale,
                                buy=buy,
                                bank=bank,
                            )

                            # update created field before create Rate object
                            rate = Rate.objects.last()
                            Rate.objects.filter(id=rate.id).update(
                                created=convert_created_db_date(currency_date))

            if date_start == date_stop:
                break
            date_start -= datetime.timedelta(days=1)
            sleep(6)