Last active
April 11, 2017 10:52
-
-
Save OctoNezd/bb43297815353fc930d94dab434d9ee5 to your computer and use it in GitHub Desktop.
Wallpaper changer for xfce4(gets pic from reddit)
This file contains 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
import mimetypes | |
import os | |
import random | |
import subprocess | |
import configparser | |
import bs4 | |
import gi | |
import gi.repository.Notify | |
import requests | |
import xmltodict | |
APPFOLDER = os.path.expanduser("~/.octowall/") | |
PICFOLDER = APPFOLDER + "pics/" | |
WALLCMD = "xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s " | |
CFG = configparser.ConfigParser() | |
if not os.path.exists(APPFOLDER): | |
os.mkdir(APPFOLDER) | |
if not os.path.exists(PICFOLDER): | |
os.mkdir(PICFOLDER) | |
if os.path.exists(APPFOLDER + "config.ini"): | |
try: | |
CFG.read(APPFOLDER + "config.ini") | |
REMOVEOLDPICS = CFG["Changer"].getboolean("RemoveOldPictures") | |
USENOTIF = CFG["Changer"].getboolean("UseNotifications") | |
REDDIT = CFG["Reddit"]["Subreddit"] | |
except configparser.Error: | |
gi.require_version('Notify', '0.7') | |
gi.repository.Notify.init("Bg-changer error") | |
notif=gi.repository.Notify.Notification.new("Error when parsing config!", | |
"", | |
"dialog-error") | |
notif.show() | |
else: | |
CFG["Changer"] = { | |
"RemoveOldPictures":"Yes", | |
"UseNotifications":"Yes", | |
} | |
CFG["Reddit"] = { | |
"Subreddit":"earthporn" | |
} | |
with open(APPFOLDER + "config.ini", 'w') as f: | |
CFG.write(f) | |
REMOVEOLDPICS = CFG["Changer"].getboolean("RemoveOldPictures") | |
USENOTIF = CFG["Changer"].getboolean("UseNotifications") | |
REDDIT = CFG["Reddit"]["Subreddit"] | |
def create_notification(name, content, notiftype): | |
if USENOTIF: | |
gi.require_version('Notify', '0.7') | |
gi.repository.Notify.init(name) | |
notif=gi.repository.Notify.Notification.new(name, content, "dialog-%s" % notiftype) | |
notif.show() | |
try: | |
t = requests.get("https://reddit.com/r/%s.rss" % REDDIT).text | |
except requests.exceptions.BaseHTTPError as e: | |
create_notification("Error when getting data from Reddit", e, "error") | |
raise SystemExit | |
try: | |
ep = xmltodict.parse(t)["feed"] | |
walls = ep["entry"] | |
walls_struct = {} | |
for wall in walls: | |
url = bs4.BeautifulSoup(wall["content"]["#text"], "html.parser") | |
url = url.find_all("a")[2].get("href") | |
walls_struct[wall["id"]] = [url, wall["title"]] | |
except Exception as e: | |
create_notification("Error when parsing RSS", e, "error") | |
raise SystemExit | |
try: | |
wallpaper = random.choice(list(walls_struct.keys())) | |
if REMOVEOLDPICS: | |
for f in os.listdir(PICFOLDER): | |
os.remove(PICFOLDER + f) | |
wp = requests.get(walls_struct[wallpaper][0]) | |
ext = mimetypes.guess_extension(wp.headers["content-type"]) | |
if ext == ".jpe": | |
ext = ext + "g" | |
fname = PICFOLDER + wallpaper + ext | |
with open(fname, 'wb') as f: | |
f.write(wp.content) | |
subprocess.call(WALLCMD + fname, shell=True) | |
create_notification("Wallpaper changed", walls_struct[wallpaper][1], "information") | |
except Exception as e: | |
create_notification("Error when changing wallpaper", | |
str(e), | |
"error") | |
raise SystemExit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment