Skip to content

Instantly share code, notes, and snippets.

@tommyp6
Last active April 11, 2022 09:49
Show Gist options
  • Select an option

  • Save tommyp6/4759417cd43b37abdce808e1521fad3a to your computer and use it in GitHub Desktop.

Select an option

Save tommyp6/4759417cd43b37abdce808e1521fad3a to your computer and use it in GitHub Desktop.
Fetch a wallpaper from reddit.
#!/bin/sh
_wallpaper=$( ls $HOME/.local/share/wallpapers/wall.* )
readarray -d . -t _wall <<< "$_wallpaper"
cp $_wallpaper $HOME/wallpapers/wallpaper-"$(date '+%y%m%d-%H%M-%S').${_wall[-1]}"
#!/bin/sh
bgloc="${XDG_DATA_HOME:-$HOME/.local/share/}/bg"
declare -x DISPLAY=":0"
declare -x XAUTHORITY="/home/$USER/.Xauthority"
[ -f "$1" ] && ln -sf "$(readlink -f "$1")" "$bgloc" && notify-send -i "$bgloc" "Changing wallpaper..."
[ -d "$1" ] && ln -sf "$(find "$(readlink -f "$1")" -iregex '.*.\(jpg\|jpeg\|png\|gif\)' -type f | shuf -n 1)" "$bgloc" && notify-send -i "$bgloc" "Random Wallpaper chosen."
wal -i "$(readlink -f "$bgloc")" -o "${XDG_CONFIG_HOME:-$HOME/.config}/wal/postrun" >/dev/null 2>&1
pidof dwm >/dev/null && xdotool key super+F12
xwallpaper --zoom "$bgloc"
#!/bin/python
import random
import os
import time
import shutil
import glob
import requests
import praw
uid = ""
secret = ""
ua = ""
def connected_to_internet(url='http://www.google.com/', timeout=5):
try:
_ = requests.get(url, timeout=timeout)
return True
except requests.ConnectionError:
return False
while not connected_to_internet():
time.sleep(60*5) # Retry every 5 minutes
reddit = praw.Reddit(client_id=uid, client_secret=secret, user_agent=ua)
subs = {
"wallpapers": 50, # Nums of posts for sub.
"art": 100,
"itookapicture": 50,
}
pics = []
for sub in subs:
for post in reddit.subreddit(sub).top("week", limit=subs[sub]):
if not post.over_18:
pics.append(post.url)
pic = random.choice(pics)
r = requests.get(pic, stream=True)
path = f"{os.environ['HOME']}/.local/share/wallpapers/"
try:
os.mkdir(path)
except FileExistsError:
pass
ext = pic.split('.')[-1]
ltd = ["com", "org", "net"]
while ext in ltd:
pic = random.choice(pics)
ext = pic.split('.')[-1]
for image in glob.glob(path+"wall.*"):
os.remove(image)
with open(path + "wall." + ext, "wb") as f:
shutil.copyfileobj(r.raw, f)
os.system(f"{os.environ['HOME']}/.local/bin/setbg {path}wall.{ext}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment