genstava789 commited on
Commit
6779b5c
·
1 Parent(s): a7af4ce
Files changed (1) hide show
  1. bot.py +22 -25
bot.py CHANGED
@@ -200,16 +200,6 @@ def get_progress_bar_string(pct) -> str:
200
  p_str += "□" * (12 - cFull)
201
  return f"[{p_str}]"
202
 
203
- def format_size(size_bytes) -> str:
204
- """Convert bytes to a human-readable format."""
205
- if size_bytes == 0:
206
- return "0B"
207
- size_name = ("B", "KB", "MB", "GB", "TB")
208
- i = int(math.floor(math.log(size_bytes, 1024)))
209
- p = math.pow(1024, i)
210
- s = round(size_bytes / p, 2)
211
- return f"{s} {size_name[i]}"
212
-
213
  # Function to extract tweet IDs
214
  def extract_tweet_ids(text: str) -> Optional[list[str]]:
215
  """Extract tweet IDs from message."""
@@ -238,20 +228,10 @@ def scrape_media(tweet_id: int) -> list[dict]:
238
  raise Exception(f'API returned error: {html.unescape(match.group(1))}')
239
  raise
240
 
241
- async def get_media_size(media_url: str) -> int:
242
- """Get the content length of the media."""
243
- async with aiohttp.ClientSession() as session:
244
- async with session.head(media_url) as resp:
245
- content_length = resp.headers.get('CONTENT-LENGTH')
246
- if content_length is not None:
247
- return int(content_length)
248
- else:
249
- return -1 # Indicate that the size is unknown
250
-
251
  async def simulate_download_progress(media_info: dict, message: Message):
252
- # Use aiohttp to get the actual size of the media
253
- total_size = await get_media_size(media_info['url'])
254
 
 
 
255
  for progress in range(0, 101, 10):
256
  progress_bar = get_progress_bar_string(progress)
257
  if total_size > 0:
@@ -262,7 +242,23 @@ async def simulate_download_progress(media_info: dict, message: Message):
262
 
263
  await message.edit_text(f"Downloading: {progress_text}")
264
  await asyncio.sleep(0.5)
265
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  # Function to reply with media
267
  async def reply_media(client: Client, chat_id: int, tweet_media: list):
268
  """Reply to message with supported media."""
@@ -431,9 +427,9 @@ async def download_tiktok(client: Client, message: Message):
431
  await simulate_download_progress({"url": url}, progress_message)
432
  await client.send_video(user_id, video_bytes, caption=f"Downloaded by {author_username}", reply_to_message_id=message.id)
433
  else:
434
- await message.reply("Failed to download the video.", reply_to_message_id=message.id)
435
  else:
436
- await message.reply("Invalid TikTok URL.", reply_to_message_id=message.id)
437
 
438
  # Delete the progress message after sending the media
439
  await progress_message.delete()
@@ -441,6 +437,7 @@ async def download_tiktok(client: Client, message: Message):
441
  await message.reply(f"An error occurred: {str(e)}", reply_to_message_id=message.id)
442
 
443
 
 
444
  def run_bot():
445
  loop = asyncio.new_event_loop()
446
  asyncio.set_event_loop(loop)
 
200
  p_str += "□" * (12 - cFull)
201
  return f"[{p_str}]"
202
 
 
 
 
 
 
 
 
 
 
 
203
  # Function to extract tweet IDs
204
  def extract_tweet_ids(text: str) -> Optional[list[str]]:
205
  """Extract tweet IDs from message."""
 
228
  raise Exception(f'API returned error: {html.unescape(match.group(1))}')
229
  raise
230
 
 
 
 
 
 
 
 
 
 
 
231
  async def simulate_download_progress(media_info: dict, message: Message):
 
 
232
 
233
+ total_size = await get_media_size(media_info['url'])
234
+
235
  for progress in range(0, 101, 10):
236
  progress_bar = get_progress_bar_string(progress)
237
  if total_size > 0:
 
242
 
243
  await message.edit_text(f"Downloading: {progress_text}")
244
  await asyncio.sleep(0.5)
245
+
246
+ async def get_media_size(url: str) -> int:
247
+ async with httpx.AsyncClient() as client:
248
+ response = await client.head(url)
249
+ return int(response.headers.get('Content-Length', 0))
250
+
251
+ def get_progress_bar_string(progress: int) -> str:
252
+ return f"[{'■' * (progress // 10)}{' ' * (10 - (progress // 10))}]"
253
+
254
+ def format_size(size: int) -> str:
255
+ # Convert size to a human-readable format
256
+ for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
257
+ if size < 1024:
258
+ return f"{size:.2f} {unit}"
259
+ size /= 1024
260
+ return f"{size:.2f} TB"
261
+
262
  # Function to reply with media
263
  async def reply_media(client: Client, chat_id: int, tweet_media: list):
264
  """Reply to message with supported media."""
 
427
  await simulate_download_progress({"url": url}, progress_message)
428
  await client.send_video(user_id, video_bytes, caption=f"Downloaded by {author_username}", reply_to_message_id=message.id)
429
  else:
430
+ await message.reply("Failed to download the video. Please check the URL and try again.", reply_to_message_id=message.id)
431
  else:
432
+ await message.reply("Invalid TikTok URL. Please provide a valid URL.", reply_to_message_id=message.id)
433
 
434
  # Delete the progress message after sending the media
435
  await progress_message.delete()
 
437
  await message.reply(f"An error occurred: {str(e)}", reply_to_message_id=message.id)
438
 
439
 
440
+
441
  def run_bot():
442
  loop = asyncio.new_event_loop()
443
  asyncio.set_event_loop(loop)