code: def main():
      opt = parse_cmdline()
      try:
          token = os.environ["APPVEYOR_TOKEN"]
      except KeyError:
          raise ScriptError(
              "please set a APPVEYOR_TOKEN to download artifacts"
          )
  
      s = requests.Session()
      s.headers["Content-Type"] = "application/json"
      s.headers["Authorization"] = f"Bearer {token}"
  
      if opt.build:
          logger.info("fetching build %s", opt.build)
          resp = s.get(f"{API_URL}/projects/{REPOS}/build/{opt.build}")
      else:
          logger.info("fetching last run")
          resp = s.get(f"{API_URL}/projects/{REPOS}")
  
      resp.raise_for_status()
      data = resp.json()
  
      updated_at = dt.datetime.fromisoformat(
          re.sub(r"\.\d+", "", data["build"]["finished"])
      )
      now = dt.datetime.now(dt.timezone.utc)
      age = now - updated_at
      logger.info(
          f"found build {data['build']['version']} updated {pretty_interval(age)} ago"
      )
      if age > dt.timedelta(hours=6):
          logger.warning("maybe it's a bit old?")
  
      jobs = data["build"]["jobs"]
      for job in jobs:
          if job["status"] != "success":
              raise ScriptError(
                  f"status for job {job['jobId']} is {job['status']}"
              )
  
          logger.info(f"fetching artifacts info for {job['name']}")
          resp = s.get(f"{API_URL}/buildjobs/{job['jobId']}/artifacts/")
          resp.raise_for_status()
          afs = resp.json()
          for af in afs:
              fn = af["fileName"]
              if fn.startswith("dist/"):
                  fn = fn.split("/", 1)[1]
              dest = Path("wheelhouse") / fn
              logger.info(f"downloading {dest}")
              resp = s.get(
                  f"{API_URL}/buildjobs/{job['jobId']}/artifacts/{af['fileName']}"
              )
              resp.raise_for_status()
              if not dest.parent.exists():
                  dest.parent.mkdir(parents=True)
  
              with dest.open("wb") as f:
                  f.write(resp.content)
  
      logger.info("now you can run: 'twine upload -s wheelhouse/*'")
  
file_path: psycopg2/scripts/build/download_packages_appveyor.py
imports: import os
  import re
  import sys
  import logging
  import datetime
  from pathlib import Path
  from argparse import ArgumentParser
  import requests
label_desc: Use image node_id 1 for calling a global function with example usage: main() without return types