def test_shouldCreatePhotoWithoutCode(self): test_photo = Photo().from_json( json.loads(''' {"code": "", "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "3333333333333333333", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) self.assertIsNotNone(test_photo) self.assertTrue(test_photo.width == 1080) self.assertTrue(test_photo.height == 1349) self.assertTrue(test_photo.owner_id == '11111111') self.assertTrue(test_photo.caption == '#sunday#black#girl#selfie') self.assertTrue(test_photo.likes_count == 12) self.assertFalse(test_photo.is_video) self.assertTrue(test_photo.id == '3333333333333333333') self.assertTrue(test_photo.code == '') self.assertTrue( test_photo.display_src == 'https://scontent-waw1-1.cdninstagram.com/whatever/whatever') test_photo = Photo().from_json( json.loads(''' { "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "3333333333333333333", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) self.assertIsNotNone(test_photo) self.assertTrue(test_photo.width == 1080) self.assertTrue(test_photo.height == 1349) self.assertTrue(test_photo.owner_id == '11111111') self.assertTrue(test_photo.caption == '#sunday#black#girl#selfie') self.assertTrue(test_photo.likes_count == 12) self.assertFalse(test_photo.is_video) self.assertTrue(test_photo.id == '3333333333333333333') self.assertTrue(test_photo.code == '') self.assertTrue( test_photo.display_src == 'https://scontent-waw1-1.cdninstagram.com/whatever/whatever')
def load_photos(photo_filename): """Load movies from file into database.""" print("Photos") for i, row in enumerate(open(photo_filename)): row = row.rstrip() # unpack file into variables img_id, url, lat, lon, city_name = row.split("|") # convert geolocation from string to float format lat = float(lat) lon = float(lon) img_id = int(img_id) photo = Photo(img_id=img_id, url=url, lat=lat, lon=lon, city_name=city_name) db.session.add(photo) db.session.commit()
def add_photo(filename, pp_id): """ adds photo to database""" # need trip id trip_id = session["current_trip_id"] # need file_path file_path = "/static/photos/" + filename # check if the photo is already in database in_db = Photo.query.filter_by(trip_id=trip_id, pp_id=pp_id, file_path=file_path, file_name=filename).first() if not in_db: # add row to photos table new_photo = Photo(file_path=file_path, file_name=filename, trip_id=trip_id, pp_id=pp_id) db.session.add(new_photo) db.session.commit() print("added photo to database") else: flash("photo already loaded") return
def post(self, username, img_max_size): if self.cur_user and self.cur_user.flag>1: self.header['Content-Type'] = "text/html" rspd = {'status': 201, 'msg':'ok'} file_content = self.request.get('filetoupload','') if file_content: imgobj = images.Image(file_content) max_w = int(img_max_size) if imgobj.width <= max_w: #img_data = file_content pass else: imgobj.resize(width=max_w) imgobj.im_feeling_lucky() img_data = imgobj.execute_transforms(output_encoding=images.JPEG, quality=90) ni_obj = Photo(key_name = '%s-%s'%(username, str(int(time()))), content = img_data) ni_obj.put() if ni_obj.is_saved(): rspd['status'] = 200 rspd['msg'] = u'图片已成功上传' rspd['url'] = '%s/photo/%s.jpg' % (BASE_URL, ni_obj.key().name()) else: rspd['status'] = 500 rspd['msg'] = u'图片上传失败,可能是网络问题或图片太大,请刷新本页再上传' else: rspd['msg'] = u'没有上传图片' self.write(json.dumps(rspd)) else: self.error(403) self.write('403:forbidden')
def get_instagram_photos(rest_id, location): """""" os.system( 'instagram-scraper --location ' + location + ' --maximum 6 --media-metadata --media-types none --destination ig_photos' ) # try with subprocess: # string = 'instagram-scraper --location ' + location + ' --maximum 4 --media-metadata --media-types none --destination ig_photos' # p = subprocess.Popen(string, stdout=subprocess.PIPE, shell=True) # TAKE OUT shell=True # p.terminate() json_file = 'ig_photos/' + location + '.json' with open(json_file) as json_data: results = json.load(json_data) for result in results: url = result['urls'][0] if url[-3:] == 'jpg': photo = Photo(rest_id=rest_id, url=url) db.session.add(photo) db.session.commit() os.system('rm ig_photos/' + location + '.json') return 'success'
def load_photos(): """Load photos from seed data into database""" with open("seed_data/photos.txt") as photos: for row in photos: photo = row.rstrip().split("|") hidden = True if photo[6] == "True" else False kwargs = dict( photo_id = photo[0], user_id = photo[1], play_id = photo[2], order = photo[3], film_id = photo[4], photo = photo[5], hidden = hidden ) keys_to_remove = [] for key in kwargs.keys(): if kwargs[key] == "": keys_to_remove.append(key) for key in keys_to_remove: del kwargs[key] photo = Photo(**kwargs) db.session.add(photo) db.session.commit()
def create_photo(user, entry, photo_url, city): photo = Photo(user=user, entry=entry, photo_url=photo_url, city=city) db.session.add(photo) db.session.commit() return photo
def create_photo(img_path, location, description, gps_url, popular_url): """ create and return a photo""" photo = Photo(img_path=img_path, location=location, description=description, gps_url=gps_url, popular_url=popular_url) db.session.add(photo) db.session.commit() return photo
def create_photo(title, desc, price, img_url): """Create and return a new photo.""" photo = Photo(title=title, desc=desc, price=price, img_url=img_url) db.session.add(photo) db.session.commit() return photo
def load_seed_photos(): """Load sample photo urls from seed_photos.csv""" for row in open("seed_data/seed_photos.csv"): row = row.rstrip() user_id, event_id, url = row.split(",") row_photo = Photo(user_id=user_id, event_id=event_id, url=url) db.session.add(row_photo) db.session.commit() print("Successfuly seeded into the photos table!")
def uploadfile(): if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) photo_location = "uploads/" + filename photo_file_path = os.path.join(app.config['UPLOAD_PHOTO_FOLDER'], filename) file.save(photo_file_path) thumbnail_file_path = os.path.splitext( photo_file_path)[0] + ".thumbnail" create_thumbnail(filename, photo_file_path, thumbnail_file_path) thumbnail_location = "uploads/" + os.path.splitext( filename)[0] + ".thumbnail" image = Image.open(photo_file_path) exif_data = get_exif_data(image) latlon = get_lat_lon(exif_data) l = str(latlon) latitude = lat(l) longitude = lon(l) timestamp = get_time(exif_data) if timestamp != None: timestamp = datetime.strptime(str(timestamp), "%Y:%m:%d %H:%M:%S") caption = request.form['caption'] p = Photo(file_location=photo_location, caption=caption, latitude=latitude, longitude=longitude, timestamp=timestamp, user_id=g.user_id, thumbnail=thumbnail_location) db_session.add(p) db_session.commit() db_session.refresh(p) if latitude == None: # photo_id is a key and p.id is a value and session is a dict print "SESSION" session['photo_id'] = p.id return redirect(url_for('addlocation', photo_id=p.id)) user = db_session.query(User).filter_by(id=g.user_id).one() # create a template that shows the view of an uploaded photo and then the user's other photos return redirect(url_for('userpage')) return render_template("upload.html")
def add_my_photo(): """Adds my photo to User1""" my_photo = Photo( user_id=1, photo_url= "http://fellowship.hackbrightacademy.com/media/CACHE/images/students/IMG_0005/6259c4fbf765821b3b73f6a0964592f9.jpg", ) print my_photo db.session.add(my_photo) db.session.commit()
def create_photo_object(pet_entry, photo_entry): """Take photo attributes out of pet mini-dictionary and instantiate a Photo object.""" # Get pet ID out of database and store as foreign key. new_photo = Photo(pet_id=pet_entry['id'], pf_id=photo_entry['@id'], photo_size=photo_entry['@size'], photo_text=photo_entry['#text']) db.session.add(new_photo) print("Loaded Photos.")
def upload_photo_sub(): print("THIS IS MY FUNCTION") if request.method == 'POST': print("POSTTTTTTTTT!!!!!") print(request.files) if 'my_photo' not in request.files: print("no file part") flash('No file part') return redirect("/upload_gb_photo") file = request.files['my_photo'] print(dir(file)) if file.filename == '': print("no selected fileeeeee") flash('No selected file') return redirect("/upload_gb_photo") lat_data = request.form["hiddenLat"] long_data = request.form["hiddenLong"] submission_timestamp = request.form["hiddenTime"] user_date = request.form["gbPhotoDate"] if lat_data == '': flash("No location! Please click the map to mark photo location") return redirect("/upload_gb_photo") file.save('static/photos/' + file.filename) # lat_data = request.form["hiddenLat"] # long_data = request.form["hiddenLong"] # submission_timestamp = request.form["hiddenTime"] # user_date = request.form["gbPhotoDate"] gb_photo = Photo(photo_blob=file.filename, submitted_by=session['user_id'], photo_lat=lat_data, photo_long=long_data, submission_date=submission_timestamp, user_date=user_date) print(session['user_id']) db.session.add(gb_photo) db.session.commit() flash('photo successfully added!') print(type(gb_photo.photo_blob)) return render_template("submit_gb_form.html", photo=None) print(session) # Serve page! return render_template("submit_gb_form.html", photo=None)
def upload_files(files, collection_id): # Go through each file, create a new Photo() and upload to S3 for file in files: file.filename = secure_filename(file.filename) s3_key = upload_file_to_s3(file, bucket, collection_id) byte = get_photo_bytestring_from_s3(bucket, s3_key) (photo_width, photo_height) = get_photo_width_height(byte) db.session.add( Photo(collection_id=collection_id, s3_key=s3_key, byte_string=byte, width=photo_width, height=photo_height)) db.session.commit()
def upload_photo(): file = request.files['image'] site_id = request.form['site_id'] new_photo = Photo(photo_blob=file.read(), user_id=session['user_id'], site_id=site_id, date=datetime.datetime.today().date()) db.session.add(new_photo) db.session.commit() return redirect('/sites/{}'.format(site_id))
def response(): if config.memcache.db: clearmem() action = cgi_get("action", choices=["post", "videopost", "comment", "photo", "photoset", "md"]) if action == "md": for dn, dz, fz in os.walk("md"): succeed([f[:-3] for f in fz]) user = cgi_get("user") if action == "comment": ent = Comment(user=user, post=cgi_get("post"), body=cgi_get("body")) elif action == "photo": pkey = cgi_get("key", required=False) if pkey: ent = Photo.query(Photo.key == pkey).get() else: ent = Photo() capt = cgi_get("caption", required=False) # imgs uploaded separately if capt: ent.caption = capt else: blurb = cgi_get("blurb", required=False) pkey = cgi_get("key", required=False) tags = cgi_get("tags", required=False) pmod = db.get_model(action) if pkey: ent = pmod.query(pmod.key == pkey).get() else: ent = pmod(user=user) ent.title = cgi_get("title") ent.live = cgi_get("live") if blurb: ent.blurb = blurb if tags: ent.tags = tags if action == "post": ent.body = cgi_get("body") ent.put() if action == "photo": # get photoset psk = cgi_get("photoset", required=False) if psk: ps = db.get(psk) # these are hacky -- fix list ops in ct if cgi_get("remove", default=False): # ps.photos.remove(ent.key) ps.photos = [p for p in ps.photos if p != ent.key] else: # ps.photos.append(ent.key) ps.photos = ps.photos + [ent.key] ps.put() succeed(ent.id())
def seed_photos_by_userid(user_id, sort='interesting', per_page=100): """ Seed a user's photos into database given a user_id. sort: faves, views, comments or interesting. per_page: The maximum value is 500. """ params = base_params() params['method'] = "flickr.photos.getPopular" params['user_id'] = user_id params['sort'] = 'interesting' params['extras'] = ','.join( ['description', 'date_taken', 'owner_name', 'geo', 'tags', 'url_q']) params['per_page'] = per_page response = requests.get(API_URL, params=params).json() photos = response['photos']['photo'] for p in photos: photo_id = p['id'].encode('utf-8') if db.session.query(Photo).get(photo_id) is None: user_id = p['owner'].encode('utf-8') username = p['ownername'].encode('utf-8') description = p['description']['_content'].encode('utf-8') tags = p['tags'].encode('utf-8') title = p['title'].encode('utf-8') url = p['url_q'].encode('utf-8') date_taken = p['datetaken'].encode('utf-8') lat = p['latitude'] lon = p['longitude'] if lat == 0 and lon == 0: country_code = None else: country_code = rg.search((lat, lon))[0]['cc'] photo = Photo(photo_id=photo_id, user_id=user_id, username=username, description=description, tags=tags, title=title, url=url, date_taken=date_taken, lat=lat, lon=lon, country_code=country_code) db_utils.add_photo(photo) else: pass
def load_photos(photos_filename): print("Photos") Photo.query.delete() for row in open(photos_filename): row = row.rstrip() file_path, file_name, trip_id, pp_id = row.split(",") photo = Photo(file_path=file_path, file_name=file_name, trip_id=trip_id, pp_id=pp_id) # We need to add to the session or it won't ever be stored db.session.add(photo) # Once we're done, we should commit our work db.session.commit()
def test_should_not_create_photo_without_id(self): test_photo = Photo().from_json( json.loads(''' {"code": "XxxXXxxXxxx", "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) self.assertTrue(test_photo == None)
def add_photo_to_trip(): '''adds a photo to the trip board for that location''' trip_id = request.form.get('trip') img_id = request.form.get('img_id') url = request.form.get('url') lat = request.form.get('lat') lon = request.form.get('lon') city_name = request.form.get('city_name') trip = Trip.get_trip(trip_id) # checks if authorized user is accessing this page if session.get('login') == trip.user_id: # if photo is not in the database, add it to the database if not Photo.get_photo(img_id): photo = Photo(img_id=int(img_id), url=url, lon=float(lon), lat=float(lat), city_name=city_name) db.session.add(photo) db.session.commit() # check if photo already exists in current users trip already_exists = TripPhotoRelationship.get_trip_photo(trip_id, img_id) if already_exists: return 'This photo is already in your trip board!' # photo is not in current trip board, add relationship to the database else: trip_photo = TripPhotoRelationship(trip_id=trip_id, photo_id=img_id) db.session.add(trip_photo) db.session.commit() return 'Photo Added' # unauthorized user, redirect to homepage else: flash('You do not have permission to access this feature') return 'Unauthorized User: Photo Not Added'
def create_photo(user_id, date_uploaded, date_taken, album_id, path, public_id=''): """Create and return photo instance""" photo = Photo(user_id=user_id, date_uploaded=date_uploaded, date_taken=date_taken, album_id=album_id, path=path, public_id=public_id, rating=0) db.session.add(photo) db.session.commit() return photo
def test_should_persist_like(self): test_photo = Photo().from_json( json.loads(''' {"code": "tedwefeqwfa4s", "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "3333333333333333333", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) dao = InstalikeSQLDAO() rows = dao.persist_like(test_photo) self.assertEqual(rows, 1)
def upload_file(): """Allow user to upload photos""" file = request.files['file'] caption = request.form['caption'] hashtag = request.form['hashtag'] user_id = session['user_id'] if file.name == '': flash('No selected photos') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = os.path.join(UPLOAD_FOLDER, filename) file.save(file_path) flash('Photo successfully uploaded') photo_user_id = session.get('user_id') new_photo = Photo(photo_user_id=photo_user_id, photo_url=('/' + file_path), caption=caption) db_hashtag = Hashtag.query.filter_by(hashtag=hashtag).first() if not db_hashtag: db_hashtag = Hashtag(hashtag=hashtag) new_photo.hashtags.append(db_hashtag) db.session.add(new_photo) db.session.commit() return redirect(f'/users/{user_id}') else: flash('Only png, jpg, jpeg, gif file types are allowed!') return redirect(request.url)
def import_from_lines(self, lines): """ 从多行数据录入数据库 :param lines: 待处理数据 :return: 读取到的记录数量,成功录入的记录数量 """ (total, succ) = (0, 0) for line in lines: total += 1 items = line.decode('utf-8').strip().split('\t') if len(items) < 3: CrawlerLogger.logger.info( 'line format error: {0}'.format(line)) continue tmp_photo = Photo(0, items[0], items[1], items[2]) if not self.insert_one_photo(tmp_photo): CrawlerLogger.logger.info( 'insert line failed: {0}'.format(line)) else: succ += 1 return total, succ
def upload(): # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect('/library') file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return redirect('/library') if file and allowed_photo_filename(file.filename): new_photo = Photo(user_id=session['user_id']) db.session.add(new_photo) db.session.flush() filename = f'{new_photo.photo_id}_{secure_filename(file.filename)}' s3.upload(file, filename) new_photo.original_photo = filename db.session.commit() return redirect(f'/processing/{new_photo.photo_id}')
def test_should_not_create_photo_from_empty_json(self): test_photo = Photo().from_json(json.loads('{}')) self.assertTrue(test_photo == None)
def test_should_not_create_photo_from_none(self): test_photo = Photo().from_json(None) self.assertTrue(test_photo == None)
def add_photo(link, username): newp = Photo(link=link, username=username) session.add(newp) session.commit()
def make_photo(post, fix_photosets, blog_name): photo_post = PhotoPost(id=post["id"]) caption_tag = post.find("photo-caption") if caption_tag: photo_post.caption = caption_tag.text.strip() # Only photosets have photo tags as children is_photoset = False if len(post.find_all("photo")) == 0 else True photos = [] # TODO: combine these two cases intelligently maybe possibly perhaps if is_photoset: # Here, we ignore the base photo-url tags photo_tags = post.find_all("photo") rows_per_image = None if fix_photosets: rows_per_image = get_image_rows(post, blog_name) for i, photo_tag in enumerate(photo_tags): photo_url_tags = photo_tag.find_all("photo-url") for img in photo_url_tags: if img["max-width"] == "1280": img_url = img.text.strip() caption = photo_tag["caption"] or None this_photo = Photo(post_id=post["id"], caption=caption, offset=i + 1, width=photo_tag["width"], height=photo_tag["height"], url=img_url) if fix_photosets and rows_per_image is not None: try: this_photo.row = rows_per_image[i] except IndexError: # print(f"encountered IndexError for index {i} on post {post['id']}") # print(f"rows per image: {rows_per_image}") print( f"unable to obtain the row for image {i + 1} for post {post['id']} - this photoset " f"will not display properly.") rows_per_image = None for photo in photos: photo.row = None this_photo.row = None photos.append(this_photo) else: photo_url_tags = post.find_all("photo-url") for img in photo_url_tags: if img["max-width"] == "1280": img_url = img.text.strip() this_photo = Photo(post_id=post["id"], offset=0, url=img_url) photos.append(this_photo) photo_post.is_photoset = is_photoset photo_post.photos = photos return photo_post