-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
221 lines (187 loc) · 5.95 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from copy import deepcopy
from time import sleep
from json import JSONDecodeError
from json import dumps as json_dumps
from json import loads as json_loads
from fuzzywuzzy.process import extractOne
from argparse import ArgumentParser
from pprint import pprint
from sys import exit
import http.client
from auth import HEADERS
# Front-end keyword: sha256Hash PersistedQueryLink
# https://github.com/apollographql/apollo-link-persisted-queries/blob/master/src/index.ts
# Back-end handling:
# https://github.com/graph-gophers/graphql-go/blob/master/graphql.go
# https://ogs.gg/twitch-stolen-content/
'''
Impersonating the account of the OG Sports player.
Fake giveaway and scamming twitch users.
This account also uses a lot of twitch bots to spam the chat.
'''
GQL_URL = 'gql.twitch.tv'
VERIFIED = {
'bigdaddy',
'saksadotaa',
'midone',
'topsonous',
'sumayyl',
'7ckngmad',
'jeraxai',
# 'ogesports',
'gorgc',
'zfreek',
'MadaraDota2',
}
IMPERSONATION_BODY = [{
"operationName": "ReportUserModal_ReportUser",
"variables": {
"input": {
"description": "video > video more options > impersonation > Impersonating someone else\n\ndescription: Impersonating the account of the OG Sports player.\nFake giveaway and scamming twitch users.\nThis account also uses a lot of twitch bots to spam the chat.", # noqa E501
"reason": "impersonation",
"content": "USER_REPORT",
"contentID": "",
"extra": "",
"wizardPath": [
"video",
"video more options",
"impersonation",
"Impersonating someone else",
]
}
},
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "dd2b8f6a76ee54aff685c91537fd75814ffdc732a74d3ae4b8f2474deabf26fc", # noqa E501
}
}
}]
BODY = [
{
"operationName": "DirectoryPage_Game",
"variables": {
"name": "dota 2",
"options": {
"includeRestricted": ["SUB_ONLY_LIVE"],
"sort":"VIEWER_COUNT",
"recommendationsContext": {"platform": "web"},
"requestID": "JIRA-VXP-2397",
"tags": [],
},
"sortTypeIsRecency": False,
"limit": 100,
},
"extensions":{
"persistedQuery": {
"version": 1,
"sha256Hash": "f2ac02ded21558ad8b747a0b63c0bb02b0533b6df8080259be10d82af63d50b3", # noqa E501
}
}
}
]
def gql_request(conn, body, debug=False):
conn.request('POST', '/gql', body=json_dumps(body), headers=HEADERS)
response = conn.getresponse()
if response.status != 200:
print(f'Invalid response: {response.status}')
return
try:
result = json_loads(response.read())
except JSONDecodeError:
result = ''
if not len(result):
print(f'Invalid result: {result}')
return
if debug:
pprint(result)
return
return result
def send_report(conn, impersonation_body, broadcaster_id):
impersonation_body[0]['variables']['input']['targetID'] = broadcaster_id
gql_request(conn, impersonation_body)
print('Report sent.')
def streams(result):
try:
streams = result[0].get('data').get('game').get('streams')
has_next_page = streams.get('pageInfo').get('hasNextPage')
edges = streams.get('edges')
except AttributeError:
return
for edge in edges:
try:
cursor = edge.get('cursor')
title = edge.get('node').get('title')
views_count = edge.get('node').get('viewersCount')
broadcaster = edge.get('node').get('broadcaster')
broadcaster_id = broadcaster.get('id')
login = broadcaster.get('login')
is_partner = broadcaster.get('roles').get('isPartner')
except AttributeError:
continue
if views_count <= 30:
return
if (
is_partner or
not (match := extractOne(login, VERIFIED, score_cutoff=69))
):
continue
verified_account, confidence = match
yield (
has_next_page,
verified_account,
confidence,
login,
title,
views_count,
broadcaster_id,
cursor,
)
def check_streams(detected_accounts, report=False, debug=False):
body = deepcopy(BODY)
impersonation_body = deepcopy(IMPERSONATION_BODY)
conn = http.client.HTTPSConnection(GQL_URL)
cursor = ''
has_next_page = True
while has_next_page:
result = gql_request(conn, body, debug)
if result is None:
break
for (
has_next_page,
verified_account,
confidence,
login,
title,
views_count,
broadcaster_id,
cursor,
) in streams(result):
if cursor:
body[0]['variables']['cursor'] = cursor
if login in detected_accounts:
continue
print(
f'\u001b[36m{verified_account}\u001b[0m {confidence}% '
f'\u001b[31m{login} '
f'\u001b[35m{views_count} '
f'\u001b[0m{title.replace(chr(10), " ")}'
)
detected_accounts.add(login)
if report and confidence >= 90:
send_report(conn, impersonation_body, broadcaster_id)
def main(debug=False):
parser = ArgumentParser(description='Twitch Stream Checker')
parser.add_argument(
'-r', '--report', action='store_true', help='send report',
)
args = parser.parse_args()
detected_accounts = set()
while True:
check_streams(detected_accounts, args.report, debug)
sleep(60)
if __name__ == '__main__':
try:
exit(main())
except KeyboardInterrupt:
rasie SystemExit(130)