Skip to content

Instantly share code, notes, and snippets.

@smzn
Created July 17, 2024 01:11
Show Gist options
  • Save smzn/92a258a6ba62bfc48dd66593c7e8b152 to your computer and use it in GitHub Desktop.
Save smzn/92a258a6ba62bfc48dd66593c7e8b152 to your computer and use it in GitHub Desktop.
import requests
import datetime
import pytz
import mysql.connector
# ISSの現在位置を取得
url = "http://api.open-notify.org/iss-now.json"
response = requests.get(url)
data = response.json()
# データの抽出
timestamp = data['timestamp']
latitude = data['iss_position']['latitude']
longitude = data['iss_position']['longitude']
# timestampをUTCからJSTに変換
utc_time = datetime.datetime.utcfromtimestamp(timestamp)
jst = pytz.timezone('Asia/Tokyo')
jst_time = utc_time.replace(tzinfo=pytz.utc).astimezone(jst)
jst_date_time = jst_time.strftime('%Y-%m-%d %H:%M:%S')
# MySQLデータベースに接続
connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="iss"
)
cursor = connection.cursor()
# データの挿入
insert_query = """
INSERT INTO point (timestamp, latitude, longitude)
VALUES (%s, %s, %s)
"""
cursor.execute(insert_query, (jst_date_time, latitude, longitude))
connection.commit()
cursor.close()
connection.close()
print("Data inserted successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment