-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.py
80 lines (68 loc) · 2.37 KB
/
common.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
from mastodon import Mastodon
from sys import exit
def get_api(url, token_name = ""):
if token_name:
try:
file = open('token/' + token_name, 'r')
except FileNotFoundError:
print('Token not found for ' + token_name)
exit()
else:
token = file.read().splitlines()[0]
file.close()
else:
token = ""
return Mastodon(access_token = token, api_base_url = url, ratelimit_method='throw', version_check_mode='none')
def list_read(name):
try:
file = open('list/' + name, 'r')
except FileNotFoundError:
file = open('list/' + name, 'x')
file.close()
return []
else:
list = file.read().splitlines()
file.close()
return list
def list_write(name, values):
file = open('list/' + name, 'w')
for value in values:
file.write(str(value) + '\n')
file.close()
def list_append(name, value):
file = open('list/' + name, 'a')
file.write(value + '\n')
file.close()
# It is not safe to get notifications from "last_id" because some may have been deleted
def get_new_notifications(api, bot_name, types=None):
last_notifications = list_read(bot_name + '_last_notifications')
notifications = api.notifications(types=types)
new_notifications = []
new_notifications_ids = []
for n in notifications:
if str(n['id']) not in last_notifications:
new_notifications.append(n)
new_notifications_ids.append(str(n['id']))
for old_notification in last_notifications:
if len(new_notifications_ids) < 100 and old_notification not in new_notifications_ids:
new_notifications_ids.append(old_notification)
list_write(bot_name + "_last_notifications", new_notifications_ids)
return new_notifications
women_pronouns = ["she","her","ella","illa"]
nb_pronouns = ["they","them","elle", "ille"]
def is_gender(pronouns, account):
for pronoun in pronouns:
if(pronoun in account.display_name.lower()):
return True
for field in account.fields:
if(pronoun in field.value.lower()):
return True
if(pronoun in account.note.lower()):
return True
return False
def get_gender(account):
if(is_gender(nb_pronouns, account)):
return 2
if(is_gender(women_pronouns,account)):
return 1
return 0