This repository was archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShopWizard.py
More file actions
232 lines (186 loc) · 8.7 KB
/
ShopWizard.py
File metadata and controls
232 lines (186 loc) · 8.7 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
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
import Constants, web, re, timestamp, time
from bs4 import BeautifulSoup
from Shop import Shop, ShopItem
import sys
class ShopWizard:
def __init__(self, session):
self.session = session
self.searches = 0
self.__shopwizard_ban = False
self.__shopwizard_ban_time = None
self.__shopwizard_ban_duration_min = 0
self.__does_not_exist = False
async def __search(self, item, max_searches, criteria="exact", min_price = 0, max_price = 999999):
new_search = ItemSearch(item)
for i in range(max_searches):
self.searches+=1
if self.__shopwizard_ban == False:
referer = "https://www.neopets.com/market.phtml?type=wizard"
postFields = {"type": "process_wizard",
"feedset": 0,
"shopwizard": item,
"table": "shop",
"criteria": criteria,
"min_price": min_price,
"max_price": max_price}
search_results_page_source = await web.post(self.session, Constants.NEO_SHOP_WIZARD, postFields, referer)
self.__check_shopwizard_ban(search_results_page_source)
new_search.add_shops(self.__parse_search(search_results_page_source, item))
return new_search
def __parse_search(self, source, item):
soup = BeautifulSoup(source , "html.parser")
search_results = []
table_index = 2
#Scrapes all the data from the table within the page source
try:
table_of_shops = soup.find_all("table")[table_index]
prices = table_of_shops.find_all("td", {"align": "right", "bgcolor":re.compile("^#F")}) # All prices
quantities = table_of_shops.find_all("td", {"align": "center", "bgcolor":re.compile("^#F")}) # all quanitities
shop_owners = table_of_shops.find_all("a", {'href':re.compile('^/browseshop.phtml?')})
shop_links = table_of_shops.find_all("a", {'href':re.compile('^/browseshop.phtml?')}) #Gives all anchors
except:
print("Error in __parse_search function : could not scrape search results")
if shop_owners: # Fixed UnboundLocalError
for x in range(len(shop_owners)):
search_results.append(SearchResult(shop_owners[x].getText(), int(quantities[x].getText()), int(prices[x].getText()[:-3].replace(",","")), shop_links[x]["href"]))
return search_results
def __check_shopwizard_ban(self, source):
if "too many searches!" in source:
self.__shopwizard_ban = True
self.__shopwizard_ban_time = timestamp.end_of_hour()
self.__shopwizard_ban_duration_min = timestamp.timeRemaining(self.__shopwizard_ban_time)
async def __send_purchase_request(self, Item_Search):
referer = "https://www.neopets.com" + Item_Search.cheapest_result().shop_link
buy_link = Constants.NEO_HOMEPAGE + Item_Search.cheapest_result().shop.shop_items[0].buy_link
source = await web.get(self.session, buy_link, referer)
Item_Search.decrease_shop_quantity()
def super_shopwizard_search(self, item):
print("------------- SSW for", item, "---------------")
self.market_price(item, max_searches = 200, shops_to_display = 15, ssw=True)
def market_price(self, item, max_searches = 10, shops_to_display = 10, ssw=False):
#Improvements for this method include average price across different subsets
#Number of items available give idea of scarcity
#Add variance in price.
searches = 0
Item_Search = ItemSearch(item)
while Item_Search.search_completed() < 13 and searches < max_searches:
search = self.__search(item, 1)
searches += 1
if search.search_results == 0 and ssw:
print("Complete search not possible - defaulting to 100 searches")
ssw = False
max_searches = 100
else:
Item_Search.add_shops(search.search_results)
if len(Item_Search.search_results) < shops_to_display:
shops_to_display = len(Item_Search.search_results)
return Item_Search
async def buy(self, item, quantity = 1, max_searches = 10, max_price=99999):
prices_paid = []
Item_Search = await self.__search(item, max_searches, max_price=max_price)
if Item_Search.cheapest_result() != None and self.__shopwizard_ban != True:
for i in range(quantity):
try:
await self.__open_shop(Item_Search)
price = Item_Search.cheapest_result().shop.shop_items[0].price
await self.__send_purchase_request(Item_Search)
prices_paid.append(price)
except Exception as e:
print(sys.exc_info())
prices_paid.append(0)
return prices_paid
'''
Sends a GET request to navigate to the shop with the cheapest priced item.
'''
async def __open_shop(self, Item_Search):
if Item_Search.cheapest_result() is not None:
response = await web.get(self.session, Constants.NEO_HOMEPAGE + Item_Search.cheapest_result().shop_link)
while "Sorry - The owner of this shop has been frozen!" in response:
Item_Search.remove_shop()
response = await web.get(self.session, Constants.NEO_HOMEPAGE + Item_Search.cheapest_result().shop_link)
Item_Search.cheapest_result().add_shop_details(response, self.session)
class ItemSearch:
def __init__(self, search_item):
self.search_item = search_item
self.search_results = [] #List of search result objects (shop owners) selling the search_item
self.search_groups = [0,0,0,0,0,0,0,0,0,0,0,0,0]
self.users_group_index = 5 #Need this update dynamically
def __sort(self):
self.search_results.sort(key=lambda x: x.price, reverse=False)
return 0
def average_price(self):
average = 0
count = 0
for i in range(len(self.search_groups)):
count+= self.search_groups[i]
for n in range(count):
average += self.search_results[n].price
return average/count
def search_completed(self):
count = 0
for i in range(len(self.search_groups)):
count+= self.search_groups[i]
return count
def cheapest_result_in_group(self):
if self.search_groups[self.users_group_index] == 0:
return None
else:
for shop in self.search_results:
if self.get_shop_group(shop.owner) == self.users_group_index:
return shop
def get_object_id(self):
if len(self.search_results) > 0:
return self.cheapest_result().obj_id
else:
return -1
def cheapest_result(self):
try:
if len(self.search_results) == 0:
return None
else:
return self.search_results[0]
except:
return None
def remove_shop(self, index = 0):
if len(self.search_results) > 0:
self.search_results.pop(index)
def add_shops(self, shops):
#Check shop group has been added already
if len(shops) > 0:
_group = self.get_shop_group(shops[0].owner)
if self.search_groups[_group] == 0:
self.search_results.extend(shops)
self.search_groups[_group] = 1
self.__sort()
return True
return False
def decrease_shop_quantity(self):
self.search_results[0].stock-= 1
if self.search_results[0].stock <= 0:
self.search_results.pop(0)
def get_shop_group(self, owner):
_group = ord(owner[0])
if _group == 95:
_group = 10
elif _group < 58:
_group = _group % 48
else:
_group = (_group % 97) % 13
return _group
def __str__(self):
s = str(self.search_groups)
for shop in self.search_results:
s = s + "\n" + str(shop)
return s
class SearchResult:
def __init__(self, owner, stock, price, shop_link):
self.owner = owner
self.stock = stock
self.price = price
self.shop_link = shop_link
self.obj_id = int(self.shop_link[self.shop_link.find("&buy_obj_info_id=") + len("&buy_obj_info_id=") : self.shop_link.find("&buy_cost_neopoints=")])
self.shop = None
def add_shop_details(self, shop_page_source, session):
self.shop = Shop(shop_page_source, session)
def __str__(self):
return self.owner + " " + str(self.stock) + " " + str(self.price)