Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hexagon5un
Created February 24, 2023 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hexagon5un/8cfaebbccd8e23fc5b5feb7e572a21fe to your computer and use it in GitHub Desktop.
Save hexagon5un/8cfaebbccd8e23fc5b5feb7e572a21fe to your computer and use it in GitHub Desktop.
Horrible code from ages ago
#! /usr/bin/env python
## Simple controller shell to interface with MPC with minimal keystrokes
## Note: ssh -p 23 -L 6600:audiobox:6600 elliotwilliams.org
## makes it work from remote locations. Tunnels into my network. :)
import mpdclient2, time, pdb, curses, os
import cPickle as pickle
## os.system("export MPD_HOST='audiobox'")
SONGLINE = 3 # default, 3 lines down
BOTTOMLINE = 21
NUMINAROW = 3 # (inclusive)
RANDOMFLAG = True
DISPLAYHELP = False
# PICKLEFILE = "playlistPickle"
class QuitException(Exception):
pass
def secondsToMinutes(seconds):
minutes = seconds / 60
seconds = seconds - minutes*60
return(minutes, seconds)
def printStatus():
global playlist
global baseSongNumber
global DISPLAYHELP
global m
global statusMessage
global screen
status = m.status()
currentSong = m.currentsong()
currentSongPosition = int(currentSong.pos)
screen.clear()
screen.move(SONGLINE, 0)
songs = [baseSongNumber + r for r in range(NUMINAROW)]
try: ## Print out Artist, album
songString = "%s\n %s\n" % (currentSong.artist, currentSong.album)
except KeyError: ## Dealing with filenames sucks
if len(playlist[currentSongPosition].file.split(os.sep)) >= 3:
# if long, assume path/artist/album/songtitle.mp3
artist = playlist[currentSongPosition].file.split(os.sep)[-3].capitalize()
album = playlist[currentSongPosition].file.split(os.sep)[-2].capitalize()
else:
# if short, assume path/artist/songtitleWithAlbum.mp3
artist = playlist[currentSongPosition].file.split(os.sep)[-2].capitalize()
album = ""
songString = "%s\n %s\n" % (artist, album)
for song in songs: ## print out numinarow songs, indicator
try:
songTitle = playlist[song].title # needs to handle out-of-range here
except KeyError:
songTitle = playlist[song].file.split(os.sep)[-1]
except IndexError:
songTitle = "[End of List]"
if song == currentSongPosition:
songString += " -- %s\n" % songTitle
else:
songString += " %s\n" % songTitle
screen.addstr(songString)
## Status line
screen.addstr(" " + "-" * 76 + "\n", curses.color_pair(1))
if int(status.random):
random = "On"
else:
random = "Off"
timeState = status.time.split(":")
position = int(timeState[0])
positionMin, positionSec = secondsToMinutes(position)
songEnd = int(timeState[1])
songEndMin, songEndSec = secondsToMinutes(songEnd)
screen.addstr("\tvolume: %s \t random: %s \t time: %s:%02d / %s:%02d" % \
(status.volume, random,
positionMin, positionSec, songEndMin, songEndSec))
if DISPLAYHELP:
screen.move(BOTTOMLINE, 0)
screen.addstr(' [space] = pause, [backspace] = next songs, [1]-[9] = n in a row, [q] = quit\n [left]/[right] = soft/loud, [up]/[down] = previous/next, [r] = reload playlist')
# if statusMessage:
# screen.move(BOTTOMLINE-2, 3)
# screen.addstr(statusMessage)
screen.refresh()
def initScreen():
screen = curses.initscr()
curses.noecho() # No echo
curses.cbreak() # react to each character
screen.keypad(1) # Allow arrow keys
curses.curs_set(0) # Don't show the cursor
curses.halfdelay(10) # delay five seconds
curses.start_color()
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
return(screen)
def killScreen(screen):
## Undo it all...
curses.nocbreak()
screen.keypad(0)
curses.echo()
curses.endwin()
curses.curs_set(1)
def processKeystroke():
global m
global NUMINAROW
global DISPLAYHELP
global playlist
global statusMessage
try:
c = screen.getch()
c = chr(c) # throws a ValueError if it's non-ASCII
if c == 'q':
raise QuitException
if c == " ":
if m.status().state == "pause":
m.pause(0)
else:
m.pause(1)
# if c == "r" or c == "z":
# if m.status().random == "0":
# m.random(1)
# else:
# m.random(0)
if c == "h":
if DISPLAYHELP:
DISPLAYHELP = False
else:
DISPLAYHELP = True
if c == "r":
playlist = m.playlistid()
if int(c) in range(1,10):
NUMINAROW = int(c)
except ValueError: # process control keys here
if c == curses.KEY_RIGHT:
volume = int(m.status().volume)
m.do.setvol(volume+1)
if c == curses.KEY_LEFT:
volume = int(m.status().volume)
m.do.setvol(volume-1)
if c == curses.KEY_UP:
m.do.previous()
if c == curses.KEY_DOWN:
m.do.next()
if c == curses.KEY_BACKSPACE:
skipThree()
except EOFError: # trap MPD's EOF's and ignore
pass
threePlayerLogic()
printStatus()
def threePlayerLogic():
global RANDOMFLAG
global baseSongNumber
global playlist
global lastPosition
global m
currentSong = m.currentsong()
position = int(currentSong.pos)
if(RANDOMFLAG): ## Randomizing, selecting a new song
if position != baseSongNumber + (NUMINAROW-1): # just changed to new song
baseSongNumber = position ## New base, turn random off, reset flag
m.random(0)
RANDOMFLAG = False
else: ## Not currently randomizing
if position == baseSongNumber + (NUMINAROW-1): # Reached third song
m.random(1)
RANDOMFLAG = True
if (len(playlist) - baseSongNumber) < (NUMINAROW-1):
# Reached a song too close to the end of the playlist
m.random(1)
RANDOMFLAG = True
lastPosition = position
def skipThree():
global RANDOMFLAG
global m
m.random(1)
m.do.next()
RANDOMFLAG = True
def initialize3():
global m
if m.status().state == "stop":
m.play()
currentSong = m.currentsong()
baseSongNumber = int(currentSong.pos)
print "\nGetting playlist: this could take a minute\n\n"
playlist = m.playlistid()
m.random(1)
lastPosition = 0
return(lastPosition, baseSongNumber, playlist)
if __name__ == "__main__":
m = mpdclient2.connect() # global connection
(lastPosition, baseSongNumber, playlist) = initialize3() # "Global" variables
statusMessage = ""
try:
screen = initScreen()
printStatus()
while True:
processKeystroke()
except QuitException:
killScreen(screen)
#except: ## All other errors, drop down to debugger
#raw_input("Press Return To Continue")
#killScreen(screen)
#pdb.set_trace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment