|
| 1 | +import os |
| 2 | +import requests |
| 3 | +from bs4 import BeautifulSoup |
| 4 | +from babel.numbers import format_currency |
| 5 | + |
| 6 | +os.system("clear") |
| 7 | + |
| 8 | +""" |
| 9 | +Use the 'format_currency' function to format the output of the conversion |
| 10 | +format_currency(AMOUNT, CURRENCY_CODE, locale="ko_KR" (no need to change this one)) |
| 11 | +""" |
| 12 | + |
| 13 | +code_url = "https://www.iban.com/currency-codes" |
| 14 | + |
| 15 | +code_request = requests.get(code_url) |
| 16 | +code_soup = BeautifulSoup(code_request.text, "html.parser") |
| 17 | + |
| 18 | +# table class = table table-bordered downloads tablesorter |
| 19 | +table = code_soup.find("table") |
| 20 | +# th(thead), tbody >> tr(table row) >> td(table data) |
| 21 | +# td[0] = th - Country, Currency, Code, Number |
| 22 | +# td[1~] = tb - Value |
| 23 | +# td[0:3] = print(td[0, 1, 2]) |
| 24 | +rows = table.find_all("tr")[1:] |
| 25 | + |
| 26 | +countries = [] |
| 27 | + |
| 28 | +for row in rows: |
| 29 | + data = row.find_all("td") |
| 30 | + # name = Country name |
| 31 | + name = data[0].text |
| 32 | + # code = Currency code |
| 33 | + code = data[2].text |
| 34 | + if name and code: |
| 35 | + # No universal currency = Currency code is empty |
| 36 | + if name != "No universal currency": |
| 37 | + country = {'name': name.capitalize(), 'code': code} |
| 38 | + countries.append(country) |
| 39 | + |
| 40 | +print("Welcome to CurrencyConvert PRO 2020\n") |
| 41 | +# enumerate = 열거하다 / 기본적으로 인덱스 값을 포함해서 출력함 |
| 42 | +for index_value, country in enumerate(countries): |
| 43 | + print(f"#{index_value} {country['name']}") |
| 44 | + |
| 45 | + |
| 46 | +def ask_country(text): |
| 47 | + print(text) |
| 48 | + try: |
| 49 | + # choose index_value |
| 50 | + choice = int(input("#: ")) |
| 51 | + if choice > len(countries): |
| 52 | + print("Choose a number from the list.") |
| 53 | + return ask_country(text) |
| 54 | + else: |
| 55 | + print(f"{countries[choice]['name']}") |
| 56 | + return countries[choice] |
| 57 | + except ValueError: |
| 58 | + print("That wasn't a number. Please wirte a number.") |
| 59 | + return ask_country(text) |
| 60 | + |
| 61 | + |
| 62 | +def ask_amount(a_country, b_country): |
| 63 | + try: |
| 64 | + print(f"\n\nHow many {a_country['code']} do you want to convert to {b_country['code']}?") |
| 65 | + amount = int(input()) |
| 66 | + return amount |
| 67 | + # input result = not int |
| 68 | + except ValueError: |
| 69 | + print("That wasn't a number. Please wirte a number.") |
| 70 | + return ask_amount(a_country, b_country) |
| 71 | + |
| 72 | + |
| 73 | +user_country = ask_country("\n\nWhere are you from? Choose a country by number.") |
| 74 | +target_country = ask_country("\n\nNow choose another country.") |
| 75 | + |
| 76 | +amount = ask_amount(user_country, target_country) |
| 77 | + |
| 78 | + |
| 79 | +currency_url = "https://transferwise.com/gb/currency-converter/" |
| 80 | + |
| 81 | +user_code = user_country['code'] |
| 82 | +target_code = target_country['code'] |
| 83 | + |
| 84 | +# KRW => USD 10000원 환전 url = {https://transferwise.com/gb/currency-converter/}{krw}-to-{usd}-rate?amount={10000} |
| 85 | +currency_request = requests.get(f"{currency_url}{user_code}-to-{target_code}-rate?amount={amount}") |
| 86 | +currency_soup = BeautifulSoup(currency_request.text, "html.parser") |
| 87 | + |
| 88 | +currency_result = currency_soup.find("input", {"id": "cc-amount-to"}) |
| 89 | +if currency_result: |
| 90 | + currency_result = currency_result['value'] |
| 91 | + amount = format_currency(amount, user_code, locale="ko_KR") |
| 92 | + currency_result = format_currency(currency_result, target_code, locale="ko_KR") |
| 93 | + print(f"{amount} is {currency_result}") |
0 commit comments