#!/usr/bin/python

from ftplib import FTP

class codeinaFtp:
    def __init__ (self, ftpurl):
        self.host = ftpurl.split('/')[0]
        self.path = '/'+'/'.join (ftpurl.split('/')[1:])
        self.open_ftp ()
        repos = self.list_files_in_folder (self.path)
        files = []
        for repo in repos:
            files += self.get_gstreamer_files_from (repo)
        for file in files:
            print 'wget ftp://'+self.host+file

    def open_ftp (self):
        self.f = FTP(self.host)
        self.f.login ()

    def list_files_in_folder (self, path):
        self.f.cwd (path)
        folders = self.f.nlst ()
        list = []
        for folder in folders:
                list.append(path+'/'+folder)
        return list

    def get_gstreamer_files_from (self, path):
        self.f.cwd(path)
        files = self.f.nlst ()
        list = []
        for file in files:
            for item in ['gstreamer0.10', 'bluez-utils', 'gnonlin', 'gst-farsight', 'istanbul']:
                if item in file:
                    list.append (path+'/'+file)
                    continue
        return list

    def close_ftp (self):
        try:
            self.f.close ()
        except:
            pass

    def __del__ (self):
        self.close_ftp ()

if __name__ == '__main__':
    import sys
    # use: ./get_plf_rpms ftp.free.fr/pub/Distributions_Linux/plf/mandriva/2008.0/free/release/binary/
    connection = codeinaFtp (sys.argv[1])
