coollsd commited on
Commit
e811cd8
·
verified ·
1 Parent(s): b009aa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -201,10 +201,11 @@ HTML_CONTENT = """
201
  margin: 15% auto;
202
  padding: 20px;
203
  border: 1px solid #333;
204
- width: 80%;
205
  max-width: 600px;
206
  border-radius: 10px;
207
  color: #e0e0e0;
 
208
  }
209
  .close {
210
  color: #aaa;
@@ -226,7 +227,23 @@ HTML_CONTENT = """
226
  border: 1px solid #555;
227
  color: #e0e0e0;
228
  border-radius: 5px;
229
- text-align: center;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  }
231
  </style>
232
  </head>
@@ -254,9 +271,9 @@ HTML_CONTENT = """
254
  <div class="modal-content">
255
  <span class="close">&times;</span>
256
  <h2>Embed Video Link</h2>
257
- <p>Copy the link to embed the video for discord:</p>
258
  <input type="text" id="embedLink" readonly>
259
- <button onclick="copyEmbedLink()" class="btn" style="margin-top: 10px;">Copy Embed Link</button>
260
  </div>
261
  </div>
262
 
@@ -421,7 +438,7 @@ HTML_CONTENT = """
421
  const copyBtn = document.createElement('button');
422
  copyBtn.textContent = 'Copy Link';
423
  copyBtn.className = 'copy-btn';
424
- copyBtn.onclick = () => {
425
  navigator.clipboard.writeText(window.location.origin + url).then(() => {
426
  alert('Link copied to clipboard!');
427
  });
@@ -430,7 +447,7 @@ HTML_CONTENT = """
430
  linkContainer.appendChild(link);
431
  linkContainer.appendChild(copyBtn);
432
 
433
- if (fileName.toLowerCase().endsWith('.mp4')) {
434
  const embedBtn = document.createElement('button');
435
  embedBtn.textContent = 'Embed Video for Discord';
436
  embedBtn.className = 'embed-btn';
@@ -493,11 +510,11 @@ async def handle_video_stream(path: str, request: Request):
493
  range_header = request.headers.get('Range')
494
 
495
  headers = {'Range': range_header} if range_header else {}
496
- response = requests.get(original_url, headers=headers, stream=True)
 
497
 
498
  def generate():
499
- for chunk in response.iter_content(chunk_size=8192):
500
- yield chunk
501
 
502
  headers = dict(response.headers)
503
  headers['Access-Control-Allow-Origin'] = '*'
@@ -550,19 +567,16 @@ async def embed_video(url: str, thumbnail: str):
550
  return HTMLResponse(content=html)
551
 
552
  async def get_cookies() -> Dict[str, str]:
553
- try:
554
- response = requests.get('https://replicate.com/levelsio/neon-tokyo', headers={
555
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
556
  })
557
  return dict(response.cookies)
558
- except Exception as e:
559
- print(f'Error fetching the page: {e}')
560
- return {}
561
 
562
  async def initiate_upload(cookies: Dict[str, str], filename: str, content_type: str) -> Dict:
563
  url = f'https://replicate.com/api/upload/{filename}?content_type={content_type}'
564
- try:
565
- response = requests.post(url, cookies=cookies, headers={
566
  'X-CSRFToken': cookies.get('csrftoken'),
567
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
568
  'Referer': 'https://replicate.com/levelsio/neon-tokyo',
@@ -580,19 +594,13 @@ async def initiate_upload(cookies: Dict[str, str], filename: str, content_type:
580
  print(f'Initiate upload response headers: {response.headers}')
581
  print(f'Response body: {response.text}')
582
  return response.json()
583
- except Exception as e:
584
- print(f'Error initiating upload: {e}')
585
- raise
586
 
587
  async def upload_file(upload_url: str, file_content: bytes, content_type: str) -> bool:
588
- try:
589
- response = requests.put(upload_url, data=file_content, headers={'Content-Type': content_type})
590
  print(f'File upload response status: {response.status_code}')
591
  print(f'File upload response headers: {response.headers}')
592
  return response.status_code == 200
593
- except Exception as e:
594
- print(f'Error uploading file: {e}')
595
- return False
596
 
597
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
598
  for attempt in range(1, max_retries + 1):
@@ -603,7 +611,7 @@ async def retry_upload(upload_url: str, file_content: bytes, content_type: str,
603
  return True
604
  if attempt < max_retries:
605
  print(f'Upload failed, retrying in {delay}s...')
606
- time.sleep(delay)
607
  delay *= 2 # Exponential backoff
608
  print('Upload failed after all retry attempts')
609
  return False
 
201
  margin: 15% auto;
202
  padding: 20px;
203
  border: 1px solid #333;
204
+ width: 90%;
205
  max-width: 600px;
206
  border-radius: 10px;
207
  color: #e0e0e0;
208
+ text-align: center;
209
  }
210
  .close {
211
  color: #aaa;
 
227
  border: 1px solid #555;
228
  color: #e0e0e0;
229
  border-radius: 5px;
230
+ font-size: 14px;
231
+ word-break: break-all;
232
+ }
233
+ .copy-embed-btn {
234
+ background-color: #ffffff;
235
+ color: #121212;
236
+ border: none;
237
+ padding: 10px 20px;
238
+ border-radius: 5px;
239
+ cursor: pointer;
240
+ transition: all 0.3s ease;
241
+ font-weight: 600;
242
+ font-size: 16px;
243
+ margin-top: 15px;
244
+ }
245
+ .copy-embed-btn:hover {
246
+ background-color: #f0f0f0;
247
  }
248
  </style>
249
  </head>
 
271
  <div class="modal-content">
272
  <span class="close">&times;</span>
273
  <h2>Embed Video Link</h2>
274
+ <p>Copy the following link to embed the video:</p>
275
  <input type="text" id="embedLink" readonly>
276
+ <button onclick="copyEmbedLink()" class="copy-embed-btn">Copy Embed Link</button>
277
  </div>
278
  </div>
279
 
 
438
  const copyBtn = document.createElement('button');
439
  copyBtn.textContent = 'Copy Link';
440
  copyBtn.className = 'copy-btn';
441
+ copyBtn.onclick = () => {
442
  navigator.clipboard.writeText(window.location.origin + url).then(() => {
443
  alert('Link copied to clipboard!');
444
  });
 
447
  linkContainer.appendChild(link);
448
  linkContainer.appendChild(copyBtn);
449
 
450
+ if (fileName.toLowerCase().endsWith('.mp4')) {
451
  const embedBtn = document.createElement('button');
452
  embedBtn.textContent = 'Embed Video for Discord';
453
  embedBtn.className = 'embed-btn';
 
510
  range_header = request.headers.get('Range')
511
 
512
  headers = {'Range': range_header} if range_header else {}
513
+ async with httpx.AsyncClient() as client:
514
+ response = await client.get(original_url, headers=headers)
515
 
516
  def generate():
517
+ yield from response.iter_bytes()
 
518
 
519
  headers = dict(response.headers)
520
  headers['Access-Control-Allow-Origin'] = '*'
 
567
  return HTMLResponse(content=html)
568
 
569
  async def get_cookies() -> Dict[str, str]:
570
+ async with httpx.AsyncClient() as client:
571
+ response = await client.get('https://replicate.com/levelsio/neon-tokyo', headers={
572
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
573
  })
574
  return dict(response.cookies)
 
 
 
575
 
576
  async def initiate_upload(cookies: Dict[str, str], filename: str, content_type: str) -> Dict:
577
  url = f'https://replicate.com/api/upload/{filename}?content_type={content_type}'
578
+ async with httpx.AsyncClient() as client:
579
+ response = await client.post(url, cookies=cookies, headers={
580
  'X-CSRFToken': cookies.get('csrftoken'),
581
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
582
  'Referer': 'https://replicate.com/levelsio/neon-tokyo',
 
594
  print(f'Initiate upload response headers: {response.headers}')
595
  print(f'Response body: {response.text}')
596
  return response.json()
 
 
 
597
 
598
  async def upload_file(upload_url: str, file_content: bytes, content_type: str) -> bool:
599
+ async with httpx.AsyncClient() as client:
600
+ response = await client.put(upload_url, data=file_content, headers={'Content-Type': content_type})
601
  print(f'File upload response status: {response.status_code}')
602
  print(f'File upload response headers: {response.headers}')
603
  return response.status_code == 200
 
 
 
604
 
605
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
606
  for attempt in range(1, max_retries + 1):
 
611
  return True
612
  if attempt < max_retries:
613
  print(f'Upload failed, retrying in {delay}s...')
614
+ await asyncio.sleep(delay)
615
  delay *= 2 # Exponential backoff
616
  print('Upload failed after all retry attempts')
617
  return False