From 3dfc0d03f43cd2302cb9cff76f4b93e175371cb5 Mon Sep 17 00:00:00 2001 From: Abhishek Modak Date: Sat, 19 Oct 2019 14:58:49 +0530 Subject: [PATCH] refactored code --- constants.py | 17 +++++++++ freshpaper.py | 96 ++++++++------------------------------------------- utils.py | 76 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 82 deletions(-) create mode 100644 constants.py create mode 100644 utils.py diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..74fbe0a --- /dev/null +++ b/constants.py @@ -0,0 +1,17 @@ +NASA_URL = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY" +BING_URL = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=EN-IN" + + +BING = "BING" +NASA = "NASA" + +#### IMAGE_LOCATIONS + +IMAGE_WINDOWS = "My Pictures" +IMAGE_LINUX = "PICTURES" +IMAGE_FOLDER = "freshpaper" + + +WINDOWS_SIGNATURE = "win32" +DEFAULT_IMG_EXTENSION = "jpg" +DECODE_METHOD = "utf-8" \ No newline at end of file diff --git a/freshpaper.py b/freshpaper.py index e7bae65..1a8cce9 100644 --- a/freshpaper.py +++ b/freshpaper.py @@ -2,13 +2,12 @@ import re import sys import click -import json import logging from random import choice -from datetime import datetime from subprocess import check_call, CalledProcessError from PIL import Image - +import constants as CT +from utils import download_image try: # for python3 from urllib.request import urlopen, urlretrieve, HTTPError, URLError @@ -40,7 +39,7 @@ def set_wallpaper(image_path): ) ) - if sys.platform.startswith("win32"): + if sys.platform.startswith(CT.WINDOWS_SIGNATURE): bmp_image = Image.open(image_path) bmp_img_path = os.path.splitext(image_path)[0] + ".bmp" @@ -54,7 +53,7 @@ def set_wallpaper(image_path): win32api.RegSetValueEx(key, "WallpaperStyle", 0, win32con.REG_SZ, "0") win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0") win32gui.SystemParametersInfo( - win32con.SPI_SETDESKWALLPAPER, bmp_img_path, 1 + 2 + win32con.SPI_SETDESKWALLPAPER, bmp_img_path, 3 ) os.remove(bmp_img_path) elif sys.platform.startswith("darwin"): @@ -107,16 +106,15 @@ def get_saved_wallpaper(wall_dir): def get_wallpaper_directory(): """ check if `default` wallpaper download directory exists or not, create if doesn't exist """ pictures_dir = "" - wall_dir_name = "freshpaper" - os.path.join(os.sep, os.path.expanduser("~"), "a", "freshpaper") - if sys.platform.startswith("win32"): - pictures_dir = "My Pictures" + os.path.join(os.sep, os.path.expanduser("~"), "a", CT.IMAGE_FOLDER) + if sys.platform.startswith(CT.WINDOWS_SIGNATURE): + pictures_dir = CT.IMAGE_WINDOWS elif sys.platform.startswith("darwin"): - pictures_dir = "Pictures" + pictures_dir = CT.IMAGE_LINUX elif sys.platform.startswith("linux"): - pictures_dir = "Pictures" + pictures_dir = CT.IMAGE_LINUX wall_dir = os.path.join( - os.sep, os.path.expanduser("~"), pictures_dir, wall_dir_name + os.sep, os.path.expanduser("~"), pictures_dir, CT.IMAGE_FOLDER ) if not os.path.isdir(wall_dir): @@ -127,7 +125,7 @@ def get_wallpaper_directory(): return wall_dir -def download_image_bing(download_dir, image_extension="jpg"): +def download_image_bing(download_dir, image_extension = CT.DEFAULT_IMG_EXTENSION): """ Download & save the image :param download_dir: directory where to download the image @@ -136,43 +134,10 @@ def download_image_bing(download_dir, image_extension="jpg"): """ # mkt(s) HIN, EN-IN - url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=EN-IN" - - try: - image_data = json.loads(urlopen(url).read().decode("utf-8")) - - image_url = "http://www.bing.com" + image_data["images"][0]["url"] - - image_name = re.search(r"OHR\.(.*?)_", image_url).group(1) - - image_url_hd = "http://www.bing.com/hpwp/" + image_data["images"][0]["hsh"] - date_time = datetime.now().strftime("%d_%m_%Y") - image_file_name = "{image_name}_{date_stamp}.{extention}".format( - image_name=image_name, date_stamp=date_time, extention=image_extension - ) - - image_path = os.path.join(os.sep, download_dir, image_file_name) - log.debug("download_dir: {}".format(download_dir)) - log.debug("image_file_name: {}".format(image_file_name)) - log.debug("image_path: {}".format(image_path)) - - if os.path.isfile(image_path): - log.info("No new wallpaper yet..updating to latest one.\n") - return image_path - - try: - log.info("Downloading..") - urlretrieve(image_url_hd, filename=image_path) - except HTTPError: - log.info("Downloading...") - urlretrieve(image_url, filename=image_path) - return image_path - except URLError: - log.error("Something went wrong..\nMaybe Internet is not working...") - raise ConnectionError + download_image(CT.BING, download_dir, image_extension) -def download_image_nasa(download_dir, image_extension="jpg"): +def download_image_nasa(download_dir, image_extension = CT.DEFAULT_IMG_EXTENSION): """ Download & save the image :param download_dir: directory where to download the image @@ -180,40 +145,7 @@ def download_image_nasa(download_dir, image_extension="jpg"): :return: downloaded image path """ - url = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY" - - try: - image_data = json.loads(urlopen(url).read().decode("utf-8")) - - image_url = image_data.get("url") - - image_name = image_data.get("title").split(" ")[0] - - image_url_hd = image_data.get("hdurl") - date_time = datetime.now().strftime("%d_%m_%Y") - image_file_name = "{image_name}_{date_stamp}.{extention}".format( - image_name=image_name, date_stamp=date_time, extention=image_extension - ) - - image_path = os.path.join(os.sep, download_dir, image_file_name) - log.debug("download_dir: {}".format(download_dir)) - log.debug("image_file_name: {}".format(image_file_name)) - log.debug("image_path: {}".format(image_path)) - - if os.path.isfile(image_path): - log.info("No new wallpaper yet..updating to latest one.\n") - return image_path - - try: - log.info("Downloading..") - urlretrieve(image_url_hd, filename=image_path) - except HTTPError: - log.info("Downloading...") - urlretrieve(image_url, filename=image_path) - return image_path - except URLError: - log.error("Something went wrong..\nMaybe Internet is not working...") - raise ConnectionError + download_image(CT.NASA, download_dir, image_extension) freshpaperSources = { diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..aa46dd7 --- /dev/null +++ b/utils.py @@ -0,0 +1,76 @@ +import os +import re +import json +from datetime import datetime +import constants as CT + +import logging +logging.basicConfig(level=logging.INFO, format="%(message)s") + +log = logging.getLogger(__name__) + +try: + # for python3 + from urllib.request import urlopen, urlretrieve, HTTPError, URLError +except ImportError: + # for python2 + from urllib import urlretrieve + from urllib2 import urlopen, HTTPError, URLError + + +def download_image_bing_(): + url = CT.BING_URL + image_data = json.loads(urlopen(url).read().decode(CT.DECODE_METHOD)) + image_url = "http://www.bing.com" + image_data["images"][0]["url"] + image_name = re.search(r"OHR\.(.*?)_", image_url).group(1) + image_url_hd = "http://www.bing.com/hpwp/" + image_data["images"][0]["hsh"] + return image_data, image_name, image_url_hd, image_url + + +def download_image_nasa_(): + url = CT.NASA_URL + image_data = json.loads(urlopen(url).read().decode(CT.DECODE_METHOD)) + image_url = image_data.get("url") + image_name = image_data.get("title").split(" ")[0] + image_url_hd = image_data.get("hdurl") + return image_data, image_name, image_url_hd, image_url + + +def download_image(source, download_dir, image_extension): + + """ + Download & save the image + :param download_dir: directory where to download the image + :param image_extension: directory where to download the image + :return: downloaded image path + """ + + if source == CT.NASA: + image_data, image_name, image_url_hd, image_url = download_image_bing_() + else: + image_data, image_name, image_url_hd, image_url = download_image_nasa_() + try: + date_time = datetime.now().strftime("%d_%m_%Y") + image_file_name = "{image_name}_{date_stamp}.{extention}".format( + image_name=image_name, date_stamp=date_time, extention=image_extension + ) + + image_path = os.path.join(os.sep, download_dir, image_file_name) + log.debug("download_dir: {}".format(download_dir)) + log.debug("image_file_name: {}".format(image_file_name)) + log.debug("image_path: {}".format(image_path)) + + if os.path.isfile(image_path): + log.info("No new wallpaper yet..updating to latest one.\n") + return image_path + + try: + log.info("Downloading..") + urlretrieve(image_url_hd, filename=image_path) + except HTTPError: + log.info("Downloading...") + urlretrieve(image_url, filename=image_path) + return image_path + except URLError: + log.error("Something went wrong..\nMaybe Internet is not working...") + raise ConnectionError \ No newline at end of file