File: viridian-cli

package info (click to toggle)
viridian 1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 952 kB
  • sloc: python: 3,838; makefile: 12; sh: 5
file content (142 lines) | stat: -rwxr-xr-x 4,326 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
# Copyright (C) 2010 Dave Eddy <dave@daveeddy.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

__program__    = "Viridian-cli"
__author__     = "David Eddy"
__license__    = "GPLv3"
__version__    = "1.1"
__maintainer__ = "David Eddy"
__email__      = "dave@daveeddy.com"
__status__     = "Release"

import AmpacheTools
from AmpacheTools import AmpacheSession, DatabaseSession, AudioEngine

import os
from getpass import getpass

VIRIDIAN_DIR = os.path.expanduser("~") + os.sep + '.viridian'

def get_valid_digit(prompt = "Enter data: "):
	valid = False
	while not valid:
		_id = raw_input("\n\n%s" % (prompt))
		if _id.isdigit():
			valid = True
		else:
			print "Error! Invalid Album ID"
		
	return _id
def print_now_playing():
	song_id   = audio_engine.get_current_song_id()
	song_dict = ampache_conn.get_song_info(song_id)
	print " - Now Playing - "
	print song_dict['song_title']
	print "By: %s" % song_dict['artist_name']
	print "On: %s" % song_dict['album_name']
	
def next_track():
	audio_engine.next_track()
	print_now_playing()
def prev_track():
	audio_engine.prev_track()
	print_now_playing()


if __name__ == "__main__":
	is_first_time = True
	if os.path.exists(VIRIDIAN_DIR):
		is_first_time = False
		db_session = DatabaseSession(VIRIDIAN_DIR + os.sep + 'viridian.sqlite')

	ampache_conn = AmpacheSession()
	audio_engine = AudioEngine(ampache_conn)

	audio_engine.set_repeat_songs(True)
	
	ready = False
	if not is_first_time:
		username = db_session.variable_get('credentials_username')
		password = db_session.variable_get('credentials_password')
		url      = db_session.variable_get('credentials_url')
		ampache_conn.set_credentials(username, password, url)
		if ampache_conn.has_credentials():
			resp = raw_input("Username '%s' found for Ampache Server '%s'.\nConnect using these credentials? [Y/n]: " % (username, url))
			if resp != 'n' and resp != 'N': # then use the credentials
				ampache_conn.authenticate()
				ready = True
	
	while not ready:
		url  = raw_input("Ampache Server URL (ie 'http://example.org/ampache'): ")
		username = raw_input("Username: ") 
		password = getpass("Password: ")
		ampache_conn.set_credentials(username, password, url)
		if ampache_conn.has_credentials():
			if ampache_conn.authenticate():
				ready = True
		else:
			print "Error! Try Again"
	
	for artist in ampache_conn.get_artists():
		print "%d: %s" % (artist['artist_id'], artist['artist_name'])

	artist_id = get_valid_digit("Artist ID: ")

	print '\n'
	for album in ampache_conn.get_albums_by_artist(artist_id):
		print "%d: %s (Year = %s, Disk = %d, Tracks = %d)" % (album['album_id'], album['album_name'], album['album_year'], album['album_disk'], album['album_tracks'])
	
	album_id = get_valid_digit("Album ID: ")
	
	print '\n'
	song_list = ampache_conn.get_songs_by_album(album_id)
	song_list = sorted(song_list, key=lambda k: k['song_track'])
	for song in song_list:
		print "%d: %s" % (song['song_track'], song['song_title'])
	list = []
	for song in song_list:
		list.append(song['song_id'])


	song_track = get_valid_digit("Track Number: ")
	audio_engine.play_from_list_of_songs(list, int(song_track)-1)

	print_now_playing()
	
	quit = False
	while not quit:
		print "Choices are 'n' for next, 'p' for previous, 'i' for info, 's' for play/pause, and 'q' for quit"
		resp = raw_input("Choice: ")
		print "\n\n"
		if resp == 'n':
			next_track()
		elif resp == 'p':
			prev_track()
		elif resp == 'i':
			print_now_playing()
		elif resp == 's':
			if audio_engine.get_state() == 'playing':
				audio_engine.pause()
				print audio_engine.get_state()
			else:
				audio_engine.play()
				print audio_engine.get_state()
		elif resp == 'q':
			quit = True
		print "\n\n"