Skip to content

Instantly share code, notes, and snippets.

@gabriel-laet
Created November 19, 2013 20:03
Show Gist options
  • Save gabriel-laet/7551606 to your computer and use it in GitHub Desktop.
Save gabriel-laet/7551606 to your computer and use it in GitHub Desktop.
Old but still useful script to create BulkLoader's XML file
#!/usr/bin/env python
# encoding: utf-8
"""
bulkloader_lazyfile.py
Created by gabriellaet on 2008-05-14.
Copyright (c) 2008 Gringo. All rights reserved.
"""
import os, sys
import re, subprocess
from optparse import OptionParser
def main():
parser = OptionParser()
parser.add_option("-o", dest="output", help="path to output file", metavar="OUTPUT")
parser.add_option("-d", dest="directory", help="directories to fetch files (comma-separated)", metavar="DIRECTORY")
(options, args) = parser.parse_args()
if options.output and options.directory:
#fetch all files
for d in options.directory.split(','):
if os.path.isdir(d): fetch_directory(d.strip())
else: print "WARNING: %s is not a directory" % d
#write the output
try:
ofile = open(options.output, 'w')
ofile.write(write_output())
ofile.close()
except IOError:
parser.error("IOError when writing the output file")
else:
parser.error("you need to set at least one directory and the output-file destination")
def fetch_directory(dir_path):
for f in os.listdir(dir_path):
path = os.path.join(dir_path, f)
basename = os.path.basename(path)
if not basename in ignore or not basename.startswith('.'):
if os.path.isdir(path):
fetch_directory(path)
elif os.path.isfile(path) and not os.path.islink(path):
files.append(read_file(path))
def read_file(file_path):
try:
ftype = get_type(file_path)
if special_types.has_key(ftype):
obj = special_types[ftype](file_path.replace(' ', '\ '))
else:
obj = {}
obj['type'] = ftype
obj['url'] = file_path
obj['weight'] = int(os.path.getsize(file_path))
return obj
except IOError:
pass
def get_type(file_path):
#proc = subprocess.Popen("file -b %s" % file_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
#return proc.communicate()[0].strip()
ext = os.path.splitext(file_path)[1][1:]
for _type in extensions:
if ext in extensions[_type]:
return _type
def get_video(file_path):
#ffmpeg SHOULD generate an error because we didn't provide the output path
info = subprocess.Popen("ffmpeg -i %s" % file_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,).stderr.read()
video = {}
try:
#duration
timestamp = re.search('Duration: (.*)', info).group(1).split(',')[0].split(':')
video['duration'] = (float(timestamp[0]) * 3600) + (float(timestamp[1]) * 60) + float(timestamp[2])
#width and height
dimensions = re.search('([0-9]{1,5})x([0-9]{1,5})', info)
video['width'] = dimensions.group(1)
video['height'] = dimensions.group(2)
#frame rate
framerate = re.search('([0-9\.]+) (fps|tb)\(r\)', info)
video['framerate'] = float(framerate.group(1))
except:
print "WARNING: couldn't get video (%s) information with ffmpeg" % file_path
return video
def get_audio(file_path):
#ffmpeg SHOULD generate an error because we didn't provide the output path
info = subprocess.Popen("ffmpeg -i %s" % file_path, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,).stderr.read()
audio = {}
try:
#duration
timestamp = re.search('Duration: (.*)', info).group(1).split(',')[0].split(':')
audio['duration'] = (float(timestamp[0]) * 3600) + (float(timestamp[1]) * 60) + float(timestamp[2])
except:
print "WARNING: couldn't get audio (%s) information with ffmpeg" % file_path
return audio
def write_output(type='xml'):
output = ''
if type == 'xml':
item = "<file %s></file>"
items = [item % ' '.join([('%s="%s"' % (p, _file[p])) for p in _file]) for _file in files]
output = '\n'.join([
'<?xml version="1.0" encoding="UTF-8"?>',
'<assets>',
'\t<files>',
'\t\t%s' % '\n\t\t'.join(items),
'\t</files>',
'</assets>'
])
return output
#global properties
files = []
ignore = ('.svn', '.DS_Store', '.actionscriptProperties', '.project', '.htaccess', )
special_types = {'video': get_video, 'sound': get_audio}
extensions = {
'image': ('jpg', 'jpeg', 'gif', 'png', ),
'text': ('txt', 'js', 'php', 'asp', 'py', 'html', 'htm', ),
'video': ('flv', 'f4v', 'f4p', 'mov', ),
'sound': ('mp3', 'f4a', 'f4b', ),
'xml': ('xml', ),
'movieclip': ('swf', )
}
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment