Checky little photo import script
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Sun, 29 Mar 2009 13:49:04 +0000 (14:49 +0100)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Sun, 29 Mar 2009 13:49:04 +0000 (14:49 +0100)
scripts/import-photos.py [new file with mode: 0644]

diff --git a/scripts/import-photos.py b/scripts/import-photos.py
new file mode 100644 (file)
index 0000000..3fd5a54
--- /dev/null
@@ -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()