All pastes #99408 Raw Edit

Mine

public python v1 · immutable
#99408 ·published 2006-07-25 08:41 UTC
rendered paste body
#!/usr/bin/env pythonimport osfrom ftplib import FTP"""Little tool to help to download sceneries of Flight Gear Flight Simulator seehttp://www.flightgear.org/ This script is ideal for some people limited to anmonth download limit. This script want to download every day (at night) xsceneries of the fgfs ftp server. Like in my case with my isp, 10 MB downloadedat night is counted as 5 MB.Define the different variables before executing this script! Otherwise itprobably not work. Place this script as an cron job task and give it the job toexecute it every 2 days for example. Similar is possible with the 'tasks' onMicrosoft Windows. Version: 0.1############ TODO: ############- Finish this script!- Almost done, need to test -> Get an content list of the ftp server to download.- Find another way, without using wget, with ftplib probably.##"""###### Defining the different variables## Numbers of download at a day(night)number_downloads = 1# Path of the sceneries downloaded#download_dir = "./downloaded/"download_dir = "/mnt/backup/fgfs_stuff/additional_sceneries/"# Utility to download the sceneriesdownloader_tool = "/usr/bin/wget --directory-prefix=%s" % (download_dir)# The FTP server addrftpserver_download_path = "ftp.flightgear.org"# The dir on the FTP serverftpserver_path_dir = "/pub/fgfs/Scenery-0.9.10"# Concatenated server download path (for downloading with wget)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 = "/home/david/Docs/fgfs_docs/sceneries_downloader/to_download.txt"# File with info of the sceneries already download#already_downloaded_file = "./already_downloaded.txt"already_downloaded_file = "/home/david/Docs/fgfs_docs/sceneries_downloader/already_downloaded.txt"# Countercounter = 0######"""If the download list text file not exist, get the content of the ftp'server_download_path' and put it in an txt file.Actually an list of files are already done manually but need somethings better,for when there an new fgfs version."""###if not download_list#ftp geek manual command :-) Need to automatisate it!# Then remove these not needed colums#cat filename | awk '{ print $9 }' > newfile"""# Try:#    file(fgfs_sceneries, "r")#if(ExceptErro: = file not found)# do next oneftp = FTP(ftpserver_download_path)# Identify to the ftp server  default, i.e : user anonymous, passwd anonymous@ftp.login()# Change the current directoryftp.cwd(ftpserver_path_dir)# Get the content of the ftp dir an put it in an varftpfiles = 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('\n' + item.rstrip())    write_newfile.close()"""# Else the download list existe### 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")# If to_download file is empty, probably nothings more to download# Read the file into a list then print it# when counter are egual to x stop itfor item in to_download:    if(counter < number_downloads):        counter += 1 # Increment counter by one        print "Ready to download item %s file %s" % (counter, item)                # Download the item	# With wget (probably to remove wiht 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 add the item of $fgfs_sceneries to $already_download        write_already_downloaded_file = file(already_downloaded_file, "a")        write_already_downloaded_file.write('\n' + line.rstrip())        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" % (line.rstrip()) # rstrip to remove \n after 'line'# Close the opened fileto_download.close()