Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
National Geographic Photo of the Day Wallpaper Changer
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import requests
import splinter
import redis
IMAGE_DIRECTORY = os.path.expanduser('~/Pictures/wallpapers/national-geographic-photo-of-the-day')
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0)
if not os.path.exists(IMAGE_DIRECTORY):
os.makedirs(IMAGE_DIRECTORY)
default_url = 'http://photography.nationalgeographic.com/photography/photo-of-the-day/'
url = redis_client.get("nationalgeographic_wallpaper_downloader_last_url") or default_url
counter = 0
with splinter.Browser("phantomjs") as browser:
browser.visit(url)
while True:
if browser.status_code.is_success():
image_url = browser.evaluate_script("""document.evaluate('.//div[@class="primary_photo"]/*/img', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.src""")
filename = os.path.basename(image_url)
filepath = os.path.join(IMAGE_DIRECTORY, filename)
if not os.path.exists(filepath):
r = requests.get(image_url, stream=True)
if r.status_code == 200:
with open(filepath, 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)
next_url = browser.evaluate_script("""document.evaluate(".//a[contains(text(), '« Previous')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.href""")
browser.visit(next_url)
counter += 1
redis_client.set("nationalgeographic_wallpaper_downloader_last_url", next_url)
print next_url, counter
#!/usr/bin/python
import os
import subprocess
import requests
import splinter
IMAGE_DIRECTORY = os.path.expanduser('~/Pictures/wallpapers/national-geographic-photo-of-the-day')
if not os.path.exists(IMAGE_DIRECTORY):
os.makedirs(IMAGE_DIRECTORY)
url = 'http://photography.nationalgeographic.com/photography/photo-of-the-day/'
with splinter.Browser("phantomjs") as browser:
browser.visit(url)
if browser.status_code.is_success():
image_url = browser.evaluate_script("""document.evaluate('.//div[@class="primary_photo"]/*/img', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.src""")
filename = os.path.basename(image_url)
filepath = os.path.join(IMAGE_DIRECTORY, filename)
if not os.path.exists(filepath):
r = requests.get(image_url, stream=True)
if r.status_code == 200:
with open(filepath, 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)
p = subprocess.Popen(["feh", "--bg-fill", filepath])
p.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment