博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
初学者python项目_面向初学者的Python项目:比特币价格通知
阅读量:2520 次
发布时间:2019-05-11

本文共 22788 字,大约阅读时间需要 75 分钟。

初学者python项目

Welcome to the first article in a series of articles on Python projects for beginners!

欢迎阅读面向初学者的系列Python系列文章中的第一篇!

In this tutorial we’re going to build a Bitcoin price notification service—

在本教程中,我们将构建一个比特币价格通知服务,

During this project you’ll learn about HTTP requests and how to send them using the (appropriately named) requests package.

在这个项目中,您将了解HTTP请求以及如何使用(适当命名的) requests包发送HTTP请求。

You’re going to learn about webhooks and how you can use them to connect your Python app to external services, such as phone notifications or Telegram messages.

您将学习有关Webhook的知识,以及如何使用它们将Python应用程序连接到外部服务,例如电话通知或电报消息。

With relatively little code (~50 lines) you’re going to arrive at a full-fledged Bitcoin price notification service that will be easily extendable to other cryptocurrencies and services.

只需较少的代码(约50行),您就可以得到一个成熟的比特币价格通知服务,该服务可以轻松扩展到其他加密货币和服务。

So let’s jump right in:

因此,让我们直接进入:

Python的比特币价格通知 (Bitcoin Price Notifications With Python)

As we all know, Bitcoin price is a fickle thing. You never really know where it’s going to be at the end of the day. So, instead of constantly checking various sites for the latest updates, let’s make a Python app to do the work for you.

众所周知,比特币的价格是善变的。 您永远不会真正知道一天结束时的情况。 因此,让我们不用制作一个Python应用程序来为您完成工作,而不必经常检查各个站点的最新更新。

For this, we’re going to use the popular automation website . IFTTT (“if this, then that”) is a web service that bridges the gap between different apps and devices.

为此,我们将使用流行的自动化网站 。 IFTTT(“如果是,那就是”)是一种网络服务,可桥接不同应用程序和设备之间的鸿沟。

We’re going to create two IFTTT applets:

我们将创建两个IFTTT小程序:

  • One for emergency notification when Bitcoin price falls under a certain threshold; and
  • another for regular updates on the Bitcoin price.
  • 当比特币价格低于一定阈值时,用于紧急通知; 和
  • 另一个用于定期更新比特币价格。

Both will be triggered by our Python app which will consume the data from the .

两者都将由我们的Python应用程序触发,该应用程序将使用的数据。

An IFTTT applet is composed of two parts: a trigger and an action.

IFTTT小程序由两部分组成:触发器和动作。

In our case, the trigger will be a webhook service provided by IFTTT. You can think of webhooks as “user-defined HTTP callbacks” and you can read more about them .

在我们的例子中,触发器将是IFTTT提供的Webhook服务。 您可以将webhooks视为“用户定义的HTTP回调”,并且可以阅读有关它们的更多信息。

Our Python app will make an HTTP request to the webhook URL which will trigger an action. Now, this is the fun part—the action could be almost anything you want. IFTTT offers a multitude of actions like sending an email, updating a Google Spreadsheet and even calling your phone.

我们的Python应用程序将向Webhook URL发出HTTP请求,从而触发操作。 现在,这是有趣的部分-动作几乎可以是您想要的任何东西。 IFTTT提供多种操作,例如发送电子邮件,更新Google Spreadsheet甚至给您的电话打电话。

项目设置 (Project Setup)

Let’s start by setting up a . Run this command to get a new Python 3 virtual environment:

让我们从设置 。 运行以下命令以获取新的Python 3虚拟环境:

$ mkvirtualenv -p $ mkvirtualenv -p $(which python3$( which python3 ) bitcoin_notifications) bitcoin_notifications

Before continuing you have to activate the virtual environment and install the required dependencies:

在继续之前,您必须激活虚拟环境并安装所需的依赖项:

You can deactivate the virtual environment by running the deactivate shell command.

您可以通过运行deactivate shell命令来停用虚拟环境。

检索比特币价格 (Retrieving the Bitcoin Price)

Time to get our hands dirty. We can start by getting the latest price from the Coinmarketcap API in the Python console:

是时候弄脏我们的双手了。 我们可以从Python控制台中的Coinmarketcap API获取最新价格开始:

First, we have to import the requests module and define the bitcoin_api_url variable which contains the Coinmarketcap API URL for Bitcoin.

首先,我们必须导入requests模块并定义bitcoin_api_url变量,该变量包含用于比特币的Coinmarketcap API URL。

Next, we send an HTTP GET request to the URL using the requests.get() function and save the response. Since the API returns a JSON response, we can convert it to a Python object by calling the .json() function on the response. As you can see, the API returned a list with one element containing the Bitcoin price data:

接下来,我们使用requests.get()函数将HTTP GET请求发送到URL并保存响应。 由于API返回了JSON响应,因此我们可以通过在响应上调用.json()函数将其转换为Python对象。 如您所见,API返回了一个列表,其中包含一个包含比特币价格数据的元素:

>>> >>>  import import requestsrequests>>> >>>  bitcoin_api_url bitcoin_api_url = = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/''https://api.coinmarketcap.com/v1/ticker/bitcoin/'>>> >>>  response response = = requestsrequests .. getget (( bitcoin_api_urlbitcoin_api_url ))>>> >>>  response_json response_json = = responseresponse .. jsonjson ()()>>> >>>  typetype (( response_jsonresponse_json ) ) # The API returns a list# The API returns a list
>>> >>> # Bitcoin data is the first element of the list# Bitcoin data is the first element of the list>>> >>> response_jsonresponse_json [[ 00 ]]{'id': 'bitcoin', 'name': 'Bitcoin', 'symbol': 'BTC', 'rank': '1', {'id': 'bitcoin', 'name': 'Bitcoin', 'symbol': 'BTC', 'rank': '1', 'price_usd': '10226.7', 'price_btc': '1.0', '24h_volume_usd': '7585280000.0', 'price_usd': '10226.7', 'price_btc': '1.0', '24h_volume_usd': '7585280000.0', 'market_cap_usd': '172661078165', 'available_supply': '16883362.0', 'market_cap_usd': '172661078165', 'available_supply': '16883362.0', 'total_supply': '16883362.0', 'max_supply': '21000000.0', 'total_supply': '16883362.0', 'max_supply': '21000000.0', 'percent_change_1h': '0.67', 'percent_change_24h': '0.78', 'percent_change_1h': '0.67', 'percent_change_24h': '0.78', 'percent_change_7d': '-4.79', 'last_updated': '1519465767'} 'percent_change_7d': '-4.79', 'last_updated': '1519465767'}

The property that we’re most interested in is 'price_usd'—the Bitcoin price in US dollars.

我们最感兴趣的属性是'price_usd'即美元的比特币价格。

发送测试IFTTT通知 (Sending a Test IFTTT Notification)

Now we can move onto the IFTTT side of things. To use IFTTT you’ll first need to and install their mobile app (if you want to receive phone notifications from your Python app). Once you set that up, we’re going to a new IFTTT applet for testing purposes.

现在我们可以进入IFTTT方面。 要使用IFTTT,您首先需要并安装其移动应用程序(如果您想从Python应用程序接收电话通知)。 设置好之后,我们将一个新的IFTTT applet以进行测试。

To create a new test applet follow these steps:

要创建一个新的测试小程序,请按照下列步骤操作:

  1. Click on the big “this” button
  2. Search for the “webhooks” service and select the “Receive a web request” trigger
  3. Let’s name the event test_event
  4. Now select the big “that” button
  5. Search for the “notifications” service and select the “Send a notification from the IFTTT app”
  6. Change the message to I just triggered my first IFTTT action! and click on “Create action”
  7. Click on the “Finish” button and we’re done
  1. 点击大的“ this”按钮
  2. 搜索“ webhooks”服务,然后选择“接收网络请求”触发器
  3. 让我们将事件命名为test_event
  4. 现在选择大的“那个”按钮
  5. 搜索“通知”服务,然后选择“从IFTTT应用发送通知”
  6. 将消息更改为I just triggered my first IFTTT action! 然后点击“创建动作”
  7. 点击“完成”按钮,我们就完成了

To see the documentation on how to use the IFTTT webhooks go to this and click on the “Documentation” button in the top right corner. The documentation page contains the webhook URL and it looks like this:

要查看有关如何使用IFTTT Webhooks的文档,请转至此 ,然后单击右上角的“文档”按钮。 文档页面包含webhook URL,如下所示:

Next up, you’ll need to substitute the {event} part with whatever name you gave our event in step 3, when you created the applet. The {your-IFTTT-key} part is already populated with your IFTTT key.

接下来,您需要在创建小程序时用在步骤3中为事件指定的名称替换{event}部分。 {your-IFTTT-key}部分已经用您的IFTTT密钥填充了。

Now copy the webhook URL and start another Python console. Again we import the requests module and define the webhook URL variable. Now we just have to send an HTTP POST request to the IFTTT webhook URL using the requests.post() function:

现在复制webhook URL并启动另一个Python控制台。 再次,我们导入requests模块并定义webhook URL变量。 现在,我们只需要使用request.post requests.post()函数将HTTP POST请求发送到IFTTT webhook URL:

>>> >>>  import import requestsrequests>>> >>>  # Make sure that your key is in the URL# Make sure that your key is in the URL>>> >>>  ifttt_webhook_url ifttt_webhook_url = = 'https://maker.ifttt.com/trigger/test_event/with/key/{your-IFTTT-key}''https://maker.ifttt.com/trigger/test_event/with/key/{your-IFTTT-key}'>>> >>>  requestsrequests .. postpost (( ifttt_webhook_urlifttt_webhook_url ))

After running the last line you should see a notification on your phone:

在运行最后一行之后,您应该在手机上看到一条通知:

创建IFTTT小程序 (Creating IFTTT Applets)

Now we’re finally ready for the main part. Before starting with the code we need to create two new IFTTT applets: one for emergency Bitcoin price notifications and one for regular updates.

现在,我们终于可以为主体做好准备了。 在开始编写代码之前,我们需要创建两个新的IFTTT小程序:一个用于紧急比特币价格通知,另一个用于定期更新。

Emergency bitcoin price notification applet:

紧急比特币价格通知小程序:

  1. Choose the “webhooks” service and select the “Receive a web request” trigger
  2. Name the event bitcoin_price_emergency
  3. For the action select the “Notifications” service and select the “Send a rich notification from the IFTTT app” action
  4. Give it a title, like “Bitcoin price emergency!”
  5. Set the message to Bitcoin price is at ${
    {Value1}}. Buy or sell now!
    (we’ll return to the {
    {Value1}}
    part later on)
  6. Optionally you could add a Link URL to the Coinmarketcap Bitcoin page: https://coinmarketcap.com/currencies/bitcoin/
  7. Create the action and finish setting up the applet
  1. 选择“ webhooks”服务,然后选择“接收网络请求”触发器
  2. 将事件命名为bitcoin_price_emergency
  3. 对于该操作,请选择“通知”服务,然后选择“从IFTTT应用发送丰富的通知”操作
  4. 给它起一个标题,例如“比特币价格紧急!”
  5. 将消息设置为Bitcoin price is at ${
    {Value1}}. Buy or sell now!
    Bitcoin price is at ${
    {Value1}}. Buy or sell now!
    (我们稍后将返回{
    {Value1}}
    部分)
  6. (可选)您可以将链接URL添加到Coinmarketcap比特币页面: https://coinmarketcap.com/currencies/bitcoin/ ://coinmarketcap.com/currencies/bitcoin/
  7. 创建动作并完成设置小程序

Regular price updates applet:

常规价格更新小程序:

  1. Again choose the “webhooks” service and select the “Receive a web request” trigger
  2. Name the event bitcoin_price_update
  3. For the action select the “Telegram” service and select the “Send message” action
  4. Set the message text to: Latest bitcoin prices:<br>{
    {Value1}}
  5. Create the action and finish with the applet
  1. 再次选择“ webhooks”服务,然后选择“接收网络请求”触发器
  2. 将事件命名为bitcoin_price_update
  3. 对于操作,请选择“电报”服务,然后选择“发送消息”操作
  4. 将消息文本设置为: Latest bitcoin prices:<br>{
    {Value1}}
  5. 创建动作并完成小程序

Note: When creating this applet you will have to authorize the IFTTT Telegram bot.

注意:创建此applet时,您必须授权IFTTT Telegram机器人。

放在一起 (Putting It All Together)

Now that we have IFTTT out of the way, let’s start coding! You’ll start by creating the standard Python command-line app skeleton shown below. Take this code and save it in a file called bitcoin_notifications.py:

现在我们已经摆脱了IFTTT的困扰,让我们开始编码! 您将首先创建如下所示的标准Python命令行应用程序框架。 采取以下代码并将其保存在名为bitcoin_notifications.py的文件中:

Next, we have to translate the two previous Python console sessions into two functions that will return the latest Bitcoin price and post to the IFTTT webhook respectively. Add the following functions above the main function:

接下来,我们必须将之前的两个Python控制台会话转换为两个函数,这两个函数将返回最新的比特币价格并将其分别发布到IFTTT Webhook。 在主要功能之上添加以下功能:

BITCOIN_API_URL BITCOIN_API_URL = = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/''https://api.coinmarketcap.com/v1/ticker/bitcoin/'IFTTT_WEBHOOKS_URL IFTTT_WEBHOOKS_URL = = 'https://maker.ifttt.com/trigger/{}/with/key/{your-IFTTT-key}''https://maker.ifttt.com/trigger/{}/with/key/{your-IFTTT-key}'def def get_latest_bitcoin_priceget_latest_bitcoin_price ():    ():    response response = = requestsrequests .. getget (( BITCOIN_API_URLBITCOIN_API_URL )    )    response_json response_json = = responseresponse .. jsonjson ()    ()    # Convert the price to a floating point number    # Convert the price to a floating point number    return return floatfloat (( response_jsonresponse_json [[ 00 ][][ 'price_usd''price_usd' ])])def def post_ifttt_webhookpost_ifttt_webhook (( eventevent , , valuevalue ):    ):    # The payload that will be sent to IFTTT service    # The payload that will be sent to IFTTT service    data data = = {
{
'value1''value1' : : valuevalue } } # inserts our desired event # inserts our desired event ifttt_event_url ifttt_event_url = = IFTTT_WEBHOOKS_URLIFTTT_WEBHOOKS_URL .. formatformat (( eventevent ) ) # Sends a HTTP POST request to the webhook URL # Sends a HTTP POST request to the webhook URL requestsrequests .. postpost (( ifttt_event_urlifttt_event_url , , jsonjson == datadata ))

The get_latest_bitcoin_price is pretty much the same, except for the part where we have to convert the price from a string to a floating point number. The post_ifttt_webhook takes in two parameters: event and value.

除了必须将价格从字符串转换为浮点数的部分外, get_latest_bitcoin_price几乎相同。 post_ifttt_webhook具有两个参数: eventvalue

The event parameter corresponds to whatever event name we gave to our trigger when setting up the IFTTT applet. Also, the IFTTT webhooks allow us to send additional data along with the request as JSON-formatted data.

event参数与设置IFTTT小程序时为触发器赋予的任何事件名称相对应。 另外,IFTTT网络挂钩允许我们将其他数据与请求一起以JSON格式发送。

That’s why we need the value parameter: When setting up our applets we left a {

{Value1}} tag in our message fields. This tag is replaced with the 'value1' text from the JSON payload. The requests.post() function allows us to send additional JSON data just by adding the json keyword.

这就是为什么需要value参数的原因:设置小程序时,我们在消息字段中保留了{

{Value1}}标签。 此标记将替换为JSON有效负载中的'value1'文本。 requests.post()函数使我们仅通过添加json关键字即可发送其他JSON数据。

Now we can move on to the core of our app in the main function. It will consist of a while True loop since we want our app to run forever. In the loop we will call the Coinmarketcap API to get the latest Bitcoin price and record the current date and time.

现在,我们可以进入main功能中应用程序的核心。 因为我们希望我们的应用程序永远运行,所以它将包含一段while True循环。 在循环中,我们将调用Coinmarketcap API以获取最新的比特币价格并记录当前日期和时间。

Based on the current price we will decide if we want to send an emergency notification. For our regular Telegram updates we will also append the current price and date to a bitcoin_history list. Once the list reaches a certain number of items (e.g. 5) we will format the items, send the update to Telegram, and reset the history for future updates.

根据当前价格,我们将决定是否要发送紧急通知。 对于我们的常规电报更新,我们还将当前价格和日期附加到bitcoin_history列表中。 一旦列表达到一定数量的项目(例如5),我们将格式化项目,将更新发送到Telegram,并重置历史记录以备将来更新。

Wheew! As you can see, there’s a lot going on in this app. If you’re having trouble following the code we’ve got thus far then take a quick break and re-read the above section again slowly. This stuff isn’t easy, so take your time and don’t worry about getting everything perfect the first time around.

he! 如您所见,此应用程序中发生了很多事情。 如果您在遵循我们到目前为止的代码时遇到麻烦,请稍作休息,然后再次缓慢地重新阅读以上部分。 这些东西并不容易,所以花点时间,不要担心第一次就能使所有事情变得完美。

An important thing is to avoid sending out these requests too frequently, for two reasons:

重要的是要避免过于频繁地发出这些请求,这有两个原因:

  • the Coinmarketcap API states that they update the data only once every 5 minutes, so there’s no point in reloading the latest pricing info more frequently than that
  • if your app sends too many requests to the Coinmarketcap API your IP might get banned or temporarily suspended.
  • Coinmarketcap API指出,它们每5分钟仅更新一次数据,因此没有必要比以前更频繁地重新加载最新的定价信息
  • 如果您的应用程序向Coinmarketcap API发送了太多请求,则您的IP可能会被禁止或暂时暂停。

That is why we need to “go to sleep” (stop the execution of the loop) for at least 5 minutes before we get new data. The code below implements all of the required features I stated above:

这就是为什么我们需要“Hibernate”(停止执行循环)至少5分钟,然后才能获取新数据。 下面的代码实现了我上面所述的所有必需功能:

We’re almost done! The only thing missing is the format_bitcoin_history function. It takes the bitcoin_history as an argument and formats it using some of the basic HTML tags allowed by Telegram, like <br>, <b>, <i>, and so on. Copy this function above the main function:

我们快完成了! 唯一缺少的是format_bitcoin_history函数。 它使用bitcoin_history作为参数,并使用Telegram允许的一些基本HTML标签对其进行格式化,例如<br><b><i>等等。 将此功能复制到main功能上方:

def def format_bitcoin_historyformat_bitcoin_history (( bitcoin_historybitcoin_history ):    ):    rows rows = = []    []    for for bitcoin_price bitcoin_price in in bitcoin_historybitcoin_history :        :        # Formats the date into a string: '24.02.2018 15:09'        # Formats the date into a string: '24.02.2018 15:09'        date date = = bitcoin_pricebitcoin_price [[ 'date''date' ]] .. strftimestrftime (( '' %d%d .%m.%Y %H:%M'.%m.%Y %H:%M' )        )        price price = = bitcoin_pricebitcoin_price [[ 'price''price' ]        ]        #  (bold) tag creates bolded text        #  (bold) tag creates bolded text        # 24.02.2018 15:09: $10123.4        # 24.02.2018 15:09: $10123.4        row row = = '{}: ${}''{}: ${}' .. formatformat (( datedate , , priceprice )        )        rowsrows .. appendappend (( rowrow )    )    # Use a 
(break) tag to create a new line # Use a
(break) tag to create a new line # Join the rows delimited by
tag: row1
row2
row3 # Join the rows delimited by
tag: row1
row2
row3 return return '
''
' .. joinjoin (( rowsrows ))

This is what the end result should look like on your phone:

这就是手机上的最终结果:

To run your price notification app, execute the following in your command-line terminal:

要运行价格通知应用程序,请在命令行终端中执行以下操作:

That’s it! In little over 50 lines of Python code, you’ve created your very own Bitcoin notification service. Congratulations! Below I’ve added the entire code so you can compare and see if you’ve missed anything:

而已! 在超过50行的Python代码中,您已经创建了自己的比特币通知服务。 恭喜你! 下面,我添加了完整的代码,以便您可以比较并查看是否错过了任何内容:

import import requestsrequestsimport import timetimefrom from datetime datetime import import datetimedatetimeBITCOIN_PRICE_THRESHOLD BITCOIN_PRICE_THRESHOLD = = 1000010000BITCOIN_API_URL BITCOIN_API_URL = = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/''https://api.coinmarketcap.com/v1/ticker/bitcoin/'IFTTT_WEBHOOKS_URL IFTTT_WEBHOOKS_URL = = 'https://maker.ifttt.com/trigger/{}/with/key/{your-IFTTT-key}''https://maker.ifttt.com/trigger/{}/with/key/{your-IFTTT-key}'def def get_latest_bitcoin_priceget_latest_bitcoin_price ():    ():    response response = = requestsrequests .. getget (( BITCOIN_API_URLBITCOIN_API_URL )    )    response_json response_json = = responseresponse .. jsonjson ()    ()    return return floatfloat (( response_jsonresponse_json [[ 00 ][][ 'price_usd''price_usd' ])  ])  # Convert the price to a floating point number# Convert the price to a floating point numberdef def post_ifttt_webhookpost_ifttt_webhook (( eventevent , , valuevalue ):    ):    data data = = {
{
'value1''value1' : : valuevalue } } # The payload that will be sent to IFTTT service # The payload that will be sent to IFTTT service ifttt_event_url ifttt_event_url = = IFTTT_WEBHOOKS_URLIFTTT_WEBHOOKS_URL .. formatformat (( eventevent ) ) # Inserts our desired event # Inserts our desired event requestsrequests .. postpost (( ifttt_event_urlifttt_event_url , , jsonjson == datadata ) ) # Sends a HTTP POST request to the webhook URL# Sends a HTTP POST request to the webhook URLdef def format_bitcoin_historyformat_bitcoin_history (( bitcoin_historybitcoin_history ): ): rows rows = = [] [] for for bitcoin_price bitcoin_price in in bitcoin_historybitcoin_history : : date date = = bitcoin_pricebitcoin_price [[ 'date''date' ]] .. strftimestrftime (( '' %d%d .%m.%Y %H:%M'.%m.%Y %H:%M' ) ) # Formats the date into a string: '24.02.2018 15:09' # Formats the date into a string: '24.02.2018 15:09' price price = = bitcoin_pricebitcoin_price [[ 'price''price' ] ] # (bold) tag creates bolded text # (bold) tag creates bolded text row row = = '{}: ${}''{}: ${}' .. formatformat (( datedate , , priceprice ) ) # 24.02.2018 15:09: $10123.4 # 24.02.2018 15:09: $10123.4 rowsrows .. appendappend (( rowrow ) ) # Use a
(break) tag to create a new line # Use a
(break) tag to create a new line return return '
''
' .. joinjoin (( rowsrows ) ) # Join the rows delimited by
tag: row1
row2
row3# Join the rows delimited by
tag: row1
row2
row3def def mainmain (): (): bitcoin_history bitcoin_history = = [] [] while while TrueTrue : : price price = = get_latest_bitcoin_priceget_latest_bitcoin_price () () date date = = datetimedatetime .. nownow () () bitcoin_historybitcoin_history .. appendappend ({
({
'date''date' : : datedate , , 'price''price' : : priceprice }) }) # Send an emergency notification # Send an emergency notification if if price price < < BITCOIN_PRICE_THRESHOLDBITCOIN_PRICE_THRESHOLD : : post_ifttt_webhookpost_ifttt_webhook (( 'bitcoin_price_emergency''bitcoin_price_emergency' , , priceprice ) ) # Send a Telegram notification # Send a Telegram notification if if lenlen (( bitcoin_historybitcoin_history ) ) == == 55 : : # Once we have 5 items in our bitcoin_history send an update # Once we have 5 items in our bitcoin_history send an update post_ifttt_webhookpost_ifttt_webhook (( 'bitcoin_price_update''bitcoin_price_update' , , format_bitcoin_historyformat_bitcoin_history (( bitcoin_historybitcoin_history )) )) # Reset the history # Reset the history bitcoin_history bitcoin_history = = [] [] timetime .. sleepsleep (( 5 5 * * 6060 ) ) # Sleep for 5 minutes (for testing purposes you can set it to a lower number)# Sleep for 5 minutes (for testing purposes you can set it to a lower number)if if __name__ __name__ == == '__main__''__main__' : : mainmain ()()

回顾与下一步 (Recap and Next Steps)

In this article, we created our very own Bitcoin notification service. You learned how to send HTTP GET and POST requests using the requests package. You saw how easy it was to connect your Python app to external services using IFTTT and webhooks.

在本文中,我们创建了自己的比特币通知服务。 您学习了如何使用requests包发送HTTP GET和POST请求。 您看到了使用IFTTT和webhooks将Python应用程序连接到外部服务有多么容易。

Now, where should you go next? With Python and IFTTT the sky is the limit. But here are some suggestions that can get you started:

现在,您下一步应该去哪里? 使用Python和IFTTT,天空是极限。 但是这里有一些建议可以帮助您入门:

  • Like spreadsheets? Use an IFTTT action that will add the Bitcoin prices to a Google Spreadsheet
  • Improve the if price < BITCOIN_PRICE_THRESHOLD condition to get the notification only once per day (otherwise it will get quite annoying if Bitcoin is consistently low)
  • Like Ethereum/Litecoin/Dogecoin better? Change the get_latest_bitcoin_price to get_latest_cryptocurrency_price that will take a cryptocurrency as a parameter and return its price
  • You want the price in a different currency? Check the Coinmarketcap API for the convert parameter.
  • 喜欢电子表格吗? 使用IFTTT操作,将比特币价格添加到Google Spreadsheet中
  • 改善if price < BITCOIN_PRICE_THRESHOLD条件,以每天仅接收一次通知(否则,如果比特币一直处于低位,将变得很烦人)
  • 像以太坊/ Litecoin / Dogecoin更好? 将get_latest_bitcoin_price更改为get_latest_cryptocurrency_price ,它将采用加密货币作为参数并返回其价格
  • 您想要其他货币的价格吗? 检查Coinmarketcap API中的convert参数。

Good luck and Happy Pythoning! Let me know what you’ve built using Python and IFTTT in the comments below!

祝你好运,Pythoning! 在下面的评论中,让我知道您使用Python和IFTTT构建的内容!

翻译自:

初学者python项目

转载地址:http://agqwd.baihongyu.com/

你可能感兴趣的文章
JAVA_HOME环境配置
查看>>
OpenJudge 1806:词典find()与end()
查看>>
【练习】表空间操作
查看>>
查找cmd命令中的字符
查看>>
阈值分割
查看>>
属性的两种定义方式
查看>>
超简短JS实现下拉菜单改变网页背景图片
查看>>
LeetCode刷题计划——2.两数相加
查看>>
周末充电之WPF(三 ) .后台动态生成控件
查看>>
HDU 1106: 排序
查看>>
通过机智云APP来学习安卓
查看>>
js 常用的正则表达式
查看>>
android 构建数据库SQLite
查看>>
部署多个tomcat
查看>>
生成器/内置函数
查看>>
Windows Phone 8: NavigationInTransition实现页面切换效果
查看>>
新概念英语(1-53)An interesting climate
查看>>
installing the numpy via pip in the enviroment dos
查看>>
ASP.NET MVC中如何在当前页面上弹出另外一个页面
查看>>
VS2015 MSVC编译FFMPEG
查看>>