This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (55 loc) · 1.82 KB
/
main.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
# -*- coding: utf-8 -*-
import time
import config
import sys
from utils import get_current_date
from spotify_authorization import SpotifyAuthorization
from spotify_api import SpotifyAPI
from radio_stations import RadioStations
# authorization
spotify_auth = SpotifyAuthorization(
config.SPOTIFY_CLIENT_ID,
config.SPOTIFY_CLIENT_SECRET,
config.SPOTIFY_REDIRECT_URI,
config.SPOTIFY_SCOPE
)
if not spotify_auth.main():
sys.exit("Refresh token could not taken.")
# api
spotify_api_obj = SpotifyAPI(
config.SPOTIFY_CLIENT_ID,
config.SPOTIFY_CLIENT_SECRET,
config.SPOTIFY_USER,
spotify_auth.refresh_token
)
# radio stations
radio_stations_obj = RadioStations()
#
playlist_date = get_current_date()
playlist_id = False
temporary_added_tracks = []
while True:
# token
if spotify_api_obj.is_token_expired():
spotify_api_obj.refresh_access_token()
# playlist
current_date = get_current_date()
if (not playlist_id) or (playlist_date != current_date):
playlist_date = current_date
playlist_name = f'{config.PLAYLIST_NAME} - {playlist_date}'
playlist_id = spotify_api_obj.is_playlist_exist(playlist_name)
if not playlist_id:
playlist_id = spotify_api_obj.create_playlist(playlist_name)
# clear the added tracks when new playlist created
temporary_added_tracks.clear()
# track
radio_stations_obj.main()
while not radio_stations_obj.tracks.empty():
track_name = radio_stations_obj.tracks.get()
print(track_name)
if track_name not in temporary_added_tracks:
track_id = spotify_api_obj.search_track(track_name)
temporary_added_tracks.append(track_name)
if track_id:
spotify_api_obj.add_track_to_playlist(track_id, playlist_id)
time.sleep(60*3)