From 5d271f43db2f70a969cbc4015cfe49ab07506cd8 Mon Sep 17 00:00:00 2001 From: Ching Date: Wed, 25 May 2022 16:32:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(heatmap):=20=E8=AE=A1=E7=AE=97=E6=98=A8?= =?UTF-8?q?=E6=97=A5=20status=20=E6=95=B0=E9=87=8F=E5=B9=B6=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 计算昨日 status 数量并记录 Signed-off-by: Ching --- heatmap.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/heatmap.py b/heatmap.py index 3a30919..deded34 100644 --- a/heatmap.py +++ b/heatmap.py @@ -7,6 +7,7 @@ from mastodon import Mastodon import pytz import time import os +import datetime # logging.basicConfig(filename='/home/captain/dev/log/heatmap.log', level=logging.INFO) # logger = logging.getLogger('/home/captain/dev/log/heatmap.log') @@ -29,6 +30,28 @@ if os.path.exists(fn): 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_) + data.append(0) + data[date.index(date_)] += 1 + if break_flag: + break + time.sleep(1) + tl = client.fetch_next(tl) else: # init @@ -47,17 +70,17 @@ else: time.sleep(1) tl = client.fetch_next(tl) - # write to file - with open(fn, 'w') as f: - for i in range(len(date)): - f.write(date[i] + ' ' + str(data[i]) + '\n') - f.close() +# write to file +with open(fn, 'w') as f: + for i in range(len(date)): + f.write(date[i] + ' ' + str(data[i]) + '\n') + f.close() cleaned_date, cleaned_data = july.utils.preprocess_inputs(date, data) -july.heatmap(cleaned_date, cleaned_data, title='', cmap="BuGn") +july.heatmap(cleaned_date, cleaned_data, cmap="BuGn") img_path = './heatmap.png' -plt.savefig(img_path) +plt.savefig(img_path, bbox_inches='tight') # upload image to mastodon media = client.media_post(img_path)