Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlan-00 committed Mar 18, 2021
1 parent ff52e58 commit 6852666
Show file tree
Hide file tree
Showing 6 changed files with 2,986 additions and 3,827 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ install-req:
cp ampache-fm.py $(INSTALLPATH) -f
cp ampache.py $(INSTALLPATH) -f
cp config.ui $(INSTALLPATH) -f
cp afm.conf.template $(INSTALLPATH) -f
cp README.md $(INSTALLPATH) -f
cp LICENSE $(INSTALLPATH) -f
cp AUTHORS $(INSTALLPATH) -f
Expand Down
85 changes: 53 additions & 32 deletions ampache-fm.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

PLUGIN_PATH = 'plugins/ampache-fm/'
CONFIGFILE = 'afm.conf'
CONFIGTEMPLATE = 'afm.conf.template'
UIFILE = 'config.ui'
C = 'conf'

Expand All @@ -53,6 +52,8 @@ class AmpacheFm(GObject.Object, Peas.Activatable, PeasGtk.Configurable):
def __init__(self):
GObject.Object.__init__(self)
RB.BrowserSource.__init__(self, name=_('ampache-fm'))
self.ampache = ampache.API()
self.ampache.set_format('json')
self.plugin_info = 'ampache-fm'
self.conf = configparser.RawConfigParser()
self.configfile = RB.find_user_data_file(PLUGIN_PATH + CONFIGFILE)
Expand Down Expand Up @@ -83,8 +84,8 @@ def __init__(self):
self.ampache_url = None
self.ampache_user = None
self.ampache_apikey = None
self.ampache_password = None
self.ampache_session = False
self.can_scrobble = False

def do_activate(self):
""" Activate the plugin """
Expand All @@ -104,16 +105,13 @@ def do_activate(self):
self.ampache_user = self.conf.get(C, 'ampache_user')
self.ampache_url = self.conf.get(C, 'ampache_url')
self.ampache_apikey = self.conf.get(C, 'ampache_api')
self.ampache_password = self.conf.get(C, 'ampache_password')
# Get a session
self.ampache_session = ampache.handshake(self.ampache_url,
ampache.encrypt_string(self.ampache_apikey, self.ampache_user))
if self.ampache_session:
self.can_scrobble = True
self._check_session()

def do_deactivate(self):
""" Deactivate the plugin """
print('deactivating ampache-fm')
print(ampache.ping(self.ampache_url, self.ampache_session))
self.nowtime = int(time.time())
self.cache_now_playing()
Gio.Application.get_default()
Expand All @@ -131,13 +129,22 @@ def ampache_auth(self, key):
self.ampache_user = self.conf.get(C, 'ampache_user')
self.ampache_url = self.conf.get(C, 'ampache_url')
self.ampache_apikey = self.conf.get(C, 'ampache_api')
self.ampache_password = self.conf.get(C, 'ampache_password')
if self.ampache_url[:8] == 'https://' or self.ampache_url[:7] == 'http://':
ping = ampache.ping(self.ampache_url, key)
if ping:
self.ampache_session = ping
return ping
auth = ampache.handshake(self.ampache_url, ampache.encrypt_string(self.ampache_apikey, self.ampache_user))
if key:
ping = self.ampache.ping(self.ampache_url, key)
if ping:
# ping successful
self.ampache_session = ping
return ping
if self.ampache_password:
mytime = int(time.time())
passphrase = self.ampache.encrypt_password(self.ampache_password, mytime)
auth = self.ampache.handshake(self.ampache_url, passphrase, self.ampache_user, mytime)
else:
auth = self.ampache.handshake(self.ampache_url, self.ampache.encrypt_string(self.ampache_apikey, self.ampache_user))
if auth:
print('handshake successful')
self.ampache_session = auth
return auth
return False
Expand All @@ -153,8 +160,6 @@ def elapsed_changed(self, shell_player, elapsed):
self.nowtime = int(time.time())
songlength = shell_player.get_playing_song_duration()
if (songlength > 30 and elapsed == 30) or (songlength <= 30 and (songlength - 4) == elapsed):
# check your session
self.ampache_auth(self.ampache_session)
# Get name/string tags
self.nowtitle = entry.get_string(RB.RhythmDBPropType.TITLE)
self.nowartist = entry.get_string(RB.RhythmDBPropType.ARTIST)
Expand All @@ -172,10 +177,9 @@ def elapsed_changed(self, shell_player, elapsed):

def cache_now_playing(self):
""" Cache the track to file or to Ampache if you are able """
if self.can_scrobble:
self.ampache_auth(self.ampache_session)
if self._check_session():
print('Sending scrobble to Ampache: ' + self.nowtitle)
Process(target=ampache.scrobble,
Process(target=self.ampache.scrobble,
args=(self.ampache_url, self.ampache_session, self.nowtitle, self.nowartist, self.nowalbum,
self.nowMBtitle, self.nowMBartist, self.nowMBalbum,
self.nowtime, 'AmpacheFM Rhythmbox')).start()
Expand All @@ -187,22 +191,36 @@ def cache_now_playing(self):
'\t' + self.nowMBartist +
'\t' + self.nowMBalbum))

def _check_session(self):
return self.ampache_auth(self.ampache_session)

def _check_configfile(self):
""" Copy the default config template or load existing config file """
""" Create the default config template or load existing config file """
if not os.path.isfile(self.configfile):
template = RB.find_user_data_file(PLUGIN_PATH + CONFIGTEMPLATE)
folder = os.path.split(self.configfile)[0]
if not os.path.exists(folder):
os.makedirs(folder)
shutil.copyfile(template, self.configfile)
""" create a default config if not available """
conffile = open(self.configfile, "w")
conffile.write('[conf]\n' +
'ampache_url = \n' +
'ampache_user = \n' +
'ampache_api = \n' +
'ampache_password = \n' +
'log_path = ' + os.path.join(RB.user_cache_dir(), 'ampache-fm.txt') + '\n' +
'log_rotate = True \n' +
'log_limit = 10760720')
conffile.close()
# read the conf file
self.conf.read(self.configfile)
# updated to add password support
if not self.conf.has_option(C, 'ampache_password'):
# set default path for the user
self.conf.read(self.configfile)
self.conf.set(C, 'log_path', os.path.join(RB.user_cache_dir(), 'ampache-fm.txt'))
datafile = open(self.configfile, 'w')
self.conf.set(C, 'ampache_password', '')
self.conf.write(datafile)
datafile.close()
# read the conf file
self.conf.read(self.configfile)
self.conf.read(self.configfile)
return

# Create the Configure window in the rhythmbox plugins menu
Expand All @@ -224,6 +242,7 @@ def do_create_configure_widget(self):
build.get_object('ampache_url').set_text(self.conf.get(C, 'ampache_url'))
build.get_object('ampache_user').set_text(self.conf.get(C, 'ampache_user'))
build.get_object('ampache_api').set_text(self.conf.get(C, 'ampache_api'))
build.get_object('ampache_password').set_text(self.conf.get(C, 'ampache_password'))
build.get_object('log_path').set_text(self.conf.get(C, 'log_path'))
build.get_object('log_limit').set_text(self.conf.get(C, 'log_limit'))
if self.conf.get(C, 'log_rotate') == 'True':
Expand All @@ -236,13 +255,15 @@ def save_config(self, builder):
self.ampache_url = builder.get_object('ampache_url').get_text()
self.ampache_user = builder.get_object('ampache_user').get_text()
self.ampache_apikey = builder.get_object('ampache_api').get_text()
self.ampache_password = builder.get_object('ampache_password').get_text()
if builder.get_object('log_rotate').get_active():
self.conf.set(C, 'log_rotate', 'True')
else:
self.conf.set(C, 'log_rotate', 'False')
self.conf.set(C, 'ampache_url', self.ampache_url)
self.conf.set(C, 'ampache_user', self.ampache_user)
self.conf.set(C, 'ampache_api', self.ampache_apikey)
self.conf.set(C, 'ampache_password', self.ampache_password)
self.conf.set(C, 'log_path',
builder.get_object('log_path').get_text())
self.conf.set(C, 'log_limit',
Expand Down Expand Up @@ -347,14 +368,14 @@ def backfill(self):
pass
# search ampache db for song
if rowtrack and rowartist and rowalbum:
self.ampache_auth(self.ampache_session)
print('Sending scrobble to Ampache: ' + str(rowtrack))
Process(target=ampache.scrobble,
args=(self.ampache_url, self.ampache_session, str(rowtrack), str(rowartist),
str(rowalbum),
str(trackmbid).replace("None", ""), str(artistmbid).replace("None", ""),
str(albummbid).replace("None", ""),
int(row[0]), 'AmpacheFM Rhythmbox')).start()
if self._check_session():
print('Sending scrobble to Ampache: ' + str(rowtrack))
Process(target=self.ampache.scrobble,
args=(self.ampache_url, self.ampache_session, str(rowtrack), str(rowartist),
str(rowalbum),
str(trackmbid).replace("None", ""), str(artistmbid).replace("None", ""),
str(albummbid).replace("None", ""),
int(row[0]), 'AmpacheFM Rhythmbox')).start()
self.spinner.stop()
while Gtk.events_pending():
Gtk.main_iteration()
Expand Down
Loading

0 comments on commit 6852666

Please sign in to comment.