[Jsign-commits] CVS: pyweb AudioGalaxy.py,NONE,1.1
Brought to you by:
borillo
|
From: Ricardo B. D. <bo...@us...> - 2001-11-16 09:39:35
|
Update of /cvsroot/jsign/pyweb
In directory usw-pr-cvs1:/tmp/cvs-serv24504
Added Files:
AudioGalaxy.py
Log Message:
Cedido por Miguel!!
--- NEW FILE: AudioGalaxy.py ---
import urllib,re,sys,string,tempfile,os,httplib
class AudioGalaxy:
def __init__(self):
self.URL = "http://www.audiogalaxy.com"
self.home = self.URL + "/user/home.php?"
self.search_url = self.URL + "/list/searches.php?"
self.artistinfo = self.URL + "/list/artistInfo.php?"
self.song = self.URL + "/list/song.php?"
self.chooseversion = self.URL + "/list/chooseVersion.php?"
#la informacion del usuario debe ser leida de las cookies o
#pedida al usuario
self.username, self.password = self.readCookie()
tmp=tempfile.mktemp()
urllib.urlretrieve(self.home,tmp)
page = open(tmp).read()
os.remove(tmp)
a = re.findall("\<input type=\"hidden\" name=\"SID\" value=\"[0-9a-zA-Z]*\"\>",page)[0]
b = string.split(a,"value=\"")[1]
self.SID=str(b[:-2])
def readCookie(self):
lc=os.listdir('c:\windows\cookies')
for i in lc:
if string.find(i,"audiogalaxy") != -1:
f=open("c:\\windows\\cookies\\"+i)
c=f.read()
f.close()
username=re.findall("cookieUsername\n[^\n]+\n",c)[0]
password=re.findall("cookiePassword\n[^\n]+\n",c)[0]
return string.replace(username,"cookieUsername\n","")[:-1], string.replace(password,"cookiePassword\n","")[:-1]
def urlencode(self,str):
a={}
a["SID"]=self.SID
a["searchType"]="0"
a["searchStr"]=str
return urllib.urlencode(a)
def search(self,cad,maxMatches=None):
off=0
a={}
cad = string.replace(cad,"?","")
cad = string.replace(cad,"&"," ")
cad = string.replace(cad,"AND"," ")
cad = string.replace(cad,"(","")
cad = string.replace(cad,")","")
cad = string.replace(cad,".","")
cad = string.replace(cad," "," ")
url = self.search_url+self.urlencode(cad)
mode = []
codartist = ""
nmatches=0
while (off >= 0):
tmp=tempfile.mktemp()
if off != 0:
offset = "&offset="+str(off)
else:
offset = ""
if off == 0 or mode == []:
urllib.urlretrieve(url+offset,tmp)
else:
urllib.urlretrieve(self.artistinfo+"&r="+codartist+offset,tmp)
page = open(tmp).read()
os.remove(tmp)
if off == 0:
mode = re.findall("Found Exact Match",page)
lcodsong=re.findall("songIDs\[[0-9]+\]\.songID = [0-9]+;",page)
lqueued=re.findall("songIDs\[[0-9]+\]\.queued = [0-9]+;",page)
p=string.replace(page,"<span class='match'>","")
p=string.replace(p,"</span>","")
if (off == 0 and mode != []):
lcodartist=re.findall("name=\"artistID\" value=[0-9]+\>",p)
codartist=string.split(lcodartist[0],"value=")[1][:-1]
if len(lcodsong) <= 0:
off = -1
else:
if mode != []: #si ha hallado un acierto perfecto
lartist=re.findall("\<span class=\"artistName\"\>[^<]*\<\/span\>",page)
artist = string.split(lartist[0],">")[1][:-6]
lavail=re.findall("/images/list/a[0-4-]\.gif",page)
lsong=re.findall("class=song\>[^<]*\<\/a\>",p)
else:
lavail=re.findall("/images/list/[0-4z]_0\.gif",page)
lartist=re.findall("artistInfo\.php\?SID=[0-9a-zA-Z]+&r=[0-9]*&fromSearchClick=[0-9]*\"\>[^<]*\<\/a\>",p)
lsong=re.findall("song\.php\?SID=[0-9a-zA-Z]+&g=[0-9]*\" class=song\>[^<]*\<\/a\>",p)
for i in range(len(lcodsong)-1): #quizas sea -1
nmatches=nmatches+1
cod = string.split(lcodsong[i],"=")[1][1:-1]
queued = string.split(lqueued[i],"=")[1][1:-1]
if mode != []:
song=lsong[i][11:-4]
avail=lavail[i][-5]
else:
avail=lavail[i][-7]
artist = string.split(lartist[i],">")[1][:-3]
song = string.split(lsong[i],">")[1][:-3]
a[cod] = [string.upper(song),string.upper(artist),queued,avail,codartist]
if nmatches==maxMatches:
break
if nmatches==maxMatches:
break
if mode != []:
if len(lcodsong) <= 5:
off = -1
else:
off = off + 25
else:
if len(lcodsong) <= 5:
off = -1
else:
off = off + 10
return a,mode!=[]
def listVersions(self,code):
h=httplib.HTTP("www.audiogalaxy.com",80)
h.putrequest("GET","/list/chooseVersion.php?&g="+code)
h.putheader("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*")
h.putheader("Accept-Languaje","en-us")
#h.putheader("Accept-Encoding","gzip, deflate")
h.putheader("Accept-Charset","iso-8859-1,*,utf-8")
h.putheader("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)")
h.putheader("Host","www.audiogalaxy.com")
h.putheader("Content-type","application/x-www-form-urlencoded")
h.putheader("Connection","Keep Alive")
h.putheader("Cookie: $Version=\"1\"; SID="+self.SID+"; cookieUsername="+self.username+"; cookiePassword="+self.password+"; $Domain=\"audiogalaxy.com/\"")
h.endheaders()
ferrcode, errmsg, headers = h.getreply()
f=h.getfile()
p=f.read()
p=string.replace(p," ","")
s=re.findall("name=\"fileSetIDStr\" value=\"[a-zA-Z0-9]+\"\>",p)
s2=re.findall("[0-9]+,[0-9,]+",p)
s3=re.findall("[0-9]+kbps",p)
s4=re.findall("\>[0-9]+:[0-9]+\<",p)
s5=re.findall("\>[0-9]+\<",p)
a={}
for i in range(len(s)):
code=string.split(s[i],"value=\"")[1][:-2]
size=s2[i]
bitrate=s3[i][:-4]
time=s4[i][1:-1]
qty=s5[i][1:-1]
a[code] = size,bitrate, time, qty
return a
def queueSong(self,codSong,codVer):
tmp=tempfile.mktemp()
urllib.urlretrieve(self.chooseversion+"SID="+self.SID+"&action=queue&songID="+codSong+"&fileSetIDStr="+codVer,tmp)
page = open(tmp).read()
os.remove(tmp)
def filterUnavailableSong(self,l):
for i in l.keys():
if l[i][3] == "-" or l[i][3] == "z":
del l[i]
return l
def filterByName(self,l,name):
c=string.upper(name)
for i in l.keys():
s=l[i][0]+l[i][1]
if string.find(s,c) == -1:
del l[i]
return l
def filterByNames(self,l,lnames):
ln=[]
for i in lnames:
ln = ln + [string.upper(i)]
for i in l.keys():
s=l[i][0]+l[i][1]
for j in ln:
if string.find(s,c) != -1:
break
del l[i]
return l
def filterExactName(self,l,name):
n=string.upper(name)
for i in l.keys():
if l[i][0] != n and l[i][1] != n: #cancion y artista respectivamente
del l[i]
return l
def filterAnyExactName(self,l,lname):
ln=[]
for i in lnames:
ln = ln + [string.upper(i)]
artist = 0
song = 0
for i in l.keys():
s=l[i][0]
a=l[i][1]
for j in ln:
if j == s:
song = 1
elif j == a:
artist = 1
if artist==song==1:
break
if (artist!= 1 or song!=1):
del l[i]
return l
def listExactMatches(self,l,song,lnames,avail=1):
ln=[]
lmat=[]
for i in lnames:
ln = ln + [string.upper(i)]
sn = string.upper(song)
sn = string.replace(sn,"?","")
sn = string.replace(sn,"AND"," ")
sn = string.replace(sn,"&"," ")
sn = string.replace(sn,"(","")
sn = string.replace(sn,")","")
sn = string.replace(sn,".","")
sn = string.replace(sn," "," ")
artist = 0
song = 0
for i in l.keys():
s=l[i][0]
a=l[i][1]
d=l[i][3] #disponibiliad de la cancion
if d=="z" or d=="-":
d=-1
d=int(d)
s = string.replace(s,"?","")
s = string.replace(s,"AND"," ")
s = string.replace(s,"&"," ")
s = string.replace(s,"(","")
s = string.replace(s,")","")
s = string.replace(s,".","")
s = string.replace(s," "," ")
for j in ln:
if sn==s or sn==a:
song = 1
if j == a or j==s:
artist = 1
if artist==song==1 and d>=avail:
if not i in lmat:
lmat=lmat+[i]
return lmat
def listPartialMatches(self,l,song,lnames,avail=1):
ln=[]
lmat=[]
for i in lnames:
ln = ln + [string.upper(i)]
sn = string.upper(song)
sn = string.upper(song)
sn = string.replace(sn,"?","")
sn = string.replace(sn,"AND"," ")
sn = string.replace(sn,"&"," ")
sn = string.replace(sn,"(","")
sn = string.replace(sn,")","")
sn = string.replace(sn,".","")
sn = string.replace(sn," "," ")
artist = 0
song = 0
for i in l.keys():
s=l[i][0]
a=l[i][1]
d=l[i][3] #disponibiliad de la cancion
if d=="z" or d=="-":
d=-1
d=int(d)
s = string.replace(s,"?","")
s = string.replace(s,"AND"," ")
s = string.replace(s,"&"," ")
s = string.replace(s,"(","")
s = string.replace(s,")","")
s = string.replace(s,".","")
s = string.replace(s," "," ")
for j in ln:
if string.find(s,sn)!=-1 or string.find(a,sn)!=-1:
song = 1
if string.find(a,j)!= -1 or string.find(s,j)!= -1:
artist = 1
if artist==song==1 and d>=avail:
if not i in lmat:
lmat=lmat+[i]
return lmat
def selectExactMatch(self,l,song,lnames,avail=1):
ln=[]
for i in lnames:
ln = ln + [string.upper(i)]
sn = string.upper(song)
sn = string.upper(song)
sn = string.replace(sn,"?","")
sn = string.replace(sn,"AND"," ")
sn = string.replace(sn,"&"," ")
sn = string.replace(sn,"(","")
sn = string.replace(sn,")","")
sn = string.replace(sn,".","")
sn = string.replace(sn," "," ")
artist = 0
song = 0
for i in l.keys():
s=l[i][0]
a=l[i][1]
d=l[i][3] #disponibiliad de la cancion
if d=="z" or d=="-":
d=-1
d=int(d)
s = string.replace(s,"?","")
s = string.replace(s,"AND"," ")
s = string.replace(s,"&"," ")
s = string.replace(s,"(","")
s = string.replace(s,")","")
s = string.replace(s,".","")
s = string.replace(s," "," ")
for j in ln:
if sn==s or sn==a:
song = 1
if j == a or j==s:
artist = 1
if artist==song==1 and d>=avail:
return i
break
return -1
def selectPartialMatch(self,l,song,lnames,avail=1):
ln=[]
for i in lnames:
ln = ln + [string.upper(i)]
sn = string.upper(song)
sn = string.upper(song)
sn = string.replace(sn,"?","")
sn = string.replace(sn,"AND"," ")
sn = string.replace(sn,"&"," ")
sn = string.replace(sn,"(","")
sn = string.replace(sn,")","")
sn = string.replace(sn,".","")
sn = string.replace(sn," "," ")
artist = 0
song = 0
for i in l.keys():
s=l[i][0]
a=l[i][1]
d=l[i][3] #disponibiliad de la cancion
if d=="z" or d=="-":
d=-1
d=int(d)
s = string.replace(s,"?","")
s = string.replace(s,"AND"," ")
s = string.replace(s,"&"," ")
s = string.replace(s,"(","")
s = string.replace(s,")","")
s = string.replace(s,".","")
s = string.replace(s," "," ")
for j in ln:
if string.find(s,sn)!=-1 or string.find(a,sn)!=-1:
song = 1
if string.find(a,j)!= -1 or string.find(s,j)!= -1:
artist = 1
if artist==song==1 and d>=avail:
return i
break
return -1
def listExactVersions(self,codsong,time,bitrate,diftime=3,minqty=1):
l=self.listVersions(codsong)
lt=string.split(time,":")
tsec=int(lt[0])*60+int(lt[1])
a=[]
for i in l.keys():
if l[i][1] != bitrate:
continue
if int(l[i][3]) < minqty:
continue
ls=string.split(l[i][2],":")
sec=int(ls[0])*60+int(ls[1])
if (tsec>=(sec-int(diftime))) and (tsec <= (sec+int(diftime))):
a=a+[(i,l[i][3])]
return a
def selectExactVersion(self,codsong,time,bitrate,diftime=3,minqty=1):
a=self.listExactVersions(codsong,time,bitrate,diftime,minqty)
min=0
code=""
for i in a:
n=int(i[1])
if n >min:
min = n
code=i[0]
if code=="":
return -1
else:
return code
def getSong(self,artist,album,song,time,bitrate):
song = string.replace(song,"?","")
song = string.replace(song,"&"," ")
song = string.replace(song,"AND"," ")
song = string.replace(song,"(","")
song = string.replace(song,")","")
song = string.replace(song,".","")
song = string.replace(song," "," ")
if string.find(song,"/") != -1:
ls=string.split(song,"/")
p1=string.strip(ls[0])
p2=string.strip(ls[1])
list,cod = self.search(p1,10)
list2,cod = self.search(p2,10)
for i in list2.keys():
list[i] = list2[i]
matches=self.listExactMatches(list,p1,[p2,artist,album,artist+" "+album,album+" "+artist])
matches= matches+[self.listExactMatches(list,p2,[p1,artist,album,artist+" "+album,album+" "+artist])]
if matches == []:
matches=self.listPartialMatches(list,p1,[p2,artist,album,artist+" "+album,album+" "+artist])
matches=matches+[self.listPartialMatches(list,p2,[p1,artist,album,artist+" "+album,album+" "+artist])]
#realizamos una segunda busqueda mas restrictiva
if matches == []:
list,cod = self.search(artist+" "+p1,10)
list2,cod = self.search(artist+" "+p2,10)
for i in list2.keys():
list[i] = list2[i]
matches=self.listExactMatches(list,p1,[p2,artist,album,artist+" "+album,album+" "+artist])
matches= matches+[self.listExactMatches(list,p2,[p1,artist,album,artist+" "+album,album+" "+artist])]
if matches == []:
matches=self.listPartialMatches(list,p1,[p2,artist,album,artist+" "+album,album+" "+artist])
matches=matches+[self.listPartialMatches(list,p2,[p1,artist,album,artist+" "+album,album+" "+artist])]
else:
list, cod = self.search(song,10)
matches=self.listExactMatches(list,song,[artist,album,artist+" "+album,album+" "+artist])
if matches == []:
matches=self.listPartialMatches(list,song,[artist,album,artist+" "+album,album+" "+artist])
#realizamos la busqueda restrictiva
if matches == []:
list, cod = self.search(artist+" "+song,10)
matches=self.listExactMatches(list,song,[artist,album,artist+" "+album,album+" "+artist])
if matches == []:
matches=self.listPartialMatches(list,song,[artist,album,artist+" "+album,album+" "+artist])
for i in matches:
ver=ag.selectExactVersion(i,time,bitrate)
if ver!=-1:
ag.queueSong(i,ver)
sys.stdout.write("OK\n")
return 0
sys.stdout.write("ERROR\n")
return -1
def getAlbum(self,lsongs,artist,album,bitrate):
c=0
for i in lsongs:
sys.stdout.write(string.ljust("Get Song--> "+i[2],60))
song=i[2]
song=string.replace(song,"AND"," ")
c = c + self.getSong(artist,album,song,i[1],bitrate)
return (c)
def getAlbumFromDB(self,artist,album,bitrate):
f=freedb()
l=f.searchDiscs(album)
l=f.filterDiscs(l,artist,album)
cnt=0
ldisc=[]
for i in l:
d,t,nt=f.getDiscInfo(i[0],i[1])
print string.rjust(str(cnt),2),i[2]," Tracks:",string.rjust(str(nt),2)," Total Time:",t
cnt=cnt+1
ldisc=ldisc+[(d,t,nt)]
if ldisc != []:
r=-1
while (r<0 or r>cnt):
sys.stdout.write("\nElige un disco (s+num para descripcion): ")
c=sys.stdin.readline()
if string.upper(c[0]) == "S":
c=string.replace(c,"s","")
c=string.replace(c,"S","")
for i in ldisc[int(c)][0]:
#print i
sys.stdout.write(i[0]+" - "+i[1]+" - "+i[2]+"\n")
r=-1
else:
r=int(c[:-1])
if self.getAlbum(ldisc[r][0],artist,album,bitrate) == 0:
print "\nDisco preparado para bajarse."
else:
print "\nNo se ha podido encontrar el disco completo."
else:
print "\nNo se ha encontrado el disco en la BD."
def getAlbumFromFile(self,file,bitrate):
f=open(file)
t=f.readline()
s=string.split(t,"/")
artist=string.strip(s[0])
album=string.strip(s[1])
t=f.readline()
t=f.readline()
ltracks = []
while t != "":
lt = string.split(t,"-")
nt=string.strip(lt[0])
time=string.strip(lt[1])
name=string.strip(lt[2])
ltracks = ltracks + [(nt,time,name)]
t=f.readline()
if ltracks != []:
if self.getAlbum(ltracks,artist,album,bitrate) == 0:
print "Disco preparado para bajarse."
else:
print "No se ha podido encontrar el disco completo."
else:
print "Error al leer el fichero."
############################### Fin Clase AudioGalaxy ###################
############################### Clase freedb ############################
class freedb:
def __init__(self):
self.URL="http://www.freedb.org/"
def searchDiscs(self, cad):
cad=string.replace(cad," ","+")
tmp=tempfile.mktemp()
urllib.urlretrieve(self.URL+"freedb_search.php?words="+cad+"&allfields=NO&fields=artist&fields=title&allcats=YES&grouping=none",tmp)
page = open(tmp).read()
os.remove(tmp)
p = string.replace(page," ","")
p = string.replace(p,"<font size=-1>","")
discos = re.findall("cat=[A-Za-z]+&id=[a-zA-Z0-9]+\"\>[^<]+\<",p)
oldname=""
lista=[]
for i in discos:
cat=string.split(i,"&id")[0][4:]
a = string.split(i,"id=")[1]
id=string.split(a,"\">")[0]
name=string.upper(string.split(i,"\">")[1][:-1])
name=string.replace(name,"&","AND")
for i in name:
if i in string.digits:
name = oldname
else:
oldname=name
break
lista = lista + [(id,cat,name)]
return lista
def filterDiscs(self,list,artist,disc):
c = artist + " / " + disc
c=string.upper(c)
l2=[]
for i in range(len(list)-1):
if list[i][2] == c:
l2=l2+[list[i]]
return l2
def getDiscInfo(self,code,cat):
tmp=tempfile.mktemp()
urllib.urlretrieve(self.URL+"freedb_search_fmt.php?cat="+cat+"&id="+code,tmp)
page = open(tmp).read()
os.remove(tmp)
total_time=re.findall("total time: [^<]+\<",page)[0][12:-1]
tracks = re.findall("\<td valign=top\>[ 0-9]+\.\</td\>\<td valign=top\> [0-9]+:[0-9]+\</td\>\<td\>\<b\>[^<]+\</b\>",page)
ntracks = len(tracks)
cnt=0
a=[]
for i in tracks:
c=string.replace(i,"<td valign=top>","")
t=re.findall("[0-9]+:[0-9]+",c)[0]
n=string.split(c,"<b>")[1]
n=n[:-4]
cnt = cnt + 1
a=a+[(string.zfill(cnt,2),t,n)]
return a,total_time,ntracks
if __name__ == "__main__":
ag=AudioGalaxy()
if len(sys.argv) <3:
sys.stdout.write("Artista: ")
artist=sys.stdin.readline()[:-1]
sys.stdout.write("Album: ")
album=sys.stdin.readline()[:-1]
sys.stdout.write("Bitrate: ")
bitrate=sys.stdin.readline()[:-1]
ag.getAlbumFromDB(artist,album,bitrate)
else:
if string.upper(sys.argv[1]) == "-R":
f = sys.argv[2]
try:
df=open(f,"r")
df.close
sys.stdout.write("Bitrate: ")
bitrate=sys.stdin.readline()[:-1]
ag.getAlbumFromFile(f,bitrate)
except:
print "El Fichero",f,"no existe."
else:
print "Parametros incorrectos"
#Listado de funciones de AudioGalaxy
#
#readCookie()
#search(self,cad,maxMatches=None)
#listVersions(self,code)
#queueSong(self,codSong,codVer)
#filterUnavailableSong(self,l)
#filterByName(self,l,name)
#filterByNames(self,l,lnames)
#filterExactName(self,l,name)
#filterAnyExactName(self,l,lname)
#listExactMatches(self,l,song,lnames,avail=1)
#listPartialMatches(self,l,song,lnames,avail=1)
#selectExactMatch(self,l,song,lnames,avail=1)
#selectPartialMatches(self,l,song,lnames,avail=1)
#listExactVersions(self,codsong,time,bitrate,diftime=3,minqty=1)
#selectExactVersion(self,codsong,time,bitrate,diftime=3,minqty=1)
#getSong(self,artist,album,song,time,bitrate)
#getAlbum(self,lsongs,artist,album,bitrate)
#getAlbumFromDB(self,artist,album,bitrate)
#Listado de funciones de freedb
#
#searchDiscs(self, cad)
#filterDiscs(self,list,artist,disc)
#getDiscInfo(self,code,cat)
|