-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitchify-slack.py
149 lines (118 loc) · 4.95 KB
/
twitchify-slack.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
#!/usr/bin/python3
from os import mkdir, environ
from os.path import exists
import requests
import slack
import json
# Twitch emote api link for emote metadata
twitch_emote_api_link = 'https://api.twitchemotes.com/api/v4/channels/0'
# Twitch emote link for emote images
twitch_emote_file_tmpl = 'https://static-cdn.jtvnw.net/emoticons/v1/'
# BTTV emote api link for emote metadata
bttv_emote_api_link = 'https://api.betterttv.net/2/emotes'
# BTTV emote link for emote images
bttv_emote_file_tmpl = 'https://cdn.betterttv.net/emote/'
def import_client_id():
if(exists('.client_id')):
try:
client_id_file = open('.client_id', 'rt')
output = client_id_file.readline().strip()
print(output)
return output
except Exception as e:
print(e)
exit(1)
else:
try:
client_id = environ['TWITCH_APP_CLIENT_ID']
return client_id
except Exception as e:
print(e)
exit(1)
def grab_id(n):
"""Map function to extract the id and code from each dictionary entry
Arguments:
n {int} -- Current dictionary object
Returns:
arr -- Array containing the id and code
"""
return [f'{n["id"]}', f'{n["code"]}']
def request_twitch_emote_ids(client_id, service):
"""Send the request for a json of the Twitch emote metadata
Arguments:
service {str} -- String containing either "Twitch" or "BTTV" to determine which code to use
Returns:
list -- List iterable of the ids and codes for the emotes returned by the request json
"""
req_dict = {}
if(service.lower() == 'twitch'):
headers = {
'accept': 'application/vnd.twitchtv.v5+json',
'Client-ID': client_id
}
req = requests.get(twitch_emote_api_link, headers=headers)
req_dict = json.loads(req.content)
req.close()
elif(service.lower() == 'bttv'):
req = requests.get(bttv_emote_api_link)
req_dict = json.loads(req.content)
req.close()
return list(map(grab_id, req_dict['emotes']))
def save_twitch_emotes_metadata(dirpath, emote_array):
"""Save the emote_array metadata to a text file to refer to later
Arguments:
dirpath {str} -- String containing the directory path for output
emote_array {list} -- List iterable of the ids and code for the emotes returned by the original request json
"""
if(not exists(f'{dirpath}/emote_metadata.txt')):
metadata_file = open(f'{dirpath}/emote_metadata.txt', 'w+')
else:
metadata_file = open(f'{dirpath}/emote_metadata.txt', 'a')
for x in emote_array:
metadata_file.write(f'id:{x[0]}\tcode:{x[1]}\n')
metadata_file.close()
def save_twitch_emotes_images(client_id, service, dirpath, emote_array):
"""Send a request for the emote images and save them locally to a specified directory
Arguments:
service {str} -- String contianing either "Twitch" or "BTTV" to determine which code to use
dirpath {str} -- String containing the directory path for output
emote_array {list} -- List iterable of the ids and codes for the emotes returned by the original request json
"""
if(not exists(dirpath)):
mkdir(dirpath)
if(service.lower() == 'twitch'):
for emote_index in emote_array:
if(not exists(f'{dirpath}/{emote_index[1].lower()}.png')):
headers = {
'accept': 'application/vnd.twitchtv.v5+json',
'Client-ID': client_id
}
req = requests.get(
f'{twitch_emote_file_tmpl}{emote_index[0]}/1.0', headers=headers)
if('/' in emote_index[1] or '\\' in emote_index[1] or ':' in emote_index[1]):
pass
else:
emote_file = open(f'{dirpath}/{emote_index[1].lower()}.png', 'bw+')
emote_file.write(req.content)
emote_file.close()
elif(service.lower() == 'bttv'):
for emote_index in emote_array:
if(not exists(f'{dirpath}/{emote_index[1].lower()}.png')):
req = requests.get(
f'{bttv_emote_file_tmpl}{emote_index[0]}/1x')
emote_file = open(f'{dirpath}/{emote_index[1].lower()}.png', 'bw+')
emote_file.write(req.content)
emote_file.close()
def main():
# Attempt import of Client ID
client_id = import_client_id()
# Grab the Twitch official emotes
emotes_array = request_twitch_emote_ids(client_id, 'twitch')
save_twitch_emotes_images(client_id, 'twitch', 'out', emotes_array)
save_twitch_emotes_metadata('out', emotes_array)
# Grab the BTTV emotes
emotes_array = request_twitch_emote_ids(client_id, 'bttv')
save_twitch_emotes_images(client_id, 'bttv', 'out', emotes_array)
save_twitch_emotes_metadata('out', emotes_array)
if(__name__ == "__main__"):
main()