0

I created a Spotipy script that pulls the last 50 songs I listened to and adds them, and their audio features, to a Google Sheets. However, I'd love to be able to run this script daily without me manually having to run it, but I have very little experience with CRON scheduling. I'm having trouble wrapping my head around how it can be run given all of the command line arguments I need to enter.

The code requires multiple command line arguments passed first, such as

export SPOTIPY_REDIRECT_URI="http://google.com"

export SPOTIPY_CLIENT_SECRET='secret' and similar for the client ID.

Additionally, the first argument after the script call is the username username = sys.argv[1].

Most importantly, it prompts me to copy and paste a redirect URL into the command line, which is unique each run.

Is it at all possible to pass the redirect URL to the command line each time the script is run using CRON?

moolz
  • 29
  • 1
  • 6

1 Answers1

0

I think what you're looking to accomplish can be achieved in one of two ways.

First, you could write a shell script to handle the export commands and passing the redirect URI to your script. Second, with a few tweaks to your script, you should be able to run it headlessly, without the intermediary copy/paste step.

I'm going to explore the second route in my answer.

Regarding the environment variables, you have a couple options. Depending on your distribution, you may be able to set these variables in the crontab before running the job. Alternatively, you can set the exports in the same job you use to run your script, separating the commands by semicolon. See this answer for a detailed explanation.

Then, regarding the username argument: fortunately, script arguments can also be passed via cron. You just list the username after the script's name in the job; cron will pass it as an argument to your script.

Finally, regarding the redirect URI: if you change your redirect URI to something like localhost with a port number, the script will automatically redirect for you. This wasn't actually made clear to me in the spotipy documentation, but rather from the command line when authenticating with localhost: "Using localhost as redirect URI without a port. Specify a port (e.g. localhost:8080) to allow automatic retrieval of authentication code instead of having to copy and paste the URL your browser is redirected to." Following this advice, I was able to run my script without having to copy/paste the redirect URL.

Add something like "http://localhost:8080" to your app's Redirect URI's in the Spotify Dashboard and then export it in your environment and run your script--it should run without your input!

Assuming that works, you can put everything together in a job like this to execute your script daily at 17:30:

30 17 * * * export SPOTIPY_REDIRECT_URI="http://localhost:8080"; export SPOTIPY_CLIENT_SECRET='secret'; python your_script.py "your_username"
shinni
  • 37
  • 4