]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blob - scripts/import-photos.py
Merge branch 'master' of /home/brettp/sommitrealweird
[sommitrealweird.git] / scripts / import-photos.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5
6 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
7
8 sys.path.append("../sommitrealweird")
9
10 from django.conf import settings
11 from photo.models import Album, Photo
12
13 # OK - so we'll name the "album" by the directory name, and that'll be that.
14 # The albums are just going to be straight imports of the directories for now
15
16 root, dirs, files = os.walk("../media/photos/").next()
17
18 for dir in dirs:
19     if Album.objects.filter(slug__exact=dir):
20         print "Already have a %s album, not importing" %(dir,)
21     else:
22         album = Album()
23         album.slug = dir
24         album.name = dir
25         if os.path.exists("../media/photos/%s/description.txt" %(dir)):
26             album.caption = open("../media/photos/%s/description.txt" %(dir)).read()
27         album.save()
28         captions = {}
29         if os.path.exists("../media/photos/%s/captions.txt" %(dir)):
30             capfh = open("../media/photos/%s/captions.txt" %(dir))
31             line = capfh.readline()
32             while line:
33                 fname, value = line.split("\t", 1)
34                 captions[fname] = value
35                 line = capfh.readline()
36         for file in os.walk("../media/photos/%s/" %(dir)).next()[2]:
37             if file[-4:] == ".jpg":
38                 photo = Photo()
39                 photo.album = album
40                 try:
41                     photo.caption = captions[file]
42                 except:
43                     pass
44                 photo.image = "photos/%s/%s" %(dir, file)
45                 photo.save()