Advertisement
Guest User

Boosty downloader

a guest
Apr 16th, 2023
9,516
3
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 3 0
  1. import requests
  2. import sys
  3. import os
  4. import subprocess
  5.  
  6. print(f"Введите ник:")
  7. try:
  8.     name = input()
  9. except Exception as err:
  10.     print(err)
  11.  
  12. cwd = os.getcwd()
  13. links_list = []
  14. media_album_payload = {"type": "all", "limit_by": "media", "offset": None}
  15. media_album_url = f"https://api.boosty.to/v1/blog/{name}/media_album/"
  16. headers = requests.utils.default_headers()
  17. headers.update(
  18.     {
  19.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  20.     }
  21. )
  22.  
  23.  
  24. # Create subdirectory
  25. try:
  26.     os.mkdir(name)
  27.     print("subfolder created")
  28. except Exception:
  29.     pass
  30.  
  31.  
  32. def get_media_album():
  33.     return requests.get(
  34.         media_album_url, params=media_album_payload, headers=headers
  35.     ).json()
  36.  
  37.  
  38. def get_media_album_offset(media_album):
  39.     is_last = media_album["extra"]["isLast"]
  40.     offset = media_album["extra"]["offset"]
  41.     if not is_last:
  42.         return offset
  43.     else:
  44.         return None
  45.  
  46.  
  47. def get_posts_from_album(media_album):
  48.     media_posts = media_album["data"]["mediaPosts"]
  49.     for media_post in media_posts:
  50.         for media in media_post["media"]:
  51.             if media["id"] != "00000000-0000-0000-0000-000000000000":
  52.                 if media["type"] == "image":
  53.                     yield get_image_link(media["id"])
  54.                 elif media["type"] == "ok_video":
  55.                     yield get_video_link(media["vid"])
  56.                 else:
  57.                     continue
  58.  
  59.  
  60. def get_image_link(id):
  61.     return "https://images.boosty.to/image/" + id
  62.  
  63.  
  64. def get_video_link(vid):
  65.     return "https://ok.ru/videoembed/" + vid
  66.  
  67.  
  68. def set_payload_offset(offset):
  69.     media_album_payload["offset"] = offset
  70.  
  71.  
  72. open(os.path.join(cwd, f"{name}/{name}.txt"), "w").close()
  73.  
  74. # fill list from generator
  75. while True:
  76.     media_album = get_media_album()
  77.     for i in get_posts_from_album(media_album):
  78.         links_list.append(i)
  79.  
  80.     next_offset = get_media_album_offset(media_album)
  81.     set_payload_offset(next_offset)
  82.     if next_offset == None:
  83.         break
  84.  
  85. # sort links
  86. links_list.sort()
  87.  
  88. # Separate images from videos
  89. video_links = [k for k in links_list if "videoembed" in k]
  90. images_links = [k for k in links_list if "images" in k]
  91.  
  92. with open(os.path.join(cwd, f"{name}/{name}.txt"), "a+") as file:
  93.     for i in links_list:
  94.         print(i)
  95.         file.write(i + "\n")
  96.  
  97. with open(os.path.join(cwd, f"{name}/{name}-video.txt"), "a+") as file:
  98.     for i in video_links:
  99.         print(i)
  100.         file.write(i + "\n")
  101.  
  102. # Download images
  103. for image_url in images_links:
  104.     print(f"download image {image_url}")
  105.     img_data = requests.get(image_url).content
  106.     image_name = image_url.split("/")[-1]
  107.     with open(os.path.join(cwd, f"{name}/{image_name}.jpg"), "wb") as handler:
  108.         handler.write(img_data)
  109.  
  110.  
  111. # Execute yt-dlp
  112. def exec_yt_dlp():
  113.     print("yt-dlp started")
  114.     filename = f"{name}-video.txt"
  115.     args = f"yt-dlp -a {cwd}/{name}/{filename} -P {cwd}/{name}"
  116.     subprocess.run(args, shell=True)
  117.  
  118.  
  119. try:
  120.     exec_yt_dlp()
  121. except Exception as err:
  122.     print(f"{name}")
  123.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement