This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.py
64 lines (58 loc) · 2.48 KB
/
twitch.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
##################################################################
# #
# twitch mod activity tracker #
# #
# Copyright (C) 2022 X Gamer Guide #
# https://github.com/X-Gamer-Guide/twitch-mod-activity-tracker #
# #
##################################################################
import os
import shelve
import requests
from disnake.ext.tasks import loop
from config import config, data_path
from constants import GQL, REQUEST_BODY, IGNORE_CHANNELS, IGNORE_ACTIONS
from utils import get_actions, put_actions
@loop(seconds=config.twitch.pause_between_requests)
async def get_mod_actions():
# Download the latest 200 mod actions from Twitch
body = REQUEST_BODY.copy()
body['variables']['channelID'] = config.twitch.channel_id
r = requests.post(
GQL,
headers={
"User-Agent": config.twitch.user_agent,
"Authorization": config.twitch.token
},
json=body
)
if not r.status_code == 200:
print(f"twitch [{r.status_code}]: {r.text}")
return
j = r.json()
# * twitch duraction: j['extensions']['durationMilliseconds']
if j['data']['channel'] is None:
print("channel not found")
return
if j['data']['channel']['moderationLogs']['actions'] is None:
print("no mod rights on this channel")
return
edges = j['data']['channel']['moderationLogs']['actions']['edges']
with shelve.open(os.path.join(data_path, "last_ids")) as db:
if config.twitch.channel_id in db:
last_id = db[config.twitch.channel_id]
else:
last_id = ""
db[config.twitch.channel_id] = edges[0]['node']['id']
actions = get_actions()
for edge in edges:
if edge['node']['id'] == last_id:
break
if edge['node']['moderator']['login'] in IGNORE_CHANNELS or edge['node']['action'] in IGNORE_ACTIONS:
continue
if not edge['node']['action'] in actions:
actions[edge['node']['action']] = {}
if not edge['node']['moderator']['login'] in actions[edge['node']['action']]:
actions[edge['node']['action']][edge['node']['moderator']['login']] = 0
actions[edge['node']['action']][edge['node']['moderator']['login']] += 1
put_actions(actions)