-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.py
64 lines (52 loc) · 1.85 KB
/
config.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
# Imports
import requests
import time
import json
import socket
import urllib3
import configparser as cp
from json import JSONDecodeError
class Config():
def __init__(self):
self.conf = cp.ConfigParser()
self.conf.read('config.ini')
self.clientid = self.conf['DEFAULT']['clientid']
self.clientsecret = self.conf['DEFAULT']['clientsecret']
self.oauthtoken = self.conf['DEFAULT']['oauthtoken']
def validate(self):
self.conf.read('config.ini')
self.oauthtoken = self.conf['DEFAULT']['oauthtoken']
validate_url = "https://id.twitch.tv/oauth2/validate"
valid_headers = {'client-id': self.clientid, 'Authorization': 'OAuth ' + self.oauthtoken}
i = 0
tries = 100
while i < tries:
i += 1
try:
valid = requests.get(validate_url, headers=valid_headers).json()
except (simplejson.errors.JSONDecodeError, requests.exceptions.ConnectionError, JSONDecodeError, json.decoder.JSONDecodeError, urllib3.exceptions.MaxRetryError, urllib3.exceptions.NewConnectionError, socket.gaierror, KeyError) as e:
time.sleep(15)
continue
else:
return valid
def refresh(self):
auth_url = "https://id.twitch.tv/oauth2/token"
auth_params = {'client_id': self.clientid, 'client_secret': self.clientsecret,
'grant_type': 'client_credentials'}
i = 0
tries = 100
while i < tries:
i += 1
try:
key = requests.post(auth_url, params=auth_params).json()
except (simplejson.errors.JSONDecodeError, requests.exceptions.ConnectionError, json.decoder.JSONDecodeError, JSONDecodeError, urllib3.exceptions.MaxRetryError, urllib3.exceptions.NewConnectionError, socket.gaierror, KeyError) as e:
time.sleep(15)
continue
else:
self.oauthtoken = key['access_token']
self.conf['DEFAULT']['oauthtoken'] = self.oauthtoken
with open('config.ini', 'w') as file:
self.conf.write(file)
file.close()
return key
conf = Config()