Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to work with praw 5.0.1 #35

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
=======
Expand Down
45 changes: 35 additions & 10 deletions change_wallpaper_reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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


Expand Down Expand Up @@ -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.")
Expand All @@ -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" \
Expand Down
3 changes: 3 additions & 0 deletions praw.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[dailywallpaper]
client_id=REPLACE_WITH_YOUR_CLIENT_ID
client_secret=REPLACE_WITH_YOUR_CLIENT_SECRET
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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