-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyrient.py
More file actions
122 lines (103 loc) · 4.56 KB
/
myrient.py
File metadata and controls
122 lines (103 loc) · 4.56 KB
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
import aiohttp
import asyncio
from thefuzz import fuzz, process
class Myrient():
def __init__(self, db):
self.db = db
async def download_async(self, session, filename, url):
print(f'Downloading: {filename}')
try:
async with session.get(url, headers=self.db.headers) as response:
# Check for error
if response.status != 200:
print(f'HTTP error for {filename}: {response.status}')
return
# Write in chunks
with open(filename, 'wb') as file:
async for chunk in response.content.iter_chunked(8192):
# Check if chunk is not empty
if chunk:
file.write(chunk)
print(f'Saved: {filename}')
except aiohttp.ClientError as error:
print(f'Request error for {filename}: {error}')
async def download_files(self, paths):
async with aiohttp.ClientSession() as session:
tasks = []
for filename in paths:
tasks.append(self.download_async(session, filename, self.db.games[filename][0]))
await asyncio.gather(*tasks)
def check_result(self, result, plus_tags, minus_tags):
# Check if the matches are close enough (if query is not blank), and check if the tags match
plus_tags_found = False
minus_tags_not_found = False
# If result is a match tuple
if type(result) == tuple:
# Check if closeness score is >= 70
if result[1] >= 70:
if plus_tags or minus_tags:
# Check if the list is not empty
if plus_tags:
# Check if any of the plus tags provided are in the result's tag list
plus_tags_found = any(item in self.db.games[result[0]][2] for item in plus_tags)
else:
# If empty, treat this check as true
plus_tags_found = True
# Do the same with minus tags
if minus_tags:
minus_tags_not_found = set(minus_tags).isdisjoint(self.db.games[result[0]][2])
else:
minus_tags_not_found = True
# Do not return anything if the closeness score is too low
else:
return None
else:
if plus_tags or minus_tags:
# Check if the list is not empty
if plus_tags:
# Check if any of the plus tags provided are in the result's tag list
plus_tags_found = any(item in self.db.games[result][2] for item in plus_tags)
else:
# If empty, treat this check as true
plus_tags_found = True
# Do the same with minus tags
if minus_tags:
minus_tags_not_found = set(minus_tags).isdisjoint(self.db.games[result][2])
else:
minus_tags_not_found = True
# Check if the tags lists aren't blank
if plus_tags or minus_tags:
# If both checks are not true, don't return result
if not (plus_tags_found and minus_tags_not_found):
return None
# Return result as befits type
if type(result) == tuple:
return result[0]
else:
return result
def search(self, query, plus_tags, minus_tags):
matches = []
# If the query isn't blank, run comparisons
if query != "":
# Find 30 results in the keys with partial matching
# Returns tuple of (result string, closeness score int)
matches = process.extract(query, self.db.games.keys(), limit=30, scorer=fuzz.partial_ratio)
# Otherwise, just look through everything
else:
matches = list(self.db.games.keys())
# Check if the matches are close enough (if query is not blank), and check if the tags match
result = []
results = []
for match in matches:
result = self.check_result(match, plus_tags, minus_tags)
if result != None:
results.append(result)
# Print the results
print(f'Results:')
if results:
for i in range(len(results)):
print(f'{i + 1}. {results[i]}')
else:
print('No results found')
# Return for use later
return results