Skip to content

Commit b8f94b1

Browse files
KrishnakGitHubcclausspre-commit-ci[bot]
authored
Add crypto_price_tracker.py to fetch live crypto prices (#13797)
* Add crypto_price_tracker.py to fetch live crypto prices * Update crypto_price_tracker to use httpx2 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md * Correct JSON response handling in price tracker Fix incorrect variable reference for JSON response. --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 7c0afd5 commit b8f94b1

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,11 @@
219219
* [Excel Title To Column](conversions/excel_title_to_column.py)
220220
* [Hex To Bin](conversions/hex_to_bin.py)
221221
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
222+
* [Int To Negative Binary Base](conversions/int_to_negative_binary_base.py)
222223
* [Ipv4 Conversion](conversions/ipv4_conversion.py)
223224
* [Length Conversion](conversions/length_conversion.py)
224225
* [Molecular Chemistry](conversions/molecular_chemistry.py)
226+
* [Negative Binary Base To Int](conversions/negative_binary_base_to_int.py)
225227
* [Octal To Binary](conversions/octal_to_binary.py)
226228
* [Octal To Decimal](conversions/octal_to_decimal.py)
227229
* [Octal To Hexadecimal](conversions/octal_to_hexadecimal.py)
@@ -1570,6 +1572,8 @@
15701572
* [Covid Stats Via Xpath](web_programming/covid_stats_via_xpath.py)
15711573
* [Crawl Google Results](web_programming/crawl_google_results.py)
15721574
* [Crawl Google Scholar Citation](web_programming/crawl_google_scholar_citation.py)
1575+
* [Crypto Price](web_programming/crypto_price.py)
1576+
* [Crypto Price Tracker](web_programming/crypto_price_tracker.py)
15731577
* [Currency Converter](web_programming/currency_converter.py)
15741578
* [Current Stock Price](web_programming/current_stock_price.py)
15751579
* [Current Weather](web_programming/current_weather.py)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Fetch the current price of a cryptocurrency in USD using CoinGecko API.
3+
"""
4+
5+
# /// script
6+
# requires-python = ">=3.11"
7+
# dependencies = [
8+
# "httpx2",
9+
# ]
10+
# ///
11+
12+
import httpx2
13+
14+
15+
def crypto_price(coin: str = "bitcoin") -> float:
16+
"""
17+
Return the current price of a cryptocurrency in USD using CoinGecko API.
18+
19+
>>> isinstance(crypto_price("bitcoin"), float)
20+
True
21+
>>> isinstance(crypto_price("ethereum"), float)
22+
True
23+
"""
24+
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin}&vs_currencies=usd"
25+
try:
26+
json_response = httpx2.get(url, timeout=10).raise_for_status().json()
27+
except httpx2.RequestError, ValueError, KeyError:
28+
return 0.0
29+
return float(json_response.get(coin, {}).get("usd", 0.0))
30+
31+
32+
if __name__ == "__main__":
33+
print(f"{crypto_price('bitcoin') = }")

0 commit comments

Comments
 (0)