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 #12

Open
wants to merge 6 commits 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
249 changes: 0 additions & 249 deletions freshpaper.py

This file was deleted.

5 changes: 5 additions & 0 deletions src/Constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NASA_IMAGE_URL = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
BING_IMAGE_URL = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=EN-IN"
BING_IMAGE_DESCRIPTION = "Bing photo of the day"
NASA_IMAGE_DESCRIPTION = "Bing photo of the day"
IMAGE_SOURCES = ['bing', 'nasa']
112 changes: 112 additions & 0 deletions src/DownloadUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import json
import logging
import os
import re
from datetime import datetime

from pip._vendor.requests import ConnectionError

from Constants import NASA_IMAGE_URL, BING_IMAGE_URL

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


logging.basicConfig(level=logging.INFO, format="%(message)s")

log = logging.getLogger(__name__)

class DownloadUtils:

def __init__(self):
pass

def download_image_bing(self, download_dir, image_extension="jpg"):
"""
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
"""
# mkt(s) HIN, EN-IN


try:
image_data = json.loads(urlopen(BING_IMAGE_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


def download_image_nasa(self, download_dir, image_extension="jpg"):
"""
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
"""

try:
image_data = json.loads(urlopen(NASA_IMAGE_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
Loading