-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarketplace.py
170 lines (137 loc) · 5.95 KB
/
marketplace.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
import numpy
import pyautogui as pyag
import pytesseract.pytesseract
import pytesseract as pyt
import cv2
import time
from datetime import datetime
from sheets import Sheets
pytesseract.pytesseract.tesseract_cmd = r'.\bin\Tesseract-OCR\tesseract.exe'
class Market:
def __init__(self):
self.__marketCoords__ = (2, 37, 1376 - 2, 860 - 37) # x, y, w ,h
# items
self.__firstItem__ = (250, 173, 1112, 55) # x, y, w, h of first item on the list
self.__itemGap__ = 2 # gap between items
self.__itemCnt__ = 10
# item (x, y, w, h)
self.__itemElCoords__ = {
'name': (89, 4, 250, 44),
'avgPrice': (421, 17, 100, 20),
'recentPrice': (581, 17, 100, 20),
'lowestPrice': (742, 17, 100, 20),
'cheapestRem': (995, 17, 100, 20),
}
# pages (x, y, w, h)
self.__pagesCntCoords__ = (790, 757, 34, 20)
# absolute coords
self.__nextPageBtnCoords__ = (873 + self.__marketCoords__[0], 767 + self.__marketCoords__[1])
self.__refreshBtnCoords__ = (1329 + self.__marketCoords__[0], 107 + self.__marketCoords__[1])
self.__firstPageBtnCoords__ = (716 + self.__marketCoords__[0], 767 + self.__marketCoords__[1])
self.__ss__ = None
self.__marketItems__ = {}
self.__sheets__ = Sheets()
def __crop__(self, img, x, y, w, h, path=None):
im = img.crop((x, y, x + w, y + h))
if path is not None:
im.save(path)
return im
def __pil2cv__(self, im):
im = numpy.array(im)
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
return im
def __preprocessImg__(self, im):
im = self.__pil2cv__(im)
im = cv2.resize(im, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im = ~im
return im
def __textRecognition__(self, im, name):
# print(f'Element: {i}: {el}')
if name == 'name':
text = pyt.image_to_string(im, lang='eng')
elif name == 'pages':
text = pyt.image_to_string(im, lang='eng',
config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789/')
else:
text = pyt.image_to_string(im, lang='eng',
config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789 ,.-')
text = text.split('\n')[0]
return text
def __click__(self, x, y, delay=0.5):
pyag.click(x, y)
time.sleep(delay)
def __scanSingleItem__(self, itemImg, whitelist=None):
if whitelist is None:
scanCols = self.__itemElCoords__.keys()
else:
scanCols = ['name']
scanCols.extend(whitelist)
item = []
for el in scanCols:
im = self.__crop__(img=itemImg,
x=self.__itemElCoords__[el][0],
y=self.__itemElCoords__[el][1],
w=self.__itemElCoords__[el][2],
h=self.__itemElCoords__[el][3])
im = self.__preprocessImg__(im)
text = self.__textRecognition__(im, el)
item.append(text)
return item
def screenShot(self):
self.__ss__ = pyag.screenshot(region=self.__marketCoords__)
# self.__ss__.save(f'./screenshots/ss.png')
def getPagesFromSS(self):
pagesImg = self.__crop__(img=self.__ss__,
x=self.__pagesCntCoords__[0],
y=self.__pagesCntCoords__[1],
w=self.__pagesCntCoords__[2],
h=self.__pagesCntCoords__[3])
im = self.__preprocessImg__(pagesImg)
text = self.__textRecognition__(im, 'pages')
text = text.split('/')
return [int(text[0]), int(text[1])]
def scanItemsFromSS(self, whitelist=None):
for i in range(self.__itemCnt__):
itemImg = self.__crop__(img=self.__ss__,
x=self.__firstItem__[0],
y=self.__firstItem__[1] + i * (self.__firstItem__[3] + self.__itemGap__),
w=self.__firstItem__[2],
h=self.__firstItem__[3])
item = self.__scanSingleItem__(itemImg, whitelist)
if item[0] == '':
break
name = item.pop(0)
self.__marketItems__[name] = item
def getScanedItems(self):
ts = time.time()
return self.__marketItems__, int(ts)
def nextPage(self):
self.__click__(self.__nextPageBtnCoords__[0], self.__nextPageBtnCoords__[1])
def firstPage(self):
self.__click__(self.__firstPageBtnCoords__[0], self.__firstPageBtnCoords__[1], delay=1)
def refresh(self):
self.__click__(self.__refreshBtnCoords__[0], self.__refreshBtnCoords__[1], delay=1)
def saveToSheets(self):
ts = time.time()
date = datetime.fromtimestamp(ts)
dateStr = date.strftime('%d.%m.%Y %H:%M:%S')
# prepare row1
row1 = self.__sheets__.readSheet('RAW DATA!A1:1')
if(len(row1) != 0):
row1 = row1[0]
list_ = ['ts']
list_.extend(self.__marketItems__.keys()) # timestamp + marketData keys
row1.extend(key for key in list_ if key not in row1) # extends row1 by list, omitting duplicated elements
# prepare row to insert
# makes sure elements are inserted under correct items
insRow = []
for i in range(len(row1)):
insRow.append('')
insRow[0] = dateStr
for key in self.__marketItems__:
insRow[row1.index(key)] = self.__marketItems__[key][0] # inserting only recentPrice
self.__sheets__.updateRow(row1, 'RAW DATA!A1:1')
self.__sheets__.insertRow(insRow, 'RAW DATA!A2:2')
print(f'Saved {len(self.__marketItems__)} items')
self.__marketItems__ = {}