Netflix Timer in Python

Lewis Summers
3 min readMay 31, 2021

--

I am using PyCharm and python 3.9 to pause a Netflix show after a certain amount of time. Full code at the bottom.

First things first we need to import all the necessary package libraries. I am using the pyautogui and time libraries, you will need to use pip to import pyautogui.

If you are on windows open the command prompt and type:

py -m pip install pyautogui

After you import the pyautogui library, open a new project in PyCharm and use the import function to make the libraries accessible, like this:

import pyautogui
import time

Next, we need to determine the length of the timer you can do this a couple of different ways, the first way is a little more simple, by just using the input function; however, if you want to make this project into an executable file(.exe) you will have the console show up, which I personally don't like because it doesn't look as good, but I’m picky about graphics sometimes. The code for the input function would look like this:

timerLength = input('How long would you like the timer to be?(in minutes)?  ')

The other method is using the alert box functions built in the pyautogui library, which is very straight forward with just one line of code. First, you reference the package you want to use(pyautogui) and then the function(prompt). The prompt function takes a couple of parameters. The most important one is the text parameter, it decides what text is displayed before the input box. I also use the default and title parameters but I won't go into detail about those. You would right the input box function like this:

timerLength = pyautogui.prompt(text='How long would you like the timer to be?(in minutes)', title='Netflix Timer', default='')

Then you end up with a box that looks like this.

Once we have our input we need to convert it into a variable type that the time.sleep() function can use. If you input an integer then the program should carry on but if you input a string like ‘dhfjk’ then the program needs to say ‘wow that's not an integer ’. We use the try function for things like that because it checks if there are any errors.

try:
int(timerLength)
except:
pyautogui.alert(text='Not compatible please try again.',title='Oops')
quit()

The int() function turns a string into an integer but will produce an error code if the string isn't a number.

Next, we need to make the timer wait for the inputted amount of time. This is where the time library comes in handy. time.sleep() is a function that waits for however many seconds you want.

Minutes = int(f) * 60
time.sleep(Minutes)

Now our script has gotten input and waited for the desired amount of time. The next step is pausing Netflix. We are going to use the pyautogui library again for that. If you press space when you have Netflix open it will pause the show so we are going to simulate a keypress with the pyautogui.press() function.

pyautogui.press('space')

Then finally we have finished the script, now sit back and relax and let python pause for you.

Full code

import pyautogui
import time
timerLength = pyautogui.prompt(text='How long would you like the timer to be?(in minutes)', title='Netflix Timer', default='')try:
int(timerLength)
except:
pyautogui.alert(text='Not compatible please try again.', title='Oops')
quit()
Minutes = int(timerLength) * 60time.sleep(Minutes) pyautogui.press('space')

--

--