Add better background setting scripts
This commit is contained in:
34
bin/.bin/get_natgeo_potd.py
Executable file
34
bin/.bin/get_natgeo_potd.py
Executable file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
today = datetime.today().strftime("%Y-%m-%d")
|
||||
home = os.path.expanduser("~")
|
||||
target_path = f"{home}/var/backgrounds/natgeo/{today}.jpg"
|
||||
|
||||
# If the file for today already exists, just exit
|
||||
if os.path.isfile(target_path):
|
||||
print(f"NatGeo image for {today} already exists, skipping download")
|
||||
exit()
|
||||
|
||||
try:
|
||||
# We've got to go rummaging for:
|
||||
# <meta name="twitter:image:src" content="<full_image_path">
|
||||
potd_uri = "https://www.nationalgeographic.com/photography/photo-of-the-day/"
|
||||
html_doc = requests.get(potd_uri).content
|
||||
soup = BeautifulSoup(html_doc, "html.parser")
|
||||
|
||||
twitter_image_tag = soup.select('meta[name="twitter:image:src"]')[0]
|
||||
|
||||
image_tag = twitter_image_tag
|
||||
img = requests.get(image_tag.attrs["content"], stream=True)
|
||||
|
||||
handle = open(target_path, "wb")
|
||||
for chunk in img.iter_content(chunk_size=512):
|
||||
if chunk: # filter out keep-alive new chunks
|
||||
handle.write(chunk)
|
||||
except: # noqa
|
||||
print("No new NatGeo image for today, sorry.")
|
||||
Reference in New Issue
Block a user