-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
393 lines (326 loc) · 12 KB
/
__main__.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python
import aiohttp
import asyncio
import orjson
import aiosqlite
import logging
import random
import socket
import ssl
import logging
import datetime
import pyrogram
from pyrogram import Client, filters
from pyrogram.types import Message
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from pyrogram.enums import ParseMode
class CustomFormatter(logging.Formatter):
def format(self, record):
variant_id = record.__dict__.get('variant_id', None)
if record.levelname == 'INFO':
if 'is now in-stock' in record.msg:
record.msg = f'[IN STOCK] {record.msg} [{datetime.datetime.fromtimestamp(record.created).strftime("%d/%m/%Y %H:%M")}]'
else:
record.msg = f'[INFO] {record.msg} [{datetime.datetime.fromtimestamp(record.created).strftime("%d/%m/%Y %H:%M")}]'
elif record.levelname == 'ERROR':
if 'Rate limited' in record.msg:
record.msg = f'[RATE LIMITED] {record.msg} [{datetime.datetime.fromtimestamp(record.created).strftime("%d/%m/%Y %H:%M")}]'
elif 'not found in the website' in record.msg:
record.msg = f'[NOT FOUND] {record.msg} [{datetime.datetime.fromtimestamp(record.created).strftime("%d/%m/%Y %H:%M")}]'
elif 'is out-of-stock' in record.msg:
record.msg = f'[OUT OF STOCK] {record.msg} [{datetime.datetime.fromtimestamp(record.created).strftime("%d/%m/%Y %H:%M")}]'
else:
record.msg = f'[ERROR] {record.msg} [{datetime.datetime.fromtimestamp(record.created).strftime("%d/%m/%Y %H:%M")}]'
return super().format(record)
custom_handler = logging.StreamHandler()
custom_handler.setFormatter(CustomFormatter())
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(custom_handler)
timeout_to_wait: int = 300
variants_to_check: list[int] = [
# PLA GALAXY
47730388173148,
47730388107612,
47730388140380,
47730388074844,
# PLA Basic Refill Bundle 2 Rolls
47513706201436,
47513706103132,
47513706070364,
47513706987868,
47513706234204,
# PLA Silk Dual Color
47489270841692,
47489270808924,
47489270776156,
47489270874460,
# PLA Glow
47396823630172,
47396823564636,
47396823531868,
47396823662940,
47396823597404,
# PLA Matte Refill Bundle 2 Rolls
47224907727196,
47513709511004,
47513715867996,
47224907759964,
47224907792732,
47224907661660,
# PLA Aero
46953506505052,
46953506537820,
# PLA Basic Gradient
46901930492252,
46901930557788,
46901930525020,
# PLA CMYK Lithophane Bundle
46669434552668,
47172230414684,
# PLA Silk
46636020629852,
46636020597084,
46636020564316,
46636020531548,
47301585142108,
47301585109340,
47301585076572,
47301585043804,
# PLA MARBLE
43964050964699,
46756997333340,
# PLA Sparkle
43959190946011,
43959190913243,
43959190880475,
46797853032796,
46797853163868,
# PLA METAL
46797850902876,
43959185015003,
46797851099484,
43959185244379,
43959185277147,
# PLA CF
47068316598620,
47068316565852,
43944001994971,
46702306296156,
46702306263388,
46702306230620,
46797856342364,
# PLA MATTE
42996742750427,
47794659688796,
47794659656028,
47794659426652,
47794659164508,
42996742848731,
42996742914267,
42996751073499,
42996742881499,
42996742783195,
42996742684891,
42996742652123,
42996742717659,
42996742619355,
42996742815963,
42996742586587,
43992833261787,
46642351866204,
46642351800668,
46642351898972,
46642351833436,
43992833294555,
46893221118300,
43992833360091,
46642354094428,
46893220036956,
46893218627932,
46893218038108,
46893219316060,
47435156259164,
43992833327323,
46893220594012,
43992833229019,
# PLA BASIC
46956986433884,
43992830017755,
46892984828252,
46756980523356,
46756980556124,
46738546622812,
47097922748764,
47413030355292,
46673378607452,
46756980490588,
46846865932636,
46892982534492,
46892985745756,
47097923338588,
43992829952219,
46956529779036,
46956524241244,
43992829984987,
46892983746908,
43992829919451,
]
pyro_client: Client = Client(
name="",
api_id=0,
api_hash="",
bot_token=""
)
pyrogram_ids: list[int] = [
-12345678 # channel id
]
async def send_telegram_notification(variant_id: int, title: str, price: float, sku: str, product_type: str, url: str):
url = f'https://eu.store.bambulab.com{url}'
message = f"🎉 New Product Alert! 🎉\n\n" \
f"🔹 *Product*: {title}\n" \
f"🔹 *Price*: {price}€\n" \
f"🔹 *SKU*: {sku}\n" \
f"🔹 *Type*: {product_type}\n\n" \
f"ℹ️ Variant ID: {variant_id}\n\n" \
f"[🛒 Buy Now]({url})"
markups = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("🛒 Buy Now", url=url)
]
]
)
for pyro_id in pyrogram_ids:
await pyro_client.send_message(pyro_id, message, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=False, reply_markup=markups)
async def create_product_table():
async with aiosqlite.connect('products.db') as db:
await db.execute('''
CREATE TABLE IF NOT EXISTS products (
variant_id INTEGER PRIMARY KEY,
title TEXT,
price REAL,
sku TEXT,
product_type TEXT,
in_stock INTEGER,
notification_sent INTEGER DEFAULT 0
)
''')
await db.commit()
class BambulabResponse:
def __init__(self, variant_id: int, status_code: int, response_json: dict) -> None:
self.variant_id = variant_id
self.status_code = status_code
self.response_json = response_json
async def parse_response(self):
try:
variant_id = self.response_json.get('variant_id')
title = self.response_json.get('title')
if self.status_code == 200:
price = self.response_json.get('presentment_price')
sku = self.response_json.get('sku')
product_type = self.response_json.get('product_type')
url = self.response_json.get('url')
async with aiosqlite.connect('products.db') as db:
cursor = await db.execute('SELECT in_stock, notification_sent FROM products WHERE variant_id = ?', (variant_id,))
existing_row = await cursor.fetchone()
if existing_row:
# Product already exists in the database
in_stock, notification_sent = existing_row
if notification_sent == 0: # Check if notification has not been sent
await send_telegram_notification(variant_id, title, price, sku, product_type, url)
await db.execute('UPDATE products SET notification_sent = 1 WHERE variant_id = ?', (variant_id,))
await db.commit()
await db.execute('''
UPDATE products
SET title = ?, price = ?, sku = ?, product_type = ?, in_stock = ?
WHERE variant_id = ?
''', (title, price, sku, product_type, 1, variant_id))
await db.commit()
else:
# Product does not exist in the database, insert it
await db.execute('''
INSERT INTO products (variant_id, title, price, sku, product_type, in_stock, notification_sent)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (variant_id, title, price, sku, product_type, 1, 0))
await db.commit()
return [variant_id, title, price, sku, product_type, url]
except Exception as e:
logging.error(f"Error parsing the response: {e}")
class BambulabRequests:
def __init__(self, aiohttp_session: aiohttp.ClientSession) -> None:
self.aiohttp_session = aiohttp_session
async def handle_check_variant_response(self, response: aiohttp.ClientResponse, variant_id: int):
match response.status:
case 200:
bambulab_response: BambulabResponse = BambulabResponse(variant_id, response.status, await response.json(content_type=None))
response_data: list = await bambulab_response.parse_response()
logging.info(f"Variant {variant_id} => {response_data[1]} is now in-stock at {response_data[2]}€")
case 404:
logging.error(f"Variant {variant_id} not found in the website")
case 429:
logging.error(f"Rate limited by the website")
case 422:
bambulab_response: BambulabResponse = BambulabResponse(variant_id, response.status, await response.json(content_type=None))
response_data: list = await bambulab_response.parse_response()
logging.error(f"Variant {variant_id} is out-of-stock")
case _:
logging.error(f"Unknown response status code: {response.status}")
async def check_variant(self, variant_id: int):
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0',
'Accept': '*/*',
'Accept-Language': 'it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'Origin': 'https://eu.store.bambulab.com',
'DNT': '1',
'Alt-Used': 'eu.store.bambulab.com',
'Connection': 'keep-alive',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
}
json_data = {
'form_type': 'product',
'utf8': '✓',
'id': f'{variant_id}',
'quantity': '1',
'product-id': '',
}
async with self.aiohttp_session.post('https://eu.store.bambulab.com/cart/add.js', headers=headers, json=json_data) as response:
await self.handle_check_variant_response(response, variant_id)
async def check_variants(self, variant_ids: list[int]):
tasks: list[asyncio.Task] = []
for variant_id in variant_ids:
tasks.append(asyncio.create_task(self.check_variant(variant_id)))
await asyncio.sleep(2)
await asyncio.gather(*tasks)
async def get_aiohttp_session(family: int = socket.AF_INET) -> aiohttp.ClientSession:
aiohttp_session = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(
family=family,
limit_per_host=0,
force_close=True,
enable_cleanup_closed=True,
),
connector_owner=True,
auto_decompress=True,
timeout=aiohttp.ClientTimeout(total=10),
skip_auto_headers=frozenset(("User-Agent", "Content-Type", "Accept-Encoding")),
)
return aiohttp_session
async def main():
await create_product_table()
await pyro_client.start()
logging.info("Starting Bambulab Restocks Bot")
while True:
aiohttp_sesion: aiohttp.ClientSession = await get_aiohttp_session(family=socket.AF_INET)
bambulab_requests = BambulabRequests(aiohttp_sesion)
await bambulab_requests.check_variants(variants_to_check)
await aiohttp_sesion.close()
logging.info(f"Waiting {timeout_to_wait} seconds before relaunch the check of the variants")
await asyncio.sleep(timeout_to_wait)
if __name__ == "__main__":
asyncio.run(main())