r/learnpython • u/Thomas929292 • Dec 24 '19
I'm working on a CLI application to change your background to the NASA astronomy picture of the day, you're welcome to help!
Although my background is not in computer science or software engineering (mostly data science), I thought it would be fun to work on a python coding project. I got familiar with the NASA APIs and one of their cooler ones is the APOD (astronomy picture of the day) which allows you to download interesting pictures every day. It seemed nice to me to have an application that could set this image as my background with very little effort.
I couldn't find a Github repository that is still actively maintained and cross-platform, so I thought I'd start one and see if anyone was willing to collaborate. Without much experience, I think it's safe to say there will be a lot of things that can be done better, both in the structure of the project and code, so please come and point them out! Additionally, I think it's a great place for beginners to contribute to an open-source project, as some of the bigger projects can be quite daunting to get started in.
Github repository over here:
https://github.com/Thomas9292/nasa-background
More instructions on Github and how to contribute to open source here
9
u/Sentouki- ,Nut Snek Dec 24 '19 edited Dec 24 '19
if is_64bit:
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, PATH, 3)
else:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, PATH, 3)
i don't really get these lines, why are you checking whether it's a 64bit or 32bit? afaik there are no differences between them
Edit: there's also a typo which leads to error "name 'is_64bit' is not defined"
8
u/Thomas929292 Dec 24 '19
I read that there are some issues in some python versions (e.g. 3.5) with using SystemParametersInfoA in 64-bit versions. I haven't tested it as I've only been working on the software on my Linux machine. As I said, if you think this is unnecessary, feel free to contribute! There is already an issue opened regarding the edit.
I will be setting up Travis soon to enable cross-platform testing. Thanks for the feedback!
2
2
Dec 24 '19
I'm sad that this isn't working for me.
When I run pip install appscript I get this output:
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-PxqlLd/appscript/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-Mvy05Z-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-PxqlLd/appscript/
and if I still try to run the backgorund.py application:
sudo python nasa_background.py --help
File "nasa_background.py", line 15
click.echo(f"Title: {meta_info['title']}\n")
^
SyntaxError: invalid syntax
(in the terminal it has the error indicator under the last quote.)
What am I doing wrong?
2
u/Thomas929292 Dec 24 '19
So at least for the syntax error at line 15: you need python version 3.6 or higher to run this because of the f-strings that are used. I'll make this clearer in the repository soon.
I do not know if this will fix the appscript install. If it doesn't, it would be amazing if you could file a bug report on Github, so we can get started fixing this. Thanks for the feedback!
2
2
Dec 24 '19
ok, some success (I'm still learning...) I was able to run the appscript after I installed pip3 and click, the script runs but fails to find the file to download:
Downloading - A Northern Winter Sky Panorama (2019-12-24) [####################################] 100%
The background file was not found..
4
u/clif_darwin Dec 24 '19
I was totally thinking of doing this.
2
1
u/tmaestor Dec 24 '19
Hey, This is an awesome idea, I have a software background and would be happy to answer questions or help if needed.
1
u/Thomas929292 Dec 25 '19
Thanks! Currently, something I have to think about is how to write tests for changing the background on the different platforms, especially in combination with Travis CI (on which only linux is supported for python currently). Any tips are welcome.
Additionally, I'm looking to get a configuration file of some sorts (json, yaml or ini) to store custom api_keys or image storage paths. I'm thinking to create a config module that abstracts away the operations to check if a custom config has been uploaded and make changes. Do you think this is a good idea?
3
3
u/Yakhov Dec 24 '19
GOLD CHALLENGE:
I will give GOLD to anyone who implements auto refreshing at midnight when they change the image and a feature that shuffles the image from the database over a user defined time interval.
If accepted, you have a week to complete this mission, starting now.
4
2
1
u/maxhosted Dec 25 '19
What is it you want to achieve?
You don’t want the python script running all the time, what you do want is to run the script once (or twice) a day and it then does its work.
To do that you would use cron in Unix/Linux or scheduler in windows. Set it up to run the script as appropriate
1
u/baubleglue Dec 25 '19
That is really ugly part
IMAGE_FOLDER = os.path.join(os.path.expanduser('~/Pictures'), 'NASA')
if not os.path.exists(IMAGE_FOLDER):
os.makedirs(IMAGE_FOLDER) I would never make changes on user's computer without asking/warning.
~/Picturesit may even work, but still...
In [1]: import os
In [2]: os.path.expanduser('~/Pictures')
Out[2]: 'C:\\Users\\myname/Pictures'better
In [3]: os.path.join(os.path.expanduser('~'), 'Pictures', 'NASA')
Out[3]: 'C:\\Users\\myname\\Pictures\\NASA'1
u/baubleglue Dec 25 '19
and another thing
requirements.txtcertifi==2019.11.28 chardet==3.0.4 click==6.7 idna==2.8 py==1.4.34 pytest==3.2.3 requests==2.22.0 urllib3==1.25.7you are locking user to specific version without any good reason. For example latest click is 7.0, if I install your requirements, it will downgrade my global installation. https://pip.pypa.io/en/stable/user_guide/
In general there are too many packages for such small application: I don't see where you use
chardet,py,urllib3,idna, even instead of click you can use argparse.I like requests, but if you can manage without it
import urllib req = urllib.request.Request(URL + POD_ENDPOINT + "?" + urllib.parse.urlencode(params)) with urllib.request.urlopen(req) as response: url = json.loads(response.read())['url'] with urllib.request.urlopen(url) as response: with open(os.path.join(IMAGE_FOLDER, url[url.rindex("/") + 1:]), "wb") as f: f.write(response.read())and you have zero dependencies.
1
u/Thomas929292 Dec 25 '19
Interesting points. Would you save the images in a folder inside the project and then include picture formats in the .gitignore? Or would you suggest a different solution?
Regarding the syntax in expanduser, I definitely agree.
Regarding the packages, I think requests and click are perfectly reasonable packages to use for this application. I haven't completely checked but I started with a fresh environment and assume the other packages are dependencies of click, requests or pytests.
1
u/baubleglue Dec 25 '19
Would you save the images in a folder inside the project and then include picture formats in the .gitignore?
You are thinking from developer's point of view, but you need to imagine yourself as an user.
If one day you make your package installed with
pip installnasa-background, I don't think having pictures under site-packages/ folder is a good idea (there is a good chance user won't even have a write permission on Linux). As I understand your program sets a new background each day, there are few use cases
program is executed as service (ex. cron job)
program is executed from command line
program is executed as service and failed (no internet connection)
program can be configured (current code doesn't handle it at all): using CLI parameters, using config file
None of the use cases need actually library for images (you need one latest image, and it probably can be deleted after installed as background), but some of them require additional things (look also BDD). I earn my money mostly by writing programming code, but I am not consider myself as a professional developer, by learning from mistakes I've defined for myself few rules:
Do not reinvent bicycle. Every time when I do something new, I ask google how other people solve that type of problems. I spend up to 0-2 days to educate myself.
Respects standards (related to previous point). Specification, best practice, standard libraries. For example, I can't describe amount of bugs in production code because developers haven't thought about daylight saving clock change.
(there are few more, not relevant for now ...)
I think there are few reasonable standard solutions:
use temp folder/file
import tempfileuse
~/.nasa-backgrounduse ~/.cache/nasa-background ~/.config/nasa-background; ~\AppData\Roaming\nasa-background
I think option #1 or/and #2 are OK. second option will allow you to have folder for configuration file (
import configparser). Find some stable python CLI app on github and check how they do things.I think requests and click are perfectly reasonable packages to use for this application
Yes, they are. Nothing wrong with them, but there are pros and cons. That is just my personal opinion. If you are developing a big applications, than having dependencies is very reasonable (my rules #1 and #2). Your application is small and better to keep it small. You already got few bug reports most of them related installation and packages - probably not what you want to focus on (for ex. moving configuration outside of code is better target). More code == more bugs, more moving parts == more bugs. Standard libraries are stable and backward compatible. None of your usage of those libraries made your code better, not even shorter. Default Conda environment doesn't have
pip(for reason), Conda packages are outdated (for reason), many of your potential users use Conda. You probably already have lost few users because they couldn't run the app.
2
u/barjarbinks Dec 24 '19 edited Dec 24 '19
I am currently working on a very similar project! It finds recent images from EarthPorn, finds the image that most closely matches your desktop resolution, and sets that as your background. I just wanted to say thanks for sharing your code! my method for setting the desktop background badly needs refactoring, so I'll be taking a closer look later to work on that.
not sure how I can help, but feel free to take a look at my project and see if it has anything you may be able to use. https://github.com/karpimpski/desktop-setter
NINJA EDIT: I ran your code and wanted to quickly check back to tell you that it runs fine on Ubuntu 19.10 running GNOME.
1
u/blackiechan99 Dec 24 '19
interested to go about doing something like this. how'd you go about learning how to work with stuff like this? (anyone can chyme in on this bit)
1
u/barjarbinks Dec 25 '19
well I think this application is really cool, but it's not too complicated. there are really just two steps.
first, the program makes an HTTP request to get the image from NASA's website. to do this you need an API key, which you can sign up for here (this is to hold programs like OP's accountable and prevent malicious use). learn about making HTTP requests and reading the data here.
then, determine the user's operating system and set the image as the user's desktop background. if you know which platforms you want to support, it's pretty easy. just find out the command you need to set the desktop background and then run it. learn to do that here. for cross platform compatability you will need to find out the necessary command for as many operating systems and, for Linux, desktop environments that you want to support.
1
u/sportsroc15 Dec 25 '19
This is good stuff. I’m kind of a Python beginner and read your code and completely understood everything you were trying to do. Cool
1
u/slicklikeagato Dec 25 '19
Such a great idea! I worked with APOD previously, but with the Twitter API. Every day I would post either the picture or video of the day to Twitter, along with the description, as well as save the information to a database. It was a fun project.
Great job!
1
Dec 25 '19
I am running a bash script on start up which downloads the national geographic picture of the day using wget. It shouldn`t be much work adjust it to this source. What are the advantages of building an application for this?
1
u/tmaestor Dec 26 '19
Yea, a standard way is to have a config.py file to allow for config to be loaded. If you want the project to be runnable anywhere— using environment variables in your config might be a good idea. Environment variables allow you to pull secrets outside the source core and can be used to deploy using travis CI, gitlab or any other CI tool. These can also be used to deploy it to AWS, Kubernetes or any cloud provider you choose in terms of infrastructure. I will take a look at your GitHub repo this week to provide any more suggestions I might have. Feel free to ask any questions.
1
u/dj_seth81 Jan 11 '20
I'm incompetent and cannot run the nasa_background.py; it says the file doesn't exist.
Any tips or helpful hints to start troubleshooting?
1
u/dj_seth81 Jan 18 '20
Mac OS user here! I managed to add a recursive feature to the existing software, and I would love for y'all to try it out! It's worked for me so far, but I only just finished and I don't have any other OS so time will truly tell. ¯_(ツ)_/¯
Github repository here (sorry I'm new to it and idk how to link it to u/Thomas929292's):
1
u/MikeTheWatchGuy Dec 24 '19
GUI? I posted a suggested start for a GUI as an issue on your github.
1
u/Piece_of_Crap Dec 24 '19
Why a GUI when you can just make script run every day/startup automatically
1
u/MikeTheWatchGuy Dec 24 '19
Why not both? An interactive version and an automatic one requiring no interaction.
5
u/[deleted] Dec 24 '19
Hi, I'm brand new to python. I was able to checkout your repo and ran the requirements, it gives an error because pkg-resources==0.0.0 isn't found.
When I try to run background it says invalid syntax on line 15. Am I doing something wrong?