66from rest_framework .response import Response
77import requests
88from .models import Note , UserProfile
9+ import os
910
1011class NoteListCreate (generics .ListCreateAPIView ):
1112 serializer_class = NoteSerializer
@@ -60,4 +61,122 @@ def get(self, request):
6061 api_key = '1e75925c' # Consider moving this to environment variables
6162
6263 response = requests .get (f'http://www.omdbapi.com/?t={ query } &apikey={ api_key } ' )
63- return Response (response .json ())
64+ return Response (response .json ())
65+
66+ class SpotifyTokenView (APIView ):
67+ permission_classes = [permissions .IsAuthenticated ]
68+
69+ def get (self , request ):
70+ try :
71+ # Get Spotify credentials from environment variables
72+ client_id = os .getenv ('SPOTIFY_CLIENT_ID' )
73+ client_secret = os .getenv ('SPOTIFY_CLIENT_SECRET' )
74+
75+ print ("Spotify credentials check:" , {
76+ 'client_id_exists' : bool (client_id ),
77+ 'client_secret_exists' : bool (client_secret )
78+ })
79+
80+ if not client_id or not client_secret :
81+ return Response (
82+ {'error' : 'Spotify credentials not configured' },
83+ status = status .HTTP_503_SERVICE_UNAVAILABLE
84+ )
85+
86+ # Get access token from Spotify
87+ auth_response = requests .post (
88+ 'https://accounts.spotify.com/api/token' ,
89+ data = {
90+ 'grant_type' : 'client_credentials'
91+ },
92+ auth = (client_id , client_secret )
93+ )
94+
95+ print ("Spotify auth response:" , {
96+ 'status_code' : auth_response .status_code ,
97+ 'content' : auth_response .text
98+ })
99+
100+ if auth_response .status_code != 200 :
101+ error_details = auth_response .json () if auth_response .text else 'No error details available'
102+ return Response (
103+ {
104+ 'error' : 'Failed to authenticate with Spotify' ,
105+ 'details' : error_details
106+ },
107+ status = status .HTTP_503_SERVICE_UNAVAILABLE
108+ )
109+
110+ return Response (auth_response .json ())
111+ except Exception as e :
112+ print ("Spotify token error:" , str (e ))
113+ return Response (
114+ {'error' : str (e )},
115+ status = status .HTTP_500_INTERNAL_SERVER_ERROR
116+ )
117+
118+ class SpotifySearchView (APIView ):
119+ permission_classes = [permissions .IsAuthenticated ]
120+
121+ def get (self , request ):
122+ try :
123+ query = request .GET .get ('q' , '' )
124+ if not query :
125+ return Response (
126+ {'error' : 'Search query is required' },
127+ status = status .HTTP_400_BAD_REQUEST
128+ )
129+
130+ # Get Spotify credentials and token
131+ client_id = os .getenv ('SPOTIFY_CLIENT_ID' )
132+ client_secret = os .getenv ('SPOTIFY_CLIENT_SECRET' )
133+
134+ if not client_id or not client_secret :
135+ return Response (
136+ {'error' : 'Spotify credentials not configured' },
137+ status = status .HTTP_503_SERVICE_UNAVAILABLE
138+ )
139+
140+ # Get access token
141+ auth_response = requests .post (
142+ 'https://accounts.spotify.com/api/token' ,
143+ data = {
144+ 'grant_type' : 'client_credentials'
145+ },
146+ auth = (client_id , client_secret )
147+ )
148+
149+ if auth_response .status_code != 200 :
150+ return Response (
151+ {'error' : 'Failed to authenticate with Spotify' },
152+ status = status .HTTP_503_SERVICE_UNAVAILABLE
153+ )
154+
155+ access_token = auth_response .json ()['access_token' ]
156+
157+ # Search Spotify
158+ search_response = requests .get (
159+ 'https://api.spotify.com/v1/search' ,
160+ params = {
161+ 'q' : query ,
162+ 'type' : 'playlist' ,
163+ 'limit' : 5
164+ },
165+ headers = {
166+ 'Authorization' : f'Bearer { access_token } '
167+ }
168+ )
169+
170+ if search_response .status_code != 200 :
171+ return Response (
172+ {'error' : 'Failed to search Spotify' },
173+ status = status .HTTP_503_SERVICE_UNAVAILABLE
174+ )
175+
176+ return Response (search_response .json ())
177+ except Exception as e :
178+ print ("Spotify search error:" , str (e ))
179+ return Response (
180+ {'error' : str (e )},
181+ status = status .HTTP_500_INTERNAL_SERVER_ERROR
182+ )
0 commit comments