diff --git a/projects/write a script to download a random image from unsplash and set it as wallpaper/README.md b/projects/write a script to download a random image from unsplash and set it as wallpaper/README.md new file mode 100644 index 000000000..ed9e07834 --- /dev/null +++ b/projects/write a script to download a random image from unsplash and set it as wallpaper/README.md @@ -0,0 +1,26 @@ +## Set a Random desktop background + +This script will download a random image from [unsplash](https://source.unsplash.com/random) and set it as the desktop background. + +**The image will be saved as "random.jpg" makesure that there are no files saved as "random.jpg" in the current directory** + +### Requirements + +#### Linux + +Install [Nitrogen](https://wiki.archlinux.org/index.php/Nitrogen) + +``` +pip install requests +``` + +### Usage + +```python +python background_linux.py +``` +OR + +```python +python background_windows.py +``` diff --git a/projects/write a script to download a random image from unsplash and set it as wallpaper/background_linux.py b/projects/write a script to download a random image from unsplash and set it as wallpaper/background_linux.py new file mode 100644 index 000000000..393fb1ab8 --- /dev/null +++ b/projects/write a script to download a random image from unsplash and set it as wallpaper/background_linux.py @@ -0,0 +1,27 @@ +from requests import get # to make GET request +from os import system, getcwd, path + + +url = "https://source.unsplash.com/random" +filename = "random.jpg" + + +def download(url, file_name): + ''' + downloading the file and saving it + ''' + with open(file_name, "wb") as file: + response = get(url) + file.write(response.content) + + +def setup(pathtofile): + ''' + setting the up file + ''' + system("nitrogen --set-auto {}".format(path.join(getcwd(), pathtofile))) + + +if __name__ == "__main__": + download(url, filename) + setup(filename) diff --git a/projects/write a script to download a random image from unsplash and set it as wallpaper/background_windows.py b/projects/write a script to download a random image from unsplash and set it as wallpaper/background_windows.py new file mode 100644 index 000000000..abb79a322 --- /dev/null +++ b/projects/write a script to download a random image from unsplash and set it as wallpaper/background_windows.py @@ -0,0 +1,39 @@ +from requests import get +import os +import ctypes +import argparse +import sys + +url = "https://source.unsplash.com/random" +filename = "random.jpg" + +def is_64bit(): + return sys.maxsize > 2 ** 32 + + +def download(url, file_name): + ''' + downloading the file and saving it + ''' + with open(file_name, "wb") as file: + response = get(url) + file.write(response.content) + + +def setup(pathtofile,version): + name_of_file = pathtofile + path_to_file = os.path.join(os.getcwd(), name_of_file) + SPI_SETDESKWALLPAPER = 20 + if is_64bit(): + ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_file, 0) + else: + ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path_to_file, 0) + + +if __name__ == "__main__": + try: + download(url, filename) + setup(file_name) + except Exception as e: + print(f"Error {e}") + raise NotImplementedError