From: Brett Parker Date: Sun, 29 Mar 2009 13:49:04 +0000 (+0100) Subject: Checky little photo import script X-Git-Url: https://git.sommitrealweird.co.uk/sommitrealweird.git/commitdiff_plain/2387e5ac0c78d77295e9bc4dc2e66b21889e1a99?ds=sidebyside Checky little photo import script --- diff --git a/scripts/import-photos.py b/scripts/import-photos.py new file mode 100644 index 0000000..3fd5a54 --- /dev/null +++ b/scripts/import-photos.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +import sys +import os + +os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' + +sys.path.append("../sommitrealweird") + +from django.conf import settings +from photo.models import Album, Photo + +# OK - so we'll name the "album" by the directory name, and that'll be that. +# The albums are just going to be straight imports of the directories for now + +root, dirs, files = os.walk("../media/photos/").next() + +for dir in dirs: + if Album.objects.filter(slug__exact=dir): + print "Already have a %s album, not importing" %(dir,) + else: + album = Album() + album.slug = dir + album.name = dir + if os.path.exists("../media/photos/%s/description.txt" %(dir)): + album.caption = open("../media/photos/%s/description.txt" %(dir)).read() + album.save() + captions = {} + if os.path.exists("../media/photos/%s/captions.txt" %(dir)): + capfh = open("../media/photos/%s/captions.txt" %(dir)) + line = capfh.readline() + while line: + fname, value = line.split("\t", 1) + captions[fname] = value + line = capfh.readline() + for file in os.walk("../media/photos/%s/" %(dir)).next()[2]: + if file[-4:] == ".jpg": + photo = Photo() + photo.album = album + try: + photo.caption = captions[file] + except: + pass + photo.image = "photos/%s/%s" %(dir, file) + photo.save()