-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer.py
executable file
·62 lines (52 loc) · 1.89 KB
/
producer.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
from kafka import KafkaProducer
from time import sleep
import json
from json import dumps
from decouple import config
import spotipy
from spotipy.oauth2 import SpotifyOAuth
################################
# CONSTANTS #
################################
PERIOD = 0.3 # here we can set how often to make calls
################################
# AUTHENTIFICATION #
################################
scope = "user-read-playback-state user-read-playback-position"
CLIENT_ID = config('CLIENT_ID', default='')
CLIENT_SECRET = config('CLIENT_SECRET', default='')
REDIRECT_URI = 'http://localhost/callback'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI, scope=scope))
################################
# PRODUCER INIT #
################################
producer = KafkaProducer(bootstrap_servers=['localhost:9092'],
value_serializer=lambda x: dumps(x).encode('utf-8'))
################################
# API REQUEST #
################################
for e in range(10000): # here we can set the time
# REQUEST
results = sp.current_playback()
print(results)
try:
artists = []
for a in results['item']['artists']:
artists.append(a['name'])
data = {
'status': 'OK',
'progress_ms': results['progress_ms'],
'currently_playing_type': results['currently_playing_type'],
'name': results['item']['name'],
'artists': artists,
'id': results['item']['id'],
'is_playing': results['is_playing']
}
except TypeError:
data = {
'status': 'NOT PLAYING'
}
producer.send('lyricgen', data)
producer.flush()
sleep(PERIOD)