Add influx data

This commit is contained in:
Alexander Hosking 2023-01-08 23:10:07 -05:00
parent 7451cf0685
commit 9c08687f25

View File

@ -1,9 +1,13 @@
import os
import imaplib
import json
from dotenv import load_dotenv
from time import strftime
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
from dotenv import load_dotenv, dotenv_values
load_dotenv()
config = dotenv_values(".env")
EMAIL_ACCOUNTS = json.loads(os.environ.get("MAIL_ACCOUNTS"))
EMAIL_PASSWORDS = json.loads(os.environ.get("MAIL_PASSWORDS"))
@ -11,6 +15,14 @@ EMAIL_PASSWORDS = json.loads(os.environ.get("MAIL_PASSWORDS"))
# to convert lists to dictionary
accounts = dict(zip(EMAIL_ACCOUNTS, EMAIL_PASSWORDS))
print(accounts)
# Influx DB
client = influxdb_client.InfluxDBClient(
url=config['DB_URL'],
token=config['DB_TOKEN'],
org=config['DB_ORG']
)
################ IMAP SSL ##############################
for account in accounts:
@ -42,4 +54,12 @@ for account in accounts:
imap_ssl.close()
if unread_messages:
message = f"{account} has {unread_messages} messages."
date = strftime("%Y-%m-%d")
time = strftime("%H:%M:%S")
timestamp = date + "T" + time + "Z"
write_api = client.write_api(write_options=SYNCHRONOUS)
point = influxdb_client.Point("unread_emails").time(timestamp).tag("account", account)\
.field("count", unread_messages)
write_api.write(bucket="personal_data",
org=config['DB_ORG'], record=point)
print(message)