Skip to content

Instantly share code, notes, and snippets.

@vlad-ds
Last active May 26, 2021 14:35
Show Gist options
  • Save vlad-ds/62aa74732d935a9b68fa002345595818 to your computer and use it in GitHub Desktop.
Save vlad-ds/62aa74732d935a9b68fa002345595818 to your computer and use it in GitHub Desktop.
import requests
def get_id(track_name: str, token: str) -> str:
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer ' + token,
}
params = [
('q', track_name),
('type', 'track'),
]
try:
response = requests.get('https://api.spotify.com/v1/search',
headers = headers, params = params, timeout = 5)
json = response.json()
first_result = json['tracks']['items'][0]
track_id = first_result['id']
return track_id
except:
return None
@junefranklin
Copy link

junefranklin commented May 26, 2021

import requests


def get_id(track_name: str, token: str) -> str:
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token,
    }
    params = [
        ('q', track_name),
        ('type', 'track'),
    ]
    try:
        response = requests.get(
            'https://api.spotify.com/v1/search', headers=headers, params=params, timeout=5
        )
        json = response.json()
        first_result = json['tracks']['items'][0]
        track_id = first_result['id']
        return track_id
    except Exception:
        return None

the "headers" variable was misaligned and causing an unindent problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment