Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored code #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -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"
96 changes: 14 additions & 82 deletions freshpaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"):
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -136,84 +134,18 @@ 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
:param image_extension: directory where to download the image
: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 = {
Expand Down
76 changes: 76 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -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