-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapod.py
executable file
·167 lines (140 loc) · 4.72 KB
/
apod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://www.makeuseof.com/beautiful-soup-tutorial/
import io
import requests
import datetime
from bs4 import BeautifulSoup
from PIL import Image, ExifTags
import os
import time
import yt_dlp
base_url = 'https://apod.nasa.gov/apod'
url = 'https://apod.nasa.gov/apod/astropix.html'
out_image = '/home/user/Pictures/apod.jpg'
default_image = '/usr/share/wallpapers/EndeavourOS/contents/images/endeavour_os_simple_wallpaper_dark.png'
tmp_path = '/tmp/apod'
best_quality = True
width_max = 1920
height_max = 1080
# ----------------------------------------------
# Обработка изображения
def processing_image(path, url=None):
if not os.path.exists(path):
print(f'\n1-Файл не найден: {path}')
exit()
im = Image.open(path)
w, h = im.size
print(f'\nwidth={w} height={h}')
# if w < h:
if w/h < 0.9:
# Поворачиваем изображение
print(f'\nw < h Rotate image')
im = im.transpose(Image.ROTATE_270)
w, h = im.size
if w > width_max and h > height_max:
# Уменьшаем изображение
print(f'\nwidth={w} height={h} Resize image near 1920x1080')
scale_w = w/width_max
scale_h = h/height_max
print(f'scale_x={round(scale_w,3)} scale_y={round(scale_h,3)}')
scale = min(scale_w, scale_h)
print(f'Min scale {round(scale,3)}')
print(f'New image size {round(w/scale)}x{round(h/scale)}')
im = im.resize((round(w/scale),round(h/scale)), Image.Resampling.BICUBIC)
im = im.convert('RGB')
im.info['comment'] = url
im.save(out_image, quality=90)
# Задаём обои
def set_background(path):
if not os.path.exists(path):
print(f'\n2-Файл не найден: {path}')
exit()
desktop_session = os.environ.get("XDG_CURRENT_DESKTOP")
print(f'\ndesktop_session: {desktop_session}')
# KDE
if desktop_session.lower() == 'kde':
command = f'plasma-apply-wallpaperimage {path}'
print(f'\n{command}\n')
os.system(command)
# CINNAMON
if desktop_session.lower() == 'cinnamon':
commands = []
commands.append(f'gsettings set org.cinnamon.desktop.background picture-uri file://{path}')
commands.append(f'gsettings get org.cinnamon.desktop.background picture-uri')
commands.append(f'gsettings set org.cinnamon.desktop.background picture-options zoom')
commands.append(f'gsettings get org.cinnamon.desktop.background picture-options')
for command in commands:
print(f'\n{command}')
os.system(command)
# Завершить сценарий, если данное изображение было скачано ранее
def check_exist(url):
if os.path.exists(out_image):
with Image.open(out_image) as im:
comment = im.info['comment']
print('\ncomment: ', comment)
print('url: ', url)
if url.encode('ASCII') == comment:
set_background(out_image)
print('Изображение было скачано ранее\ncomment и url совпадают')
print('\nЗавершаем сценарий')
exit()
# ----------------------------------------------
#set_background(default_image)
# Скачиваем html страницу
#url = 'https://apod.nasa.gov/apod/ap231230.html'
website = requests.get(url, timeout=5)
if not website:
# Не удалось скачать htmlset_background
print(website)
exit()
soup = BeautifulSoup(website.content, 'html.parser')
#print(soup.prettify())
try:
# Низкое качество
img_src = soup.img['src']
except Exception:
img_src = None
print('\nimg_src', img_src)
img_href = None
if img_src:
# Изображение по ссылке (хорошее качество)
for a in soup.find_all('a', href=True):
if '.jpg' in a['href'] or '.png' in a['href']:
img_href = a['href']
break
else: img_href = None
print('\nimg_href', img_href)
if img_href and best_quality:
img_url = f'{base_url}/{img_href}'
elif img_src:
img_url = f'{base_url}/{img_src}'
print('\nimage url:', img_url)
image_ext = img_url[img_url.rfind('.')+1:]
print('\nimage ext:', image_ext)
check_exist(img_url)
try:
r = requests.get(img_url, timeout=5, stream=True)
if r.status_code == 200:
with open(f'{tmp_path}.{image_ext}', 'wb') as f:
f.write(r.content)
except Exception:
print(f'Не удалось скачать изображение {img_url}')
else: processing_image(f'{tmp_path}.{image_ext}', img_url)
if (not img_href) and (not img_src):
print('\nИщем ссылку YouTube\n')
iframe = soup.find('iframe')
if iframe:
check_exist(iframe['src'])
print(iframe['src'])
yt_opts = {
'writethumbnail': True,
'embedthumbnail': True,
'skip_download': True,
'outtmpl': tmp_path,
'socket_timeout': 5
}
with yt_dlp.YoutubeDL(yt_opts) as ydl:
ydl.download(iframe['src'])
processing_image(f'{tmp_path}.webp', iframe['src'])
set_background(out_image)