-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (104 loc) · 3.63 KB
/
main.py
File metadata and controls
122 lines (104 loc) · 3.63 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
import csv
import re
import time
from pathlib import Path
import requests
from bs4 import BeautifulSoup
from pathos.multiprocessing import ProcessPool
class Collection:
def __init__(self, collection_id, name):
self.collection_id = collection_id
self.name = name
self._search_url = f"https://mtgcollectionbuilder.com/collections/{collection_id}/search"
self.found = []
def search_card(self, card):
strings = []
page = requests.post(self._search_url, data={"cardNameSearch": card})
soup = BeautifulSoup(page.content, 'html.parser')
# return soup.select('tr.success')
for r in soup.select('tr.success'):
tds = r.select("td")
name = tds[0].text.strip()
img_url = r.td.a["data-img"]
row = f'<tr><td><a class="thumbnail" href="{tds[0].a["href"]}" target="_blank" rel="noopener noreferrer">{name}<span><img src="{img_url}" /></span></a></td>{tds[1]}{tds[2]}{tds[3]}{tds[4]}</tr>'
strings.append(row)
return strings
def search_card_list(self, search_list):
print(f"Searching in {self.name}:", end="")
# self.found += self.search_card(search_list[0])
# return
pool = ProcessPool(nodes=16)
results = pool.amap(self.search_card, search_list)
while not results.ready():
time.sleep(0.5)
print(".", end='')
for r in results.get():
self.found += r
print(" Done!")
def get_html(self):
ret = f"""
<h1><a href="https://mtgcollectionbuilder.com/collections/{self.collection_id}">{self.name} collection</a></h1>
<table class="styled-table">
<thead>
<tr>
<th class="header">Name</th>
<th class="header">Qt.</th>
<th class="header">Foil Qt.</th>
<th class="header">Set</th>
<th class="header">Low $</th>
</tr>
</thead>
<tbody>
{'\n'.join(self.found)}
</tbody>
</table>
"""
return ret
def process_file(path):
out = set()
regex = re.compile(r"(?:\d+ )?([^(]*)(?:\(.*\))?(?: .*)?")
lines = path.read_text().splitlines()
for line in lines:
if not line or line[0] == "#":
continue
match = regex.match(line)
if match:
out.add(match.group(1).strip())
return out
def save_report(collections, report_name="report.html"):
print(f"\nSaving report to {report_name}")
html_report = """
<html>
<head>
<link rel="stylesheet" href="thumbs.css">
<link rel="stylesheet" href="table.css">
</head>
<body>
"""
for collection in collections:
html_report += collection.get_html()
html_report += "\n</body></html>"
(Path.cwd() / report_name).write_text(html_report)
def main(report_name="report.html", single_list=None, single_collection=None):
search_list = set()
if single_list:
search_list = process_file(Path.cwd() / single_list)
else:
for path in Path.cwd().glob("*.txt"):
print(f"reading from {path}")
search_list.update(process_file(path))
collections = []
if single_collection:
collections.append(single_collection)
else:
friends_path = Path.cwd() / "friends.csv"
friends_list = friends_path.read_text().splitlines()
reader = csv.reader(friends_list[1:], delimiter=',')
for row in reader:
collections.append(Collection(row[1], row[0]))
for collection in collections:
collection.search_card_list(search_list)
save_report(collections, report_name)
if __name__ == '__main__':
# moje = Collection("49772", "moje")
main("krak.html", "krak.txt")