-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·98 lines (72 loc) · 3.25 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
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
from functools import cache
import lyricsgenius
from dotenv import load_dotenv
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import sys
script_directory = os.path.dirname(os.path.realpath(__file__))
env_file = script_directory + "/.env"
def load_credentials():
if os.path.isfile(script_directory + "/.env"):
dotenv_path = script_directory + "/.env"
load_dotenv(dotenv_path)
SPOTIPY_CLIENT_ID = os.environ.get("SPOTIPY_CLIENT_ID")
SPOTIPY_CLIENT_SECRET = os.environ.get("SPOTIPY_CLIENT_SECRET")
SPOTIPY_REDIRECT_URI = os.environ.get("SPOTIPY_REDIRECT_URI")
GENIUS_TOKEN = os.environ.get("GENIUS_TOKEN")
if SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET and SPOTIPY_REDIRECT_URI != "":
print("Valid credentials found!")
return (
SPOTIPY_CLIENT_ID,
SPOTIPY_CLIENT_SECRET,
SPOTIPY_REDIRECT_URI,
GENIUS_TOKEN,
)
else:
print("Credentials not valid. Pleae try again.")
def get_lyrics(title, artist):
genius = lyricsgenius.Genius(os.environ["GENIUS_TOKEN"])
song = genius.search_song(title, artist)
print("\n" + song.lyrics)
def spotify_playback(SPOTIPY_CLIENT_ID,SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI):
scope = "user-read-playback-state"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
scope=scope, cache_path="./cache.txt"))
results = sp.current_playback()
if results == None:
return None
else:
title = results["item"]["name"]
artist = results["item"]["artists"][0]["name"]
return title, artist
def last_song_played(SPOTIPY_CLIENT_ID,SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI):
scope = "user-read-recently-played"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
scope=scope, cache_path="./cache.txt"))
results = sp.current_user_recently_played(limit=1)
last_song_title = results["items"][0]["track"]["name"]
last_song_artist = results["items"][0]["track"]["album"]["artists"][0]["name"]
return last_song_title, last_song_artist
if __name__ == "__main__":
credentials = load_credentials()
data = spotify_playback(credentials[0], credentials[1], credentials[2])
if data == None:
data = last_song_played(credentials[0], credentials[1], credentials[2])
print("No song is being played at the moment on your Spotify account, sorry.\n")
user_input = input(
"Do you want to get the lyrics of your last played song: {} by {} ? Y/N\n".format(
data[0], data[1]
)
)
if user_input.lower() == "y":
get_lyrics(data[0], data[1])
else:
print("\nOk, bye.")
sys.exit()
else:
get_lyrics(data[0], data[1])