From 2387e5ac0c78d77295e9bc4dc2e66b21889e1a99 Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Sun, 29 Mar 2009 14:49:04 +0100 Subject: [PATCH] Checky little photo import script --- scripts/import-photos.py | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 scripts/import-photos.py 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() -- 2.30.2