-
Notifications
You must be signed in to change notification settings - Fork 0
/
dazzlefix.py
executable file
·162 lines (141 loc) · 5.67 KB
/
dazzlefix.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
#!/usr/bin/env python
import os, sys, re
import urllib2, urllib, cookielib
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, Tag, NavigableString
from optparse import OptionParser
class booksku_error(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class booksku_session:
"""
Authenticate with BookSku in constructor, then use those credentials to
scrape the condition notes by SKU.
"""
def __init__(self, username, password):
self.jar = cookielib.CookieJar()
self.handler = urllib2.HTTPSHandler()
self.cjh = urllib2.HTTPCookieProcessor(self.jar)
self.opener = urllib2.build_opener(self.handler, self.cjh)
loginvalues = {'username': username,
'userpass': password,
'userlogin': 'Log In',
'autologin': '1'}
data = urllib.urlencode(loginvalues)
page = self.opener.open("https://bs.booksku.com/bs/login.php", data)
# Fix: handle errors more gracefully here. Occassionally we fail to
# resolve bs.booksku.com for some reason; re-running the script will
# then succeed.
self.loginsoup = BeautifulSoup(page)
if len(self.loginsoup.findAll('input', attrs={'id': 'username'})):
raise booksku_error("Login failed, wrong password?")
page.close()
def condition(self, sku):
url = "https://bs.booksku.com/bs/catalog_files/index_search.php?action=edit&sku=%s&ownder_id=282" % sku
page = self.opener.open(url)
soup = BeautifulSoup(page)
alldetails = soup.findAll(attrs={'name': 'detail_string'})
if len(alldetails) != 1:
raise booksku_exception("Expected 1 'detail_string' field for sku '%s', found %d" % (sku, len(alldetails)))
return alldetails[0].attrMap['value']
class dazzle_error(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class dazzle_doc:
"""
Parse DAZzle document and provide a method to add lines of text as
<RubberStamp[stampbase]> ... <RubberStamp[stampbase+n]> elements.
"""
def __init__(self, fname, stampbase):
self.doc = BeautifulStoneSoup(open(fname, "r").read())
self.base = stampbase
self.last = 20
def skus(self):
for p in self.doc.findAll("package"):
skus = p.findAll("rubberstamp2")
if (len(skus) != 1):
raise dazzle_error("expected exactly 1 'rubberstamp2' field in package, found %d" % len(skus))
sku = skus[0]
yield sku.contents[0]
def pkg(self, sku):
for p in self.doc.findAll("package"):
skuelem = p.findAll("rubberstamp2")[0]
if skuelem.contents[0] == sku:
return p
def set_condition_notes(self, sku, condition_lines):
p = self.pkg(sku)
if p is None:
raise dazzle_error("package with sku '%s' not found" % sku)
# make sure we don't have these fields set yet
for i in xrange(self.base, self.base+len(condition_lines)):
stamp = p.findAll("rubberstamp%d" % i)
if len(stamp):
raise dazzle_error("package with sku '%s' already has field 'rubberstamp%d'" % (sku, i))
i = self.base
for cl in condition_lines:
n = Tag(self.doc, "rubberstamp%d" % i)
n.append(NavigableString(cl))
p.append(n)
i += 1
while(i < self.last):
n = Tag(self.doc, "rubberstamp%d" % i)
n.append(NavigableString("."))
p.append(n)
i += 1
return p
def save(self, fname):
f = open(fname, "w")
f.write(self.doc.renderContents())
f.close()
def break_lines(text, linelength):
l = ""
lines = []
for w in text.split():
if len(l) + len(w) + 1 > linelength:
lines.append(l)
l = w
else:
l += " %s" % w
lines.append(l)
return lines
def fix_docs(opts, args):
print "Opening session with booksku..."
try:
s = booksku_session(opts.username, opts.password)
except booksku_error, e:
print e
return
for fname in args:
print "Processing '%s'..." % fname
d = dazzle_doc(fname, opts.basestamp)
for sku in d.skus():
print " processing SKU %s with line length %d..." % (sku, opts.linelength),
try:
c = s.condition(sku)
lines = break_lines(c, opts.linelength)
pkg = d.set_condition_notes(sku, lines)
except (booksku_error, dazzle_error), e:
print "FAILED to set condition notes: %s!" % e
else:
print "set %d lines" % len(lines)
sname = fname.replace(".xml", ".fixed.xml")
print " ...saving '%s'" % sname
d.save(sname)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-u", "--username", dest="username",
help="BookSku account name", metavar="USER", default="roguebooks")
parser.add_option("-p", "--password", dest="password",
help="BookSku account password", metavar="PASSWORD")
parser.add_option("-l", "--linelength", type="int", help="Maximum length (in characters) of condition notes lines", default=100)
parser.add_option("-b", "--basestamp", type="int", help="Use 'RubberStamp<basestamp>' as the first field of condition notes", default=10)
(opts, args) = parser.parse_args()
try:
fix_docs(opts, args)
except KeyboardInterrupt:
print
print "Canceling."
sys.exit(-1)