Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (54 loc) · 2.1 KB
/
Copy pathapp.py
File metadata and controls
61 lines (54 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
import argparse
from secret import spotify_token
def Search(artist):
"""Search For the Song"""
query = "https://api.spotify.com/v1/search?q={}&type=artist".format(artist)
response = requests.get(query,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
}
)
response_json = response.json()
data = response_json["artists"]["items"][0]
return data
def toptracks(data):
id=data["uri"].split(":")[-1]
query="https://api.spotify.com/v1/artists/{}/top-tracks?market=ES".format(id)
response=requests.get(query,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
})
response_json = response.json()
tracks=response_json["tracks"]
for num in range(len(tracks)):
print(tracks[num]["name"])
def topalbums(data):
list=[]
id=data["uri"].split(":")[-1]
query="https://api.spotify.com/v1/artists/{}/albums".format(id)
response=requests.get(query,headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(spotify_token)
})
response_json=response.json()
items=response_json["items"]
for num in range(len(items)-1):
list.append(items[num]["name"])
uniqueName = []
for i in list:
if not i in uniqueName:
uniqueName.append(i)
for name in uniqueName:
print(name)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Script that uses spotify API to search for an artist, displaying there top tracks and albums released in spotify.",allow_abbrev=False)
parser.add_argument('-xd',"--name", action='store', type=str,help="Enter the artist name")
args = parser.parse_args()
if args.name:
print("Top Tracks are:\n")
toptracks(Search(args.name))
print("\nTop Albums are:\n")
topalbums(Search(args.name))