#!/usr/bin/env python
import os
"""
Little tool to 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. This script want to download every day (at night) some
sceneries. Like in my case, 10 MB downloaded at night is counted as 5 MB.
Place this script as an cron job task and give it the job to execute it every 2
days for example.
Actually in alpha version!
Not yet tested!
###########
# TODO: #
###########
- Get an content list of the ftp server to download.
##
"""
#####
# Defining the different variables
#
# Numbers of download at a day
number_downloads = 20
# Download path of the sceneries downloaded
#download_dir = "./downloaded/"
download_dir = "/mnt/backup/fgfs_stuff/additional_sceneries/"
# Utility to download the sceneries
downloader_tool = "/usr/bin/wget --directory-prefix=%s" % (download_dir)
# Server download path
server_download_path = "ftp://ftp.flightgear.org/pub/fgfs/Scenery-0.9.10"
# File with info of the sceneries to download
fgfs_sceneries = "./to_download.txt"
# File with info of the sceneries already download
already_downloaded_file = "./already_downloaded.txt"
# Counter
counter = 0
#
#####
## Get the content of the ftp 'server_download_path' and put it in an txt file
# If the download list not exist, create it
#if not download_list
#ftp geek manual command :-) Need to automatisate it!
# Then remove these not needed colums
#cat filename | awk '{ print $9 }' > newfile
# Else the download list existe and nothing to do
##
# Read an item of the $fgfs_sceneries file
# first open the file to read(r)
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 it
for line in to_download:
if(counter < number_downloads):
counter += 1 # Increment counter by one
print "Ready to download item %s file %s" % (counter, line)
# Download the item
os.popen("%s %s/%s" % (downloader_tool, server_download_path, line))
# 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 file
to_download.close()