get_natgeo_potd.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. import os
  3. from datetime import datetime
  4. import requests
  5. from bs4 import BeautifulSoup
  6. today = datetime.today().strftime("%Y-%m-%d")
  7. home = os.path.expanduser("~")
  8. target_path = f"{home}/var/media/backgrounds/natgeo/{today}.jpg"
  9. # If the file for today already exists, just exit
  10. if os.path.isfile(target_path):
  11. print(f"NatGeo image for {today} already exists, skipping download")
  12. exit()
  13. try:
  14. # We've got to go rummaging for:
  15. # <meta name="twitter:image:src" content="<full_image_path">
  16. potd_uri = "https://www.nationalgeographic.com/photography/photo-of-the-day/"
  17. html_doc = requests.get(potd_uri).content
  18. soup = BeautifulSoup(html_doc, "html.parser")
  19. twitter_image_tag = soup.select('meta[name="twitter:image:src"]')[0]
  20. image_tag = twitter_image_tag
  21. img = requests.get(image_tag.attrs["content"], stream=True)
  22. handle = open(target_path, "wb")
  23. for chunk in img.iter_content(chunk_size=512):
  24. if chunk: # filter out keep-alive new chunks
  25. handle.write(chunk)
  26. except: # noqa
  27. print("No new NatGeo image for today, sorry.")