-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovvy_items.py
379 lines (343 loc) · 11.6 KB
/
movvy_items.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import json
import os.path
import socket
# from cryptography.fernet import Fernet
from datetime import datetime
import pyperclip
from pymongo import MongoClient
from tabulate import tabulate
ver = "0.64a" # TODO Add version to database and build a check, force upgrade.
def menuSystem():
print("Movoda Utils:")
print("Choose a menu selection:")
menuSelections = (
"Add: Finder Spell",
"Add: Store Parse",
"Add: Scan Auction",
"Search: Item (Strict)",
"Search: Store (Strict)",
"Search: Location (Strict)",
"Exit",
)
for num, item in enumerate(menuSelections, start=1):
print(f" {num}. {item}")
menuItem = input("\n > ")
if menuItem == "1": # Finder Spell Parse
clipboard_finder_parse()
menuSystem()
elif menuItem == "2": # Store Parse
clipboard_store_parse()
menuSystem()
elif menuItem == "3": # Auction Parse
# auction_scan_parse()
menuSystem()
elif menuItem == "4": # Search by Item Name (strict)
search = input("Item to find: ")
searchQuery = search.split(",")
lookUp(searchQuery, "item")
menuSystem()
elif menuItem == "5": # search by Store
search = input("Store to check: ")
searchQuery = search.split(",")
lookUp(searchQuery, "store")
menuSystem()
elif menuItem == "6": # search by Location
while True:
search = input("Location to check: ")
searchQuery = search.split(",")[0]
if checkLocation_new(searchQuery) == True:
lookUp(searchQuery, "location")
break
menuSystem()
elif menuItem == "7":
print("Exiting...")
elif menuItem == "loc":
print(locations.find({}))
else:
print(f"\n {menuItem} is not a valid option.")
menuSystem()
def checkLocation_new(location):
"""checks location versus database"""
for i in locations.find({"name": location}):
if i["name"] == location:
return True
else:
print(f"{location} does not exist.")
return False
def database_reader_new(searchQuery, queryItemType, term):
"""searchQuery, queryItemType, term, all strings to find data from database"""
data = []
searchQuery = "".join(searchQuery)
if term == "store":
for i in prices.find({"store": searchQuery}):
table = (
i["timestamp"],
i["location"],
i["clan"],
i["type"],
i["item"],
int(i["price"].replace(",", "")),
i["store"],
)
data.append(table)
elif term == "item":
for i in prices.find({"item": searchQuery}):
if i["type"] == queryItemType.lower():
table = (
i["timestamp"],
i["location"],
i["clan"],
i["type"],
i["item"],
int(i["price"].replace(",", "")),
i["store"],
)
data.append(table)
elif queryItemType == "both":
table = (
i["timestamp"],
i["location"],
i["clan"],
i["type"],
i["item"],
int(i["price"].replace(",", "")),
i["store"],
)
data.append(table)
elif term == "location":
for i in prices.find({"location": searchQuery}):
table = (
i["timestamp"],
i["location"],
i["clan"],
i["type"],
i["item"],
int(i["price"].replace(",", "")),
i["store"],
)
data.append(table)
if len(data) == 0:
print(f"No result found for {searchQuery}.\n")
else:
if queryItemType == "sell":
sorted_data = sorted(data, key=lambda x: x[5], reverse=True)
elif queryItemType == "buy":
sorted_data = sorted(data, key=lambda x: x[5], reverse=False)
else:
sorted_data = data
return sorted_data
def database_writer_new(line_to_write):
"""writes to mongodb using data in line_to_write (list)"""
(
timestamp,
location,
clan,
queryItemType,
item,
price,
store,
login,
hostname,
) = line_to_write
data = {
"timestamp": timestamp,
"location": location,
"clan": clan,
"type": queryItemType,
"item": item,
"price": price,
"store": store,
"login": read_json("login"),
"source": hostname,
}
result = prices.insert_one(data)
def checkBuildingType(line):
"""Checks if the building type is a house, or if the item is being bought or sold"""
if " is buying " in line:
return "buy"
elif " is selling " in line:
return "sell"
else:
return "house"
def getPrice(building_type, line):
"""Gets the price of the item that is being bought or sold"""
if building_type == "buy":
return line.split(" is buying ")[1].split(" for ")[0].strip()
if building_type == "sell":
return line.split(" is selling ")[1].split(" for ")[0].strip()
def clipboard_finder_parse():
"""Grabs the data in the clipboard that matches the finder spell data, and parses it to add to the database"""
item = ""
line_to_write = ""
count = 0
for line in pyperclip.paste().split("\n"):
try:
if len(line.split(" results for ")) == 2:
item = line.split(" results for ")[1]
stamp = datetime.now().strftime("%m/%d/%y %H:%M:%S")
except:
pass
try:
building_location = line.split("-")[0].strip()
building_clan = line.split("-")[1].split(" is ")[0].strip()
building_type = checkBuildingType(line)
building_item = getPrice(building_type, line)
building_item_price = (
line.split(" for ")[1].split(" in ")[0].strip()[:-1].replace(",", "")
)
building_name = line.split(" in ")[1].strip()
line_to_write = [
stamp,
building_location,
building_clan,
building_type,
building_item,
building_item_price,
building_name,
read_json("login"),
hostname,
]
database_writer_new(line_to_write)
count += 1
except:
pass
if item == "":
print(f"Added {count} items at {stamp}\n")
else:
print(f"Added {count} items at {stamp} while searching for {item}\n")
pyperclip.copy("")
def clipboard_store_parse():
"""Grabs the data in the clipboard that matches store data, and parses it to add to the database"""
while True:
location = input("Location Name: ")
if checkLocation_new(location) == True:
break
clan = input("Clan: ")
line_to_write = ""
building_name = ""
count = 0
stamp = datetime.now().strftime("%m/%d/%y %H:%M:%S")
for line in pyperclip.paste().split("\n"):
building_item = ""
building_price = ""
building_type = ""
buy_sep = " for "
sell_sep = "\t"
if count == 0:
building_name = line.replace("(Change Name)", "").strip()
if count > 1:
if buy_sep in line:
building_item = (
line.split(buy_sep)[0]
.replace("Building Administration", "")
.strip()
)
building_price = (
line.split(buy_sep)[1].replace("V", "").replace(",", "").strip()
)
building_type = "buy"
if sell_sep in line:
building_item = (
line.split(sell_sep)[0]
.replace("Building Administration", "")
.strip()
)
building_price = (
line.split(sell_sep)[1].replace("V", "").replace(",", "").strip()
)
building_type = "sell"
if building_type != "":
line_to_write = [
stamp,
location,
clan,
building_type,
building_item,
building_price,
building_name,
read_json("login"),
hostname,
]
database_writer_new(line_to_write)
count += 1
print(
f"Added {count} items at {stamp} from {location} - {clan} - {building_name}\n"
)
def auction_scan_parse():
"""Once done will scan the auction house and new building type called auction"""
return
def lookUp(searchQuery, term):
"""Looks up items based on item, store or location"""
if len(searchQuery) == 2:
queryItem = searchQuery[0].strip()
queryItemType = searchQuery[1].strip()
else:
queryItem = searchQuery
queryItemType = "both"
sorted_data = database_reader_new(queryItem, queryItemType, term)
if sorted_data != None:
print(
tabulate(
sorted_data,
headers=[
"timestamp",
"location",
"clan",
"type",
"item",
"price",
"store",
],
tablefmt="psql",
),
"\n",
)
# TODO Add file Encryption.
def write_json(user, password):
"""Pass username and password"""
data = {}
data["credentials"] = []
data["credentials"].append(
{
"login": user,
"uri": f"mongodb+srv://{user}:{password}@cluster0.ltipa.gcp.mongodb.net/movoda?retryWrites=true&w=majority",
}
)
with open("data.json", "w") as f:
json.dump(data, f)
# TODO Add file Decryption.
def read_json(item):
"""reads a json file with login credentials"""
if os.path.isfile("data.json"):
with open("data.json", "r") as f:
data = json.load(f)
for line in data["credentials"]:
return line[item]
def fileEncrypt():
pass
def fileDecrypt():
pass
if __name__ == "__main__":
item = False
hostname = socket.gethostname()
if os.path.isfile("data.json"):
try:
print(f"Attempting to login as {read_json('login')} on {hostname}")
client = MongoClient(read_json("uri"))
db = client.movoda
prices = db.prices
locations = db.locations
users = db.users
for item in users.find({}): # just brain farting...
item = True
except Exception:
print("Credentials invalid...")
os.remove("data.json")
else:
print("Missing Credentials.")
user = input("Enter username: ")
password = input("Enter password: ")
write_json(user, password)
print("Restart to login")
if item == True:
menuSystem()