-
Notifications
You must be signed in to change notification settings - Fork 30
/
spotify.py
122 lines (105 loc) · 4.95 KB
/
spotify.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import re
from variables import spotify_shortened_link_pattern, spotify_track_link_pattern, spotify_album_link_pattern, spotify_playlist_link_pattern, spotify_apps_list, welcome_message
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import requests # for get_redirect_link
import random
import os
def get_redirect_link(shortened_link):
response = requests.head(shortened_link, allow_redirects=True)
return response.url
def get_link_type(text):
if re.match(spotify_track_link_pattern, text):
return "track"
elif re.match(spotify_album_link_pattern, text):
return "album"
elif re.match(spotify_playlist_link_pattern, text):
return "playlist"
elif re.match(spotify_shortened_link_pattern, text):
return "shortened"
else:
return False
def get_valid_spotify_links(text):
regexes = [spotify_shortened_link_pattern, spotify_track_link_pattern, spotify_album_link_pattern, spotify_playlist_link_pattern]
# Create a compiled regular expression object
# by joining the regex patterns with the OR operator |
regex_combined = re.compile("|".join(regexes))
# Find all matches and store them in a list
all_matches = [match.group() for match in regex_combined.finditer(text)]
print(all_matches) # as debug
return all_matches
def get_track_ids(link):
# # experimental - set up proxy for spotipy too
# # Custom session with SOCKS proxy for spotipy
# proxies = {
# 'http': 'socks5://localhost:40000',
# 'https': 'socks5://localhost:40000'
# }
# # Create a custom requests session with the proxy
# session = requests.Session()
# session.proxies.update(proxies)
# experimental new multiple spotify app system
spotify_app = random.choice(spotify_apps_list)
spotify_client_id = spotify_app[0]
spotify_client_secret = spotify_app[1]
#Authentication - without user
client_credentials_manager = SpotifyClientCredentials(client_id=spotify_client_id, client_secret=spotify_client_secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
# sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager, requests_session=session) # to use proxy
# get id of link, album or playlist
link_id = link.split("/")[-1].split("?")[0]
link_type = get_link_type(link)
if link_type == "track":
# # old method - sends link track to spotify api too
# tracks = sp.track(link_id)
# track_ids = [tracks["id"]]
# new method - extracts track id directly from link without api
track_ids = [link_id]
elif link_type == "album":
tracks = sp.album_tracks(link_id)["items"]
track_ids = [t["id"] for t in tracks]
elif link_type == "playlist":
# handle spotify results paginated in 100 items
# https://stackoverflow.com/questions/39086287/spotipy-how-to-read-more-than-100-tracks-from-a-playlist
results = sp.playlist_tracks(link_id)
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
track_ids = []
for t in tracks:
try:
if ("track" in t) and (t["track"] is not None) and ("id" in t["track"]) and (t["track"]["id"] is not None):
# due to a bug, a small number of tracks in playlists don't have
# ["track"]["id"] and cause the program to crash
# like 75th track in https://open.spotify.com/playlist/64r1Ry0JIWHboowR4LWp5R
# which is https://open.spotify.com/track/46cdw28EXOhDPnD1emDC6T
# so we check and make sure this field exists
track_ids.append(t["track"]["id"])
except:
print("error in getting a track id")
else:
return []
return(track_ids)
def get_track_image(track_link):
# # experimental - set up proxy for spotipy too
# # Custom session with SOCKS proxy for spotipy
# proxies = {
# 'http': 'socks5://localhost:40000',
# 'https': 'socks5://localhost:40000'
# }
# # Create a custom requests session with the proxy
# session = requests.Session()
# session.proxies.update(proxies)
# experimental new multiple spotify app system
spotify_app = random.choice(spotify_apps_list)
spotify_client_id = spotify_app[0]
spotify_client_secret = spotify_app[1]
#Authentication - without user
client_credentials_manager = SpotifyClientCredentials(client_id=spotify_client_id, client_secret=spotify_client_secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
# sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager, requests_session=session)
track_id = track_link.split("/")[-1].split("?")[0]
track = sp.track(track_id)
cover_image_url = track['album']['images'][0]['url']
return cover_image_url