Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,11 @@
* [Excel Title To Column](conversions/excel_title_to_column.py)
* [Hex To Bin](conversions/hex_to_bin.py)
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
* [Int To Negative Binary Base](conversions/int_to_negative_binary_base.py)
* [Ipv4 Conversion](conversions/ipv4_conversion.py)
* [Length Conversion](conversions/length_conversion.py)
* [Molecular Chemistry](conversions/molecular_chemistry.py)
* [Negative Binary Base To Int](conversions/negative_binary_base_to_int.py)
* [Octal To Binary](conversions/octal_to_binary.py)
* [Octal To Decimal](conversions/octal_to_decimal.py)
* [Octal To Hexadecimal](conversions/octal_to_hexadecimal.py)
Expand Down Expand Up @@ -1570,6 +1572,8 @@
* [Covid Stats Via Xpath](web_programming/covid_stats_via_xpath.py)
* [Crawl Google Results](web_programming/crawl_google_results.py)
* [Crawl Google Scholar Citation](web_programming/crawl_google_scholar_citation.py)
* [Crypto Price](web_programming/crypto_price.py)
* [Crypto Price Tracker](web_programming/crypto_price_tracker.py)
* [Currency Converter](web_programming/currency_converter.py)
* [Current Stock Price](web_programming/current_stock_price.py)
* [Current Weather](web_programming/current_weather.py)
Expand Down
33 changes: 33 additions & 0 deletions web_programming/crypto_price_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Fetch the current price of a cryptocurrency in USD using CoinGecko API.
"""

# /// script
# requires-python = ">=3.11"
# dependencies = [
# "httpx2",
# ]
# ///

import httpx2


def crypto_price(coin: str = "bitcoin") -> float:
"""
Return the current price of a cryptocurrency in USD using CoinGecko API.

>>> isinstance(crypto_price("bitcoin"), float)
True
>>> isinstance(crypto_price("ethereum"), float)
True
"""
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin}&vs_currencies=usd"
try:
json_response = httpx2.get(url, timeout=10).raise_for_status().json()
except httpx2.RequestError, ValueError, KeyError:
return 0.0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: web_programming/crypto_price_tracker.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 137, in parse
    runner = LintRunner(file.path, source)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 29:19.
parser error: error at 28:32: expected one of !=, %, &, (, *, **, +, -, ., /, //, :, <, <<, <=, ==, >, >=, >>, @, [, ^, and, as, if, in, is, not, or, |

        return 0.0
                  ^

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: web_programming/crypto_price_tracker.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 137, in parse
    runner = LintRunner(file.path, source)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 28:19.
parser error: error at 27:32: expected one of !=, %, &, (, *, **, +, -, ., /, //, :, <, <<, <=, ==, >, >=, >>, @, [, ^, and, as, if, in, is, not, or, |

        return 0.0
                  ^

return float(json_response.get(coin, {}).get("usd", 0.0))


if __name__ == "__main__":
print(f"{crypto_price('bitcoin') = }")
Loading