ZhangZhihui's Blog  

 

 

To retrieve these images, I frequently make use of the NASA Astronomy Picture of the Day API (https://apod.nasa.gov/apod/astropix.html) to gather a new image. This is a free API requiring an API key to be created but is easily accessible.

 

 

 

 

import requests
import json
from datetime import date

 

from NASA_Keys import api_key

url = f'https://api.nasa.gov/planetary/apod?api_key={api_key}'
response = requests.get(url).json()
response

 

{'date': '2025-01-19',
 'explanation': "What would it look like to land on Saturn's moon Titan? The European Space Agency's Huygens probe set down on the Solar System's cloudiest moon in 2005, and a time-lapse video of its descent images was created. Huygens separated from the robotic Cassini spacecraft soon after it achieved orbit around Saturn in late 2004 and began approaching Titan. For two hours after arriving, Huygens plummeted toward Titan's surface, recording at first only the shrouded moon's opaque atmosphere. The computerized truck-tire sized probe soon deployed a parachute to slow its descent, pierced the thick clouds, and began transmitting images of a strange surface far below never before seen in visible light. Landing in a dried sea and surviving for 90 minutes, Huygen's returned unique images of a strange plain of dark sandy soil strewn with smooth, bright, fist-sized rocks of ice.",
 'media_type': 'video',
 'service_version': 'v1',
 'title': 'Titan Touchdown: Huygens Descent Movie',
 'url': 'https://www.youtube.com/embed/msiLWxDayuA?rel=0'}

 

today_image = 'https://apod.nasa.gov/apod/image/2311/Kirkjufell2023Nov9_1024.jpg'
r = requests.get(today_image)
with open(f'todays_image_{date.today()}.png', 'wb') as f:
    f.write(requests.get(today_image).content)

 

复制代码
import json
import pathlib
import airflow
import requests
import requests.exceptions as request_exceptions
from datetime import date
from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.decorators import task
from datetime import datetime, timedelta


dag_owner = 'Frank'
复制代码

 

复制代码
def _get_pictures():
    pathlib.Path("/tmp/images").mkdir(parents=True,
    exist_ok=True)
    api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    url = f'https://api.nasa.gov/planetary/apod?api_key={api_key}'
    response = requests.get(url).json()
    # today_image = response['url']
    today_image = 'https://apod.nasa.gov/apod/image/2311/Kirkjufell2023Nov9_1024.jpg'
    with open(f'todays_image_{date.today()}.png', 'wb') as f:
        f.write(requests.get(today_image).content)
复制代码

 

default_args = {'owner': dag_owner,
    'depends_on_past': False,
    'retries': 2,
    'retry_delay': timedelta(minutes=5)
}

 

with DAG(dag_id='download_APOD_image',
    default_args=default_args,
    description='download and notify ',
    start_date = airflow.utils.dates.days_ago(0),
    schedule_interval='@daily',
    catchup=False,
    tags=['None']
):

 

    get_pictures = PythonOperator(
        task_id="get_pictures",
        python_callable=_get_pictures,
    )

 

    notify = BashOperator(
        task_id="notify",
        bash_command='echo f"Image for today has been added!"',
    )

 

    get_pictures >> notify

 

 

复制代码
import json
import pathlib
import airflow
import requests
import requests.exceptions as request_exceptions
from datetime import date
from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.decorators import task
from datetime import datetime, timedelta


dag_owner = "Frank"

default_args = {
    "owner": dag_owner,
    "depends_on_past": False,
    "retries": 2,
    "retry_delay": timedelta(minutes=5),
}


def _get_pictures():
    pathlib.Path("/tmp/images").mkdir(parents=True, exist_ok=True)
    api_key = "w9meQXZnGW7SJgN8vVUdV5uKriWTXobbMx6YGTm4"
    url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
    response = requests.get(url).json()
    # today_image = response['url']
    today_image = "https://apod.nasa.gov/apod/image/2311/Kirkjufell2023Nov9_1024.jpg"
    with open(f"todays_image_{date.today()}.png", "wb") as f:
        f.write(requests.get(today_image).content)


with DAG(
    dag_id="download_APOD_image",
    default_args=default_args,
    description="download and notify ",
    start_date=airflow.utils.dates.days_ago(0),
    schedule_interval="@daily",
    catchup=False,
    tags=["None"],
):
    get_pictures = PythonOperator(
        task_id="get_pictures",
        python_callable=_get_pictures,
    )

    notify = BashOperator(
        task_id="notify",
        bash_command='echo f"Image for today has been added!"',
    )

    get_pictures >> notify
复制代码

 

 

 

posted on   ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2024-01-19 Go - Pad a number with zeros when printing
 
点击右上角即可分享
微信分享提示