首页 > 其他分享 >Airflow - Study Notes 4

Airflow - Study Notes 4

时间:2025-01-19 15:43:23浏览次数:1  
标签:Airflow get Study api image import requests Notes today

 

 

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

 

 

 

标签:Airflow,get,Study,api,image,import,requests,Notes,today
From: https://www.cnblogs.com/zhangzhihui/p/18679611

相关文章

  • Airflow - Study Notes 3
       (.venv)frank@ZZHUBT:~/venvs/my_airflow_project$airflowconfigget-valuecoreexecutorSequentialExecutor         ......
  • Airflow - Study Notes 1
    ApacheAirflowisknownwithinthedataengineeringcommunityasthego-toopensource platformfor“developing,scheduling,andmonitoringbatch-orientedworkflows.”       ......
  • day1-study markdown
    MARKDOWN学习标题标题:#+空格+标题名(一级标题)##+空格+标题名(二级标题)字体粗体:helloworld(****)斜体:helloworld(**)删除:helloworld(~~~~)引用坚持的男生最帅(>+空格+引用的话)分割线(三个减号线)图片(超链接哔哩哔哩([超链接名字](链接地址))列表......
  • PySpark - Study Notes 2
       ......
  • PySpark - Study Notes 1
     frank@ZZHUBT:~$dockerpulljupyter/pyspark-notebook dockerrun--namepyspark-notebook-p8888:8888-v~/dkvols/pyspark-notebook/:/home/jovyan/work/-djupyter/pyspark-notebook:latest frank@ZZHUBT:~$dockerlogspyspark-notebookEnteredstart.......
  • DuckDB - Study Notes 11
      (zpy310)frank@ZZHUBT:~$pipinstallduckdb(zpy310)frank@ZZHUBT:~$pipinstallharlequin......SuccessfullyinstalledMarkupSafe-3.0.2click-8.1.8harlequin-1.25.2jinja2-3.1.5linkify-it-py-2.0.3markdown-it-py-3.0.0mdit-py-plugins-0.4.2m......
  • DuckDB - Study Notes 8
         pi_relation=duckdb.sql("SELECTpi()ASpi")type(pi_relation)#duckdb.duckdb.DuckDBPyRelation pi_relation.show() ┌───────────────────┐│pi││double│├─────────......
  • Airflow:深入理解Airflow Sensor
    ApacheAirflowSensors是实现特定感知的任务,它可以持续监控外部条件或事件,并阻止下游任务的执行,直到满足指定的条件。它们对于编排复杂的工作流是必不可少的,在这些工作流中,任务需要在继续之前等待外部依赖关系变得可用。在这个全面的指南中,我们将详细探讨ApacheAirflowS......
  • Markdown-study
    Markdown-study标题一级标题一个#二级标题两个#三级标题三个#字体粗体左右两个*斜体左右一个*横线左右两个~引用一个>+空格即可分割线三个-或者三个*图片![图片](路径)超链接点击跳转列表数字+空格B减-+空格B表格右键插入即可......
  • DuckDB - Study Notes 6
      DuckDB’snesteddatatypes:LIST, MAP,andSTRUCT. DSELECT[7,8,9]ASlist_int;┌───────────┐│list_int││int32[]│├───────────┤│[7,8,9]│└───────────┘ DSELECT['Quantumof......