Skip to content

Instantly share code, notes, and snippets.

@battleguard
Last active March 18, 2022 16:09
Show Gist options
  • Save battleguard/92fa012c15de1c7ac27750b0280407c3 to your computer and use it in GitHub Desktop.
Save battleguard/92fa012c15de1c7ac27750b0280407c3 to your computer and use it in GitHub Desktop.
grabs the latest todays image off astrobin. Can be used with a daily service runner to auto populate a wallpaper folder over time.
from bs4 import Tag, BeautifulSoup
import requests
from pathlib import Path
def get_todays_page():
page = requests.get('https://www.astrobin.com/iotd/archive/')
soup = BeautifulSoup(page.content, 'html.parser')
image_html: Tag = soup.find('div', class_="iotd-archive-image")
image_path_segment = image_html.find("a").attrs["href"]
image_page_url = f'https://www.astrobin.com/full{image_path_segment}0/?real='
return image_page_url
def get_image_url(image_page_url: str):
page = requests.get(image_page_url)
soup = BeautifulSoup(page.content, 'html.parser')
image_tag: Tag = soup.find('img', class_="astrobin-image real")
image_url = image_tag.attrs["src"]
return image_url
def main():
image_url = get_image_url(get_todays_page())
astro_images_dir: Path = Path.home().joinpath('Pictures', 'atrobin')
astro_images_dir.mkdir(exist_ok=True)
r = requests.get(image_url, stream=True, headers={'User-agent': 'Mozilla/5.0'})
image_path = astro_images_dir.joinpath(Path(image_url).name)
open(str(image_path), 'wb').write(r.content)
if __name__ == '__main__':
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment