r/learnpython • • Jan 17 '20

I wrote a script that grabs the hottest post on /r/wallpapers and sets it as your background.

Inspired by /u/glof2 's post, I created a script that uses PRAW to scrape /r/wallpapers and sets it as your desktop wallpaper. It only works for windows, but I might work with some friends in my classes and develop a linux/macOS version. Otherwise, if ya wanna try and make it yourself feel free! If you like it, use it with task scheduler to set a new wallpaper every day.

Link

This was a fun project because I learned a lot about PRAW, different packages, and how to use GitHub. Feedback appreciated!

529 Upvotes

48 comments sorted by

36

u/PunkPen Jan 17 '20

That's really clever! Saved!

7

u/SereneKoala Jan 17 '20

I'm glad you like it!!

24

u/the_programs Jan 17 '20

Inspired by the same post, I made a similar program recently, the only difference is that it selects randomly between /r/wallpapers and /r/wallpaper and then makes a random choice between the 1000 best posts of all time.

8

u/SereneKoala Jan 18 '20

That sounds cool!

10

u/kandeel4411 Jan 18 '20

I made a similar bot a while back, you could check it out if you want for the linux/Mac support.

A bit of feedback, right now no one will be able to run your script because you haven't specified what are its dependencies. (PRAW is pretty clear but you might use some other dependency later and the user might not be aware) One of the standard formats for defining dependencies is pip's requirements.txt file which is widely used in python, I suggest you check it out if you aren't familiar with it.

I also suggest that you check out PEP8, which are a set of agreed on conventions most python projects use for better readability and general best practices. This is all I have for now, nice project and keep up the good work :)

9

u/SereneKoala Jan 18 '20

Hey, I just checked out your bot. All I can say, its way better than mine! I admire how well developed your repo is and I appreciate the feedback. I'm still new to coding so thanks for the help!

5

u/Lukabratzee Jan 18 '20

Hey! Love the sound of your script.
I tried to run it, though i'm getting a traceback error saying about mentioning a praw.ini but providing none.

configparser.NoSectionError: No section: 'bot'

You provided the name of a praw.ini configuration which does not exist.
Is there a set up beforehand I need to do, or a config I need to provide?
For context i'm on Windows 10.

5

u/SereneKoala Jan 18 '20

Hey, sorry about that. I updated the repo's readme and it should be more clear how to setup your praw.ini

1

u/Lukabratzee Jan 18 '20

Not at all! There's some steps that I'm still unsure of, however.

Whilst creating the script, it asks what redirect URL to use. When you say place the personal script in the local dir, the is just the script ID/script client secret that are tokened to you after registering a script right? I imagine you're gonna create a more fleshed out README soon :p

1

u/SereneKoala Jan 19 '20

Correct. You should be able to grab client_id and client_secret after creating your own Reddit app I’m your preferences. Name your file praw.ini and it should work fine.

1

u/CarlosDNieto Jan 18 '20

Thank you very much, finally I could make it work.

3

u/CarlosDNieto Jan 18 '20

Hey, I like your idea, but I'm getting an error and I don't know why it is, I've searched and it seems to be an error with the praw.init configuration.

Here is the error:

configparser.NoSectionError: No section: 'bot'

You provided the name of a praw.ini configuration which does not exist.

And is in this line of the code:

reddit = praw.Reddit('bot',user_agent='reddit2desktop')

Hope that you can help me with this, and sorry if it's a dumb error, I actually don't know much about the praw module and this error is driving me crazy. I'll appreciate a lot your help in this.

4

u/SereneKoala Jan 18 '20 edited Jan 18 '20

Check the readme on the repository, I posted an explanation on how to run it. Hope this helps. If you have anymore problems pm me !!

2

u/Zeenie10 Jan 18 '20

how does this deal with resolution? if i want specific reso wall papers... or does it just choose whatever size and fit it to my screen?

would love to use this but only if it has correct resolution wallpapers :)

3

u/SereneKoala Jan 18 '20

Currently it chooses the size posted by the user, but making an option to adjust the aspect ratio of an image would be something I might consider in the future!

2

u/hypnotic-hippo Jan 18 '20

What happens if the wallpaper doesn't have a 16:9 aspect ratio? Currently the hottest wallpaper on the subreddit has a 1:1 aspect ratio. Does the program crop or stretch it?

2

u/SereneKoala Jan 18 '20

I believe it fits the picture to the screen, but I'm not sure how ctypes uses SPI_DESKWALLPAPER.

2

u/[deleted] Jan 18 '20

How would i keep the code running over long periods of time, like between boots? Does my IDE need to keep open? Sorry I’m new to python.

3

u/SereneKoala Jan 18 '20

You can use windows scheduler and schedule the script to run once a day, giving you a new wallpaper everyday.

76

u/xelf Elf Jan 17 '20

ok everyone, you know what to do, start upvoting something odd... =)

3

u/hoppi_ Jan 18 '20

... why odd? :)

20

u/HomemadeLemonaid Jan 18 '20

So he gets a shitty wallpaper

3

u/hoppi_ Jan 18 '20 edited Jan 18 '20

Oh.... lol I was stupid. I had totally misread /u/xelf's post.

Thanks :)

1

u/[deleted] Jan 19 '20

You weren't the only one!

1

u/NoelGalaga Jan 18 '20

This might help with OS X but the code will have to change depending on the OS version:

https://apple.stackexchange.com/questions/40644/how-do-i-change-desktop-background-with-a-terminal-command/145174

1

u/SereneKoala Jan 18 '20

I'll be sure to check it out!

1

u/dr3d3d Jan 22 '20 edited Jan 22 '20

Thanks for the beggining of a great script... I had to modify it to work under KDE Plasma(Linux) as well as windows.

https://github.com/Dreded/reddit2desktop

While I was at it I added a config file and dropped the usage of praw.ini as well as made the sub configurable and randomly select from the top X images of multiple subs(I defaulted it to 10 from /r/wallpaper or /r/wallpapers)

It will also save the image with a timestamp so it never deletes them if that is whats desired or overwrites it each call as you did it, depending how you set the option in the config file.

1

u/SereneKoala Jan 23 '20

I checked it out, really awesome work!

2

u/kplugged Jan 18 '20

The way you approached this is really nice. Kudos mate.😊

2

u/Miraalynn Jan 17 '20

ur insane!!! saved

1

u/GalMalblood Jan 18 '20

Probably not going to use it, except to examine it for educational purposes, but that's fucking cool!

1

u/doncalgar Jan 18 '20

python OP ftw❤❤❤❤❤

1

u/iggy555 Jan 18 '20

How do you know which module to use?

1

u/Rutherfordio Jan 18 '20

Was just actually looking to make exactly this myself, I'll use this! Thanks for the work!

1

u/Useful-Blackberry Jan 18 '20

I will adapt it to have my own authomatic meme library. Thanks!

1

u/BlackFeather97 Jan 18 '20

Great idea.

I also made something like this yesterday using Node JS.

https://github.com/ManishKarki1997/reddownload

But it just downloads wallpapers, including imgur scraping. Code is bad but works lol. Think I'll also try to add the desktop background feature. Thanks

1

u/DeathWrangler Jan 18 '20

Rather than just modify your code for my needs, I'll ask do you plan on making a version for /r/WidescreenWallpaper

1

u/b00mfunk Jan 19 '20

Hi, I'm a little confused. What redirect URI should I set when creating a reddit script?

0

u/[deleted] Jan 17 '20

[deleted]

7

u/SereneKoala Jan 17 '20

no problem, ill update the repo soon

6

u/winnerszn Jan 17 '20

Would appreciate this very much aswell since i am learning to code myself!

4

u/SereneKoala Jan 18 '20

I added main_comments.py on the repo, I hope this helps!

0

u/[deleted] Jan 18 '20

[deleted]

2

u/SereneKoala Jan 18 '20

Thanks, I'll be sure to check out black/isort for some of my future work

0

u/[deleted] Jan 18 '20

[deleted]

1

u/SereneKoala Jan 19 '20

I actually completely glossed over regex when I learned python, i wish i could help :P

1

u/o5a Jan 19 '20

If you use json instead of rss then you recieve json that is basically a dictionary, you won't need to regexp search for your image url, you can just take it right away.

0

u/[deleted] Jan 18 '20

[deleted]

1

u/SereneKoala Jan 19 '20

url_redirection shouldn’t be necessary for this, so only include client_id and client_secret

0

u/[deleted] Jan 19 '20

[deleted]

1

u/SereneKoala Jan 20 '20

Can you send me a screenshot ?

0

u/[deleted] Jan 18 '20

[deleted]

2

u/SereneKoala Jan 19 '20

thanks man!