-
Notifications
You must be signed in to change notification settings - Fork 0
/
vscraper_utils.py
247 lines (205 loc) · 6.34 KB
/
vscraper_utils.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
"""
es-vscraper utilities
MIT-LICENSE
Copyright 2017, Valerio 'valerino' Lupi <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import io
from time import sleep
from PIL import Image
import select
import sys
import os
import time
import threading
import urllib.request
if os.name == 'nt':
import msvcrt
class MultipleChoicesException(Exception):
"""
raised when multiple entries are found for a game
"""
def __init__(self, choices):
self._choices = choices
Exception.__init__(self)
def choices(self):
"""
the entries
:return: [{ name, publisher, year, url, system}]
"""
return self._choices
class GameNotFoundException(Exception):
"""
raised when a game is not found
"""
pass
def __input_with_timeout_win(prompt, timeout):
"""
input with timeout, unix version (internal)
"""
result=''
class KeyboardThread(threading.Thread):
def run(self):
self.timedout = False
self.input = ''
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 0x0d:
break
self.input += str(chr,'utf-8')
if len(self.input) == 0 and self.timedout:
break
print(prompt)
it = KeyboardThread()
it.start()
it.join(timeout)
it.timedout = True
if len(it.input) > 0:
# wait for rest of input
it.join()
result = it.input
return result
def __input_with_timeout_unix(prompt, timeout):
"""
input with timeout, unix version (internal)
"""
sys.stdout.write(prompt)
sys.stdout.flush()
ready, _, _ = select.select([sys.stdin], [],[], timeout)
if ready:
return sys.stdin.readline().rstrip('\n')
return ''
def input_with_timeout(prompt, timeout = 0):
"""
show prompt with timeout
:param prompt: prompt to be shown
:param timeout: if not 0, timeout to wait for in seconds
:return: the input as string, may be empty if timeout occurred
"""
if timeout == 0:
return input(prompt)
if os.name == 'nt':
return __input_with_timeout_win(prompt, timeout)
return __input_with_timeout_unix(prompt, timeout)
def get_csv_parameter(params, name):
"""
get parameter from csv string of name=value
:param params: name=value[,name=value,...]
:param name: parameter name
:return: the value
"""
l = params.split(',')
for ll in l:
s = ll.split('=')
if s[0].lower() == name:
return s[1]
return ''
def find_href(root, substring):
"""
browse all 'a' tags for specific 'href'
:param root: an html node
:param substring: a substring (match with startswith()), may be None
:return: array or None
"""
tags = root.find_all('a')
if tags is None:
return None
res = []
for t in tags:
if t['href'].startswith(substring):
res.append(t)
if len(res) == 0:
return None
return res
def add_text_from_href(root, substring, coll, key):
"""
browse all 'a' tags for specific 'href', and set coll[key] as a csv of the 'text' attr of each
:param root: an html node
:param substring: a substring (match with startswith())
:param coll: the collection
:param key: the key into collection
:return:
"""
t = find_href(root, substring)
if t is None:
# empty
coll[key] = ''
return
s = ''
for tt in t:
s += tt.text + ','
coll[key] = s[:-1]
def get_text_no_tags(tag, to_strip):
"""
get text from tag, stripping all given attributes
:param tag a tag
:param to_strip attribute to strip
:return:
"""
for t in tag.find_all(to_strip):
t.replaceWith('')
return tag.text
def write_to_file(path, buffer):
"""
write buffer to file
:param path: path to the file (will be overwritten)
:param buffer: the buffer
:return:
"""
with open(path, 'wb') as f:
f.write(buffer)
def read_from_file(path):
"""
read file to buffer
:param path: path to the file
:return: buffer
"""
with open(path, 'rb') as f:
buffer = f.read()
return buffer
def download_file(url, path, no_overwrite):
"""
download file from http/s url
:param url: the complete url i.e. http:https://path/to/file.zip
:param path: the destination path
:param no_overwrite: if true, just exits if the file already exists
:return: -1 if path exists
"""
if no_overwrite == True:
if os.path.exists(path):
return -1
try:
os.remove(path)
except Exception as e:
pass
# will except on error
urllib.request.urlretrieve(url,path)
# check if the file exists and is sane
size = os.path.getsize(path)
if size == 0:
os.remove(path)
raise Exception('Download error!')
def img_to_png(buffer):
"""
convert an image buffer to PNG
:param buffer: the image
:return: PNG image buffer or None
"""
if buffer is None:
return None
try:
img_buffer = io.BytesIO()
tmp = Image.open(io.BytesIO(buffer))
tmp.save(img_buffer, 'png')
if len(img_buffer.getvalue()) > 0:
return img_buffer.getvalue()
return None
except Exception as e:
return None