[EN/PL] How to acquire Bitcoin using Python code / Jak zdobyć Bitcoina przy użyciu kodu Pythona
[ENGLISH] - język polski poniżej!
In the last tutorial, we installed Python and Visual Studio Code, so it's time to put them to use. We will write a simple script that will fetch the current price of Bitcoin using the Coingecko API.
How to code
- We're going to download a library using the pip tool that comes with the Python package. To do this, we need to open the console in our VSCode using the shortcut CTRL + Shift + ` and then type the following commands:
pip install requests
- We're importing the requests library, which is one of the most commonly used libraries in the Python language.
import requests
- We're creating a variable called url and assigning it the API endpoint from Coingecko.
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
- We're creating another variable, this time called parameters, and assigning it an object:
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
- We're making a request by passing the variables url and parameters as arguments to the get method of the requests class (object):
response = requests.get(url, params=parameters)
- We're assigning the response to the variable data in the form of a JSON object.
data = response.json()
- We're extracting the price from the returned object and assigning it to the variable price:
price = data["market_data"]["current_price"]["usd"]
- We're displaying the current price of Bitcoin in the console using the print function.
print("Current price:", price)
For the lazy ones, here's the entire code:
import requests
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
price = data["market_data"]["current_price"]["usd"]
print("Current price:", price)
The code can be enhanced with an option to choose a currency and playing around with Coingecko API parameters. It all depends on your willingness and creativity.
Summary
The main takeaways from this tutorial are understanding what a variable is and what objects are. In future tutorials, there will be code that you can use to do something, so your task will be to delve into the key concepts of the language because the basis of programming is searching for information and reading documentation. Without that, you won't be able to write a program that someone else hasn't written because everything will be a problem for you.
Programming requires independent thinking, and what I'm doing is just an attempt to inspire others and show that sometimes you can do more with a few simple lines of code than you think.
[POLISH]
W ostatnim tutorialu instalowaliśmy Pythona i Visual Studio Code pora więc zrobić z nich użytek. Napiszemy prosty skrypt, który będzie pobierał aktualną cenę bitcoin korzystając z api coingecko.
How to code
- Pobieramy bibliotekę przy użyciu narzędzia pip dostarczonego razem z paczką Pythona. Do tego celu musimy otworzyć konsolę w naszym VSCode używając skrótu CTRL + Shift + ` i wpisujemy kolejno:
pip install requests
- Importujemy bibliotekę requests która jest jedną z najczęściej używanych w języku python.
import requests
- Tworzymy zmienną url do której przypisujemy endpoint api z coingeko
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
- Tworzymy kolejną zmienną, tym razem parameters i przypisujemy do niej obiekt
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
- Wykonujemy zapytanie podając zmienne url i parameters jako zmienne metody get klasy (obiektu) requests
response = requests.get(url, params=parameters)
- Przypisujemy odpowiedź do zmiennej data w postaci obiektu JSON
data = response.json()
- Wyciągamy cenę ze zwróconego obiektu i przypisujemy ją do zmiennej price
price = data["market_data"]["current_price"]["usd"]
- Wyświetlamy obecną cenę bitcoina w konsoli korzystając z funkcji print
print("Current price:", price)
Dla leniuszków cały kod:
import requests
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
price = data["market_data"]["current_price"]["usd"]
print("Current price:", price)
Kod można ubarwić w opcję wybierania waluty, pobawić się parametrami api coingeko. Wszystko zależy od chęci i tego co sam wymyślisz.
Podsumowanie
Wnioski z tego tutoriala jakie powinny zostać wyciągnięte to przede wszystkim to jak wygląda i czym jest zmienna, oraz zanotować, że istnieje coś takiego jak obiekty.
W tutorialach będzie pojawiał się kod, który można do czego wykorzystać tak by nie powielać nudnych poradników a Twoim zadaniem będzie zgłębienie zaganień kluczowych dla danego języka, gdyż podstawą programowania jest szukanie informacji oraz czytanie dokumentacji. Bez tego nie będziesz w stanie napisać programu, którego nie napisał ktoś inny bo wszystko będzie dla Ciebie problemem.
Programowanie wymaga samodzielnego myślenia a to co ja robię to jedynie próba zajawienia innych i pokazania, że kilkoma prostymi liniami kodu można czasami zrobić więcej niż się komuś wydaje