-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify_api_scrape.py
217 lines (184 loc) · 6.97 KB
/
classify_api_scrape.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
from urllib.parse import urlencode, urlunparse
import time
import requests
import unicodedata
import logging
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from urllib.parse import urlencode
from urllib.request import urlopen
import xmltodict
from lxml import etree
logger = logging.getLogger('my_logger')
logger.setLevel(logging.INFO)
handler = logging.FileHandler('logfile_unimported_books.log')
formatter = logging.Formatter(
'[%(asctime)s] %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
def get_dewey_value_scrape_selenium(isbn=None, title=None, author=None) -> int:
driver = create_driver()
base_url = 'classify.oclc.org'
path = '/classify2/ClassifyDemo'
DEWEY_VALUE_XPATH = '//table[@id="classSummaryData"]/tbody/tr/td[2]'
result_url = None
# Prepare the query parameters based on the provided input
params = {}
params['startRec'] = 0
if isbn:
params['search-standnum-txt'] = isbn
elif title and author:
params['search-title-txt'] = title
params['search-author-txt'] = author
query_string = urlencode(params)
full_url = urlunparse(('https', base_url + path, '', '', query_string, ''))
driver.get(full_url)
time.sleep(5)
try:
if result_link := driver.find_element(By.XPATH, '//span[@class="title"]/a'):
result_link.click()
ddc_class_number = driver.find_element(By.XPATH, DEWEY_VALUE_XPATH).text
try:
return int(float(ddc_class_number))
except ValueError:
pass
except NoSuchElementException:
logger.info(
'Selenium unable to search for book, isbn="%s", title="%s", author="%s"',
isbn, title, author
)
return 0 # placeholder value
def create_driver():
"""
Create driver to webscrape dewey values
Response using requests library: Enable JavaScript and cookies to continue"
"""
#TODO: Add rotating user agents
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"
chrome_options = ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage') # Overcomes limited resource problems
chrome_options.add_argument('--disable-blink-features=AutomationControlled') # Disables the flag that identifies WebDriver sessions
chrome_options.add_argument(f"user-agent={user_agent}")
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)
return driver
def get_dewey_value_scrape(isbn=None, title=None, author=None):
base_url = 'https://classify.oclc.org'
path = '/classify2/ClassifyDemo'
DEWEY_VALUE_XPATH = '//table[@id="classSummaryData"]/tbody/tr/td[2]/text()'
result_url = None
# Prepare the query parameters based on the provided input
params = {}
params['startRec'] = 0
if isbn:
params['search-standnum-txt'] = isbn
elif title and author:
params['search-title-txt'] = title
params['search-author-txt'] = author
response = requests.get(base_url + path, params=params)
if "Sorry, your current search did not return any results" in response.text:
return None
tree = etree.HTML(response.content)
if isbn:
ddc_class_number = tree.xpath(DEWEY_VALUE_XPATH)[0]
try:
return int(float(ddc_class_number))
except ValueError:
pass
# title and author
result_link = tree.xpath('//span[@class="title"]/a')
if result_link:
result_url = result_link[0].get('href')
if result_url:
response = requests.get(base_url + result_url)
tree = etree.HTML(response.content)
ddc_class_number = tree.xpath(DEWEY_VALUE_XPATH)[0]
try:
return int(float(ddc_class_number))
except ValueError:
pass
return None
# ------------ API IS NO LONGER PUBLICLY ACCSESSIBLE --------
def get_dewey_value_api(isbn, title, author):
"""
Use Classify API to find the dewey decimal number using:
1. ISBN: uses isbn_to_dewey()
OR
2. title & author
returns XML data for each book. Parse until dewey decimal
number is found
"""
base = "http://classify.oclc.org/classify2/Classify?"
summaryBase = "&summary=true"
if isbn:
parm_type = "isbn"
parm_value = str(isbn)
search_url = (
base + urlencode({parm_type: parm_value.encode("utf-8")}) + summaryBase
)
else:
param_type_title = "title"
parm_value_title = title
param_type_author = "author"
parm_value_author = author
search_url = (
base
+ urlencode({param_type_title: parm_value_title.encode("utf-8")})
+ "&"
+ urlencode({param_type_author: parm_value_author.encode("utf-8")})
+ summaryBase
)
# xml_content = urlopen(search_url).read()
xml_content = requests.get(search_url).text
book_data = xmltodict.parse(xml_content)
return isbn_to_dewey(book_data)
def isbn_to_dewey(book_data):
"""
Traverse though dictionary to find the dewey value.
response_code determines how to traverse
"""
response_code = book_data.get("classify").get("response").get("@code")
SINGLE_BOOK = "0"
MULTIPLE_BOOKS = "4"
if response_code == SINGLE_BOOK:
try:
dewey_number = (
book_data.get("classify")
.get("recommendations")
.get("ddc")
.get("mostPopular")
.get("@sfa")
)
return dewey_number
except AttributeError:
return "Returned book response, but has no dewey number"
elif response_code == MULTIPLE_BOOKS:
try:
owi_number = (
book_data.get("classify").get("works").get("work")[0].get("@owi")
)
base = "http://classify.oclc.org/classify2/Classify?"
parm_type = "owi"
parm_value = owi_number
search_url = base + urlencode({parm_type: parm_value.encode("utf-8")})
xml_content = urlopen(search_url).read()
book_data = xmltodict.parse(xml_content)
dewey_number = (
book_data.get("classify")
.get("recommendations")
.get("ddc")
.get("mostPopular")
.get("@sfa")
)
return dewey_number
except AttributeError:
return "multiple works found, even after using OWI"
else:
return "Cannot find dewey"