Last active
April 15, 2024 04:06
-
-
Save tudoanh/52ef0722466e85e4928bb32eda0de54d to your computer and use it in GitHub Desktop.
Random wallpaper from Imgur
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sqlite3 | |
import os | |
import random | |
import subprocess | |
import requests | |
# Set up the database connection and cursor | |
conn = sqlite3.connect("imgur.db") | |
c = conn.cursor() | |
c.execute( | |
"CREATE TABLE IF NOT EXISTS imgur (id INTEGER PRIMARY KEY AUTOINCREMENT, gallery_id TEXT, image_link TEXT)" | |
) | |
# Imgur Client ID and API settings | |
client_id = '' | |
headers = { | |
'Authorization': f'Client-ID {client_id}' | |
} | |
def get_album_images(album_id): | |
url = f"https://api.imgur.com/3/album/{album_id}/images" | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
return response.json()['data'] | |
else: | |
return None | |
# Choose a random gallery id | |
gallery_id = random.choice( | |
["SU6bL", "vdC7V", "WPZmE", "wUh9E", "CM8hQ", | |
"ggKF3", "wmSMe", "uwzOb", "Ugsmp", "qZ3FX", "Zpp5hNv", | |
] | |
) | |
# Check if gallery_id exists | |
c.execute("SELECT * FROM imgur WHERE gallery_id = ?", (gallery_id,)) | |
if c.fetchone(): | |
print(f"Gallery id: {gallery_id} exists") | |
total_image = c.execute( | |
"SELECT COUNT(*) FROM imgur WHERE gallery_id = ?", (gallery_id,)) | |
print(f"Total image: {total_image.fetchone()[0]}") | |
c.execute("SELECT * FROM imgur WHERE gallery_id = ?", (gallery_id,)) | |
image = random.choice(c.fetchall()) | |
print(image) | |
path = os.path.abspath("/home/james/Downloads/wallpaper.jpg") | |
subprocess.run(["curl", "-s", "-o", path, image[2]]) | |
subprocess.call(["feh", "--bg-scale", path]) | |
print("Set wallpaper succeeded") | |
else: | |
# Fetch album images using the API | |
album_images = get_album_images(gallery_id) | |
if album_images: | |
# Save to SQLite database | |
for image in album_images: | |
if image['width'] > image['height'] and image['width'] >= 1920 and image['height'] >= 1080: | |
# Insert if not exist | |
c.execute( | |
"INSERT OR IGNORE INTO imgur (gallery_id, image_link) VALUES (?, ?)", | |
(gallery_id, image['link']), | |
) | |
conn.commit() | |
image = None | |
for _ in range(10): | |
temp_image = random.choice(album_images) | |
if temp_image['width'] > temp_image['height'] and temp_image['width'] >= 1920 and temp_image['height'] >= 1080: | |
image = temp_image | |
break | |
if image: | |
path = os.path.abspath("/home/james/Downloads/wallpaper.jpg") | |
subprocess.run(["curl", "-s", "-o", path, image['link']]) | |
# If you using GNOME, uncomment this instead | |
# subprocess.call( | |
# [ | |
# "gsettings", | |
# "set", | |
# "org.gnome.desktop.background", | |
# "picture-uri", | |
# f"file://{path}", | |
# ] | |
# ) | |
# If you using i3wm, uncomment this | |
subprocess.call(["feh", "--bg-scale", path]) | |
print(f"Set wallpaper succeeded using {image['link']}") | |
Add cached to Sqlite3 for better performance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need Imgur API application ID & secret