#!/usr/bin/env python

from BeautifulSoup import BeautifulSoup
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
import urllib, tempfile, os, commands, mutagen.id3

pl_base_path = 'http://www.qwartz.org/mambots/content/plugin_media_gallery/media_dir.php?album='
file_base_path = 'http://www.qwartz.org/images/albums/'

urls = ['http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Album/menu-id-114.html',
        'http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Recherche/Experimentation/menu-id-118.html',
        'http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Decouverte/menu-id-118.html',
        'http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Compilation/menu-id-118.html',
        'http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Anthologie/menu-id-118.html',
        'http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Titre/menu-id-118.html',
        'http://www.qwartz.org/index.php?/Edition-6/Qwartz-6-Dancefloor/Clubbing/menu-id-118.html']

pls = []
for u in urls:
    doc = BeautifulSoup(urllib.urlopen(u).read())
    for a in doc.findAll('a'):
        for at in a.attrs:
            if at[0] == 'onclick' and at[1].startswith("loadFile('"):
                pl_path = at[1].split(pl_base_path)[1].split("'}")[0]
                pl_url = pl_base_path + urllib.quote(pl_path)
                if not (pl_path, pl_url) in pls: pls.append((pl_path, pl_url))
    for s in doc.findAll('script'):
        if len(s.contents) and s.contents[0].find(pl_base_path)!= -1:
            pl_path = s.contents[0].split(pl_base_path)[1].split('",')[0]
            pl_url = pl_base_path + urllib.quote(pl_path)
            if not (pl_path, pl_url) in pls: pls.append((pl_path, pl_url))

for pl_path, pl_url in pls:

    pl_path = pl_path.replace('  ', ' ')
    pl_path = pl_path.replace(' - ', '____')
    pl_path = pl_path.replace('- ', ' - ')
    pl_path = pl_path.replace('____', ' - ')
    if pl_path.find(' - ') != -1:
        alb_path = '/'.join(pl_path.split('/')[1:])
        alb_path_split = alb_path.split(' - ')
        if len(alb_path_split) == 2: album, author = alb_path_split
        else: author, album, label = alb_path_split
    else:
        album = pl_path
        author = ''

    if not os.path.isdir(pl_path): os.makedirs(pl_path)

    pl = BeautifulSoup(urllib.urlopen(pl_url).read())

    count = 1
    for t in pl.findAll('track'):
        location = t.findAll('location')[0].contents[0].split(file_base_path)[1]
        location = file_base_path +  urllib.quote(location)
        attrs = [location]
        num = str(count)
        if len(num) == 1: num = '0' + num
        attrs.append(num)
        creator = t.findAll('creator')[0].contents
        if len(creator):
            creator = creator[0].replace('/', '-')
            attrs.append(creator)
        else: creator = None
        title = t.findAll('title')[0].contents[0].replace('/', '-')
        attrs.append(title)
        count += 1
        t_path = os.path.join(pl_path, ' - '.join(attrs[1:]).replace('  ', ' ')+'.mp3')
        if not os.path.isfile(t_path):
            print 'Downloading ' + t_path
            tmp = tempfile.mkstemp(dir='./')[1]
            if commands.getstatusoutput('wget %s -O %s' % (attrs[0], tmp))[0] == 0:
                os.rename(tmp, t_path)
                if not creator: creator = author
                tag = MP3(t_path, ID3=EasyID3)
                try: tag.add_tags(ID3=EasyID3)
                except mutagen.id3.error: pass        
                tag['title'] = title
                tag['artist'] = creator
                tag['album'] = album
                tag.save()
                print 'Done.'
            else:
                os.unlink(tmp)
                print 'Failed.'
        else: print 'Skipping ' + t_path
