#! /usr/bin/env python
# -*- coding: utf-8 -*-

import urllib
import re
import os
from optparse import OptionParser
from getpass import getpass
import sys

lonet_page = 'http://www.lo-net2.de/ww3ee/100005.php'
folder_pattern = '<a href="([^"]*)"><img src="\.\./pics/fx_folder.gif">([^<]*)'
file_pattern = 'OpenPopUp\(\'([^\']*)\'\);"><img src="\.\./pics/i_download.gif" [^>]*></a></td><td width="99%"><img src="[^"]*">&#160;([^<]*)</td>'
group_pattern = '<option class="top_option" value="100009.php\?sid=(\d+)">([^<]*)</option>'
dl_link_pattern = '<a href="//([^"]+)" id="a_down">[^<]+</a>'

htmlspecials = {u"&Auml;" : u"Ä", u"&auml;" : u"ä", u"&Uuml;" : u"Ü", u"&uuml;" : u"ü", u"&Ouml;" : u"Ö", "&ouml;" : u"ö", u"&szlig;" : u"ß"}

#Command Line Options parsen
parser = OptionParser()
parser.add_option("-u","--update",action="store_true",dest="update",default=False,help="Nur neue Dateien herrunterladen")
parser.add_option("-p","--password", action="store", type="string", dest="password")
parser.add_option("-d","--download-dir",action="store",type="string",dest="dir",metavar="PATH")
parser.set_usage("%s [options] <username>" % (os.path.basename(sys.argv[0])))
(options, args) = parser.parse_args()
if not args:
    parser.error("Bitte den LoNet-Usernamen angeben!")
options.username = args[0]

if not options.password:
    options.password = getpass('Passwort:')
if not options.dir:
		options.dir = os.getcwd()

#Funktionen definieren
def replace_htmlspecials(was):
    for f,t in htmlspecials.iteritems():
        asd = unicode(was,"iso-8859-1")
        asd = asd.replace(f,t)
    return asd

def dl_files(page_content,dl_dir):
    files = []
    for match in re.findall(file_pattern,page_content):
        files.append({'name' : match[1], 'link' : match[0]})
    #Dateien herunterladen
    for file in files:
        file['name'] = replace_htmlspecials(file['name'])

        if options.update and os.path.exists(os.path.join(dl_dir,file['name'])):
            continue
        
        #urllib.urlretrieve("http://www.lo-net2.de/ww3ee/" + file['link'],os.path.join(group_dir,file['name']+".html"))
        dl_content = urllib.urlopen("http://www.lo-net2.de/ww3ee/" + file['link']).read()
        
        #DL Link aus dem Popup herausgrabben
        dl_link = re.search(dl_link_pattern,dl_content).group(1)
        dl_link = "http://" + dl_link 

        print "Lade %s herunter" % (file['name'])
        urllib.urlretrieve(dl_link,os.path.join(dl_dir,file['name']))

######################
# Jetzt gehts los... #
######################

#Login Seite laden
params = urllib.urlencode({"login_login" : options.username,"login_password": options.password})
#urllib.urlretrieve(lonet_page,"test.html",data=params)
page = urllib.urlopen(lonet_page,params)
page_content = page.read()

#War der Login erfolgreich?
if -1 == page_content.find("Status"):
    print "Login fehlgeschlagen"
    sys.exit()
else:
    print "Login erfolgreich!"

#Gruppen laden
groups = []

for match in re.findall(group_pattern,page_content):
    groups.append({'name': match[1], 'link_id': match[0]}) 

#Dateiablage für die Gruppen aufrufen
for group in groups:
    group['name'] = replace_htmlspecials(group['name'])
    print "Gruppe: %s" % (group['name'])
    #Gruppenverzeichnisse anlegen
    group_dir = os.path.join(options.dir,group['name'])
    if not os.path.exists(group_dir):
        os.mkdir(group_dir)
    #urllib.urlretrieve("http://www.lo-net2.de/ww3ee/125520.php?sid="+group['link_id'],os.path.join(group_dir,group['name']+".html"))
    page_content = urllib.urlopen("http://www.lo-net2.de/ww3ee/125520.php?sid="+group['link_id']).read()

    folders = []
    for match in re.findall(folder_pattern,page_content):
        folders.append({'name' : match[1], 'link' : match[0]})
        
    #Dateiliste abrufen (Root-Folder der Gruppe) und herrunterladen
    dl_files(page_content,group_dir)
        
    #Dateiliste für die Subfolder abrufen und Dateien herrunterladen
    for folder in folders:
        folder['name'] = replace_htmlspecials(folder['name'])
        print "Wechsele Verzeichnis zu %s" % (folder['name'])        
        subdir = os.path.join(group_dir,folder['name'])
        if not os.path.exists(subdir):
            print "created"
            os.mkdir(subdir)
        page_content = urllib.urlopen("http://www.lo-net2.de/" + folder['link']).read()
        dl_files(page_content,subdir)

print "Done"

