-
Notifications
You must be signed in to change notification settings - Fork 1
/
thltb.py
187 lines (154 loc) · 5.04 KB
/
thltb.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
import discord
from howlongtobeatpy import HowLongToBeat
from tutil import flatten_sublist
from tutil import debug
from constants import DIVIDER, VERBOSE
regex_is_year = r"\d{4}"
async def helper(message: discord.Message, op_override=None):
"""
Main entry point from main.py
:param op_override: Activate a specific operator
:param message: <Discord.message object>
:return: <lambda function> Internal function dispatch
"""
args = message.content.split()
ops = {'search', 'help'}
operator = 'search'
if len(args) > 1 and args[1] in ops:
operator = args[1]
if op_override:
operator = op_override
result = {
'search': lambda: search_dispatch(args),
'help': lambda: get_help(),
}.get(operator, lambda: None)()
if not result:
return 'Nothing found. Please see `$hltb search -h` or `$hltb help` for more information.'
result = rendered(result)
return result
def rendered(message: list) -> str:
"""
Convert raw results to something Discord safe.
:param message:
:return:
"""
similar = message[1]
message = message[0]
result = []
if not similar:
for each in message:
attrs = vars(each)
result.append(f"**{attrs['game_name']}**")
result.append(f"Game Image: {attrs['game_image_url']}")
result.append(f"Web Link: {attrs['game_web_link']}")
result.append(f"Review Score: {attrs['review_score']}")
result.append(f"Developer: {attrs['profile_dev']}")
result.append(f"Platforms: {attrs['profile_platforms']}")
result.append(f"Release Date: {attrs['release_world']}")
result.append(f"Similarity to your search: {attrs['similarity'] * 100:.2f}%")
result.append(f"{DIVIDER.strip()}")
result.append(f"Main Story: {attrs['main_story']}")
result.append(f"Main Extra: {attrs['main_extra']}")
result.append(f"Completionist: {attrs['completionist']}")
else:
result = message
return '\r'.join(result)
def search_dispatch(args):
"""
Search How Long To Beat dot com.
hltb search <title>
hltb search <option> <title>
Options:
-h --help Show this help prompt
-id Search by id
:param args:
:return:
"""
# Sanitize input
# Some users will do --option=argument while some will do --option argument
args = [arg.split('=') for arg in args]
args = flatten_sublist(args)
# args = args.strip()
# Add if statement to this to avoid -D becoming equivalent to -d, etc
args = [arg.lower().strip() if not arg.startswith('-') else arg for arg in args]
# TODO add Speller library
args = args[1:]
if VERBOSE >= 2:
print(f'args are: {args}')
# Setup blanks
options = {
"library": None,
"limit": None,
"title": None,
"id": None
}
result = None
similar = False
summary = False
if VERBOSE >= 2:
print(f'options set: {options}')
# check if no parameters were passed
basic_search = True
for each in args:
if each.startswith('-'):
basic_search = False
break
if basic_search:
options["title"] = ' '.join(args)
# Handle special options
# help
if {'-h', '--help'}.intersection(args):
return get_search_help()
# all
if {'-id'}.intersection(args):
options["id"] = int(args[args.index('-id') + 1])
if options["id"]:
result = search_hltb_by_id(options)
# Generic search
if not result:
result, similar = search_hltb(options)
return [result, similar]
def search_hltb(options: dict) -> tuple:
"""
:param options:
:return:
"""
best_element = None
results_list = HowLongToBeat().search(options["title"])
multiple = False
if results_list is not None and len(results_list) > 1:
multiple = True
for each in results_list:
best_element = [f'{each.game_name} {each.similarity * 100:.2f}% -- {each.game_id}' for each in results_list]
# Saving in case I decide to do 'pick best automatically'
# best_element = max(results_list, key=lambda element: element.similarity)
else:
best_element = results_list
return best_element, multiple
def search_hltb_by_id(options: dict) -> list:
"""
:param options:
:return:
"""
return [HowLongToBeat().search_from_id(options['id'])]
def get_search_help() -> str:
"""
Return help file for usage
:return:
"""
return """
```
Search How Long To Beat dot com.
If more than one matching title is found a list will be provided with:
{game name} {similarity percentage} -- {game id}
If this happens, use the -id option to get the game you want.
hltb search <title>
hltb search <option> <title>
Options:
-h --help Show this help prompt
-id Search by id
```
"""
# TODO update to match when finished
def get_help():
pass