import requests
import sys
import os
import subprocess
print(f"Введите ник:")
try:
name = input()
except Exception as err:
print(err)
cwd = os.getcwd()
links_list = []
media_album_payload = {"type": "all", "limit_by": "media", "offset": None}
media_album_url = f"https://api.boosty.to/v1/blog/{name}/media_album/"
headers = requests.utils.default_headers()
headers.update(
{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
}
)
# Create subdirectory
try:
os.mkdir(name)
print("subfolder created")
except Exception:
pass
def get_media_album():
return requests.get(
media_album_url, params=media_album_payload, headers=headers
).json()
def get_media_album_offset(media_album):
is_last = media_album["extra"]["isLast"]
offset = media_album["extra"]["offset"]
if not is_last:
return offset
else:
return None
def get_posts_from_album(media_album):
media_posts = media_album["data"]["mediaPosts"]
for media_post in media_posts:
for media in media_post["media"]:
if media["id"] != "00000000-0000-0000-0000-000000000000":
if media["type"] == "image":
yield get_image_link(media["id"])
elif media["type"] == "ok_video":
yield get_video_link(media["vid"])
else:
continue
def get_image_link(id):
return "https://images.boosty.to/image/" + id
def get_video_link(vid):
return "https://ok.ru/videoembed/" + vid
def set_payload_offset(offset):
media_album_payload["offset"] = offset
open(os.path.join(cwd, f"{name}/{name}.txt"), "w").close()
# fill list from generator
while True:
media_album = get_media_album()
for i in get_posts_from_album(media_album):
links_list.append(i)
next_offset = get_media_album_offset(media_album)
set_payload_offset(next_offset)
if next_offset == None:
break
# sort links
links_list.sort()
# Separate images from videos
video_links = [k for k in links_list if "videoembed" in k]
images_links = [k for k in links_list if "images" in k]
with open(os.path.join(cwd, f"{name}/{name}.txt"), "a+") as file:
for i in links_list:
print(i)
file.write(i + "\n")
with open(os.path.join(cwd, f"{name}/{name}-video.txt"), "a+") as file:
for i in video_links:
print(i)
file.write(i + "\n")
# Download images
for image_url in images_links:
print(f"download image {image_url}")
img_data = requests.get(image_url).content
image_name = image_url.split("/")[-1]
with open(os.path.join(cwd, f"{name}/{image_name}.jpg"), "wb") as handler:
handler.write(img_data)
# Execute yt-dlp
def exec_yt_dlp():
print("yt-dlp started")
filename = f"{name}-video.txt"
args = f"yt-dlp -a {cwd}/{name}/{filename} -P {cwd}/{name}"
subprocess.run(args, shell=True)
try:
exec_yt_dlp()
except Exception as err:
print(f"{name}")