103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
|
|
import requests
|
||
|
|
import subprocess
|
||
|
|
import readchar
|
||
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import random
|
||
|
|
import string
|
||
|
|
from bs4 import BeautifulSoup
|
||
|
|
|
||
|
|
URL = 'https://wallhaven.cc'
|
||
|
|
TMP_DIR = ".tmp"
|
||
|
|
|
||
|
|
def ping():
|
||
|
|
try:
|
||
|
|
return requests.get(URL, timeout=5).status_code
|
||
|
|
except:
|
||
|
|
return 0
|
||
|
|
|
||
|
|
def find_walls(page_number):
|
||
|
|
if page_number < 1:
|
||
|
|
return []
|
||
|
|
print(f"\n[Система] Загрузка списка обоев со страницы {page_number}...")
|
||
|
|
page = requests.get(f'{URL}/latest?page={page_number}')
|
||
|
|
content = BeautifulSoup(page.text, 'html.parser')
|
||
|
|
links = ["https:" + fig["data-src"] if fig["data-src"].startswith("//") else fig["data-src"]
|
||
|
|
for fig in content.find_all("img", class_='lazyload')]
|
||
|
|
return links
|
||
|
|
|
||
|
|
def make_tmp_name():
|
||
|
|
characters = string.ascii_letters + string.digits
|
||
|
|
return ''.join(random.choices(characters, k=10)) + ".jpg"
|
||
|
|
|
||
|
|
def download_page_thumbnails(links):
|
||
|
|
paths = []
|
||
|
|
if not os.path.exists(TMP_DIR):
|
||
|
|
os.mkdir(TMP_DIR)
|
||
|
|
|
||
|
|
for link in links:
|
||
|
|
save_path = os.path.join(TMP_DIR, make_tmp_name())
|
||
|
|
subprocess.run(['curl', '-s', '-o', save_path, link])
|
||
|
|
paths.append(save_path)
|
||
|
|
return paths
|
||
|
|
|
||
|
|
def print_wallpaper(file_path, page, pos, total_on_page):
|
||
|
|
os.system('clear')
|
||
|
|
print(f"--- Страница: {page} | Фото: {pos + 1} из {total_on_page} ---")
|
||
|
|
command = f'viu -h 25 "{file_path}"'
|
||
|
|
subprocess.run(command, shell=True)
|
||
|
|
print("\n[←/→] Фото | [↑/↓] Страницы | [Enter] Сохранить | [Esc] Выход")
|
||
|
|
|
||
|
|
def main():
|
||
|
|
if ping() != 200:
|
||
|
|
print("Сервер недоступен!")
|
||
|
|
return
|
||
|
|
|
||
|
|
cache = {}
|
||
|
|
current_page = 1
|
||
|
|
current_pos = 0
|
||
|
|
|
||
|
|
try:
|
||
|
|
while True:
|
||
|
|
if current_page not in cache:
|
||
|
|
links = find_walls(current_page)
|
||
|
|
if not links:
|
||
|
|
print("Обои не найдены на этой странице.")
|
||
|
|
current_page = max(1, current_page - 1)
|
||
|
|
continue
|
||
|
|
cache[current_page] = download_page_thumbnails(links)
|
||
|
|
|
||
|
|
images = cache[current_page]
|
||
|
|
total_on_page = len(images)
|
||
|
|
|
||
|
|
current_pos = max(0, min(current_pos, total_on_page - 1))
|
||
|
|
|
||
|
|
print_wallpaper(images[current_pos], current_page, current_pos, total_on_page)
|
||
|
|
|
||
|
|
key = readchar.readkey()
|
||
|
|
|
||
|
|
if key == readchar.key.RIGHT:
|
||
|
|
current_pos = (current_pos + 1) % total_on_page
|
||
|
|
elif key == readchar.key.LEFT:
|
||
|
|
current_pos = (current_pos - 1) % total_on_page
|
||
|
|
elif key == readchar.key.UP:
|
||
|
|
current_page += 1
|
||
|
|
current_pos = 0
|
||
|
|
elif key == readchar.key.DOWN:
|
||
|
|
if current_page > 1:
|
||
|
|
current_page -= 1
|
||
|
|
current_pos = 0
|
||
|
|
elif key == readchar.key.ESC:
|
||
|
|
break
|
||
|
|
elif key == readchar.key.ENTER:
|
||
|
|
print(f"\n[!] Файл {images[current_pos]} выбран для скачивания (нужно дописать логику).")
|
||
|
|
readchar.readkey() # Пауза
|
||
|
|
|
||
|
|
finally:
|
||
|
|
if os.path.exists(TMP_DIR):
|
||
|
|
shutil.rmtree(TMP_DIR)
|
||
|
|
print("\nПрограмма завершена. Временные файлы удалены.")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|