-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (139 loc) · 5.72 KB
/
main.py
File metadata and controls
146 lines (139 loc) · 5.72 KB
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
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
import random
import requests
import ctypes
import webbrowser
from flox import Flox
PEXELS_URL = "https://api.pexels.com/v1/search?query={category}&page={page}"
PEXELS_API_PAGE = "https://www.pexels.com/api/"
class WallpaperChanger(Flox):
# Available wallpaper categories
categories = ["Random"] + sorted([
"Nature", "Waves", "Animals", "Landscape", "Mountains", "Quote", "City",
"Abstract", "Beach", "Trees", "Sunset", "Flowers", "Space",
"Underwater", "Minimalist", "Cars", "Sports", "Night Sky", "Forests"
])
def query(self, query):
"""Process the query from the user."""
try:
query = query.strip()
api_key = self.settings.get("pexels_api_key", "")
# Process API key update command
if query.lower().startswith("key"):
key_argument = query[3:].strip()
if not key_argument:
if api_key:
self.add_item(
title="API Key already set",
subtitle="To update your API key, type: wall key <new-api-key>",
icon="Images/app.png"
)
else:
self.add_item(
title="API Key not set",
subtitle="Usage: wall key <your-api-key>",
icon="Images/app.png"
)
else:
self.add_item(
title="Save API Key",
subtitle=f"Press Enter to save API key: {key_argument}",
icon="Images/app.png",
method="save_api_key",
parameters=[key_argument]
)
# Prompt the user if API key is missing
elif not api_key:
self.add_item(
title="Pexels API key not set - Click to open Pexels API",
subtitle="Use 'wall key <your-api-key>' to set it.",
icon="Images/app.png",
method="open_pexels_api_page",
parameters=[]
)
# If no query is provided, list all categories
elif not query:
for cat in self.categories:
self.add_item(
title=f"{cat} wallpaper",
subtitle="Click to change wallpaper",
icon="Images/app.png",
method="change_wallpaper",
parameters=[cat]
)
# Use the query as the category
else:
self.add_item(
title=f"{query.title()} wallpaper",
subtitle="Click to change wallpaper",
icon="Images/app.png",
method="change_wallpaper",
parameters=[query]
)
except Exception:
pass
return
def save_api_key(self, api_key):
"""Save the API key to settings."""
try:
self.settings["pexels_api_key"] = api_key
self.change_query(self.action_keyword + " ", True)
self.add_item(
title="Pexels API Key Saved",
subtitle="Your API key has been saved successfully.",
icon="Images/app.png"
)
except Exception:
pass
return
def open_pexels_api_page(self):
"""Open the Pexels API page in the default browser."""
try:
webbrowser.open(PEXELS_API_PAGE)
self.add_item(
title="Opened Pexels API Page",
subtitle="Copy your API key and set it using 'wall key <your-api-key>'.",
icon="Images/app.png"
)
except Exception:
pass
return
def change_wallpaper(self, category):
"""Fetch and set a new wallpaper based on the given category."""
try:
api_key = self.settings.get("pexels_api_key", "")
if not api_key:
self.add_item(
title="API Key Missing",
subtitle="Set your Pexels API key using 'wall key <your-api-key>'.",
icon="Images/app.png"
)
else:
if category.lower() == "random":
category = random.choice([cat for cat in self.categories if cat.lower() != "random"])
url = PEXELS_URL.format(category=category, page=random.randint(1, 10))
response = requests.get(url, headers={"Authorization": api_key})
if response.status_code != 200:
return
images = [img for img in response.json().get("photos", [])
if img.get("width", 0) > img.get("height", 0)]
if not images:
return
selected_image = random.choice(images)["src"]["original"]
wallpaper_path = os.path.join(os.path.expanduser("~"), "wallpaper.jpg")
with open(wallpaper_path, "wb") as f:
f.write(requests.get(selected_image).content)
# Set the wallpaper
ctypes.windll.user32.SystemParametersInfoW(20, 0, wallpaper_path, 3)
self.add_item(
title="Wallpaper Changed",
subtitle=f"Wallpaper set to a {category} image.",
icon="Images/app.png"
)
except Exception:
pass
return
if __name__ == "__main__":
WallpaperChanger()