avatarYang Zhou

Summary

The website content provides an overview of how Python programming can be utilized to automate various daily tasks, including stock price monitoring, email and WhatsApp messaging, cryptocurrency price tracking, URL shortening, QR code generation, HTTP server setup, Twitter bot creation, and smart home automation.

Abstract

The article on the website emphasizes the practical applications of Python in streamlining and enhancing daily activities through automation. It introduces nine Python scripts that cater to different aspects of life, such as financial investment tracking, communication, and web development. The scripts leverage popular Python libraries and APIs, including yfinance for stock prices, smtplib and ssl for secure email transmission, pywhatkit for WhatsApp messaging, requests for accessing the Binance API to monitor cryptocurrency prices, cuttpy for URL shortening, qrcode for QR code generation, Python's built-in http.server for setting up a simple HTTP server, twitter for building Twitter bots, and integrating Python scripts with Home Assistant for smart home control. The article aims to demonstrate that with Python, users can create personalized tools to increase productivity and make technology work more efficiently for them.

Opinions

  • The author suggests that programming skills, particularly in Python, are not just for professional use but can significantly enhance personal productivity.
  • Python is portrayed as an accessible and powerful tool for automating repetitive tasks, such as checking stock prices or sending messages.
  • The use of Python for personal automation is presented as both enjoyable and beneficial, with the ability to customize scripts according to individual needs.
  • The article implies that staying informed about stock or cryptocurrency prices is crucial for investors, and Python automation can provide timely notifications.
  • The author seems to value the convenience of modern communication channels like email and WhatsApp and shows how Python can automate interactions on these platforms.
  • There is an emphasis on the ease of integrating Python with various APIs and services, which simplifies the process of creating custom automation solutions.
  • The article encourages readers to engage with technology creatively by building their own tools, such as a Twitter bot or a smart home system using Home Assistant.
  • The author promotes the idea of becoming a Medium member to access more content on programming and technology, indicating a belief in the value of shared knowledge and community support.

Python

9 Time-Saving Python Scripts To AutomateYour Daily Life

Python is your best assistant for handling boring stuff

Image from Wallhaven

Learning programming is not only for getting a job.

If you are a programmer, you can build software for yourself and make your life easier.

If you happen to be a Python developer, congratulations, building your own arsenal is simpler and happier.

Python is the best choice for daily automation jobs to boost your productivity and make your life easier. There are lots of existing Python packages waiting for you to use. All you need to do is to assemble and customise them based on your personal requirements.

This article will show you 9 helpful Python scripts for daily automation and simplification. They are as follows:

  1. Let Python check your stocks price
  2. Send e-mails automatically
  3. Send WhatsApp messages automatically
  4. Get Real-time Crypto Price
  5. Shorten a URL
  6. Generating a QR code for your website
  7. Set up a simple HTTP server for temporary usage
  8. Build a Twitter bot
  9. Home assistant with Python

Talk is cheap, let’s see the code.

1. Checking Stocks Price Using Python

I see my colleagues opening their stock investment APPs frequently every day. A few enthusiastic “investors” even check the APPs every few minutes.

They are not productive.

Cause how frequently they check the stocks’ price will never affect the market. They only need to be notified when the price hit a goal in their mind and they can take action then.

In this case, the famous yfinance module can help.

It’s easy to install it using pip:

pip install yfinance

Then, let’s write a simple program to check the stock price of Tesla:

import yfinance as yf
Tesla = yf.Ticker("TSLA").info
market_price = Tesla['regularMarketPrice']
print(market_price)

That’s it. Thank YFinance API, we can easily get the stock price of Tesla with a few lines of Python code.

Now, we need to let the program send us emails to notify us if the stock price hit a certain action goal.

Here comes the email automation techniques in Python.

2. Sending Emails Automatically

Simply put, there are two steps to send an email in Python:

  • Connect and log in a mail server
  • Send the email from the mail server to the recipient

Mail servers use the Simple Mail Transfer Protocol (SMTP) to send emails to one another across the Internet. Python has a built-in module called smtplib which provides relative methods to help us send emails. In addition, to make everything secure, we need the secure sockets layer (SSL) protocol to encrypt an SMTP connection. Python also has a built-in module named ssl.

With the help of the above two modules, we can let our previous program send us emails when the stock price is lower than $100:

import smtplib, ssl
import yfinance as yf
Tesla = yf.Ticker("TSLA").info
market_price = Tesla['regularMarketPrice']
if market_price<=100:
    SERVER = 'mail.testserver.com'
    PORT = 465
    FROM = '[email protected]'
    PASSWORD = "1234567890"
    TO = '[email protected]'
    MSG="""\
    Subject: Time To Buy Tesla
    Hi, the tesla's stock price is lower than $100 now!"""
    
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL(SERVER, PORT, context=context) as server:
        server.login(FROM, PASSWORD)
        server.sendmail(FROM, TO, MSG)

The above example script is useful enough. However, you may use WhatsApp more frequently than emails nowadays. It’s okay. Python can also send WhatsApp messages automatically for you. 😎

3. Sending WhatsApp Messages Automatically

In this case, we will need pywhatkit, which is a convenient Python package for WhatsApp automation:

import pywhatkit
import yfinance as yf
Tesla = yf.Ticker("TSLA").info
market_price = Tesla['regularMarketPrice']
if market_price<=100:
    # Send a WhatsApp message at 17:30 when you get off work
    pywhatkit.sendwhatmsg("+910123456789", "Hi, time to buy Tesla!", 17, 30)

For complex settings and usages of pywhatkit, please check its official site.

4. Getting Real-Time Crypto Currencies Price

With the similar idea, if you are an investor of crypto currencies. You can also use Python to help you monitor the prices.

Due to the convenient Binance API, this task is super easy:

import requests

# defining key/request url
key = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"

# requesting data from url
data = requests.get(key)
data = data.json()
print(f"{data['symbol']} price is {data['price']}")

The outputs are as follows:

BTCUSDT price is 16555.14000000

5. Shorten a URL

If you would like to display a link on your website but it’s too long, the URL shortener is the right tool for you.

There are many Python packages that can do the URL shortening job. My favourite one is cuttpy. Before using it, you need to get an API key from its official website.

A basic example is as follows:

from cuttpy import Cuttpy
api_key = 'YOUR_API_KEY'
cuttly = Cuttpy(api_key)
response = cuttly.shorten("https://yangzhou1993.medium.com/membership")
print(response.shortened_url)

6. Generating a QR Code for Your Website

QR codes are everywhere in the mobile age. If you need to generate a QR code for your website, Python is your friend who can help you again.

We can use pip to install a package called qrcode (which will include the pillow package for generating images):

pip install qrcode[pil]

Now, let’s generate a QR code for my Medium home page:

import qrcode
img = qrcode.make('https://yangzhou1993.medium.com/')
img.save("yang_website_qr.png")

Simple and elegant, isn’t it? You can now scan the following QR code through a mobile phone to access my Medium homepage:

7. Setting up a Simple HTTP Server for Temporary Usage

If you need to get a testing HTTP server on your laptop, or just need to find a way to access your laptop’s files through your mobile phone. Python has a built-in approach for you:

python3 -m http.server

The above code can start a very basic Web server serving files relative to the current directory of your laptop. Its default port is 8000. Then, you just need to open the 192.168.x.xx:8000 with other devices under the same network to check out the files directly and seamlessly. (By the way, 192.168.x.xx stands for your laptop’s IP address.)

Simple and cool, isn’t it?

8. Building a Twitter Bot

Are you wasting too much time on Twitter? If you are, why not build a bot to help you get the information you need automatically?

Python is also a great language to build a Twitter bot.

Firstly, a Twitter developer account is required for using Twitter API, you can sign up on Twitter’s website.

Secondly, you need to find a Python wrapper for the Twitter API, there are many packages in the market, let’s use a simple one just called twitter:

pip install twitter

This tool is easy-to-learn and convenient to use. Here is a basic example from its page:

from twitter import *

t = Twitter(
    auth=OAuth(token, token_secret, consumer_key, consumer_secret))

# Get your "home" timeline
t.statuses.home_timeline()

# Get a particular friend's timeline
t.statuses.user_timeline(screen_name="boogheta")

Finally, you can develop your Twitter bot as you like.

9. Home Assistant with Python

The smart home is a hot topic recently. You can even build your smart home with the help of Python as well.

Here comes the famous open-source project for smart home building — Home Assistant.

You can treat it like an open-source version of Apple’s HomeKit, but with more controls and customization by yourself.

Of course, you can use Python to customize it. Its official website provides a few good examples.

Thanks for reading. If you like it, please become a Medium member through my referral link to enjoy more articles about programming and technologies:

Python
Programming
Technology
Automation
Coding
Recommended from ReadMedium