nagasurendra commited on
Commit
ecdf36a
·
verified ·
1 Parent(s): 5c5d1ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -355,27 +355,49 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
355
  # Zapier webhook settings
356
  ZAPIER_WEBHOOK_URL = os.getenv('ZAPIER_WEBHOOK_URL') # Load webhook URL from environment variable
357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
 
359
 
360
  @app.route('/camera')
361
  def camera():
362
- return render_template('camera.html') # Render the camera capture page
363
 
364
  @app.route('/capture', methods=['POST'])
365
  def capture():
366
  try:
367
- # Get the base64 encoded image from the request
368
  data = request.form['image']
369
  header, encoded = data.split(",", 1)
370
  binary_data = base64.b64decode(encoded)
371
 
372
- # Save the image to the server
373
  filename = f"capture_{len(os.listdir(app.config['UPLOAD_FOLDER'])) + 1}.jpg"
374
  filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
375
  with open(filepath, "wb") as f:
376
  f.write(binary_data)
377
 
378
- # Return the URL of the saved image
379
  image_url = f"/{filepath}"
380
  return jsonify({
381
  'status': 'success',
@@ -388,12 +410,10 @@ def capture():
388
  @app.route('/upload_zapier', methods=['POST'])
389
  def upload_zapier():
390
  try:
391
- # Get the image URL from the request
392
  image_url = request.form['image_url']
393
  if not image_url.startswith('/static/captures/'):
394
  return jsonify({'status': 'error', 'message': 'Invalid image path.'})
395
 
396
- # Send the image URL to Zapier
397
  result = send_to_zapier(image_url.lstrip('/'))
398
  if 'error' in result:
399
  return jsonify({'status': 'error', 'message': result['error']})
@@ -402,30 +422,6 @@ def upload_zapier():
402
  logging.error(f"Error in upload_zapier: {str(e)}")
403
  return jsonify({'status': 'error', 'message': str(e)})
404
 
405
- def send_to_zapier(image_path):
406
- if not ZAPIER_WEBHOOK_URL:
407
- logging.error("Zapier webhook URL not set.")
408
- return {"error": "Zapier webhook URL not set. Please set the ZAPIER_WEBHOOK_URL environment variable."}
409
- try:
410
- payload = {
411
- 'image_url': request.url_root + image_path,
412
- 'filename': os.path.basename(image_path),
413
- 'content_type': 'image/jpeg'
414
- }
415
-
416
- session = requests.Session()
417
- retries = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504])
418
- session.mount('https://', HTTPAdapter(max_retries=retries))
419
- logging.debug(f"Sending image URL to Zapier webhook: {ZAPIER_WEBHOOK_URL}")
420
- response = session.post(ZAPIER_WEBHOOK_URL, json=payload, timeout=10)
421
- response.raise_for_status()
422
- logging.debug("Image URL sent to Zapier successfully.")
423
- return {"status": "success"}
424
- except requests.exceptions.RequestException as e:
425
- logging.error(f"Failed to send to Zapier: {str(e)}")
426
- if e.response is not None:
427
- logging.error(f"Response details: {e.response.text}")
428
- return {"error": f"Failed to send to Zapier: {str(e)}"}
429
 
430
 
431
 
 
355
  # Zapier webhook settings
356
  ZAPIER_WEBHOOK_URL = os.getenv('ZAPIER_WEBHOOK_URL') # Load webhook URL from environment variable
357
 
358
+ def send_to_zapier(image_path):
359
+ if not ZAPIER_WEBHOOK_URL:
360
+ logging.error("Zapier webhook URL not set.")
361
+ return {"error": "Zapier webhook URL not set. Please set the ZAPIER_WEBHOOK_URL environment variable."}
362
+ try:
363
+ payload = {
364
+ 'image_url': request.url_root + image_path,
365
+ 'filename': os.path.basename(image_path),
366
+ 'content_type': 'image/jpeg'
367
+ }
368
+
369
+ session = requests.Session()
370
+ retries = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504])
371
+ session.mount('https://', HTTPAdapter(max_retries=retries))
372
+ logging.debug(f"Sending image URL to Zapier webhook: {ZAPIER_WEBHOOK_URL}")
373
+ response = session.post(ZAPIER_WEBHOOK_URL, json=payload, timeout=10)
374
+ response.raise_for_status()
375
+ logging.debug("Image URL sent to Zapier successfully.")
376
+ return {"status": "success"}
377
+ except requests.exceptions.RequestException as e:
378
+ logging.error(f"Failed to send to Zapier: {str(e)}")
379
+ if e.response is not None:
380
+ logging.error(f"Response details: {e.response.text}")
381
+ return {"error": f"Failed to send to Zapier: {str(e)}"}
382
+
383
 
384
 
385
  @app.route('/camera')
386
  def camera():
387
+ return render_template('camera.html')
388
 
389
  @app.route('/capture', methods=['POST'])
390
  def capture():
391
  try:
 
392
  data = request.form['image']
393
  header, encoded = data.split(",", 1)
394
  binary_data = base64.b64decode(encoded)
395
 
 
396
  filename = f"capture_{len(os.listdir(app.config['UPLOAD_FOLDER'])) + 1}.jpg"
397
  filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
398
  with open(filepath, "wb") as f:
399
  f.write(binary_data)
400
 
 
401
  image_url = f"/{filepath}"
402
  return jsonify({
403
  'status': 'success',
 
410
  @app.route('/upload_zapier', methods=['POST'])
411
  def upload_zapier():
412
  try:
 
413
  image_url = request.form['image_url']
414
  if not image_url.startswith('/static/captures/'):
415
  return jsonify({'status': 'error', 'message': 'Invalid image path.'})
416
 
 
417
  result = send_to_zapier(image_url.lstrip('/'))
418
  if 'error' in result:
419
  return jsonify({'status': 'error', 'message': result['error']})
 
422
  logging.error(f"Error in upload_zapier: {str(e)}")
423
  return jsonify({'status': 'error', 'message': str(e)})
424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
 
426
 
427