-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapod.py
144 lines (114 loc) · 4.3 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
#!/usr/bin/env python3
import os
import requests
import subprocess
import re
import time
from datetime import datetime
from pathlib import Path
WALLPAPER_DIR = "/home/arx/Pictures/Wallpapers/apod" # where wallpapers are saved
HYPRPAPER_CONFIG = os.path.expanduser("~/.config/hypr/hyprpaper.conf")
APOD_URL = "https://apod.nasa.gov/apod/"
def get_image_info(element, text):
regex = f'<{element}="(image.*?)"'
match = re.search(regex, text, re.IGNORECASE)
if not match:
return None, None, None
file_url = match.group(1)
if not file_url.startswith('http'):
file_url = f"{APOD_URL}{file_url}"
try:
response = requests.head(file_url)
response.raise_for_status()
file_size = float(response.headers.get("content-length", 0))
filename = os.path.basename(file_url)
return file_url, filename, file_size
except requests.RequestException:
return None, None, None
def get_image_url():
try:
response = requests.get(APOD_URL)
response.raise_for_status()
file_url, filename, file_size = get_image_info('a href', response.text)
if file_url is None or file_size < 500:
file_url, filename, file_size = get_image_info('img src', response.text)
if file_url is None or file_size < 500:
print("Could not find a valid image on the APOD page")
return None, None
return file_url, filename
except requests.RequestException as e:
print(f"Error fetching APOD page: {e}")
return None, None
def download_image(url, filename):
try:
response = requests.get(url)
response.raise_for_status()
os.makedirs(WALLPAPER_DIR, exist_ok=True)
if not filename:
ext = '.jpg'
filename = f"apod_{datetime.now().strftime('%Y%m%d')}{ext}"
filepath = os.path.join(WALLPAPER_DIR, filename)
with open(filepath, 'wb') as f:
f.write(response.content)
return filepath
except requests.RequestException as e:
print(f"Error downloading image: {e}")
return None
def update_hyprpaper_config(filepath):
try:
config_dir = os.path.dirname(HYPRPAPER_CONFIG)
os.makedirs(config_dir, exist_ok=True)
if os.path.exists(HYPRPAPER_CONFIG):
with open(HYPRPAPER_CONFIG, 'r') as f:
config_lines = f.readlines()
else:
config_lines = []
new_config = []
preload_found = False
wallpaper_found = False
for line in config_lines:
if line.startswith('preload = '):
new_config.append(f'preload = {filepath}\n')
preload_found = True
elif line.startswith('wallpaper = '):
new_config.append(f'wallpaper = ,{filepath}\n')
wallpaper_found = True
else:
new_config.append(line)
if not preload_found:
new_config.append(f'preload = {filepath}\n')
if not wallpaper_found:
new_config.append(f'wallpaper = ,{filepath}\n')
with open(HYPRPAPER_CONFIG, 'w') as f:
f.writelines(new_config)
return True
except Exception as e:
print(f"Error updating config file: {e}")
return False
def restart_hyprpaper(): # thanks hyprpaper for being so easy to use you become a nightmare
try:
subprocess.run(['killall', '-e', 'hyprpaper'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
time.sleep(1)
subprocess.Popen(['hyprpaper'])
time.sleep(1)
return True
except subprocess.CalledProcessError as e:
print(f"Error restarting hyprpaper: {e}")
return False
def main():
image_url, filename = get_image_url()
if not image_url:
print("Failed to get APOD image URL")
return
filepath = download_image(image_url, filename)
if not filepath:
print("Failed to download APOD image")
return
if update_hyprpaper_config(filepath) and restart_hyprpaper():
print("Wallpaper set successfully")
else:
print("Failed to set wallpaper")
if __name__ == "__main__":
main()