-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_status.py
98 lines (76 loc) · 2.11 KB
/
github_status.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
#!/usr/bin/env python3
from time import sleep
from pytz import timezone
from datetime import datetime
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
# OAuth token (user scope required)
api_token = '<access token>'
# Time zone
time_zone = 'Europe/Zurich'
_tz = timezone(time_zone)
# GitHub GraphQL API v4
endpoint = 'https://api.github.com/graphql'
# Transport
_transport = RequestsHTTPTransport(
url=endpoint,
use_json=True,
headers={'Authorization': f'bearer {api_token}'}
)
# Client
client = Client(
transport=_transport
)
def curr_time():
# Current time
return datetime.now(tz=_tz)
def time_suffix(now):
# Emojis require 1-12 and not 0-11
tf = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
# Suffix for 'clock' emoji
time = f'{tf[now.hour % 12]}'
if now.minute >= 30:
time = f'{time}30'
return time
def update_status_emoji(time):
# GraphQL query
query = gql(f'''
mutation ChangeStatusEmoji {{
changeUserStatus(input: {{emoji: ":clock{time}:"}}) {{
status {{
emoji
}}
}}
}}
''')
# Execute query and return result
return client.execute(query)
def main():
while True:
# Current time
now = curr_time()
ts = time_suffix(now)
# Try to update the status emoji
try:
transaction = update_status_emoji(ts)
except Exception as e:
transaction = 'Transaction error: ' + str(e)
updated = False
else:
updated = True
# Transaction log
with open('transaction.log', 'a') as f:
f.write(str(now) + ': ' + str(transaction) + '\n')
# Time until the next hour or half-hour
now = curr_time()
sleep_time = 30 - now.minute
if now.minute >= 30:
sleep_time += 30
sleep_time = sleep_time * 60 - now.second - now.microsecond/1000000
# Retry after 1 min in case of a transaction error
if not updated:
sleep_time = 60
# Sleep
sleep(sleep_time)
if __name__ == '__main__':
main()