Automate clapping posts with Python

Steadylearner
4 min readDec 24, 2018
Photo by rawpixel on Unsplash

When you see the article that helps you know what you really wanted, You may click a clap button in medium to cheer the author. It is very simple task for you just have to do a mouse left click once.

But what if you want to give them applause several times? Medium allows you until 50 claps for each post.

Personally, I have given clap for every post that I liked the maximum clap that I could give but doing same thing again was very tedious work.

Recently I found the solution to this while learning Python. You may not had the same need that I did. But it may be also a greate chance for learning Python and I want to share what I learnt with you in this post.

What we need to do is to automate the process. For that, We have to analyze the process into small pieces first.

It is very simple for our case.

  1. We position our mouse cursor on the clap image
  2. Left Click n times to clap

Now We are going to code Python files for each process.(I wouldn’t give time to Python environment setup process here. To do that you may visit this link from DigitalOcean.

First, we have to imitate mouse movement to put cursor on specific location.

I borrowed and improved the code snippet for this process from the automateboringstuff website.

It is good place to start with programming. I suggest you to visit there to learn how to code if you are a beginner.

The entire code snippet is like a image below. I will explain what is happening here and for details you can visit the official site of pyautogui.

mouse_now_pixel improved by steadylearner

The main part of this code is after try: part.

You get the current x, y position with pyautogui api and you can also get a RGB color format of the pixel at the specific mouse position and use it if you are a web designer or just liked the color etc.

From the original code from the book, the difference is

  1. Python .format syntax was used some codes were written under the except: to make the code more concise
  2. termcolor package was used to decorate the output on console
  3. Some codes were written wtih os under the KeyboardInterrupt: to delete unwanted screenshot .png file that is saved automatically after quitting the process.

If you execute the code with $python mouse_now_pixel or somewhat similar command inside your favorite Python development environment.

medium clap image postion by steadylearner

It shows the position of the clap icon inside the desktop and that is somewhat X: 181 and Y: 298. The process here may be time consuming but you need to for every desktop would have difference position for the clap icon for their different size and you don’t have to do it more than once.

For we found the position that we needed, we can now move to the next process “Mouse left click n times”.

The process is much simpler than the previous one.

The code snippet would be

python automated click n times by steadylearner

Here we imported built in module sys to use command line. The arguments you pass after the Python file(.py) could be used with this

You can visit the pythonforbeginners for more details.

Next, we use pyautogui moveTo api to move the cursor to the position we found in the previous process. We will pass it as command line arguments later.

and finally we will pass how many times we will repeat the click with Python for loop.

Now you can test the process with $python py_click.py 10 181 298 for this case or for your own case.

You may not want to repeat typing $python py_click.py 10 181 298 all the time.

For that, You can save this command inside ~/.bash file. First, type $pwd to find the location or others to find the location of py_click.py file and you can save the command like this

alias test=”cd ~/Desktop/Learn/Python/environments/bin/practice python py_clapper.py 10 181 298"

Then type $source ~/.bashrc and whenever you type the $test inside your console it will clap the medium post instead of you.(The process may be different for each environement.)

and that was the whole process.

The entire code snippet can be found at Python_Practice.

After writing this posts, I think that it is not so easy to setup this process. But I want it to be helpful for people who are practicing Python or wanted to improve the original example.

Thanks for reading.

p.s

  1. Did you like my coding style? Work with me. Please, send messages via LinkedIn. I am also the writer of React Package prop-passer you may read How To Write React Efficiently with Prop-Passer
  2. I am also developing React FrontEnd and Rust BackEnd Website www.steadylearner.com. It lacks contents but I would write it again together with real posts written here.

I am not a native English speaker. When some words don’t make sense, grammar problems etc. Please leave a comment below. I will read this post again and edit them right away.

--

--