Python Forum
Get lyrics from songs playing in spotify - Need help making it better
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get lyrics from songs playing in spotify - Need help making it better
#1
Check out this simple project. Im just starting out in python. Anything I can do better?
Maybe use flask to make it a webpage.
Or some code that listens for change and auto updates the lyrics

https://github.com/thorep/LyricGetter

Add option for changing song in the terminal

Added*
Reply
#2
Interesting project. It'd be good for a party to turn Spotify into karaoke.

Generally, I recommend organizing the code into functions or into a class. Doing so will streamline future updates.

In lyrics.py, there are a few items that could be done better.

Line 18, use in instead of multiple equality checks. Also, anytime that you're checking user inputs, use str.lower() or str.upper() to exert control over the input. In this way, you can account for incidental capitalization issues (e.g. user left caplocks on).

while user_choice.lower() in ("r", "n", "p", "s", "o"):
Lines 23 through 27, when formatting in this manner, it is better to keep your arguments indented only one level more than the function name. It looks cleaner and doesn't push anything over the wrap line.

token = util.prompt_for_user_token(
    username,
    scope = "user-read-playback-state user-read-currently-playing user-modify-playback-state",
    client_id = CLIENT_ID,
    client_secret = CLIENT_SECRET,
    redirect_uri = REDIRECT_URI
)
Line 41 does not accomplish anything. The variable current is set on line 42.

Lines 45 and 46 have a typo in the variable. The typo is replicated and consistent so the script works.

Lines 65 through 100 are a prime opportunity for a function. You have a pattern going on: input, clear, input, clear, etc. It would be better to make a function that requests input and checks the input type and implement in a loop. This keeps the code clean, makes updates easier, and allows you to keep the messages and types separate from implementation.

def input_specified_type(message, type):
    while True:
        try:
            user_in = type(input(message))
        except ValueError:
            continue
        else: return user_in

messages = (
    ("Float - A confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high\n confidence the track is acoustic.: ", float, "target_acousticness_input"),
    ("Float - Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable. : ", float, "target_danceability_input")
)
ratings = {arg[2]: 0 for arg in messages}

# Next 10 lines are equivalent to lines 65 through 108 when all message/type/arg trios are implemented
for trio in messages:
    ratings[trio[2]] = input_specified_type(trio[0], trio[1])
    ClearScreen.clear()

rec_tracks = s.recommendations(
    seed_tracks = [track_uri],
    country = "NO",
    limit = 20,
    **ratings
)
In spotipy/util.py, the function prompt_for_user_token() should be broken up into smaller functions. I have no doubt that it works and it matches the standard set by many other Python projects. However, large functions like this are difficult to troubleshoot and horrid to modify. Smaller functions that work together allow for easier testing and greater modularity.

Line 71 should be at the top. Imports should always be at the top of the file.

That's all I looked through. Overall, that looks pretty good for a Python newbie.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020