Get the current livestream viewer count and save it in a postgresql database along with the URL and time
(also my first time working with databases... absolute hell. what am i doing what am i doing)
- postgresql server already set up
- yt-dlp in PATH
- Python in PATH
psycopg2
andpython-dotenv
installed via pip
Note
For Windows users, please check if Python is in PATH. And make sure to run Python correctly as most times it's py
or python
instead of python3
(at least on Windows systems). In this guide, this will be referred to as python
at the start of the command.
- Git clone the repository/download it as a .zip
- Open your postgresql console and the
viewcounts.sql
file - Make sure you're connected to a database, copy the command in the .sql file and paste it in
- Rename
.env.example
to.env
and enter your postgresql information - Run
python track.py
appended with a valid YouTube Live URL - If completed successfully you should have an entry in the newly created
viewers
table.
I've pre-included a batch file for Windows users that automatically scrapes the viewer count every minute (because that's around the time YouTube takes to refresh the count on their frontend). Edit run_example.bat with your livestream's ID. If anyone's willing to do a bash version, go right ahead! PRs are welcome.