<------------to_download.txt file ---------------> slackware-10.2-install-d1.iso slackware-10.2-install-d2.iso slackware-10.2-source-d3.iso slackware-10.2-source-d4.iso <--------------main python script file-------------> #!/usr/bin/env python import os, sys from ftplib import FTP """ Little tool to automatisate and help to download sceneries of Flight Gear Flight Simulator see http://www.flightgear.org/ This script is ideal for some people limited to an month download limit and for those who want all the sceneries. The goal of this script is to automatisate the download every x day (at night) y sceneries of the fgfs ftp server. Like in my case with my ISP, 10 MB downloaded at night is counted as 5 MB. Define the different variables before executing this script! Otherwise it probably not work like expected. Place this script as an cron job task and give it the job to execute it every 2 days for example. Similar is possible with the 'tasks' on Microsoft Windows. Author: David Van Mosselbeen Email: davidDOTvanDOTmosselbeenATtelenetDOTbe Version: 0.1 Date: 07/26/2006 ########## # USAGE: # ########## #To get the files... python sceneries_downloader.py # FUTURE ONE currently not yet implemented ########################################## # If you have already donwload some sceneries and not want to redownload these. # Or if all the files are donwloaded but there is an need to verify that we have # downloaded all the files. python sceneries_downloader.py compare ########### # TODO: # ########### - Add the univesal line seperator see 'os.linesep' to make it cross platform. - Add an compare system. - If server reject us request, tell it or wait x time. ### """ #################################### # Defining the different variables # #################################### # Numbers of download at a day(night) number_downloads = 6 # Path of the sceneries downloaded #download_dir = "./downloaded/" download_dir = "/mnt/docs/ISOS/slackware_10.2/" #download_dir = C:\\python24\\share\\foo # Windows way?? # or r'C:\test.dat' or 'C:/test.dat' # Utility to download the sceneries, To remove, there is an build in now #downloader_tool = "/usr/bin/wget --directory-prefix=%s" % (download_dir) # The FTP server addr ftpserver_download_path = "ftp.belnet.be" # The dir on the FTP server ftpserver_path_dir = "/packages/slackware/slackware-10.2-iso" # Concatenated server download path (for downloading with wget - NO MORE NEEDED) #server_download_path = "ftp://" + ftpserver_download_path + ftpserver_path_dir # File with info of the sceneries to download #fgfs_sceneries = "./to_download.txt" fgfs_sceneries = "/mnt/docs/ISOS/slackware_10.2/sceneries_downloader/to_download.txt" # File with info of the sceneries already download #already_downloaded_file = "./already_downloaded.txt" already_downloaded_file = "/mnt/docs/ISOS/slackware_10.2/sceneries_downloader/already_downloaded.txt" # #################################### counter = 0 # Change the current directory to 'download_dir' so that the downloaded files # are been set to the right place. os.chdir(download_dir) ########### # TO TEST # ########### """ An check system to verify that there are the same amount of local compressed files as on the server. Usually this option is used when we assume that we have download all the files of the ftp server or if we have already downloaded some compressed files manually before using this script. """ """ if(sys.argv[0] == 'compare'): print "We gona check some things" # Make list of local 'download_dir' files files = os.listdir(download_dir) #sorted(os.listdir()) files.sort() for item in files: print item # Compare 'download_dir' with the server content # Compare list with that of the content of the ftp server # If there are some diferences, make an 'fgfs_sceneries' file. # Backup old 'fgfs_sceneries' file if there are one # When done, exit sys.exit() """ """ If the 'download_list' file not exist, get the content of the ftp 'server_download_path' and put it localy in an 'download_list' file. """ # Try to open the file 'download_list'. try: file(fgfs_sceneries, "r") # If the file 'donwload_list' not exist, get the server content list and make one. except IOError: # Change the current directory to 'download_dir' so that the downloaded files # are been set to the right place. os.chdir(download_dir) print "Whe need to get the 'donwload_file'" print "Getting the 'download_file'..." ftp = FTP(ftpserver_download_path) # Identify to the ftp server default, i.e : user anonymous, passwd anonymous@ ftp.login() # Change the current directory. There is an shorter way! ftp.cwd(ftpserver_path_dir) # Get the content of the ftp dir an put it in an var ftpfiles = ftp.nlst() ftp.close() # Append each item to an text file ('fgfs_sceneries') for item in ftpfiles: write_newfile = file(fgfs_sceneries, "a") write_newfile.write(os.linesep + item.rstrip()) write_newfile.close() # Remove the first 3 unwanted lines of the 'fgfs_file'. # There is another way with 'del lines[0:2]'. count_three = 0 while (counts_three < 3): fp = open(fgfs_sceneries, "r") lines = fp.readlines() fp.close() del lines[0] # Delete firt line fp = open(fgfs_sceneries, "w") fp.writelines(lines) fp.close() count_three += 1 # Increment counter by one # Else the download list existe and we can continue print "The download list detected..." """ Read an item of the 'fgfs_sceneries' file first open the file to read(r) If 'to_download file' is empty, probably nothings more to download """ to_download = file(fgfs_sceneries, "r") print "Ready to download ..." # Read the file into a list then print it for item in to_download: # Remove the strip() otherwise downloaded file is ex: e100s10.tgz? item = item.strip() # when counter are egual to x stop it if(counter < number_downloads): counter += 1 # Increment counter by one print "Downloading item %s file %s" % (counter, item) # Download the item # With wget (probably to remove with wget!) #os.popen("%s %s/%s" % (downloader_tool, server_download_path, item)) # Do it with ftplib! ftp = FTP(ftpserver_download_path) ftp.login() # Change the current directory ftp.cwd(ftpserver_path_dir) # Get the item ftp.retrbinary('RETR %s' % (item), open('%s' % (item), 'wb').write) ftp.close() # Once downloaded move the item of 'fgfs_sceneries' to 'already_download' write_already_downloaded_file = file(already_downloaded_file, "a") write_already_downloaded_file.write(item.rstrip() + '\n') write_already_downloaded_file.close() # Remove the downloaded item of 'fgfs_sceneries' fp = open(fgfs_sceneries, "r") lines = fp.readlines() fp.close() del lines[0] # Delete firt line fp = open(fgfs_sceneries, "w") fp.writelines(lines) fp.close() ## print "Downloaded item %s done" % (item.rstrip()) # rstrip to remove \n after 'line' # Close the opened file to_download.close()