def lastfm_playing(): try: if playing and playing['submittable']: if not scrobbler.SESSION_ID: lastfm_login() scrobbler.submit(playing['artist'], playing['track'], playing['time'], length = playing['length'], autoflush = True) except: pass
def run(self): # well this is just fugly. call it "experimental" while Config.running: try: scrobble_item = self.queue.get(0) try: song = scrobble_item.song type = scrobble_item.type error = scrobble_item.error etime = scrobble_item.etime try: (tracknumber, artist, album, track) = [escape(item) for item in song.tags] except ValueError: log.info("skipping scrobble for {} (bad tags)".format(song.path)) continue if type == NOW_PLAYING: log.debug(u"scrobbling now playing %s %s %s" % (artist, track, album)) self.login() scrobbler.now_playing( artist, track) # now_playing auto flushes, apparently. don't call # flush here or it will throw an exception, which is not # what we want. elif type == PLAYED: # See: http://exhuma.wicked.lu/projects/python/scrobbler/api/public/scrobbler-module.html#login # if mimetype is wrong, length == 0 if song.length < 30: log.warn(u"song length %s" % song.length) # wait 60 seconds before re-trying # submission if error: if (time.time() - etime) < 60: break log.debug(u"scrobbling played %s %s %s %s" % (artist, track, album, song.length)) self.login() scrobbler.submit( artist, track, int(time.mktime(datetime.datetime.now().timetuple())), source=escape(u'P'), length=song.length, album=album) scrobbler.flush() except Exception as e: log.exception(u"scrobble error: %s" % e) # put it back scrobble_item.error = True scrobble_item.etime = time.time() self.queue.put(scrobble_item) except Queue.Empty: pass # AS API enforced limit -- do not change. time.sleep(10)
def scrobble(self, media, end_time): if not self.authenticated: return if media.length <= 30: self.l.info("%s is too short to be scrobbled" % media) return time_played = (media.length + time.time() - time.mktime(end_time.timetuple())) if time_played < 240 and time_played < media.length * 0.5: self.l.info("%s has not played long enough" % media) return scrobbler.submit(media.artist, media.title, int(time.mktime(end_time.timetuple()) - media.length), length=int(media.length)) scrobbler.flush()
def scrobble(track): artist = track['Artist'] trackName = track['Name'] playDate = int(mktime(track['Play Date UTC'].timetuple())) source = 'P' rating = '' trackLength = track.get('Total Time', 0) // 1000 album = track.get('Album', '') trackNumber = track.get('Track Number', '') MusicBrainId = '' autoflush = False if trackLength > 30: return scrobbler.submit(artist, trackName, playDate, source, rating, trackLength, album, trackNumber, MusicBrainId, autoflush) return True
def _real_commit(self, now_playing, cachefile, username, password, scrobbler_url): """this is quite ugly spaghetti code. maybe we could make this a little bit more tidy?""" logger.info('Begin scrobbling to %s', scrobbler_url) if (not do_now_playing): logger.debug('Now playing disabled') now_playing = None success = False tosubmit = set() tosubmitted = set() cache_count = 0 retry_sleep = None retry_count = 0 while not success: if retry_sleep is None: retry_sleep = 60 else: retry_count = retry_count + 1 if retry_count > 7: logger.info('Giving up scrobbling to %s', scrobbler_url) break logger.info('Sleeping %d minute(s)', retry_sleep / 60) time.sleep(retry_sleep) retry_sleep = min(retry_sleep * 2, 120 * 60) #handshake phase logger.debug('Handshake') try: scrobbler.login(username, password, hashpw=False, client=CmuScrobbler.CLIENTID, url=scrobbler_url) except Exception, e: logger.error('Handshake with %s failed: %s', scrobbler_url, e) log_traceback(e) continue #submit phase if os.path.exists(cachefile): logger.info('Scrobbling songs to %s', scrobbler_url) (_, _, _, _, _, _, _, _, mtime, _) = os.stat(cachefile) fo = file(cachefile, 'r') line = fo.readline() while len(line) > 0: (path, artist, track, playtime, source, length, album, trackno) = line.split('\t') trackno = trackno.strip() mbid = get_mbid(unquote(path).decode('utf-8')) tosubmit.add((playtime, artist, track, source, length, album, trackno, mbid)) line = fo.readline() fo.close() logger.info('Read %d songs from cachefile %s', len(tosubmit), cachefile) logger.debug('Sorting songlist') submitlist = list(tosubmit) submitlist.sort(key=lambda x: int(x[0])) retry = False for (playtime, artist, track, source, length, album, trackno, mbid) in submitlist: if (playtime, artist, track, source, length, album, trackno, mbid) in tosubmitted: logger.debug( 'Track already submitted or in cache: %s - %s', unquote(artist), unquote(track)) continue if cache_count >= 3: logger.info('Flushing. cache_count=%d', cache_count) if self._flush(): logger.info('Flush successful.') retry_sleep = None cache_count = 0 else: retry = True break sb_success = False for tries in xrange(1, 4): logger.debug( 'Try to submit: %s, %s, playtime=%d, source=%s, length=%s, album=%s, trackno=%s, mbid=%s', unquote(artist), unquote(track), int(playtime), source, length, unquote(album), trackno, mbid) try: sb_success = scrobbler.submit( unquote(artist).decode('utf-8'), unquote(track).decode('utf-8'), int(playtime), source=source.decode('utf-8'), length=length.decode('utf-8'), album=unquote(album).decode('utf-8'), trackno=trackno.decode('utf-8'), mbid=mbid, ) except Exception, e: logger.error('Submit error: %s', e) log_traceback(e) sb_success = False if sb_success: tosubmitted.add((playtime, artist, track, source, length, album, trackno, mbid)) cache_count += 1 logger.info('Submitted. cache_count=%d: %s - %s', cache_count, unquote(artist), unquote(track)) break logger.error('Submit failed. Try %d', tries) if not sb_success: retry = True break if cache_count >= 3: logger.info('Flushing. cache_count=%d', cache_count) if self._flush(): logger.info('Flush successful.') retry_sleep = None cache_count = 0 else: retry = True break if retry: logger.error('Restaring') continue if cache_count > 0: logger.info('Cache not empty: flushing') if self._flush(): logger.info('Flush successful.') retry_sleep = None cache_count = 0 else: logger.error('Restarting') continue (_, _, _, _, _, _, _, _, newmtime, _) = os.stat(cachefile) if newmtime != mtime: logger.info( 'Cachefile changed since we started. Restarting') continue logger.info('Scrobbled all Songs, removing cachefile') os.remove(cachefile)
def _real_commit(self, now_playing, cachefile, username, password, scrobbler_url): """this is quite ugly spaghetti code. maybe we could make this a little bit more tidy?""" logger.info('Begin scrobbling to %s', scrobbler_url) if (not do_now_playing): logger.debug('Now playing disabled') now_playing = None success = False tosubmit = set() tosubmitted = set() cache_count = 0 retry_sleep = None retry_count = 0 while not success: if retry_sleep is None: retry_sleep = 60 else: retry_count = retry_count + 1 if retry_count > 7: logger.info('Giving up scrobbling to %s', scrobbler_url) break logger.info('Sleeping %d minute(s)', retry_sleep / 60) time.sleep(retry_sleep) retry_sleep = min(retry_sleep * 2, 120 * 60) #handshake phase logger.debug('Handshake') try: scrobbler.login(username, password, hashpw=False, client=CmuScrobbler.CLIENTID, url=scrobbler_url) except Exception, e: logger.error('Handshake with %s failed: %s', scrobbler_url, e) log_traceback(e) continue #submit phase if os.path.exists(cachefile): logger.info('Scrobbling songs to %s', scrobbler_url) (_, _, _, _, _, _, _, _, mtime, _) = os.stat(cachefile) fo = file(cachefile,'r') line = fo.readline() while len(line) > 0: try: (path, artist, track, playtime, source, length, album, trackno) = line.split('\t') trackno = trackno.strip() mbid = get_mbid(unquote(path).decode('utf-8')) tosubmit.add((playtime, artist, track, source, length, album, trackno, mbid)) except Exception, e: logger.debug('cache read error: %s', e) line = fo.readline() fo.close() logger.info('Read %d songs from cachefile %s', len(tosubmit), cachefile) logger.debug('Sorting songlist') submitlist = list(tosubmit) submitlist.sort(key=lambda x: int(x[0])) retry = False for (playtime, artist, track, source, length, album, trackno, mbid) in submitlist: if (playtime, artist, track, source, length, album, trackno, mbid) in tosubmitted: logger.debug('Track already submitted or in cache: %s - %s', unquote(artist), unquote(track)) continue if cache_count >= 3: logger.info('Flushing. cache_count=%d', cache_count) if self._flush(): logger.info('Flush successful.') retry_sleep = None cache_count = 0 else: retry = True break sb_success = False for tries in xrange(1, 4): logger.debug('Try to submit: %s, %s, playtime=%d, source=%s, length=%s, album=%s, trackno=%s, mbid=%s', unquote(artist), unquote(track), int(playtime), source, length, unquote(album), trackno, mbid) try: sb_success = scrobbler.submit(unquote(artist).decode('utf-8'), unquote(track).decode('utf-8'), int(playtime), source=source.decode('utf-8'), length=length.decode('utf-8'), album=unquote(album).decode('utf-8'), trackno=trackno.decode('utf-8'), mbid=mbid, ) except Exception, e: logger.error('Submit error: %s', e) log_traceback(e) sb_success = False if sb_success: tosubmitted.add((playtime, artist, track, source, length, album, trackno, mbid)) cache_count += 1 logger.info('Submitted. cache_count=%d: %s - %s', cache_count, unquote(artist), unquote(track)) break logger.error('Submit failed. Try %d', tries) if not sb_success: retry = True break if cache_count >= 3: logger.info('Flushing. cache_count=%d', cache_count) if self._flush(): logger.info('Flush successful.') retry_sleep = None cache_count = 0 else: retry = True break
def run(self): # well this is just fugly. call it "experimental" while Config.running: try: scrobble_item = self.queue.get(0) try: song = scrobble_item.song type = scrobble_item.type error = scrobble_item.error etime = scrobble_item.etime try: (tracknumber, artist, album, track) = [escape(item) for item in song.tags] except ValueError: log.info("skipping scrobble for {} (bad tags)".format( song.path)) continue if type == NOW_PLAYING: log.debug(u"scrobbling now playing %s %s %s" % (artist, track, album)) self.login() scrobbler.now_playing(artist, track) # now_playing auto flushes, apparently. don't call # flush here or it will throw an exception, which is not # what we want. elif type == PLAYED: # See: http://exhuma.wicked.lu/projects/python/scrobbler/api/public/scrobbler-module.html#login # if mimetype is wrong, length == 0 if song.length < 30: log.warn(u"song length %s" % song.length) # wait 60 seconds before re-trying # submission if error: if (time.time() - etime) < 60: break log.debug(u"scrobbling played %s %s %s %s" % (artist, track, album, song.length)) self.login() scrobbler.submit( artist, track, int( time.mktime( datetime.datetime.now().timetuple())), source=escape(u'P'), length=song.length, album=album) scrobbler.flush() except Exception as e: log.exception(u"scrobble error: %s" % e) # put it back scrobble_item.error = True scrobble_item.etime = time.time() self.queue.put(scrobble_item) except Queue.Empty: pass # AS API enforced limit -- do not change. time.sleep(10)