-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (44 loc) · 1.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from datetime import datetime, timezone
import json
import os
import requests
from google.cloud import storage
# Get datetime
dt = datetime.now(timezone.utc)
dtstr = dt.strftime("%Y-%m-%d %H:%M:%S.%f")
out_file = f'{dt.strftime("%Y-%m-%d-%H-%M-%S")}.json'
TMP_FILE = 'last_ip.json'
GOOGLE_CREDS = '<service-role-that-can-upload-to-cloud-storage>.json'
BUCKET_NAME = '<bucket-name-for-data>'
BUCKET_FOLDER = '<bucket-folder-for-data>'
# Load data if file exists
try:
with open(TMP_FILE, 'r') as f:
data = json.load(f)
except (json.decoder.JSONDecodeError, FileNotFoundError) as e:
data = {}
# Get external IP
r = requests.get('https://api.ipify.org?format=json')
r.raise_for_status()
external_ip = r.json()['ip']
# TODO - confirm it is in IP format
# Quit if IP has not changed
if data.get('external_ip') == external_ip:
print(f"IP has not changed: {external_ip}")
quit()
# Log change
print(f"IP changed to {external_ip} from {data.get('external_ip')}")
# Create payload
new_data = {"datetime": dtstr, "external_ip": external_ip}
out_data = json.dumps(new_data)
# Upload file to Google Storage
print('Upload to Google Storage')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = GOOGLE_CREDS
storage_client = storage.Client()
bucket = storage_client.bucket(BUCKET_NAME)
blob = bucket.blob(f'{BUCKET_FOLDER}/{out_file}')
blob.upload_from_string(out_data)
# If it changed overwrite local file
print('Overwrite local file')
with open(TMP_FILE, 'w') as f:
f.write(out_data)