Python Forum
interloper help appreciated combining two python3 scripts to add new conditions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
interloper help appreciated combining two python3 scripts to add new conditions.
#1
First off I apologize if I use terminology wrong, I don't know python coding but got help from an expert, currently unavailable, who made me a few complex scripts using spotipy python library that uses spotify api, and a program called anybar that monitors current song status and displays as different colored dots in mac menu bar.

Which is necessary when dealing with thousands of songs and deep digging through spotifies catalog. Which is my main hobby.

Anyway, the relevant section in the monitoring script is if a song I marked as liked (I actually heard and disliked) the dot will be one color, or false (I haven't come across it or have and like it) the dot automatically switches to another color.

As I started using it heavily I realized I forgot the scenario of songs I heard AND liked AND are in a playlist already, so just skip them when digging as I would a bad song.

So I'd like to to add three more options (can you add that to a true/false choice in python easily?), that it's true, false, UNLESS it's in playlist 1, or 2, or 3, in that case it will be a different color, I'll choose the corresponding colors to those scenarios.

Maybe posting the scripts would better help explain what he did and how I need to add one part to another to make one big code.

I removed any creditials etc, but they're in the real scripts.

this is the add to playlist code.

import spotipy
from spotipy.oauth2 import SpotifyOAuth

# START Custom Credentials
SPOTIPY_CLIENT_ID = ''
SPOTIPY_CLIENT_SECRET = ''
SPOTIFY_USERNAME = ''
# ID of the playlist where the track needs to be added
SPOTIFY_PLAYLIST_ID = ''
# The cache file absolute path
SPOTIFY_CACHE = ''
# END Custom Credentials


SPOTIPY_REDIRECT_URI = ''

scope = "user-library-read user-read-currently-playing user-read-playback-state playlist-modify-public playlist-modify-private"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, client_id=SPOTIPY_CLIENT_ID,
                                               username=SPOTIFY_USERNAME,
                                               client_secret=SPOTIPY_CLIENT_SECRET, redirect_uri=SPOTIPY_REDIRECT_URI, cache_path=SPOTIFY_CACHE))

current_track = sp.current_playback()
if current_track is None:
    print('No track is beeing played right now')
else:
    # Track info
    track_id = current_track['item']['id']
    print('Currently playing {track} ({track_id}'.format(
        track=current_track['item']['name'], track_id=track_id))
    try:
        added = sp.user_playlist_add_tracks(
            user=SPOTIFY_USERNAME, playlist_id=SPOTIFY_PLAYLIST_ID, tracks=[track_id])
    except:
        added = None
    if added is None:
        print('# Unable to add the track')
    else:
        print('# Track has been added successfully to your playlist')
and this is the anybar monitoring part that needs the first script incorporated into it somehow

the liked_colors section needs new conditions,

so that it's True: "question",
False: "exclamation", unless it's in SPOTIFY_PLAYLIST_ID = (and there will be three playlists, but I can fill the colors and playlist IDs myself

from pprint import pprint
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from anybar import AnyBar
import threading
# START Custom Credentials
SPOTIPY_CLIENT_ID = ''
SPOTIPY_CLIENT_SECRET = ''
SPOTIFY_USERNAME = ''
# The cache file absolute path
SPOTIFY_CACHE = ''
# END Custom Credentials


SPOTIPY_REDIRECT_URI = ''
PATH_TO_BASH_SCRIPT = ''
INTERVAL_SECONDS = 1
playing_colors = {
    True: "green",
    False: "red",
}
liked_colors = {
    True: "question",
    False: "exclamation",
}
shuffle_colors = {
    True: "purple",
    False: "blue",
}
repeat_colors = {
    "track": "green",
    "context": "blue",
    "off": "cyan",
}
scope = "user-library-read user-read-currently-playing user-read-playback-state playlist-modify-public playlist-modify-private"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope, client_id=SPOTIPY_CLIENT_ID,
                                               username=SPOTIFY_USERNAME,
                                               client_secret=SPOTIPY_CLIENT_SECRET, redirect_uri=SPOTIPY_REDIRECT_URI, cache_path=SPOTIFY_CACHE))
playing_status = AnyBar(port=)
liked_status = AnyBar(port=)
shuffle_status = AnyBar(port=)
repeat_status = AnyBar(port=)


def thread():
    global playing_status
    global liked_status
    global shuffle_status
    global repeat_status
    global playing_colors
    global liked_colors
    global shuffle_colors
    global repeat_colors
    track = sp.current_playback()

    if track is None:
        # Playing Mode
        playing_status.change(playing_colors[False])
    else:
        if track.get('is_playing') is not None and not track['is_playing']:
            playing_status.change(playing_colors[False])
        else:
            playing_status.change(playing_colors[True])
        isLiked = sp.current_user_saved_tracks_contains(
            tracks=[track['item']['id']])[0]
        if isLiked:
            # Liked Mode
            liked_status.change(liked_colors[False])
        else:
            liked_status.change(liked_colors[True])
        # Shuffle mode
        shuffle_status.change(
            shuffle_colors[track['shuffle_state']])
        # Repeat mode
        repeat_status.change(repeat_colors[track['repeat_state']])
    threading.Timer(INTERVAL_SECONDS, thread).start()


thread()
anyway, if it's a simple cut and paste kind of thing it would be greatly appreciated if someone could look it over and come up with the solution, if it's difficult I understand, no hard feelings, figured I'd try, and will try and get him when he's not so busy if he is nice enough to help me out again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 954 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  Any help is appreciated! Butch12 3 724 Jul-22-2023, 03:23 AM
Last Post: Larz60+
  new to programming, any help would be appreciated! 01andy 9 3,508 Oct-13-2020, 07:10 PM
Last Post: deanhystad
  Combining 2 or more scripts to one. Makada 8 2,816 Apr-01-2020, 05:14 AM
Last Post: Makada
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,895 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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