def mp3_info(path): info = {} if os.stat(path)[6] == 0: return {} f = open(path,'rb') f.seek(-128,2) id3block = f.read(128) f.close() if id3block[0:3] == 'TAG': def strip_zeros(str): index = string.find(str,'\000') if index == -1: return string.strip(str) return string.strip(str[0:index]) info['music_title'] = utility.force_unicode(strip_zeros(id3block[3:33])) info['music_artist'] = utility.force_unicode(strip_zeros(id3block[33:63])) info['music_album'] = utility.force_unicode(strip_zeros(id3block[63:93])) off = 0 eof = 0 h = 0 i = 0 tot = 4096 fp = open(path) word = getword(fp, off) if off==0: id3v2 = get_v2head(fp) if id3v2: off = off + id3v2['tagsize'] tot = tot + off word = getword(fp, off) while 1: h = get_head(word) #h=get_newhead(word) if not h: break off=off+1 word = getword(fp, off) if off>tot: #print "BAD FILE", path, os.stat(path)[6] return info if is_mp3(h): break fp.seek(0, 2) eof = fp.tell() try: fp.seek(-128, 2) except IOError, reason: return info
def on_insert(self, _w, iter, str, length): str = utility.force_unicode(str) if not self.buffer_user_active: self._on_insert_helper(iter.get_offset(),str,length) return if self.mode == 'direct_mode': self.buffer.emit_stop_by_name("insert-text") self.handler.handle_direct_mode(str) elif self.mode == 'command_mode': if iter.get_offset() < self.prompt_end: self.buffer.emit_stop_by_name("insert-text") self.buffer.place_cursor(self.buffer.get_iter_at_offset(self.prompt_end)) self.buffer.insert(self.buffer.get_iter_at_offset(self.prompt_end), str) return self._on_insert_helper(iter.get_offset(),str,length) elif self.mode == 'edit_mode': if iter.get_offset() < self.edited_field.start: self.buffer.emit_stop_by_name("insert-text") self.buffer.place_cursor(self.buffer.get_iter_at_offset(self.edited_field.start)) return if iter.get_offset() > self.edited_field.end: self.buffer.emit_stop_by_name("insert-text") self.buffer.place_cursor(self.buffer.get_iter_at_offset(self.edited_field.start)) return change = len(str) self.edited_field.offset_successors(change) self.edited_field.end += change else: print "unknown mode"
def on_key_release(self, _w,_e): if not self.app.config['augmented_text']: return if not self.key_presses.has_key(_e.keyval): return press_time, offset = self.key_presses[_e.keyval] del self.key_presses[_e.keyval] string = utility.force_unicode(_e.string) if not string or \ ord(string) < 32: return if offset < 0 or offset >= len(self.augmentation): return if string.isalpha(): interval = (_e.time - press_time) / 1000.0 level = int( interval*4 *255) if level < 0: level = 0 if level > 255: level = 255 else: level = 128 tag = "aug" + str(level/16) iter1 = self.buffer.get_iter_at_offset(self.prompt_end+offset) iter2 = iter1.copy() iter2.forward_char() self.augmentation = self.augmentation[:offset] + \ chr(level) + \ self.augmentation[offset+1:] self.buffer.remove_all_tags(iter1,iter2) self.buffer.apply_tag_by_name(tag,iter1,iter2)
def write(self, text, app=None, action=None): text = utility.force_unicode(text) symbols = re.split(r'( |\n|\t)', text) for symbol in symbols: this_action = action tags = ['default'] stripped_symbol = strip_symbol(symbol) if not this_action and \ (stripped_symbol[:5] == 'http:' or \ stripped_symbol[:4] == 'ftp:' or \ stripped_symbol[:4] == 'www.'): def this_action(symbol=stripped_symbol): try: utility.browse_url(symbol) except error.Error,err: app.show_error(err) if not this_action and \ stripped_symbol[:12] == 'circle-file:' and app: def this_action(symbol=stripped_symbol,app=app): try: import search search.search_for_name(hash.url_to_hash(symbol),'Files matching url', app.node,app) except error.Error,err: app.show_error(err)
def ogg_info(path): info = {} try: import ogg.vorbis vf = ogg.vorbis.VorbisFile(path) info['time'] = int( vf.time_total(0)*100) vc = vf.comment() vi = vf.info() info['frequency'] = vi.rate info['bitrate'] = vf.bitrate(0)/1024 for key, val in vc.items(): if key == 'TITLE': info['music_title'] = utility.force_unicode(val) elif key == 'ARTIST': info['music_artist'] = utility.force_unicode(val) elif key == 'ALBUM': info['music_album'] = utility.force_unicode(val) finally: return info
def do_play(self,param): import searcher for item in self.file_list: if utility.force_unicode(item.item['filename']) == param[0]: leaf=item break else: self.show( 'unknown file %s.\n'%param[0],'grey') return try: searcher.set_leaf_player(leaf,self.app) playername=leaf.players.items()[0][0] except: self.show( 'I do not know how to play this file.\n','grey') return if playername: searcher.play_file(leaf,playername,self.app)
def update_info(self): circle_gtk.check_is_gtkthread() if self.playing_row!=-1 and self.playlist: player = self.playlist[self.playing_row] else: player= self.player if player: filename=os.path.basename(player.filename) title = player.info.get('music_title') artist = player.info.get('music_artist') album = player.info.get('music_album') frequency = player.info.get('frequency') bitrate = player.info.get('bitrate') time = player.info.get('time') else: filename='' title='' artist='' album='' frequency='' bitrate='' time='' if title: str = title+' by '+artist else: str = filename self.title_label.set_text(utility.force_unicode(str)) self.title_label.show() str="" if frequency: str= str + "%dHz "%frequency if bitrate: str = str + " %dkb/s"%bitrate if time: str = str + " %dmin%02ds "% ((time/100/60),((time/100)%60)) self.bitstream_label.set_text(str) self.bitstream_label.show()
def do_get(self, param): """downloads a list of files from the last search results""" download_list=[] if param[0]=='*': self.lock.acquire() for item in self.file_list: if item.item['type']=='file': download_list.append(item) self.lock.release() else: for item in self.file_list: if utility.force_unicode(item.item['filename']) in param: download_list.append(item) if not download_list: self.show( 'unknown file %s.\n'%param[0],'grey') return last_field=self.get_field() first_field=self.get_field_before(last_field) bad_list = [ ] dir_list = [ ] size=0 for item in download_list: if item.item['type'] == 'file': downloader=self.app.file_server.download( item.item,item.sources,self.app.config['download_dir'],no_overload=1) if downloader: size=size+item.item['length'] else: bad_list.append(item) elif item.item['type'] == 'directory': item.download(self.app) dir_list.append(item) else: raise error.Error("bug") if not download_list: str=_('The list of files is empty.\n') elif len(download_list)-len(bad_list)==0: str=_('Not downloading.\n') elif len(download_list)-len(bad_list)==1: if dir_list: str=_('Downloading 1 directory. \n') else: str=_('Downloading 1 file. \n') else: str=_('Downloading %d files. \n')%(len(download_list)-len(bad_list)) if bad_list: if len(bad_list)==len(download_list): str=_('Requested files are already in your directory. ')+str elif len(bad_list)==1: str=_('1 file already in your directory. ')+str else: str=_('%d files already in your directory. ')%len(bad_list)+str first_field.show(str,['grey']) first_field.close() if size: last_field.show(_('Total %s.\n'%utility.human_size(size)),['grey']) last_field.close()
def append_song(self, info, play_now, downloader=None, field=None): """ appends song. if play_now, stops current player and starts this one. method called by other classes """ circle_gtk.check_is_gtkthread() if info.get('local_path'): f = info['local_path'] else: f = downloader.filename size = info.get('length') # first check if already in playlist for i in range(len(self.playlist)): if self.playlist[i].filename == f: self.selected_row=i self.playing_row=i self.playlist[i].field=field if play_now: if self.player != self.playlist[i]: self.stop_music() self.player = self.playlist[i] self.play_row() else: if not self.player.playing: self.play_row() return if play_now: self.stop_music() self.player=None mime=info.get('mime') extension = string.split(f, ".")[-1] lext = string.lower(extension) if mime=='audio/x-mp3' or lext == 'mp3': player=Mp3Player(f,downloader,size,field) elif mime=='audio/x-ogg' or lext == 'ogg': try: import ogg.vorbis import linuxaudiodev except: raise error.Error('I do not know how to play ogg files.\n'+ 'Please install the python libraries for Ogg Vorbis:\n'+ 'http://www.andrewchatham.com/pyogg/') try: player=LADPlayer(f,downloader,size,field) except: raise error.Error('Cannot play %s: file not found'%f) else: raise error.Error('Cannot play : unrecognized file format') player.info = info if info.get('time'): player.time = float(info['time'])/100 else: player.time = None if play_now: self.playlist.insert(0,player) self.list.insert(0,[utility.force_unicode(os.path.basename(f)),'*','']) self.list.set_text(0,1,'0') self.selected_row=0 self.playing_row=0 self.play_row() else: self.playlist.append(player) self.list.append([utility.force_unicode(os.path.basename(f)),'-',''])