Skip to content

Commit 79ed5a2

Browse files
committed
sorter sender
1 parent bec1e9d commit 79ed5a2

File tree

8 files changed

+104
-5
lines changed

8 files changed

+104
-5
lines changed

Backend/LatestSorter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def process_file(file_name):
118118
print(batch)
119119

120120
result=' '.join(str(element) for element in batch)
121-
new_content = '\n'.join(result[1:]).strip()
122-
121+
new_content = result
123122

123+
#print(result)
124124
updated_content =new_content
125125

126126
# Upload the updated content to S3

Backend/SortedPQYsender.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import boto3
2+
import io
3+
from fastapi import APIRouter
4+
import os
5+
import requests
6+
from fastapi.responses import FileResponse
7+
8+
app = APIRouter()
9+
10+
# S3 credentials
11+
AWS_ACCESS_KEY_ID = 'AKIAZTHHIOR4JJ5HLTUB'
12+
AWS_SECRET_ACCESS_KEY = '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'
13+
S3_BUCKET_NAME = 'learnmateai'
14+
S3_FOLDER_NAME = 'Sorted_PYQS/'
15+
16+
# Initialize S3 client
17+
s3_client = boto3.client(
18+
's3',
19+
aws_access_key_id=AWS_ACCESS_KEY_ID,
20+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
21+
)
22+
23+
PDFSHIFT_API_KEY = 'c3fc79849a954be4bf9c719cdd3fee52'
24+
25+
@app.get("/generate_pdf")
26+
def generate_pdf():
27+
# Retrieve all text files from the S3 folder
28+
response = s3_client.list_objects_v2(
29+
Bucket=S3_BUCKET_NAME,
30+
Prefix=S3_FOLDER_NAME
31+
)
32+
33+
file_names = [obj['Key'] for obj in response['Contents'] if obj['Key'].endswith('.txt')]
34+
35+
# Generate HTML content
36+
content = ""
37+
38+
for i,file_name in enumerate(file_names, start=1):
39+
# Retrieve text content from each text file
40+
obj = s3_client.get_object(Bucket=S3_BUCKET_NAME, Key=file_name)
41+
text_content = obj['Body'].read().decode('utf-8')
42+
43+
# Replace newline characters with <br> tags
44+
text_content = text_content.replace("\n", "<br>")
45+
46+
# Define the CSS class and color for the container
47+
css_class = f"box box-{i % 2 + 1}" # Alternate between box-1 and box-2
48+
color = "lightblue" if i % 2 == 1 else "lightgreen"
49+
50+
# Add heading and content to the HTML
51+
content += f"<div class='{css_class}'>"
52+
content += f"<h2>Module {i}</h2>"
53+
content += f"<p>{text_content}</p>"
54+
content += "</div>"
55+
56+
# Prepare the HTML payload
57+
html_payload = f"""
58+
<html>
59+
<head>
60+
<style>
61+
.box {{
62+
margin: 10px;
63+
padding: 10px;
64+
font-family: Arial, sans-serif;
65+
font-size: 12px;
66+
}}
67+
.box-1 {{
68+
background-color: lightblue;
69+
}}
70+
.box-2 {{
71+
background-color: lightgreen;
72+
}}
73+
</style>
74+
</head>
75+
<body>
76+
{content}
77+
</body>
78+
</html>
79+
"""
80+
81+
# Convert HTML to PDF using PDFShift API
82+
response = requests.post(
83+
'https://api.pdfshift.io/v3/convert/pdf',
84+
auth=('api',PDFSHIFT_API_KEY),
85+
json={'source': html_payload,"landscape": False, "use_print": False}
86+
)
87+
88+
response.raise_for_status()
89+
90+
# Save PDF response to a temporary file
91+
temp_pdf_path = "temp_pdf/temp_pdf.pdf" # Replace with the desired path to save the temporary PDF file
92+
with open(temp_pdf_path, "wb") as temp_pdf_file:
93+
for chunk in response.iter_content(chunk_size=8192):
94+
temp_pdf_file.write(chunk)
95+
96+
# Return the temporary PDF file
97+
return FileResponse(temp_pdf_path, filename='combined_pdf.pdf', media_type='application/pdf')
-26 Bytes
Binary file not shown.
2.4 KB
Binary file not shown.

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ COPY Backend ./Backend
1111
COPY client ./client
1212
COPY Files ./Files
1313
COPY images ./images
14+
COPY images ./temp_pdf
1415
COPY Local_Storage ./Local_Storage
1516

1617
COPY requirements.txt .

__pycache__/app.cpython-310.pyc

136 Bytes
Binary file not shown.

app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#from Backend.Notes_Analyser import router as api4_router
99
#from Backend.Narrator import router as api5_router
1010
from Backend.NotesChunker import app as chunker
11-
#from Backend.NotesToText import router as notestotxt
11+
from Backend.NotesToText import router as notestotxt
12+
from Backend.SortedPQYsender import app as pyqsender
1213

1314
# import other API routers as needed
1415

@@ -31,9 +32,9 @@
3132
#app.include_router(api1_router)
3233

3334
app.include_router(sorter)
34-
#app.include_router(api4_router)
35+
app.include_router(notestotxt)
3536
app.include_router(chunker)
36-
#app.include_router(notestotxt)
37+
app.include_router(pyqsender)
3738

3839
# include other API routers as needed
3940

temp_pdf/temp_pdf.pdf

31.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)