forked from collective/collective.plonetruegallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importscript.py
66 lines (60 loc) · 2.24 KB
/
importscript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from lxml.html import fromstring
from zope.app.component.hooks import setSite
import transaction
from datetime import datetime, timedelta
import requests
import random
from plone.i18n.normalizer import idnormalizer
from StringIO import StringIO
SITE_ID = 'ptg'
START_FOLDER_ID = 'gallery'
NUM_GALLERIES = 20
START_DATE = datetime(2012, 1, 1)
MAX_GALLERY_SIZE = 100
def importit(app):
site = app[SITE_ID]
setSite(site)
if START_FOLDER_ID not in site:
site.invokeFactory('Folder', START_FOLDER_ID, title="Gallery")
gallery = site[START_FOLDER_ID]
gallery.setLayout('galleryview')
gallery.reindexObject()
else:
gallery = site[START_FOLDER_ID]
def populate(context, date):
size = random.randint(20, MAX_GALLERY_SIZE)
page = 1
while len(context.objectIds()) < size:
url = 'http://www.flickr.com/explore/interesting/%s/page%i/' % (
date.strftime('%Y/%m/%d'), page)
resp = requests.get(url)
dom = fromstring(resp.content)
for img in dom.cssselect('span.photo_container a img'):
title = img.attrib['alt']
id = idnormalizer.normalize(title).strip('_')
if not id or id in context.objectIds():
continue
print 'import %s' % title
src = img.attrib['src'].replace('_m.', '_z.')
imgresp = requests.get(src)
context.invokeFactory('Image', id, title=title)
imObj = context[id]
imObj.setImage(StringIO(imgresp.content),
content_type="image/jpeg")
imObj.reindexObject()
page += 1
date = START_DATE
for num in range(NUM_GALLERIES):
id = START_FOLDER_ID + '-' + str(num)
print 'folder %s' % id
if id not in gallery.objectIds():
gallery.invokeFactory('Folder', id,
title='Gallery ' + str(num + 1))
folder = gallery[id]
folder.setLayout('galleryview')
folder.reindexObject()
else:
folder = gallery[id]
date = date + timedelta(days=1)
populate(folder, date)
transaction.commit()