style(heatmap): lint with Black

lint with Black

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2022-05-27 14:16:34 +08:00
parent 5d271f43db
commit 5211f9cb8a

View File

@ -1,98 +1,99 @@
from pydoc import cli import datetime
import numpy as np
import matplotlib.pyplot as plt
import july
import logging import logging
from mastodon import Mastodon import matplotlib.pyplot as plt
import os
import pytz import pytz
import time import time
import os
import datetime
from mastodon import Mastodon
import july
# logging.basicConfig(filename='/home/captain/dev/log/heatmap.log', level=logging.INFO) # logging.basicConfig(filename='/home/captain/dev/log/heatmap.log', level=logging.INFO)
# logger = logging.getLogger('/home/captain/dev/log/heatmap.log') # logger = logging.getLogger('/home/captain/dev/log/heatmap.log')
banner_id = 108361439460735817 banner_id = 108361439460735817
client = Mastodon( client = Mastodon(
access_token = 'Ug_bUMWCk3RLamOnqYIytmeB0nO6aNfjdmf06mAj2bE', access_token="Ug_bUMWCk3RLamOnqYIytmeB0nO6aNfjdmf06mAj2bE",
api_base_url = 'https://nofan.xyz' api_base_url="https://nofan.xyz",
) )
fn = './heatmap.txt' fn = "./heatmap.txt"
# read from file if exists # read from file if exists
date = [] date = []
data = [] data = []
if os.path.exists(fn): if os.path.exists(fn):
with open(fn) as f: with open(fn) as f:
data_list = f.read().splitlines() data_list = f.read().splitlines()
f.close() f.close()
for data_ in data_list: for data_ in data_list:
date_, count = data_.split(' ') date_, count = data_.split(" ")
date.append(date_)
data.append(int(count))
me = client.me()
tl = client.account_statuses(me.id)
zh = pytz.timezone('Asia/Shanghai')
while tl:
print(tl[0].created_at)
break_flag = False
for status in tl:
date_ = status.created_at.astimezone(zh).date().strftime('%Y-%m-%d')
yesterday = datetime.datetime.now(pytz.timezone('Asia/Shanghai')).date() - datetime.timedelta(days=1)
# break if date is not yesterday
if date_ != yesterday.strftime('%Y-%m-%d'):
break_flag = True
break
if date_ not in date:
date.append(date_) date.append(date_)
data.append(0) data.append(int(count))
data[date.index(date_)] += 1 me = client.me()
if break_flag: tl = client.account_statuses(me.id)
break zh = pytz.timezone("Asia/Shanghai")
time.sleep(1)
tl = client.fetch_next(tl) while tl:
print(tl[0].created_at)
break_flag = False
for status in tl:
date_ = status.created_at.astimezone(zh).date().strftime("%Y-%m-%d")
yesterday = datetime.datetime.now(
pytz.timezone("Asia/Shanghai")
).date() - datetime.timedelta(days=1)
# break if date is not yesterday
if date_ != yesterday.strftime("%Y-%m-%d"):
break_flag = True
break
if date_ not in date:
date.append(date_)
data.append(0)
data[date.index(date_)] += 1
if break_flag:
break
time.sleep(1)
tl = client.fetch_next(tl)
else: else:
# init # init
me = client.me() me = client.me()
tl = client.account_statuses(me.id) tl = client.account_statuses(me.id)
zh = pytz.timezone('Asia/Shanghai') zh = pytz.timezone("Asia/Shanghai")
while tl: while tl:
print(tl[0].created_at) print(tl[0].created_at)
for status in tl: for status in tl:
date_ = status.created_at.astimezone(zh).date().strftime('%Y-%m-%d') date_ = status.created_at.astimezone(zh).date().strftime("%Y-%m-%d")
if date_ not in date: if date_ not in date:
date.append(date_) date.append(date_)
data.append(0) data.append(0)
data[date.index(date_)] += 1 data[date.index(date_)] += 1
time.sleep(1) time.sleep(1)
tl = client.fetch_next(tl) tl = client.fetch_next(tl)
# write to file # write to file
with open(fn, 'w') as f: with open(fn, "w") as f:
for i in range(len(date)): for i in range(len(date)):
f.write(date[i] + ' ' + str(data[i]) + '\n') f.write(date[i] + " " + str(data[i]) + "\n")
f.close() f.close()
cleaned_date, cleaned_data = july.utils.preprocess_inputs(date, data) cleaned_date, cleaned_data = july.utils.preprocess_inputs(date, data)
july.heatmap(cleaned_date, cleaned_data, cmap="BuGn") july.heatmap(cleaned_date, cleaned_data, cmap="BuGn")
img_path = './heatmap.png' img_path = "./heatmap.png"
plt.savefig(img_path, bbox_inches='tight') plt.savefig(img_path, bbox_inches="tight")
# upload image to mastodon # upload image to mastodon
media = client.media_post(img_path) media = client.media_post(img_path)
headers = {} headers = {}
headers['Authorization'] = 'Bearer ' + client.access_token headers["Authorization"] = "Bearer " + client.access_token
params = { params = {"status": "今天浑浑噩噩了吗", "media_ids": [media.id]}
'status': '今天浑浑噩噩了吗',
'media_ids': [media.id]
}
kwargs = dict(headers=headers, files={}, timeout=client.request_timeout, json=params) kwargs = dict(headers=headers, files={}, timeout=client.request_timeout, json=params)
url = client.api_base_url + '/api/v1/statuses/%s/' % banner_id url = client.api_base_url + "/api/v1/statuses/%s/" % banner_id
resp = client.session.request('PUT', url, **kwargs) resp = client.session.request("PUT", url, **kwargs)
if resp.status_code == 200: if resp.status_code == 200:
client.session.request('GET', 'https://up.tunpok.com/api/push/qku6CYquGu?msg=OK&ping=') client.session.request(
"GET", "https://up.tunpok.com/api/push/qku6CYquGu?msg=OK&ping="
)