-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (40 loc) · 1.54 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
#!/bin/python
import requests
from bs4 import BeautifulSoup, SoupStrainer
from dataclasses import dataclass
@dataclass
class Piece:
id:str
name:str
total_quantity:int
used_quantity:int
img_url:str
piece_url:str
def get_pieces() -> dict[str, Piece]:
lego_id = input('What is the lego id ? : ')
BASE_URL = 'https://www.bricklink.com'
pieces:dict[str, Piece] = {}
soup = BeautifulSoup(requests.get(
f'{BASE_URL}/catalogItemInv.asp?S={lego_id}-1',
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0'}
).content, 'lxml', parse_only=SoupStrainer({'tr': {'class':'IV_ITEM'}}))
for piece in soup.select('tr.IV_ITEM'):
name = piece.select('td')[3].text
quantity = int(piece.select_one('td[align="RIGHT"]').text.strip())
try: pieces[name].total_quantity += quantity
except: pieces[name] = Piece(
piece['class'][0].replace('IV_', ''),
name, quantity, quantity,
piece.select_one('img')['src'],
BASE_URL + piece.select_one('a')['href']
)
return pieces
def pieces_to_csv(pieces:dict[str, Piece]) -> str:
DELIMITER = '; '
pieces_csv = DELIMITER.join(Piece.__annotations__.keys())+'\n'
for piece in pieces.values(): pieces_csv += DELIMITER.join(map(str, vars(piece).values())) + '\n'
with open('pieces.csv', 'w+', encoding='utf-8') as f: f.write(pieces_csv)
return pieces_csv
if __name__ == '__main__':
pieces = get_pieces()
print(pieces_to_csv(pieces))