def process_photo(photo_id): """Convert photo""" json = request.get_json() dataset_id = json.get('dataset_id', 0) if dataset_id == 0: model_name = 'portraits_pix2pix' else: dataset = Dataset.query.filter( Dataset.state == Dataset.TRAINING_COMPLETED, Dataset.dataset_id == dataset_id, Dataset.user_id == session['user_id'], ).one() model_name = dataset.model_filename photo = Photo.query.filter( Photo.user_id == session['user_id'], Photo.photo_id == photo_id, ).one() s3.get_image(photo.original_photo) processed_filename = colorize.process(UPLOAD_FOLDER, photo.original_photo, model_name) photo.processed_photo = processed_filename db.session.commit() file = open(os.path.join(app.config['UPLOAD_FOLDER'], processed_filename), 'rb') s3.upload(file, processed_filename, content_type='image/png') url = s3.url_for(processed_filename) return url
def webhook(): data = json.loads(request.forms.get('payload')) repo_name = data['repository']['name'] repo_user = data['repository']['owner']['name'] print 'Updating local copy of %s' % repo_name repo.update(repo_name, repo_user) print 'Uploading %s' % repo_name s3.upload(repo_name)
def do_work(worker, data): out_path = util.tempnam(util.get_extension(data['output'])) try: midi.synthesise(s3.url(data['bucket'], data['input']), out_path, default_program=73) # 73=flute s3.upload(data['bucket'], out_path, data['output']) finally: os.unlink(out_path) worker.log('Synthesised %s' % data['input'])
def create_new_qr_code(user_email): qr_text = "impression://" + user_email filename = str(WORKING_DIR) + "/temp/QR_" + user_email + ".png" qr_code = pyqrcode.create(qr_text) qr_code.png(filename, scale=5, module_color=[0, 0, 0, 128], background=[0xFF, 0xFF, 0xFF]) s3.upload(filename, user_email + "/qr.png") os.remove(filename)
def test_upload(self): for test in self.upload_test_params: if test[KEY_ID] == 1: with mock.patch('boto3.s3.transfer.S3Transfer.upload_file', self.empty_2): try: s3.upload(test[KEY_INPUT[0]], test[KEY_INPUT[1]]) response = "Pass" except: response = "Unexpected Exception" expected = test[KEY_EXPECTED] self.assertEqual(response, expected)
def my_shot(): with picamera.PiCamera() as camera: print("start to capture") camera.rotation = rotation camera.capture(TEMPFILE) print("shot") put_filename = create_put_filename() s3.upload(TEMPFILE, BUCKET, put_filename) return jsonify({"success": True, "filename": put_filename}) return jsonify({"success": False, "filename": None})
def upload(): '''Upload a new video.''' if request.method == 'POST': video = request.files.get('file') name = request.form.get('name') if not (video and name): flash("You must fill in all the fields") return redirect('/upload.html') else: try: filename = videos.save(video) # flask writes the video to disk filepath = os.path.join(app.config['UPLOADED_VIDEOS_DEST'], filename) except UploadNotAllowed: flash("Only videos are allowed.") return redirect('/upload.html') else: s3_url = s3.upload(filepath) cloudfront_url = cloudfront.distribute(s3_url=s3_url) try: rds.save_video(name=name, s3_url=s3_url, streaming_url=cloudfront_url) return redirect('/index.html') except IntegrityError as err: flash("Duplicate video title. Try again.") return redirect('/upload.html') elif request.method == 'GET': return redirect('/upload.html')
def test_storage(self): """ Storage testing. """ text = '' storage = S3Storage(host='s3.amazonaws.com') file_length = random.randrange(300, 1300) text = get_string(file_length) filename_length = random.randrange(5, 12) filename = get_string(filename_length) self.assertFalse(storage.exists(filename)) test_file = ContentFile(text) test_file.name = filename uploaded_url = upload(test_file, host='s3.amazonaws.com') self.assertTrue(storage.exists(filename)) url = 'http://' + BOTO_S3_BUCKET + '.s3.amazonaws.com/' + filename self.assertEqual(uploaded_url, url) page = urllib2.urlopen(uploaded_url) self.assertEqual(text, page.read()) self.assertEqual(len(text), storage.size(filename)) self.assertEqual(url, storage.url(filename)) storage.delete(filename) self.assertFalse(storage.exists(filename))
def upload( key, data ): signal( 'uploading', data={ 'key' : key } ) s3.upload( s3_access, s3_secret, bucket, key, data=data, headers={ 'x-amz-acl' : 'public-read', 'Content-Type' : 'image/jpeg', 'Content-Length' : len(data) }, callback=(lambda response: upload_complete( response, key )) )
def runv2json(video, h, results): status[h] = "Downloading from S3..." pwd = os.getcwd() + '/' with tempfile.TemporaryDirectory() as td: os.chdir(td) if video.startswith('http'): filename = subprocess.run(['youtube-dl', '--get-filename', video], universal_newlines=True, stdout=subprocess.PIPE).stdout[:-1] else: filename = video jb = s3.download(h) if not jb: if video.startswith('http'): status[h] = "Downloading video..." ytdl = subprocess.run(["youtube-dl", video], check=True) try: open(filename) except FileNotFoundError: filename = os.path.splitext(filename)[0] + ".mkv" try: open(filename) except FileNotFoundError: status[h] = "ERROR. Try again" return status[h] = "Getting images..." fps = subprocess.run([pwd + "getimages.sh", filename], check=True, universal_newlines=True, stdout=subprocess.PIPE).stdout.split('\n')[0] status[h] = "OCRing images..." sd = ocr.img2text(td, fps) status[h] = "Extracting audio..." subprocess.run([pwd + "getaudio.sh", filename]) status[h] = "Transcribing audio..." ad = audio.aud2text(filename + ".wav", h) status[h] = "Converting to JSON..." d = {'screen': sd, 'audio': ad} jv = json.dumps(d) s = io.BytesIO(jv.encode()) status[h] = "Uploading to S3..." s3.upload(s, h) results[h] = jv else: results[h] = jb print("FINISHED " + h)
def callback(ch, method, properties, body): print("callback run") print(str(body)[2:-1]) path = detect(str(body)[2:-1]) url = upload(path) removeFile(path) produce(url) print("callback done")
def save(): """Turn off auto-saving, and then upload the file to S3.""" with minecraft_server_lock: if minecraft_server: minecraft_server.stdin.write('/save-off\n') minecraft_server.stdin.write('/save-all\n') minecraft_server.stdin.flush() time.sleep(1) # FIXME try: now = datetime.datetime.now().isoformat() s3.pack() s3.upload(now) return json.dumps(now) finally: if minecraft_server: minecraft_server.stdin.write('/save-on\n') minecraft_server.stdin.flush() return json.dumps('ok')
def upload(filename): """ Makes 10 attempts to upload image to S3 server. Returns true if image is uploaded successfully, false if not """ uploaded = False attempts = 0 while (not uploaded) and (attempts < 10): uploaded = s3.upload('uploads/' + filename) attempts += 1 return uploaded
def main(): """ Use command line args and config to do the business """ # prepare options = get_options() output_dir = options['out_dir'] empty_dir(output_dir) # create various files in output directory static_filenames = assets.make_static_assets(options) pages.make_404(output_dir, static_filenames) post_datas = pages.make_posts( options['publish'], options['post_source_dir'], output_dir, static_filenames, ) pages.make_homepage(output_dir, static_filenames, post_datas) print('output done ' + datetime.datetime.now().strftime('%H:%M:%S')) # upload if options['upload']: s3.upload(options['out_dir']) print('upload done')
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 capture_and_upload_image(): imgpath = capture() dest_path = upload(S3_BUCKET_NAME, imgpath) remove(imgpath) return dest_path
def search_tweets(): auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) today = datetime.today() one_day_ago = relativedelta(days=1) yesterday = datetime.strftime(today - one_day_ago, f"%Y-%m-%d") q = f"#松岡茉優 OR 松岡茉優 -'松岡茉優似' filter:media exclude:retweets min_faves:10 since:{yesterday} min_retweets:0" tweets = tweepy.Cursor( api.search, q=q, tweet_mode='extended', result_type="mixed", include_entities=True, ).items(20) contents = [] for tweet in tweets: print(tweet.full_text) try: media = tweet.extended_entities['media'] print(media) for m in media: print(m) preview = m['media_url_https'] if m['type'] == 'video': origin = [ variant['url'] for variant in m['video_info']['variants'] if variant['content_type'] == 'video/mp4' ][0] else: origin = m['media_url_https'] content = { 'preview': preview, 'origin': origin, 'type': m['type'] } contents.append(content) print('--------------------------------------------') except: print('noUrl') print('--------------------------------------------') messages = [] for index, media in enumerate(contents): print(media['origin']) item = VideoSendMessage( original_content_url=media['origin'], preview_image_url=media['preview'] ) if media['type'] == 'video' else ImageSendMessage( original_content_url=media['origin'], preview_image_url=media['preview']) messages.append(item) time.sleep(1) try: s3.upload(media['origin'], index) continue except urllib.error.URLError as e: print('TIMEOUT ERROR') print(e) continue else: print('maybe unknown error') continue return [messages[0:5], messages[5:10]]
import s3 def help(): print('Usage:') print( 'python manage_vcpkg_archive_cache.py <action> <dir> <os> <bucket> <access_key> <secret_key>' ) if (len(sys.argv) != 7 and len(sys.argv) != 5): help() sys.exit(1) action_name = sys.argv[1] dir_name = sys.argv[2] os_name = sys.argv[3] bucket_name = sys.argv[4] if (action_name == 'upload' and len(sys.argv) == 7): access_key = sys.argv[5] secret_key = sys.argv[6] s3.upload(os_name, dir_name, bucket_name, access_key, secret_key) elif (action_name == 'download'): s3.download(os_name, dir_name, bucket_name) else: help() sys.exit(1) sys.exit(0)