Search code examples
pythoncurlterminalspotifyos.system

Open Terminal and Run Curl in Python


I have been trying to run this code,

import csv
import os

token = 'Bearer xxx"'

with open('uris.csv', 'r', newline = '',encoding = 'utf-8') as ifp:
    ir = csv.reader(ifp)

    for i, row in enumerate(ir):
        v = ('curl -X "POST" "https://api.spotify.com/v1/playlists/7miRhC7OZhQUvnP1ONghJm/tracks?uris=spotify%3Atrack%3A'+
            ', '.join(row)+
            '" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: '+
            token + '\n')
        os.system("gnome-terminal -e 'bash -c \""+v+"; sleep 10\" '")
        print(v)

which takes some spotify track uris from a csv and puts them into the curl that anyone can get from Spotify Api. This curl adds the specific track to my playlist. Then I open a new terminal and execute the curl. (the os.system command was found from open terminal run command python)

The problem is that when I execute this code with python3 code.py new terminals for each curl open but none curl is executed. The curls themselves are right. If I copy paste one curl and run it, the track is added to my playlist. Also if I run a curl separately, I get a response in the terminal which i don't get if I run curls through the code. The response is something like this:

{
  "snapshot_id" : "MTQxxx"
}

Thanks a lot.


Solution

  • Silly to ask, but why not just directly call curlOutput = os.system("curl [your concatenation]")?

    Might just solve your issue.