Skip to content

Commit

Permalink
add new source: Nat Geo - Photo of the Day
Browse files Browse the repository at this point in the history
- implement nat geo photo of the day

closes #3
  • Loading branch information
ahmetelgun authored and guptarohit committed Oct 29, 2019
1 parent 96a5980 commit 381ff39
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ freshpaper
:target: https://github.com/ambv/black
:alt: Code style: black

freshpaper automatically sets `Bing <https://www.bing.com/>`_ photo of the day or `NASA-APOD <https://apod.nasa.gov/apod/astropix.html/>`_ or `Random Unsplash photo <https://source.unsplash.com>`_ as your desktop's wallpaper. Available for Windows, macOS & Linux.
freshpaper automatically sets `Bing <https://www.bing.com/>`_ photo of the day or `NASA-APOD <https://apod.nasa.gov/apod/astropix.html/>`_ or `Random Unsplash photo <https://source.unsplash.com>`_ or `Nat Geo - Photo of the Day <https://www.nationalgeographic.com/photography/photo-of-the-day/>`_ as your desktop's wallpaper. Available for Windows, macOS & Linux.


Installation
Expand All @@ -49,7 +49,7 @@ To update the wallpaper simply run:

$ freshpaper

The default source of wallpaper is ``bing``. Available sources: ``bing``, ``nasa``, ``unsplash_random``.
The default source of wallpaper is ``bing``. Available sources: ``bing``, ``nasa``, ``unsplash_random``, ``nat_geo``.

To change the source of the wallpaper, run:

Expand All @@ -65,7 +65,7 @@ Help command of cli utility:
Usage: freshpaper [OPTIONS] COMMAND [ARGS]...

Options:
--source [bing|nasa|unsplash_random] Source for setting the wallpaper.
--source [bing|nasa|unsplash_random|nat_geo] Source for setting the wallpaper.
--help Show this message and exit.

Contributing
Expand Down
57 changes: 57 additions & 0 deletions freshpaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,70 @@ def download_image_unsplash_random(download_dir, image_extension="jpg"):
raise ConnectionError


def download_image_nat_geo(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
"""
url = "https://www.nationalgeographic.com/photography/photo-of-the-day/"

try:
request = urlopen(url)
except URLError:
log.error("Something went wrong..\nMaybe Internet is not working...")
raise ConnectionError

html = request.read().decode("utf-8")
url_regex = r"twitter:image:src\" content=\"(.*)\""
image_url = re.findall(url_regex, html)[0]

if not image_url:
log.info("No National Geographic image of the day available.\n")
return None

image_name_regex = r"json\":{\"title\":\"(.*)\""
image_name = re.findall(image_name_regex, html)[0]

try:
if not image_name:
image_name = "nat_geo"

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

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


freshpaper_sources = {
"bing": {"download": download_image_bing, "description": "Bing photo of the day"},
"nasa": {"download": download_image_nasa, "description": "NASA photo of the day"},
"unsplash_random": {
"download": download_image_unsplash_random,
"description": "Unsplash random photo",
},
"nat_geo": {
"download": download_image_nat_geo,
"description": "National Geographic photo of the day",
},
}


Expand Down

0 comments on commit 381ff39

Please sign in to comment.