forked from hishamhm/usercount
-
Notifications
You must be signed in to change notification settings - Fork 3
/
publish.py
executable file
·99 lines (82 loc) · 3.34 KB
/
publish.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python3
import os
import sys
from subprocess import call
from mastodon import Mastodon
import time
import common
def get_mastodon(config_file):
config = common.get_json(config_file)
if config is None:
print("File %s not found, exiting." % config_file, file=sys.stderr)
sys.exit(1)
mastodon = Mastodon(
client_id=config["client_id"],
client_secret=config["client_secret"],
access_token=config["access_token"],
api_base_url='https://' + config["mastodon_hostname"] # E.g., mastodon.social
)
return mastodon
def find_closest_timestamp(input_array, seek_timestamp):
closest = input_array[-1]
for item in reversed(input_array):
if abs(int(closest[0])-seek_timestamp) >= abs(int(item[0])-seek_timestamp):
closest = item
else:
return closest
return closest
def main():
# config.txt, mastostats.csv, generate.gnuplot, etc. are in the same folder as this file
os.chdir(os.path.dirname(os.path.abspath(__file__)))
masto_array = common.get_mastostats()
del masto_array[0]
ts = int(time.time())
current_val = find_closest_timestamp(masto_array, ts)
user_count = int(current_val[1])
print("Number of users: %s " % user_count)
print("Number of toots: %s " % current_val[3])
print("Number of instances: %s " % current_val[2])
toot_text = format(user_count, ",d") + " accounts \n"
descriptionForAccessibility = ("Four time-based charts\n\n"
"Upper blue area: Number of Mastodon users\n"
"Upper cyan area: Hourly increases of number of users\n"
"Lower orange area: Number of active instances\n"
"Lower yellow area: Thousand toots per hour\n\n"
"For current figures please read the text of this post")
one_hour = 60 * 60
hours = [1, 24, 168]
prefix = ["Hourly", "Daily", "Weekly"]
suffix = ["hour", "day", "week"]
# Calculate difference in times
for i in range(3):
if len(masto_array) <= hours[i]:
continue
old_ts = ts - hours[i] * one_hour
old_val = find_closest_timestamp(masto_array, old_ts)
change = user_count - int(old_val[1])
print("%s change %s" % (prefix[i], change))
if change > 0:
toot_text += "+" + format(change, ",d") + " in the last " + suffix[i] + "\n"
# Generate chart
call(["gnuplot", "generate.gnuplot"])
# Upload chart
file_to_upload = 'graph.png'
mastodon = get_mastodon(config_file="config.txt")
for i in range(3):
try:
media_dict = None
print("Uploading %s..." % file_to_upload)
media_dict = mastodon.media_post(file_to_upload, mime_type=None, description=descriptionForAccessibility)
print("Uploaded file, returned:")
print(str(media_dict))
print("Tooting...")
print(toot_text, end='')
mastodon.status_post(toot_text, in_reply_to_id=None, media_ids=[media_dict])
print("Successfully tooted!")
sys.exit(0)
except Exception as e:
print("Exception while uploading: " + str(e), file=sys.stderr)
time.sleep(3)
sys.exit(1)
if __name__ == "__main__":
main()