File size: 5,215 Bytes
0237868
 
 
 
 
 
 
 
 
ba098c5
 
0237868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6daa7df
0237868
 
 
 
 
 
6daa7df
0237868
6daa7df
0237868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse, StreamingResponse
import asyncio
import random
import string
import uvicorn

app = FastAPI()


@app.head("/")
@app.get("/", response_class=HTMLResponse)
async def get_form():
    return """
    <html>
        <head>
            <title>URL Input</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    background-color: #f0f0f0;
                }
                form {
                    background: white;
                    padding: 2em;
                    border-radius: 5px;
                    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                    margin-bottom: 20px;
                }
                input[type="text"] {
                    width: 300px;
                    padding: 0.5em;
                    margin-bottom: 1em;
                    border: 1px solid #ccc;
                    border-radius: 4px;
                }
                input[type="text"][name="filename"] {
                    width: 200px;
                    margin-left: 10px;
                }
                button {
                    padding: 0.5em 1em;
                    color: white;
                    background-color: #007BFF;
                    border: none;
                    border-radius: 4px;
                    cursor: pointer;
                }
                button:hover {
                    background-color: #0056b3;
                }
                .output-box {
                    width: 80%;
                    height: 400px;
                    border: 1px solid #ccc;
                    border-radius: 4px;
                    overflow-y: auto;
                    padding: 1em;
                    background-color: white;
                }
                .output-content {
                    white-space: pre-wrap;
                    font-family: monospace;
                }
            </style>
        </head>
        <body>
            <div>
                <form id="urlForm" action="/submit" method="post">
                    <input type="text" name="url" id="urlInput" placeholder="Enter URL" required>
                    <input type="text" name="filename" id="filenameInput" placeholder="File Name">
                    <button type="submit">Submit</button>
                </form>
                <div class="output-box" id="output-box">
                    <div class="output-content" id="output-content"></div>
                </div>
            </div>
            <script>
                document.getElementById('urlInput').addEventListener('input', function() {
                    const url = this.value;
                    const defaultFilename = url.split('/').pop() || '';
                    if (filenameInput.value !== defaultFilename) {
                        filenameInput.value = defaultFilename;
                    }
                });

                const form = document.getElementById('urlForm');
                form.addEventListener('submit', async function(event) {
                    console.log('before');
                    event.preventDefault();
                    console.log('after');
                    const formData = new FormData(form);
                    const response = await fetch('/submit', {
                        method: 'POST',
                        body: formData
                    });

                    const reader = response.body.getReader();
                    const decoder = new TextDecoder();
                    const outputContent = document.getElementById('output-content');
                    const outputBox = document.getElementById('output-box');

                    outputContent.innerHTML = '';  // Clear previous content

                    while (true) {
                        const { done, value } = await reader.read();
                        if (done) break;
                        const chunk = decoder.decode(value, { stream: true });
                        outputContent.innerHTML += chunk;
                        outputBox.scrollTop = outputBox.scrollHeight;  // Scroll to the latest content
                    }
                });
            </script>
        </body>
    </html>
    """

@app.post("/submit")
async def submit_url(url: str = Form(...), filename: str = Form(None)):
    if not filename:
        filename = url.split("/")[-1]

    async def generate_html():
        yield f'<p>Filename: {filename}</p><p>Lorem Ipsum Text:</p>'
        lorem_text = ''.join(random.choices(string.ascii_letters + string.digits + ' ', k=1000))
        for char in lorem_text:
            await asyncio.sleep(0.01)  # 10ms delay
            yield char

    headers = {
        "Content-Type": "text/html",
        "Transfer-Encoding": "chunked",
        "Cache-Control": "no-cache",
        "X-Accel-Buffering": "no"
    }

    return StreamingResponse(generate_html(), headers=headers)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)