Marthee commited on
Commit
b1af348
·
verified ·
1 Parent(s): dcb3e16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -38
app.py CHANGED
@@ -1,51 +1,61 @@
1
- from flask import Flask, request, jsonify, abort
2
- import tsadropboxretrieval
3
- import pdftotext
4
  import requests
 
 
5
  app = Flask(__name__)
6
- # API_KEY = "adrpdftotext" # Replace with your actual API key
7
 
8
- # def check_api_key():
9
- # api_key = request.headers.get("x-api-key")
10
- # if api_key != API_KEY:
11
- # abort(403) # Forbidden if API key is missing or incorrect
12
- @app.route("/",methods=["GET", "POST"])
13
- def thismain():
14
- print('ayhaga')
15
- return jsonify('done')
16
-
17
- @app.route('/api/process-data', methods=['POST'])
18
- def process():
19
- # check_api_key()
20
- try:
21
-
22
- print('In process [Try]')
23
- data = request.form
24
- # data = json.loads(request.data)
25
- print('here',data)
26
- # data = request.get_json() # Use get_json() to parse JSON data
27
- # print('Received data:', data)
28
- # Extracting values
29
- filePath = data.get('filePath')
30
- groupName = data.get('groupName')
31
 
 
 
32
 
33
- # dbxTeam = tsadropboxretrieval.ADR_Access_DropboxTeam('user')
34
- # print('3')
35
- # md, res = dbxTeam.files_download(path=filePath)
36
- # pdf_data = res.content
37
- print('4')
38
-
 
 
 
 
39
 
40
- pdftext = pdftotext.texts_from_pdf(filePath,groupName)
41
- print('5')
 
 
 
42
 
43
- return jsonify(pdftext)
44
-
 
 
 
45
  except Exception as e:
46
- print(f"Error: {e}")
47
  return jsonify({"error": str(e)}), 500
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  if __name__ == '__main__':
50
  app.run(host='0.0.0.0', port=7860)
51
  # from flask import Flask, request, jsonify
 
1
+ from flask import Flask, send_from_directory, jsonify, redirect, url_for
2
+ import os
 
3
  import requests
4
+ from io import BytesIO
5
+
6
  app = Flask(__name__)
 
7
 
8
+ # Directory for storing temporary PDFs
9
+ PDF_DIRECTORY = './temp_pdfs'
10
+ TEMP_PDF_NAME = 'temp_downloaded.pdf'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Ensure the directory exists
13
+ os.makedirs(PDF_DIRECTORY, exist_ok=True)
14
 
15
+ @app.route('/download-pdf', methods=['GET'])
16
+ def download_pdf():
17
+ """
18
+ Download PDF from a Dropbox link and save it temporarily.
19
+ """
20
+ link = 'https://www.dropbox.com/scl/fi/fjykwhhn9gu9t3kqrflxd/LA002-NOR-ZZ-ZZ-T-A-2403_Architectural-Specification-F10-Brick-and-Block-Walling_A4-_C01.pdf?rlkey=ek9i66i79m0hwp8z5yjs6rp5p&st=jh05a6qs&dl=0'
21
+
22
+ # Modify Dropbox link for direct download
23
+ if 'dl=0' in link:
24
+ link = link.replace('dl=0', 'dl=1')
25
 
26
+ try:
27
+ response = requests.get(link)
28
+ response.raise_for_status() # Raise exception for HTTP errors
29
+ pdf_content = BytesIO(response.content) # Store PDF in memory
30
+ local_pdf_path = os.path.join(PDF_DIRECTORY, TEMP_PDF_NAME)
31
 
32
+ # Save the PDF to the temporary directory
33
+ with open(local_pdf_path, 'wb') as f:
34
+ f.write(pdf_content.getvalue())
35
+
36
+ return jsonify({"message": "PDF downloaded successfully."})
37
  except Exception as e:
 
38
  return jsonify({"error": str(e)}), 500
39
 
40
+ @app.route('/view-pdf', methods=['GET'])
41
+ def view_pdf():
42
+ """
43
+ Serve the PDF for viewing in an iframe or direct link.
44
+ """
45
+ local_pdf_path = os.path.join(PDF_DIRECTORY, TEMP_PDF_NAME)
46
+ if os.path.exists(local_pdf_path):
47
+ return send_from_directory(PDF_DIRECTORY, TEMP_PDF_NAME)
48
+ else:
49
+ return jsonify({"message": "PDF not found. Please download it first via /download-pdf"}), 404
50
+
51
+ @app.route('/pdf-page/<int:page>', methods=['GET'])
52
+ def pdf_page(page):
53
+ """
54
+ Redirect to the PDF page with navigation.
55
+ """
56
+ return redirect(url_for('view_pdf') + f"#page={page}")
57
+
58
+
59
  if __name__ == '__main__':
60
  app.run(host='0.0.0.0', port=7860)
61
  # from flask import Flask, request, jsonify