Skip to content

Instantly share code, notes, and snippets.

@ispedals
Created May 20, 2013 21:45
Show Gist options
  • Select an option

  • Save ispedals/5615833 to your computer and use it in GitHub Desktop.

Select an option

Save ispedals/5615833 to your computer and use it in GitHub Desktop.
Recursivly traverses a FTP directory, picking a random user-given number of files and outputting them in the format {image: http://<filename>}
"""
Recursivly traverses a FTP directory, picking a random user-given number of files and outputting them in the format
{image: http://<filename>}
"""
import ftplib
import random
from optparse import OptionParser
def traverse(ftp, depth=0):
"""
return a recursive listing of an ftp server contents (starting
from the current directory)
listing is returned as a recursive dictionary, where each key
contains a contents of the subdirectory or None if it corresponds
to a file.
@param ftp: ftplib.FTP object
"""
level = {}
for entry in (path for path in ftp.nlst() if path not in ('.', '..')):
try:
ftp.cwd(entry)
level[entry] = traverse(ftp, depth+1)
ftp.cwd('..')
except ftplib.error_perm:
level[entry] = None
return level
FILES=[]
def process_dict(d, path=()):
for k, v in d.iteritems():
if not v: #if v is none, meaning k is a file
FILES.append('/'.join(path) + '/' + k)
else:
process_dict(v, path + (k,))
usage = "usage: %prog [options] ip_address directory"
parser = OptionParser(usage=usage)
parser.add_option("-u", "--username", dest="username",
help="username for FTP account")
parser.add_option("-p", "--password", dest="password",
help="password for FTP account")
parser.add_option("-n", "--number", dest="number", type="int",
help="number of files to pick", default=30)
(options, args) = parser.parse_args()
ftp = ftplib.FTP(args[0])
BASE_PATH=args[1]
ftp.connect()
try:
ftp.login(options.username, options.password)
ftp.set_pasv(True)
ftp.cwd(BASE_PATH)
recursiveFiles=traverse(ftp)
finally:
ftp.quit()
FILES=[f for f in recursiveFiles.keys() if recursiveFiles[f] is None] #add files in current directory
for f in FILES: #and filter them out
del recursiveFiles[f]
process_dict(recursiveFiles)
FILES=['http://localhost/' + BASE_PATH + f for f in FILES]
FILES=random.sample(FILES, options.number)
for f in FILES[:-1]:
print '\t' * 14 + "{image : '%s'},\n" % f
print '\t' * 14 + "{image : '%s'}" % FILES[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment