-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
119 lines (103 loc) · 4.34 KB
/
utils.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
""" Utilities for the HQ Trivia bot project """
import re
from enum import Enum
from glob import glob
from configparser import ConfigParser
from json import JSONDecodeError
from requests import post, get
from nltk.corpus import stopwords
class Colours(Enum):
""" console colours """
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def find_keywords(keywords, data):
""" Find keywords in specified data """
words_found = []
for keyword in keywords:
if len(keyword) > 2:
if keyword in data and keyword not in words_found:
words_found.append(data.count(keyword))
return words_found
def make_it_rain(headers):
""" Make it rain """
resp = post('https://api-quiz.hype.space/easter-eggs/%s' % 'makeItRain', headers=headers)
try:
print('Make it rain: %s' % resp.json())
except ValueError:
pass
def make_it_rain_for_all(headers):
""" make it rain for me and then others"""
try:
for filename in sorted(glob('config*.ini')):
config = ConfigParser()
config.read(filename)
other_headers = headers.copy()
other_headers.update({'Authorization': 'Bearer %s' % config.get('Auth', 'bearer_token')})
print("Making it rain for %s:" % (filename.split('-')[1] if len(filename.split('-')) > 1 else 'me'))
make_it_rain(other_headers)
except TypeError:
pass
def get_significant_words(question_words):
""" Returns a list of the words from the input string that are not in NLTK's stopwords """
our_stopwords = set(stopwords.words('english'))
return list(filter(lambda word: word not in our_stopwords, question_words.split(' ')))
def get_raw_words(data):
""" Extract raw words from data """
data = re.sub(r'[^\w ]', '', data).replace(' and ', ' ').strip()
words = data.replace(' ', ' ').lower()
return words
def generate_token(headers, number):
""" Generate an auth token for number """
unauth_headers = headers.copy()
unauth_headers.pop('Authorization', None)
phone_resp = post('https://api-quiz.hype.space/verifications', headers=unauth_headers, data={
'method': 'sms',
'phone': number
}).json()
verification_id = phone_resp.get('verificationId')
if not verification_id:
print('Something went wrong. %s' % phone_resp.get('error', ''))
else:
print('Verification sent to %s.' % number)
code = input("Please enter the code: ")
code_resp = post('https://api-quiz.hype.space/verifications/%s' % verification_id,
headers=unauth_headers, data={'code': code}).json()
if not code_resp.get('auth'):
print('Something went wrong. %s' % code_resp.get('error', ''))
else:
verify_file = 'config-%s-%s.ini' % (code_resp.get('auth').get('username'), code)
with open(verify_file, 'w') as out:
out.write('%s\n%s\n%s' % (
'[Auth]',
'user_id = %s' % code_resp.get('auth').get('userId'),
'bearer_token = %s' % code_resp.get('auth').get('accessToken')
))
print('Verification successful. Details stored in %s' % verify_file)
def get_stats(headers, username):
""" Query play stats for a given user """
resp = get(f'https://api-quiz.hype.space/users?q={username}', headers=headers)
try:
json = resp.json()
user_id = None
users = json.get('data', [])
if users is not None:
for user in users:
if user.get('username') == username:
user_id = user.get('userId')
user = get('https://api-quiz.hype.space/users/{}'.format(user_id), headers=headers).json()
print('User:\t\t{}'.format(user.get('username')))
print('Total Earnings:\t{}'.format(user.get('leaderboard').get('total')))
print('Games Played:\t{}'.format(user.get('gamesPlayed')))
print('Wins:\t\t{}'.format(user.get('winCount')))
if not user_id:
print('%s is not a user.' % username)
else:
print('%s is not a user.' % username)
except JSONDecodeError:
pass