def add_post_photo(post_id): info = extract(request) uid = token_to_uid(info) if uid is None: return invalid_token() if not info.get('photo', ''): return missing() post = Post.query.filter_by(id=post_id).first() if not post: return nopost() if not post.uid == uid: return wrong_token() if len(post.photos) > 3: return json.dumps({ 'success': False, 'error': "Post already has four photos. Please delete one before adding another." }), 400 photo = Photo.query.filter_by(val=info.get('photo')).first() if photo is not None: return json.dumps({'success': True, 'data': photo.serialize()}), 200 photo = Photo(post_id=post_id, val=info.get('photo')) db.session.add(photo) db.session.commit() return json.dumps({'success': True, 'data': photo.serialize()}), 201
def get(self,item_id=None): if item_id is None: # list entire table rtn = [x.to_json() for x in Photo.get_all()] for itm in rtn: redis.publish('list_view',itm) else: rtn = [x.to_json() for x in Photo.get_all() if x.group_id == item_id] for itm in rtn: redis.publish('list_view_{0}'.format(item_id),itm) res = flask.make_response(json.dumps(rtn)) res.headers['Content-Type'] = 'application/json' return res
def save_photos(photos): c = 0 for media in photos: if media.created_time > settings.period['start'] and media.location.point != None: try: photo = Photo.create( insta_id = media.id, thumb = media.get_thumbnail_url(), url = media.link, username = media.user.username, insta_filter = media.filter, date = media.created_time, longitude = media.location.point.longitude, latitude = media.location.point.latitude, message = media.caption, like_count = media.like_count, user_in_photo_count = len(media.users_in_photo) ) except peewee.IntegrityError: pass else: if hasattr(media, 'tags'): for t in media.tags: tag, tag_created = Tag.get_or_create(name = pattern.sub('', t.name)) PhotoTag.get_or_create(tag=tag, photo=photo) c += 1 print 'New media: %d / %d' % (c, len(photos))
def get(self,item_id): photo = Photo.get(item_id) if photo is not None: photo.add_view() rtn = flask.make_response(json.dumps(photo.to_json())) rtn = flask.make_response(json.dumps(dict(error='photo with id:{0} was not found'.format(item_id)))) rtn.headers['Content-Type'] = 'application/json' return rtn
def fetch_photos(library): photosdb = osxphotos.PhotosDB(library.path) images = photosdb.photos(images=True, movies=False) videos = [] # photosdb.photos(images=False, movies=True) albums = {} # photosdb.albums_as_dict photos = [Photo(p, library) for p in filter(is_image_supported, images)] logging.info( f"Found {len(photos)} photos, {len(videos)} videos, {len(albums)} albums" ) return (photos, videos, albums)
def update_photos(): i = 0 empty = 0 for photo in Photo.select(): i += 1 if photo.colors == "": empty += 1 for pt in list(photo.tags): pt.delete_instance() photo.delete_instance() print i print empty
def post(self): data = json.loads(flask.request.data) if flask.request.data else dict(flask.request.form.items()) user_id = data.get('user_id',None) group_id = data.get('group_id',None) image_url = data.get('image_url',None) photo = Photo(image_url=image_url,user_id=user_id,group_id=group_id).save() redis.publish('list_view',photo.to_json()) redis.publish('list_view_{0}'.format(group_id),photo.to_json()) rtn = flask.make_response( json.dumps( dict( image_url = photo.image_url, user_id = photo.user_id, group_id = photo.group_id, views = photo.views, ) ) ) rtn.headers['Content-type'] = 'application/json' return rtn
1230, #urbanwalls 1237, #graffitiart, 227, #streetphotography 1228, #streetarteverywhere 183, #londonstreetart 2262, #streetartuk 1967, #shoreditchstreetart 1975, #graffitilondon 4085, #murales ] if __name__ == "__main__": n_photos = 0 n_tags = 0 n = 0 for p in Photo.select(): n += 1 if n % 100 == 0: print 'processed: ' + str(n) print 'Removed photos: ' + str(n_photos) print 'Removed tags: ' + str(n_tags) if p.tags.count() == 0: continue for pt in p.tags: if pt.tag.id in save_tags: break else: n_photos += 1 for pt in p.tags: if pt.tag.photos.count() == 1:
import json from db import db, Photo, Tag, PhotoTag steps = {} if __name__ == '__main__': photos = [p.to_dict() for p in Photo.select()] with open('data/photos.json', 'w') as fp: json.dump(photos, fp)
import json from db import db, Photo, Tag, PhotoTag steps = {} if __name__ == '__main__': with open('data/data.txt', 'w') as fp: fp.write(Photo.string_header() + '\n') for photo in Photo.select(): fp.write(photo.to_string().encode('utf8') + '\n') #photo.to_string() + '\n'
img_path = 'data/img/%s.jpg' def calc_color(photo): img = Image.open(img_path % photo.insta_id) img.thumbnail((1, 1)) photo.color = '%d,%d,%d' % img.getpixel((0, 0)) photo.save() def remove_photo(photo): os.remove(img_path % photo.insta_id) if __name__ == "__main__": counter = 0 photos = list(Photo.select()) for photo in photos: if not os.path.isfile(img_path % photo.insta_id): r = requests.get(photo.thumb, stream=True) if r.status_code == 200: with open(img_path % photo.insta_id, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) #calc_color(photo) else: print 'Cannot download %s' % photo.insta_id for pt in list(photo.tags): pt.delete_instance() photo.delete_instance() #elif photo.color == None: # calc_color(photo)
import os import shutil import settings from db import Photo,PhotoTag, Tag img_path = 'data/img/%s.jpg' new_path = 'data/no_tag/%s.jpg' if __name__ == "__main__": n = 0 for p in Photo.select().where(Photo.date >= settings.period['start']): n += 1 if n % 100 == 0: print 'processed: ' + str(n) if p.tags.count() == 0: shutil.copyfile(img_path % p.insta_id, new_path % p.insta_id,)