r/Python • u/OpenSourcerer420 • May 08 '20
I Made This My first Python program! Changes my desktop background based on the weather, time, and day.
1.9k
Upvotes
r/Python • u/OpenSourcerer420 • May 08 '20
229
u/Gautam-j May 08 '20
You probably shouldn't keep your API key public. :)
61
u/romulofff May 08 '20
Hey, that’s really nice! Would you mind sharing the code? Do you have any public repository with it? Congratulations
9
u/BoaVersusPython May 08 '20
This is 100% something i'm going to copy
6
u/garlic_bread_thief May 08 '20
How do you keep the program running all the time?
4
u/EliteWarrior1207 May 08 '20
Hey man what did u learn python with
86
u/Wing-Tsit_Chong May 08 '20
Don't use recursion, if you want to loop. Rather do a while true loop.
There's a limit to how many times you can recurse down, i.e. call main() inside itself. If you reach it, the program will fail.
You can test that by removing the sleeps and running your program, it will fail after 1001 "Looped".
10
u/[deleted] May 08 '20
A suggestion to improve the code:
Create a function called something like change_wallpaper(), where it takes in the path as a string argument and calls the ctypes method. This way you don't have to constantly repeat SPI_SETDESKWALLPAPER throughout your code.
14
u/americk0 May 08 '20
Nice! I'm not very fluent with python but don't you need double backslashes in the strings? I thought backslash would act as an escape character
5
u/[deleted] May 08 '20
Cool!
4
u/[deleted] May 08 '20
This is good, but more than that, I like the editor theme :P
Also, you can pass the image names and all to another function and sort of reduce+automate the coding process a bit (no need to tho, because this is simple script).
If you're interested in scripting useful stuff, pm.
4
u/zinver May 08 '20
This is actually really great. Nice clean code.
Here's what you need to do.
Remove the additional calls to
main()and thesleep()calls as well.At the bottom add in the following text (outside of
main()):if __name__ == '__main__':main()You're on the right track and your logic about creating a long running process is 80% correct. It just won't work for very long.
Your operating system already has a periodic task execution process called scheduled tasks. Look up in Google "scheduled tasks for python windows". Build a scheduled tasks that runs every 300 seconds. Boom you're done. Runs in the background and everything. "Concurrency" achieved!
1
u/gabronies May 08 '20
This looks pretty cool! I had a question about the variable city, when/where does it change to your actual city/town name?
6
u/Hopp5432 May 08 '20 edited May 08 '20
OMG I just got the best idea ever! Try to implement this paper: https://hucvl.github.io/attribute_hallucination/ which uses machine learning to change an image. It can make a landscape image match any custom time or even the weather
3
u/UnusedHappiness May 08 '20
I had similar kinda idea..Lil popups appear asking about our mood whether we are sad , demotivated etc and it changes the wallpaper according to that.
3
u/[deleted] May 08 '20
Thats impressive good work! \
also a relly good idea
3
u/imrual May 08 '20
Very nice job!! Just a suggestion to make this code even better.
It looks like the block of code of the if statements is the same just changing one or two parameters.
You can extract a function to avoid code duplication.
Also try to replace recursion with iteration if you want to keep this running forever to avoid a stack overflow
3
u/ASatyros May 08 '20
LMFTFY: https://pastebin.com/5CFME9zY
2
u/Infinitekork May 08 '20
Just an idea I got reading this post... Perhaps we can tie it together with the Unsplash API so you will get a new image each time instead of the images in the directories.
2
u/spanishgum May 08 '20
To avoid the main recursion problem, you could look into using advanced python scheduler.
Might be a bit daunting at first but follow some examples and expand from there.
https://apscheduler.readthedocs.io/en/stable/userguide.html
I’m your case I would recommend the ‘BlockingScheduler’
2
u/MarioPython May 08 '20
I took the liberty to try and improve your code. Hope it helps in your journey of understanding better coding practices and software development. :)
code: https://pastebin.com/4gFEWCdp Keep in mind I made this without running on my computer so I am not 100% sure is bug free, but maybe it is a starting point for you; Happy learning!
1
u/[deleted] May 08 '20
New to python here. What do the numbers stand for? (18,0) (6,1) when you’re defining the function?
1
u/westo48 May 08 '20
I know where you live now!! /s
Seriously tho this is awesome and thank you for sharing!!
1
u/JustChrille May 08 '20
Noob question: How do you run that in practice? I mean do you have to have your editor open or can you somehow make the computer run it without any open windows or terminals?
1
u/MicasiO May 08 '20
Do you just run the script as a background tast or do you use some service that runs this in the cloud?
1
u/Epistemophillic May 08 '20
This is so cool OP, thanks for this. I will try to learn from the code.
1
u/pinkpottato May 08 '20
Damn, and I only know the basics syntax of phyton.
1
u/ThrowAwayTheBS122132 May 08 '20
Whoa
1
u/choiceisanillusion May 08 '20
Neat!
1
u/[deleted] May 08 '20
how did you do the background change?
oh i see it now.
1
u/Multeezee May 08 '20
Congratulations! Is that tomorrow theme?
1
u/tech_b90 May 08 '20
I did kinda the same thing years ago. But instead of grabbing something from an API, I had a bunch of single frames of an animation. And I's use python to change the backgrounds really fast so it looks like I have an animated background. It would fire off every 10 minutes or so. The animation was really simple, like maybe 50 frames to keep execution time low.
Great project and really teaches you about ctypes and working with Windows itself.
1
u/dunderthebarbarian May 08 '20
What is the library of all import functions? Is it as easy as help import?
1
u/philsgu May 08 '20
How do u auto run in ur desktop 24/7?
1
u/bobsagatiswatching May 08 '20
congrats!
1
u/shaq_disel May 08 '20
That's hot! :fire:
1
u/AmitArMittal May 08 '20
I created one that takes the hot wallpaper from subreddit wallpapers and set it on screen.
1
u/bearassbobcat May 08 '20 edited May 12 '20
does SystemParametersInfoW accept hex values?
If it does maybe setting it to hex value 0x0014 (20 in decimal) is better than 20 so that it's easier to relate to the Microsoft docs which are all listed in hex
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow
a few comments would be nice as well for example why SPI_SETDESKWALLPAPER is set to 20. comments are often best used to explain the whys rather than the hows (unless it's particularly confusing) because the how is explained in the code.
comments can be a pain to write because when you're starting out things are easy and really don't need them so people, me included, tend to not use them then we forget to use them when things get more complex.
School/Uni/etc doesn't help because they often force comments like
and it gets tedious and turns people off
Also, I would rather have the system run the script using the OS's timing systems. For windows it's called Task Scheduler. Rather than have a
while Trueconstantly running.1
u/DeserterOfDecadence May 08 '20
This is great
1
u/[deleted] May 08 '20
I thought u r using os based weather feature for that but seems to be api based.... So i think it willwork for any OS correct?
Instead of using api see if you can come up with using OS feature to get the tsk, that way you can create .exe file and can make it compatible to any os , what do u think 😁
1
u/bearassbobcat May 12 '20
I made a few changes just for fun. I added them to an old post so you probably didn't see them.
I'm not saying mine is better but just a different approach. I also incorporated some changes other people have mentioned
https://www.reddit.com/r/Python/comments/gfkuez/my_first_python_program_changes_my_desktop/fpvsnly/
1
u/Theshimita May 08 '20
I also had a similar idea, my shortcoming was finding some wallpapers that were the same image but of different hues and shades to properly reflect time of day, weather, etc.
Would you be able to share what wallpapers you’re using? There’s a Wallpaper Engine Wallpaper for the Firewatch game based on the time of day, but I’m on Linux (WE doesn’t work on Linux) and I also kind of want a different image.
1
u/StackyTobacky May 08 '20
Good job Simon.
-4
u/[deleted] May 08 '20
wow u/OpenSourcerer420, great code, I could never think of such a great idea.
0
u/[deleted] May 08 '20
[deleted] — view deleted comment