本文整理汇总了Python中models.FeaturedArtwork.all方法的典型用法代码示例。如果您正苦于以下问题:Python FeaturedArtwork.all方法的具体用法?Python FeaturedArtwork.all怎么用?Python FeaturedArtwork.all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.FeaturedArtwork
的用法示例。
在下文中一共展示了FeaturedArtwork.all方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def get(self):
ARTWORKS = json.loads(open(os.path.join(os.path.split(__file__)[0], 'lt-artworks.json')).read())
# ARTWORKS = filter(lambda a: '_stars' in a and a['_stars'] >= 1, ARTWORKS)
# Fetch latest 300 artworks (for blacklisting)
latest_artworks = (FeaturedArtwork.all()
.order('-publish_date')
.fetch(300))
# List dates for which artwork exists
dates_with_existing_art = set(a.publish_date for a in latest_artworks)
# List target dates that we want artwork for, but for which no artwork exists
target_dates = [date.today() + timedelta(days=n) for n in range(-1, LOOKAHEAD_DAYS)]
target_dates = [d for d in target_dates if d not in dates_with_existing_art]
# Create a blacklist of keys to avoid repeats
blacklist = set(artwork_key(a.details_url) for a in latest_artworks)
logging.debug('starting blacklist size: %d' % len(blacklist))
chosen_artworks = []
for target_date in target_dates:
# Pick from available artworks, excluding artwork in the blacklist
random_artwork = None
while True:
if len(ARTWORKS) == 0:
logging.error('Ran out of artworks to choose from, cannot continue')
return
random_artwork = random.choice(ARTWORKS)
key = artwork_key(random_artwork['detailsUri'])
if key not in blacklist:
# Once chosen, remove it from the list of artworks to choose next
ARTWORKS.remove(random_artwork)
chosen_artworks.append(random_artwork)
break
target_details_url = str(random_artwork['detailsUri'])
logging.debug('%(date)s: setting to %(url)s' % dict(url=target_details_url, date=target_date))
# Store the new artwork
if self.request.get('dry-run', '') != 'true':
new_artwork = FeaturedArtwork(
title=random_artwork['title'],
byline=random_artwork['byline'],
attribution=random_artwork['attribution'],
image_url=random_artwork['imageUri'],
thumb_url=random_artwork['thumbUri'],
details_url=random_artwork['detailsUri'],
publish_date=target_date)
new_artwork.save()
if self.request.get('output', '') == 'html':
self.response.out.write(get_html(artworks_json=json.dumps(chosen_artworks)))
# Finish up
logging.debug('done')
示例2: move_artwork
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def move_artwork(self, artwork, publish_date, initial_artwork_id):
# cascade moves
current_artwork_at_date = FeaturedArtwork.all().filter("publish_date =", publish_date).get()
if current_artwork_at_date and current_artwork_at_date.key().id() != initial_artwork_id:
self.move_artwork(current_artwork_at_date, publish_date + datetime.timedelta(hours=24), initial_artwork_id)
artwork.publish_date = publish_date
artwork.save()
示例3: post
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def post(self):
artwork_json = json.loads(self.request.get('json'))
publish_date = (datetime.datetime
.utcfromtimestamp(artwork_json['publishDate'] / 1000)
.date())
if FeaturedArtwork.all().filter('publish_date=', publish_date).get() != None:
webapp2.abort(409, message='Artwork already exists for this date.')
crop_tuple = tuple(float(x) for x in json.loads(self.request.get('crop')))
new_image_url, new_thumb_url = maybe_process_image(
artwork_json['imageUri'],
crop_tuple,
publish_date.strftime('%Y%m%d') + ' '
+ artwork_json['title'] + ' '
+ artwork_json['byline'])
if not new_thumb_url and 'thumbUri' in artwork_json:
new_thumb_url = artwork_json['thumbUri']
new_artwork = FeaturedArtwork(
title=artwork_json['title'],
byline=artwork_json['byline'],
attribution=artwork_json['attribution'] if 'attribution' in artwork_json else None,
image_url=new_image_url,
thumb_url=new_thumb_url,
details_url=artwork_json['detailsUri'],
publish_date=publish_date)
new_artwork.save()
self.response.set_status(200)
示例4: post
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def post(self):
artwork_json = json.loads(self.request.get("json"))
publish_date = datetime.datetime.utcfromtimestamp(artwork_json["publishDate"] / 1000).date()
if FeaturedArtwork.all().filter("publish_date=", publish_date).get() != None:
webapp2.abort(409, message="Artwork already exists for this date.")
crop_tuple = tuple(float(x) for x in json.loads(self.request.get("crop")))
new_image_url, new_thumb_url = backroomarthelper.maybe_process_image(
artwork_json["imageUri"],
crop_tuple,
publish_date.strftime("%Y%m%d") + " " + artwork_json["title"] + " " + artwork_json["byline"],
)
if not new_thumb_url and "thumbUri" in artwork_json:
new_thumb_url = artwork_json["thumbUri"]
new_artwork = FeaturedArtwork(
title=artwork_json["title"],
byline=artwork_json["byline"],
attribution=artwork_json["attribution"] if "attribution" in artwork_json else None,
image_url=new_image_url,
thumb_url=new_thumb_url,
details_url=artwork_json["detailsUri"],
publish_date=publish_date,
)
new_artwork.save()
self.response.set_status(200)
示例5: render_with_headers
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def render_with_headers(self, callback):
now = datetime.utcnow()
headers = {}
current = None
# Get up to 5 artworks published earlier than 2 days from now, ordered by latest first
latest_artworks = (FeaturedArtwork.all()
.filter('publish_date <=', date.today() + timedelta(days=2))
.order('-publish_date')
.fetch(5))
# Pick out the first artwork in that set that has actually been published
for artwork in latest_artworks:
if now >= datetime.combine(artwork.publish_date, START_TIME):
current = artwork
break
ret_obj = dict()
if current is not None:
# Found the next featured artwork
ret_obj = dict(
title=current.title.strip(),
byline=current.byline.strip(),
imageUri=current.image_url,
detailsUri=current.details_url)
if current.thumb_url:
ret_obj['thumbUri'] = current.thumb_url
if current.attribution:
ret_obj['attribution'] = current.attribution
# The next update time is the next START_TIME
next_start_time = datetime.combine(date.today(), START_TIME)
while next_start_time < now:
next_start_time += timedelta(hours=24)
ret_obj['nextTime'] = _serialize_datetime(next_start_time + NEXT_PADDING)
# Caches expire in an hour, but no later than the next start time minus padding
cache_expire_time = min(
now + MAX_HTTP_CACHE_AGE,
next_start_time)
expire_seconds = max(0, (cache_expire_time - now).total_seconds())
# Note that this max-age header will be cached, so max-age may be off by the memcache
# cache time which is set above to 60 seconds
headers['Cache-Control'] = 'max-age=%d, must-revalidate, public' % expire_seconds
headers['Expires'] = cache_expire_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
headers['Pragma'] = 'public'
else:
# Found no featured artwork; hopefully this is temporary; don't cache this response
headers['Cache-Control'] = 'max-age=0, no-cache, no-store'
headers['Pragma'] = 'no-cache'
body = json.dumps(ret_obj, sort_keys=True)
if callback:
body = '%s(%s)' % (callback, body)
return (body, headers)
示例6: render
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def render(self):
start = datetime.date(day=1,
month=int(self.request.get('month')) + 1,
year=int(self.request.get('year')))
start -= datetime.timedelta(weeks=2)
queue = (FeaturedArtwork.all()
.filter('publish_date >=', start)
.order('publish_date')
.fetch(1000))
return json.dumps([artwork_dict(a) for a in queue])
示例7: render
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def render(self):
queue = (FeaturedArtwork.all()
.filter('publish_date >=', datetime.date.today() - datetime.timedelta(days=30))
.order('publish_date')
.fetch(1000))
return json.dumps([dict(
id=a.key().id(),
title=a.title,
byline=a.byline,
imageUri=a.image_url,
thumbUri=a.thumb_url,
detailsUri=a.details_url,
publishDate=date_to_timestamp(a.publish_date),)
for a in queue])
示例8: get
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def get(self):
ARTWORKS = json.loads(open(os.path.join(os.path.split(__file__)[0], 'lt-artworks.json')).read())
# Fetch latest 300 artworks (for blacklisting)
latest_artworks = (FeaturedArtwork.all()
.order('-publish_date')
.fetch(300))
# List dates for which artwork exists
dates_with_existing_art = set(a.publish_date for a in latest_artworks)
# List target dates that we want artwork for, but for which no artwork exists
target_dates = [date.today() + timedelta(days=n) for n in range(-1, LOOKAHEAD_DAYS)]
target_dates = [d for d in target_dates if d not in dates_with_existing_art]
# Create a blacklist of keys to avoid repeats
blacklist = set(artwork_key(a.details_url) for a in latest_artworks)
self.response.out.write('starting blacklist size: %d<br>' % len(blacklist))
for target_date in target_dates:
# Pick from available artworks, excluding artwork in the blacklist
random_artwork = None
while True:
random_artwork = random.choice(ARTWORKS)
key = artwork_key(random_artwork['detailsUri'])
if key not in blacklist:
# Once chosen, add to the blacklist to avoid repeats within the lookahead
blacklist.add(key)
break
target_details_url = str(random_artwork['detailsUri'])
self.response.out.write('%(date)s: setting to <b>%(url)s</b><br>' % dict(url=target_details_url, date=target_date))
# Store the new artwork
new_artwork = FeaturedArtwork(
title=random_artwork['title'],
byline=random_artwork['byline'],
attribution=random_artwork['attribution'],
image_url=random_artwork['imageUri'],
thumb_url=random_artwork['thumbUri'],
details_url=random_artwork['detailsUri'],
publish_date=target_date)
new_artwork.save()
# Finish up
self.response.out.write('done<br>')
示例9: render
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def render(self, callback):
now = datetime.datetime.utcnow()
current = None
# Get up to 5 artworks published earlier than 2 days from now, ordered by latest first
latest_artworks = (FeaturedArtwork.all()
.filter('publish_date <=', datetime.date.today() + datetime.timedelta(days=2))
.order('-publish_date')
.fetch(5))
# Pick out the first artwork in that set that has actually been published
for artwork in latest_artworks:
if now >= datetime.datetime.combine(artwork.publish_date, START_TIME):
current = artwork
break
ret_obj = dict()
if current is not None:
featured = dict(
title=current.title,
byline=current.byline,
imageUri=current.image_url,
detailsUri=current.details_url)
if current.thumb_url:
featured['thumbUri'] = current.thumb_url
# The next update time is at START_TIME tomorrow
next_time = datetime.datetime.combine(datetime.date.today() \
+ datetime.timedelta(days=1), START_TIME) + NEXT_PADDING
featured['nextTime'] = _serialize_datetime(next_time)
# Caches expire in an hour, but no later than the next start time minus 5 minutes
cache_expire_time = min(
datetime.datetime.now() + datetime.timedelta(hours=1),
next_time - datetime.timedelta(minutes=5))
expire_seconds = max(0, (cache_expire_time - now).total_seconds())
self.response.headers['Cache-Control'] = 'max-age=%d, must-revalidate, public' % expire_seconds
self.response.headers['Expires'] = cache_expire_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
ret_obj = featured
s = json.dumps(ret_obj, sort_keys=True)
if callback:
return '%s(%s)' % (callback, s)
else:
return s
示例10: render
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def render(self):
start = datetime.date(day=1,
month=int(self.request.get('month')),
year=int(self.request.get('year')))
queue = (FeaturedArtwork.all()
.filter('publish_date >=', start)
.order('publish_date')
.fetch(1000))
return json.dumps([dict(
id=a.key().id(),
title=a.title,
byline=a.byline,
imageUri=a.image_url,
thumbUri=a.thumb_url,
detailsUri=a.details_url,
publishDate=date_to_timestamp(a.publish_date),)
for a in queue])
示例11: render
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def render(self):
start = datetime.date(day=1, month=int(self.request.get("month")) + 1, year=int(self.request.get("year")))
start -= datetime.timedelta(weeks=2)
queue = FeaturedArtwork.all().filter("publish_date >=", start).order("publish_date").fetch(1000)
return json.dumps(
[
dict(
id=a.key().id(),
title=a.title,
byline=a.byline,
imageUri=a.image_url,
thumbUri=a.thumb_url,
detailsUri=a.details_url,
publishDate=date_to_timestamp(a.publish_date),
)
for a in queue
]
)
示例12: get
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def get(self):
# Fetch latest 1000 artworks
latest_artworks = (FeaturedArtwork.all()
.order('-publish_date')
.fetch(1000))
# List dates for which artwork exists
dates_with_existing_art = set(a.publish_date for a in latest_artworks)
# List target dates that we want artwork for, but for which no artwork exists
target_dates = [date.today() + timedelta(days=n) for n in range(-1, 9)]
target_dates = [d for d in target_dates if d not in dates_with_existing_art]
for target_date in target_dates:
self.response.out.write('looking for artwork for date ' + str(target_date) + '<br>')
# Create a blacklist of the most recent 200 artwork
# (don't want to repeat one of the last 200!)
blacklist_artwork_keys = set(sanitized_artwork_key(a) for a in latest_artworks[:200])
if len(blacklist_artwork_keys) < 5:
blacklist_artwork_keys = set() # should never happen, but just in case of a reset
# Pick from one of the oldest 500, excluding artwork in the blacklist
random_artwork = None
while True:
random_artwork = random.choice(latest_artworks[500:])
key = sanitized_artwork_key(random_artwork)
if 'wikiart.org' in key or 'wikipaintings.org' in key or 'metmuseum.org' in key:
if key not in blacklist_artwork_keys:
break
target_details_url = str(random_artwork.details_url)
self.response.out.write('recycling ' + target_details_url + ' for date ' + str(target_date) + '<br>')
backroomarthelper.add_art_from_external_details_url(
target_date,
target_details_url)
self.response.out.write('done<br>')
示例13: get
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
#.........这里部分代码省略.........
archive_metadata = json.loads(existing_archive_lines[0])
archive_image_blobs = filter(lambda x: len(x) > 0, existing_archive_lines[1:])
except:
self.response.out.write('error reading from existing archive, starting from scratch<br>')
latest_current_archive_from_month = None
latest_archive_gcs_path = None
# construct the query
query_from = None
if latest_current_archive_from_month:
# get everything after the latest archive this month
query_from = datetime.date(*latest_current_archive_from_month) + datetime.timedelta(days=1)
else:
# get everything from this month
query_from = datetime.date(
archive[0], archive[1], archive[2] if len(archive) == 3 else 1).replace(day=1)
query_from = max(ARCHIVE_START_DATE, query_from)
query_to = None
archive_name = None
if len(archive) == 3:
# partial month archive
archive_name = '%04d%02d%02d' % archive
query_to = datetime.date(*archive)
else:
# full month archive
archive_name = '%04d%02d' % archive
next_month = datetime.date(archive[0], archive[1], 1)
if next_month.month == 12:
next_month = next_month.replace(year=next_month.year + 1, month=1)
else:
next_month = next_month.replace(month=next_month.month + 1)
query_to = next_month - datetime.timedelta(days=1)
# fetch artworks that match this query
artwork_objs = (FeaturedArtwork.all()
.order('publish_date')
.filter('publish_date >=', query_from)
.filter('publish_date <=', query_to)
.fetch(1000))
for artwork_obj in artwork_objs:
metadata_item = dict(
publish_date=artwork_obj.publish_date.isoformat(),
title=artwork_obj.title,
byline=artwork_obj.byline,
thumb_url=artwork_obj.thumb_url,
details_url=artwork_obj.details_url,)
# fetch the image
image_result = urlfetch.fetch(artwork_obj.thumb_url)
if image_result.status_code < 200 or image_result.status_code >= 300:
raise IOError('Error downloading image: HTTP %d.' % image_result.status_code)
# resize and crop thumb
thumb = images.Image(image_result.content)
if thumb.width > thumb.height:
thumb.resize(width=4000, height=ARCHIVE_IMAGE_SIZE)
thumb.crop(
(float(thumb.width - thumb.height) / thumb.width) / 2, 0.,
1 - (float(thumb.width - thumb.height) / thumb.width) / 2, 1.)
else:
thumb.resize(width=ARCHIVE_IMAGE_SIZE, height=4000)
thumb.crop(
0., (float(thumb.height - thumb.width) / thumb.height) / 2,
1., 1 - (float(thumb.height - thumb.width) / thumb.height) / 2)
# compute average color
histogram = thumb.histogram()
avg_color = tuple([int(x) for x in img_weighed_average(histogram)])
avg_color_hex = "#%0.2X%0.2X%0.2X" % avg_color
metadata_item['color'] = avg_color_hex
# export thumb
thumb_data_uri = 'data:image/jpeg;base64,' + base64.b64encode(
thumb.execute_transforms(output_encoding=images.JPEG, quality=40))
# append the metadata
archive_metadata.append(metadata_item)
archive_image_blobs.append(thumb_data_uri)
self.response.out.write('query: from ' + repr(query_from) + ' to ' + repr(query_to) + '<br>')
self.response.out.write('artworks: ' + str(len(artwork_objs)) + '<br>')
#self.response.out.write('<pre>' + json.dumps(archive_metadata, indent=2) + '</pre>')
# create the archive contents
s = json.dumps(archive_metadata) + '\n'
for blob in archive_image_blobs:
s += blob + '\n'
# gzip and write the archive
gcs_path = CLOUD_STORAGE_ARCHIVE_PATH + '/' + archive_name + '.txt'
self.response.out.write('writing to: ' + gcs_path + '<br>')
gcsf = gcs.open(gcs_path, 'w',
content_type='text/plain', options={'content-encoding':'gzip'})
gcsf.write(gzip_compress(s))
gcsf.close()
# delete the previous archive
if latest_archive_gcs_path:
gcs.delete(latest_archive_gcs_path)
示例14: add_art_from_external_details_url
# 需要导入模块: from models import FeaturedArtwork [as 别名]
# 或者: from models.FeaturedArtwork import all [as 别名]
def add_art_from_external_details_url(publish_date, url):
if FeaturedArtwork.all().filter('publish_date =', publish_date).get() != None:
webapp2.abort(409, message='Artwork already exists for this date.')
result = urlfetch.fetch(url)
if result.status_code < 200 or result.status_code >= 300:
webapp2.abort(400, message='Error processing URL: HTTP %d. Content: %s'
% (result.status_code, result.content))
soup = BeautifulSoup(result.content, 'html.parser')
attribution = None
if re.search(r'wikiart.org', url, re.I) or re.search(r'wikipaintings.org', url, re.I):
attribution = 'wikiart.org'
details_url = re.sub(r'#.+', '', url, re.I | re.S) + '?utm_source=Muzei&utm_campaign=Muzei'
title = soup.find('h1').get_text()
author = soup.find('a', class_='artist-name').get_text()
completion_year = None
try:
completion_year = unicode(soup.find(text='Date:').parent.next_sibling).strip()
except:
pass
byline = author + ((', ' + completion_year) if completion_year else '')
image_url = get_wikiart_image_url(soup)
elif re.search(r'metmuseum.org', url, re.I):
attribution = 'metmuseum.org'
details_url = re.sub(r'[#?].+', '', url, re.I | re.S) + '?utm_source=Muzei&utm_campaign=Muzei'
title = soup.find('h2').get_text()
author = ''
try:
author = unicode(soup.find(text='Artist:').parent.next_sibling).strip()
except:
pass
author = re.sub(r'\s*\(.*', '', author)
completion_year = None
try:
completion_year = unicode(soup.find(text='Date:').parent.next_sibling).strip()
except:
pass
byline = author + ((', ' + completion_year) if completion_year else '')
image_url = soup.find('a', class_='download').attrs['href']
else:
webapp2.abort(400, message='Unrecognized URL')
if not title or not author or not image_url:
webapp2.abort(500, message='Could not parse HTML')
image_url, thumb_url = maybe_process_image(image_url,
NO_CROP_TUPLE,
publish_date.strftime('%Y%m%d') + ' ' + title + ' ' + byline)
# create the artwork entry
new_artwork = FeaturedArtwork(
title=title.strip(),
byline=byline.strip(),
attribution=attribution,
image_url=image_url,
thumb_url=thumb_url,
details_url=details_url,
publish_date=publish_date)
new_artwork.save()
return new_artwork