diff --git a/README.md b/README.md index 4c224a7..f95b9f2 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,15 @@ The rest is the same. Using script ======= +Update praw.ini: + +You need to register this app with your reddit account. +https://www.reddit.com/prefs/apps/ + +You need to copy your client id and client secret into the praw.ini file. +The id will be in the upper left corner of your app information (it will be a long alphanumeric string). +The secret will be labeled "secret". + Simply run: ``` python /home/silvio/Scripts/change_wallpaper_reddit.py @@ -43,6 +52,12 @@ If you wanna use other subreddit, include argument with the subreddit name: python /home/silvio/Scripts/change_wallpaper_reddit.py --subreddit art ``` +If you want to use a public multireddit, you must specify the name of the redditor that owns the multireddit and the name of the multireddit. +Example: +``` +python /home/silvio/Scripts/change_wallpaper_reddit.py --user redditor --multireddit nameOfMultireddit +``` + If you don't want to change your wallpaper daily, you can use newest, hourly, weekly, monthly or yearly wallpaper too by adding one of the following arguments: ```new```, ```hour```, ```week```, ```month```, ```year``` to the script. Example: @@ -101,7 +116,37 @@ or Running every minute or hour ======= -Look into using cronjobs on Linux or Task Scheduler on Windows for performing this. +You can configure this script to run at timed intervals in Windows using the Task Scheduler. + +In Linux, you can write a cronjob or an anacron script. +There are two environment variables that need to be set during the cron job or anacron script. +``` +export DISPLAY=:0 +``` + +The other variable is dependent on your desktop environment + +KDE: +``` +export KDE_FULL_SESSION=true +``` + +GNOME or Cinnamon: +``` +export GNOME_DESKTOP_SESSION_ID=anything +``` + +Lubuntu: +``` +export DESKTOP_SESSION=Lubuntu +``` + +mate: +``` +export DESKTOP_SESSION=mate +``` + +[Click here for instructions on configuring cron jobs.](https://help.ubuntu.com/community/CronHowto) Configuration file ======= diff --git a/change_wallpaper_reddit.py b/change_wallpaper_reddit.py index 201f0a0..dc81383 100755 --- a/change_wallpaper_reddit.py +++ b/change_wallpaper_reddit.py @@ -27,6 +27,8 @@ def load_config(): default["time"] = "day" default["display"] = "0" default["output"] = "Pictures/Wallpapers" + default["user"] = None + default["multireddit"] = None config_path = os.path.expanduser("~/.config/change_wallpaper_reddit.rc") section_name = "root" @@ -57,6 +59,8 @@ def add_to_ret(fun, name): add_to_ret(config.getint, "display") add_to_ret(config.get, "time") add_to_ret(config.get, "output") + add_to_ret(config.get, "user") + add_to_ret(config.get, "multireddit") return ret @@ -80,34 +84,40 @@ def parse_args(): help="Desktop display number on OS X (0: all displays, 1: main display, etc") parser.add_argument("-o", "--output", type=str, default=config["output"], help="Set the outputfolder in the home directory to save the Wallpapers to.") + parser.add_argument("-u", "--user", type=str, default=config["user"], + help="Set the reddit username that owns the multireddit to use.") + parser.add_argument("-m", "--multireddit", type=str, default=config["multireddit"], + help="Name of multireddit to use.") args = parser.parse_args() return args -def get_top_image(sub_reddit): +def get_top_image(submissions): """Get image link of most upvoted wallpaper of the day :sub_reddit: name of the sub reddit :return: the image link """ - submissions = sub_reddit.get_new(limit=10) if args.time == "new" else sub_reddit.get_top(params={"t": args.time}, - limit=10) for submission in submissions: + ret = {"id": submission.id} if not args.nsfw and submission.over_18: continue url = submission.url # Strip trailing arguments (after a '?') url = re.sub(R"\?.*", "", url) + imageType = url.split('.')[-1] if url.endswith(".jpg") or url.endswith(".png"): ret["url"] = url + ret["type"] = imageType return ret # Imgur support if ("imgur.com" in url) and ("/a/" not in url) and ("/gallery/" not in url): if url.endswith("/new"): url = url.rsplit("/", 1)[0] id = url.rsplit("/", 1)[1].rsplit(".", 1)[0] - ret["url"] = "http://i.imgur.com/{id}.jpg".format(id=id) + ret["url"] = "http://i.imgur.com/{id}.{imageType}".format(id=id, imageType=imageType) + ret["type"] = imageType return ret @@ -158,15 +168,28 @@ def detect_desktop_environment(): args = parse_args() subreddit = args.subreddit + multireddit = args.multireddit + user = args.user save_dir = args.output supported_linux_desktop_envs = ["gnome", "mate", "kde", "lubuntu"] # Python Reddit Api Wrapper - r = praw.Reddit(user_agent="Get top wallpaper from /r/{subreddit} by /u/ssimunic".format(subreddit=subreddit)) - + r = praw.Reddit('dailywallpaper', user_agent="Get top wallpaper by /u/ssimunic") + + # Get top submissions + if ((multireddit is not None) and (user is not None)): + print("Using {user}'s multireddit {multireddit}".format(user=user, multireddit=multireddit)) + multi_reddit = r.multireddit(user, multireddit) + submissions = multi_reddit.new(limit=10) if args.time == "new" else multi_reddit.top(time_filter=args.time, limit=10) + sourceType = multireddit + else: + sub_reddit = r.subreddit(subreddit) + submissions = sub_reddit.new(limit=10) if args.time == "new" else sub_reddit.top(time_filter=args.time, limit=10) + sourceType = subreddit + # Get top image link - image = get_top_image(r.get_subreddit(subreddit)) + image = get_top_image(submissions) if "url" not in image: sys.exit("Error: No suitable images were found, the program is now" \ " exiting.") @@ -178,10 +201,12 @@ def detect_desktop_environment(): if response.status_code == requests.codes.ok: # Get home directory and location where image will be saved # (default location for Ubuntu is used) + home_dir = os.path.expanduser("~") - save_location = "{home_dir}/{save_dir}/{subreddit}-{id}.jpg".format(home_dir=home_dir, save_dir=save_dir, - subreddit=subreddit, - id=image["id"]) + save_location = "{home_dir}/{save_dir}/{subreddit}-{id}.{imageType}".format(home_dir=home_dir, save_dir=save_dir, + subreddit=sourceType, + id=image["id"], + imageType=image["type"]) if os.path.isfile(save_location): sys.exit("Info: Image already exists, nothing to do, the program is" \ diff --git a/praw.ini b/praw.ini new file mode 100644 index 0000000..24f6538 --- /dev/null +++ b/praw.ini @@ -0,0 +1,3 @@ +[dailywallpaper] +client_id=REPLACE_WITH_YOUR_CLIENT_ID +client_secret=REPLACE_WITH_YOUR_CLIENT_SECRET diff --git a/requirements.txt b/requirements.txt index 1271f8b..ed8bb75 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ decorator==4.0.10 future==0.15.2 -praw==3.5.0 +praw==5.3.0 requests==2.10.0 six==1.10.0 update-checker==0.11