-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrape_sothebys.py
126 lines (107 loc) · 3.5 KB
/
scrape_sothebys.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
#!/usr/bin/env python
import sys
import os
import urllib.parse
import re
import json
import urllib
from bs4 import BeautifulSoup
import requests
from pathvalidate import sanitize_filepath
from multiprocessing.pool import ThreadPool as Pool
from urllib.parse import urlparse, parse_qs, urljoin
query = sys.argv[1]
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "o28sy4q7wu-dsn.algolia.net",
}
def worker(item):
image_url = item["image"]
parsed_url = urlparse(image_url)
parsed_qs = parse_qs(parsed_url.query)
url = parsed_qs["url"][0]
if url is None:
print(f"!!! WARNING: No original URL: {image_url}")
url = image_url
artist = item["artistName"]
if artist is None:
print("*** SKIPPING (no artist): " + url)
return
path = sanitize_filepath(os.path.join("sothebys", artist, os.path.basename(url)))
os.makedirs(os.path.dirname(path), exist_ok=True)
if os.path.exists(path):
print("*** SKIPPING (exists): " + path)
else:
resp = requests.get(url)
if resp.status_code != 200:
print(f"!!! FAILED saving: {url}")
return
print("Saving: " + url)
with open(path, "wb") as f:
for chunk in resp:
f.write(chunk)
artists = ", ".join(item["artists"])
artwork_name = item["title"]
txt = f"{artists}, {artwork_name}"
# if "departments" in item and item["departments"]:
# txt += ", " + ", ".join(item["departments"])
# if "fullText" in item and item["fullText"]:
# txt += f", {item['fullText']}"
txt_name = os.path.splitext(os.path.basename(path))[0] + ".txt"
txt_path = os.path.join(os.path.dirname(path), txt_name)
with open(txt_path, "w", encoding="utf-8") as f:
f.write(txt)
no = 1
while True:
print(f"=== Page {no} ===")
req_params = {
"highlightPreTag": "<ais-highlight-0000000000>",
"highlightPostTag": "</ais-highlight-0000000000>",
"clickAnalytics": "true",
"hitsPerPage": "51",
"maxValuesPerFacet": "9999",
"page": str(no - 1),
"filters": 'type:"Bid" OR type:"Buy Now" OR type:"Lot" OR type:"Private Sale" OR type:"Retail"',
"query": query,
"facets": '["type","endDate","lowEstimate","highEstimate","artists"]',
"tagFilters": "",
}
params = {
"requests": [
{
"indexName": "bsp_dotcom_prod_en",
"params": urllib.parse.urlencode(req_params),
}
],
}
query_params = {
"x-algolia-agent": "Algolia for JavaScript (4.2.0); Browser (lite); react (16.13.1); react-instantsearch (6.7.0); JS Helper (3.2.2)",
"x-algolia-api-key": "e732e65c70ebf8b51d4e2f922b536496",
"x-algolia-application-id": "O28SY4Q7WU",
}
resp = requests.post(
"https://o28sy4q7wu-dsn.algolia.net/1/indexes/*/queries",
data=json.dumps(params),
params=query_params,
headers=HEADERS,
)
resp.raise_for_status()
resp = resp.json()
if "results" not in resp:
print("Finished.")
break
j = resp["results"]
if not j:
print("Finished.")
break
j = j[0]["hits"]
if not j:
print("Finished.")
break
p = Pool(processes=8)
for res in p.imap_unordered(worker, j):
pass
p.close()
p.join()
no += 1