#!/usr/bin/python # mp3get.py - mp3get client for use on SUBnet # Copyright (C) 2001 Emil Styrke # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # requires smbclient and egrep import os, sys,time,string from gtk import * mp3list = {} currentDownload = None def OneSecTimer(): global currentDownload if currentDownload: time = currentDownload["time"] size = currentDownload["size"] g = os.waitpid(0, os.WNOHANG) if g != (0,0): cb = currentDownload["callback"] currentDownload = None prog.set_percentage(0) print g stbar.pop(1) if g[1] > 0: stbar.push(1, "Underprocessen misslyckades") else: stbar.push(1, "Nedladdningen är klar") if cb: cb("fulhack") else: try: done = os.stat(os.path.expanduser(conf["downloaddir"])+"/"+currentDownload["file"])[6] except OSError: currentDownload["time"] = 0 # time elapsed done = 0 else: currentDownload["time"] += 1 prog.set_percentage(1.0*done/size) delta = done - currentDownload["completed"] currentDownload["completed"] = done stbar.pop(1) stbar.push(1, "%d%% of %s @ %d Kb/s" % (100.0*done/size, currentDownload["file"], delta/1024)) return 1 def getfile(path, sz = -1, callback = None, localpath=None): global currentDownload if path[:2] != "//": raise "Bad SMB path", path if localpath == None: localpath = conf["downloaddir"] # Translate the filename to cp437 path = unicode(path, "latin1").encode("cp437") elements = path[2:].split("/") host = elements[0] share = elements[1] dir = "\\".join(elements[2:-1]) file = elements[-1] size = -1 found = 0 # find out the size of the file cmd = 'smbclient "\\\\\\\\%s\\\\%s" -N -c "cd \\"%s\\";ls"' % (host, share, dir) print cmd for i in os.popen(cmd): if i.find(file) > 0: try: size = int(i.split()[-6]) except ValueError: size = -1 else: found = 1 if size == -1: size = sz if size == -1: size = 1 print "Size:", size cmd = ('smbclient', '\\\\%s\\%s' % (host, share), "-E", '-N', '-c', 'lcd "%s";cd "%s";get "%s"' % (os.path.expanduser(localpath), dir, file)) print cmd,cmd[1] currentDownload = {"host": host, "share": share, "dir": dir, "file": file, "size": size, "time": 0, "completed": 0, "callback": callback} return os.spawnv(os.P_NOWAIT, "/usr/bin/smbclient", cmd) get_list = 0 def getlist(foo = None): global get_list if foo == "fulhack": get_list = 2 if get_list == 1: return if get_list == 0: getfile("//perry/mp3 listan/SUBnets MP3Lista.txt", callback = getlist, localpath=conf["listdir"]) get_list = 1 return get_list = 0 f = open(os.path.expanduser(conf["listdir"])+"/SUBnets MP3Lista.txt") g = open(os.path.expanduser(conf["listdir"])+"/SUBnets MP3Lista - Filnamn.txt", "w") first = 0 total = os.stat(os.path.expanduser(conf["listdir"])+"/SUBnets MP3Lista.txt")[6] if total == 0: total = 1 stbar.push(1, "Konverterar listan.. (detta tar ett tag)") done = 0 for i in f.xreadlines(): done += len(i) if i[:5] == "---\\\\": curdir = i[3:i.rfind("---")] first = 1 else: if first == 0: continue if i[:3] == "---": continue size = i[:12] name = i[12:] size=size.strip() name=name.rstrip() g.write(size + " " + curdir + name + '\n') prog.set_percentage(done/total) mainiteration(0) prog.set_percentage(0) stbar.pop(1) stbar.push(1, "Klart") f.close() g.close() os.remove(os.path.expanduser(conf["listdir"])+"/SUBnets MP3Lista.txt") def search(type = "and"): terms = searcharea.get_text().split() # Rensa listan först searchlist.clear() matchlist = [] if not os.path.exists(os.path.expanduser(conf["listdir"]) + "/SUBnets MP3Lista - Filnamn.txt"): stbar.push(1, "Listan finns inte") return cmd = 'cat %s/SUBnets\\ MP3Lista\\ -\\ Filnamn.txt' % conf["listdir"] for i in terms: cmd += '| egrep -i "%s"' % i print cmd match = 0 stop = 0 for i in os.popen(cmd).xreadlines(): if stop: continue size = i[:i.find("\\\\") - 1] name = i[len(size) + 1:-1] size.strip() path = name[:name.rfind("\\") + 1] name = name[len(path):] size = size.replace("\xa0", "").replace(" kB", "").strip() try: size = int(size)*1024 except ValueError: pass searchlist.append((name, str(size), path)) match += 1 if match >= 2000: searchlist.append(("--- För många träffar", "---", "---")) stop = 1 print "found",match,"matches" return scol = -1 sorder = -1 def colclicked(foo, col): global scol, sorder if scol == col: if sorder == -1: foo.set_sort_type(SORT_ASCENDING) sorder = 1 else: foo.set_sort_type(SORT_DESCENDING) sorder = -1 else: scol = col foo.set_sort_column(col) foo.set_sort_type(SORT_ASCENDING) sorder = 1 foo.sort() def rowclicked(list, row, col, event): if event.type == 5: #double-click getfile(list.get_text(row, 2).replace("\\", "/")+list.get_text(row, 0), int(list.get_text(row, 1))) def kpfunc(foo, bar): print dir(bar) def main(): global searcharea, searchlist, prog,stbar, conf try: f = open(os.path.expanduser("~/.pymp3get")) except IOError: f = open(os.path.expanduser("~/.pymp3get"), "w") f.write('{ "listdir": "~/mp3get", # Plats för mp3listfilen. Katalogen måste existera\n') f.write(' "downloaddir": "~/mp3get", # Plats för nedladdade filer. Katalogen måste existera\n') f.write(' "dontrun": 1 ### Ta bort denna rad när du har gått igenom inställningarna\n') f.write('}') f.close() print "Skapade konfigurationsfil ~/.pymp3get" print "Du måste editera den för att mp3get.py ska starta" sys.exit(0) conf = eval(f.read()) if conf.has_key("dontrun"): print "Du måste editera ~/.pymp3get för att mp3get.py ska starta" sys.exit(0) window = GtkWindow(WINDOW_TOPLEVEL) window.connect("destroy", mainquit) mainbox = GtkVBox(0) mainbox.set_border_width(3) searchbox = GtkHBox(0, 3) label = GtkLabel("Sök:") label.show() searcharea = GtkEntry() searcharea.show() searcharea.connect("activate", search) searchbutton = GtkButton("Sök") searchbutton.connect("clicked", search) searchbutton.set_usize(100, 0) searchbutton.show() searchbox.pack_start(label, 0, 0) searchbox.pack_start(searcharea, 1, 1) searchbox.pack_start(searchbutton, 0, 0) searchbox.show() scroll = GtkScrolledWindow() searchlist = GtkCList(3, ("Namn", "Storlek", "Plats")) searchlist.set_column_auto_resize(0, 1) searchlist.set_column_auto_resize(1, 1) searchlist.connect("click-column", colclicked) searchlist.connect("select-row", rowclicked) searchlist.show() scroll.add(searchlist) scroll.show() w = GtkHButtonBox() quitbutton = GtkButton("Avsluta") quitbutton.connect("clicked", mainquit) quitbutton.show() w.add(quitbutton) refresh = GtkButton("Uppdatera lista") refresh.connect("clicked", getlist) refresh.show() w.add(refresh) w.show() stbar = GtkStatusbar() stbar.set_homogeneous(0) stbar.show() prog = GtkProgressBar() prog.set_usize(100, 0) prog.show() stbar.pack_start(prog, 0, 0) mainbox.pack_start(searchbox, 0, 0, 3) mainbox.pack_start(scroll,1,1, 3) mainbox.pack_start(w, 0, 0, 3) mainbox.pack_start(stbar, 0, 0, 3) mainbox.show() window.add(mainbox) window.show() timeout_add(1000, OneSecTimer) mainloop() sys.exit() if __name__ == "__main__": main()