Created
July 29, 2016 05:09
-
-
Save aaossa/1ad62177da4daca8dfdd4c0876d3f7b6 to your computer and use it in GitHub Desktop.
Run it and change your desktop wallpaper to "Bing's Image of the Day". Tested in Win7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests import Session | |
from os.path import join | |
from os import getcwd, remove | |
from PIL import Image | |
import ctypes | |
URL = 'http://www.bing.com' | |
PATH = getcwd() + '\\images' | |
SPI_SETDESKWALLPAPER = 20 | |
def main(): | |
with Session() as session: | |
# Get Bing.com html text | |
html_request = session.get( | |
URL, | |
headers={ | |
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) ' | |
+ 'AppleWebKit/537.36 (KHTML, like Gecko) ' | |
+ 'Chrome/43.0.2357.124 Safari/537.36'} | |
) | |
html_text = html_request.text | |
print("Status = {status}".format(status=html_request.status_code)) | |
# Get image url | |
text = html_text.split('g_img={url:')[1] | |
text = text.split(',')[0].replace("'", "") | |
image_url = URL + text.replace('_1366x768.jpg', '_1920x1080.jpg') | |
__forbidden_chars = [" ", "\\", "\""] | |
image_url = ''.join(url_char | |
for url_char in image_url | |
if url_char not in __forbidden_chars) | |
print("Image url = {img_url}".format(img_url=image_url)) | |
# Define filename and folder | |
fname = image_url.split('/')[-1] | |
image_path_dload = join(PATH, fname) | |
image_path_desktop = join(PATH, fname.replace('.jpg', '.bmp')) | |
print("Filename = {filename}".format(filename=fname)) | |
# Download image | |
img = session.get(image_url, stream=True) | |
with open(image_path_dload, 'wb+') as file: | |
for block in img.iter_content(1024): | |
file.write(block) | |
print("Path = {dir}".format(dir=image_path_dload)) | |
# Convert .jpg to .bmp | |
Image.open(image_path_dload).save(image_path_desktop) | |
print("Image converted to bmp") | |
# Delete .jpg version | |
remove(image_path_dload) | |
print("Delete jpeg image") | |
# Set as desktop background | |
success = ctypes.windll.user32.SystemParametersInfoW( | |
SPI_SETDESKWALLPAPER, 0, image_path_desktop, 3) | |
print("Success = {success}".format(success=success)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment