-
Notifications
You must be signed in to change notification settings - Fork 20
/
importscript.py
executable file
·90 lines (77 loc) · 2.89 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
from Testing.makerequest import makerequest
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManager import setSecurityPolicy
from Products.CMFCore.tests.base.security import PermissiveSecurityPolicy, \
OmnipotentUser
def spoofRequest(app):
"""
Make REQUEST variable to be available on the Zope application server.
This allows acquisition to work properly
"""
_policy = PermissiveSecurityPolicy()
setSecurityPolicy(_policy)
newSecurityManager(None, OmnipotentUser().__of__(app.acl_users))
return makerequest(app)
# Enable Faux HTTP request object
app = spoofRequest(app)
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()
importit(app)