# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import FileField from Products.Archetypes.public import FileWidget from Products.Archetypes.public import OrderedBaseFolder, BaseContent, registerType from Globals import InitializeClass from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME from Common import CommonMix import urllib2 from urllib2 import URLError import urllib import xml.dom.minidom import time import datetime schema = BaseSchema class GroupPhotoFolder(OrderedBaseFolder, CommonMix): """ GroupPhotoFolder""" meta_type = "GroupPhotoFolder" archetype_name = "Group Photo Folder" global_allow = 0 exclude_from_nav = True filter_content_types = True allowed_content_types = ('Image',) security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/base_view', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/base_edit', 'permissions': ('Access Denied',), }, { 'id':'metadata', 'name':'Properties', 'action':'string:${object_url}/base_metadata', 'permissions': ('Access Denied',), }, ) def __init__(self, id): self.id = id self.nagiservice = {'username':'', 'keyword':''} self.flickrservice = {'username':'', 'keyword':''} self.ps_service = 'flickr' self.p_changed = 0 def getNagiService(self): return self.nagiservice def getFlickrService(self): service = self.flickrservice if not service.has_key('username'): service['username'] = '' return service def setPhotoService(self,ps_keyword,ps_service,ps_username): """save service data""" if not hasattr(self, 'nagiservice'): self.nagiservice = {'username':'metz','keyword':'kala'} if not hasattr(self, 'flickrservice'): self.flickrservice = {'username':'metz','keyword':'kala'} self.ps_service = ps_service author = str(self.REQUEST.get('AUTHENTICATED_USER')) time = datetime.datetime.today() time = time.strftime("%Y-%m-%d %H:%M:%S") allphotos = self.getAllPhotos() key = {} keyword = urllib.quote_plus(ps_keyword) if str(ps_service) == 'nagi': self.nagiservice['username'] = ps_username self.nagiservice['keyword'] = ps_keyword self.setNagiPhotoStream() key['link'] = 'http://nagi.ee/photos/'+ps_username+'/keyword/'+keyword+'?picture_type=sq' elif str(ps_service) == 'flickr': self.flickrservice['username'] = ps_username self.flickrservice['keyword'] = ps_keyword user_id = ps_username if user_id != '': url = "http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=3b123c3da3c5bd4ec58a33c0c411d821&username="+urllib.quote_plus(ps_username) try: f = urllib2.urlopen(url) except URLError: user_id = '' print 'URLError' if user_id != '': html = f.read() dom = xml.dom.minidom.parseString(html) user = dom.getElementsByTagName("user") if user == []: user_id = '' else: user_id = user[0].getAttribute("id") self.flickrservice['user_id'] = user_id self.setFlickrPhotos() key['link'] = 'http://www.flickr.com/search/?m=tags&z=t&q='+keyword+'&user_id='+user_id key['user_id'] = user_id key['username'] = ps_username key['keyword'] = ps_keyword key['service'] = ps_service key['author'] = author key['time'] = time allphotos.append([time, key]) self.setAllPhotos(allphotos) self.p_changed = 1 return self.REQUEST.RESPONSE.redirect(self.aq_parent.absolute_url()) def getService(self): """ """ if not hasattr(self, 'ps_service'): self.ps_service = 'flickr' return self.ps_service def changedornot(self): """ """ if not hasattr(self, 'p_changed'): self.p_changed = 0 return self.p_changed def setNagiPhotoStream(self): """get photos from mujalt""" service_data = self.getNagiService() keyword = service_data['keyword'] keyword = keyword.decode('utf-8') username = service_data['username'] username = username.decode('utf-8') limit = 10 #how many images in row size = 'sq' #sq - square 75x75, t - tiny 100x100, s - small 250x250, m - medium 500x500, l - large 800x800, o - original stream = '' stream += '\n' stream += '\n' self.nagiphotos = stream today = datetime.datetime.today() today = str(today).split('.') today = today[0] date = time.strptime(str(today), "%Y-%m-%d %H:%M:%S") self.lasttime = int(time.mktime(date)) return stream def getPreviousNagiPhotos(self): if not hasattr(self, 'nagiphotos'): self.nagiphotos = [] return self.nagiphotos def setFlickrPhotos(self): """ """ photos = [] service_data = self.getFlickrService() user_id = service_data.get('user_id', None) if user_id is None: user_id = '' keywords = urllib.quote_plus(service_data['keyword']) url = "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=3b123c3da3c5bd4ec58a33c0c411d821&tags="+str(keywords)+"&tag_mode=all&per_page=10&user_id="+user_id print 'Url', url import urllib2 try: f = urllib2.urlopen(url) except URLError: f = '' print 'URLError' pass if f != '': html = f.read() import xml.dom.minidom dom = xml.dom.minidom.parseString(html) photos = dom.getElementsByTagName("photo") images = [] self.hrefs = [] for photo in photos: photoId = photo.getAttribute("id") owner = photo.getAttribute("owner") secret = photo.getAttribute("secret") server = photo.getAttribute("server") farm = photo.getAttribute("farm") images.append('http://farm'+str(farm)+'.static.flickr.com/'+str(server)+'/'+str(photoId)+'_'+str(secret)+'_s.jpg') self.hrefs.append('http://www.flickr.com/photos/'+str(owner)+'/'+str(photoId)) self.flickrImages = images self.lasttime = time.time() return images def getHrefs(self): """ """ if not hasattr(self, 'hrefs'): self.hrefs = [] return self.hrefs def getPreviousFlickrPhotos(self): if not hasattr(self, 'flickrImages'): self.flickrImages = [] return self.flickrImages def getFlickrPhotos(self): """ get photos from Flickr """ lastu = self.getLastUpdate() print time.localtime(lastu), ">", time.localtime(time.time()-900) if lastu>time.time()-900: print 'eelmised' return self.getPreviousFlickrPhotos() return self.setFlickrPhotos() def getNagiPhotos(self): """ """ lastu = self.getLastUpdate() if lastu>time.time()-900: print 'eelmised' return self.getPreviousNagiPhotos() return self.setNagiPhotoStream() def getLastUpdate(self): return getattr(self, 'lasttime', 0) def setAllPhotos(self, all): self.allphotos= all def getAllPhotos(self): if not hasattr(self, 'allphotos'): return [] photos = self.allphotos photos.sort() photos.reverse() return photos registerType(GroupPhotoFolder, PROJECT_NAME)