-
Notifications
You must be signed in to change notification settings - Fork 0
/
comic.py
364 lines (317 loc) · 16.5 KB
/
comic.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from __future__ import print_function
from selenium import webdriver
import bs4
import time
from difflib import SequenceMatcher
from datetime import date
import pandas as pd
import sys
import random
import gspread
import locale
locale.setlocale( locale.LC_ALL, '' )
# =============================================================================
# Variables
# =============================================================================
rundate = date.today().strftime("%Y-%m-%d")
htmlBody = ''
# The error codes
NO_SEARCH_RESULTS_FOUND = 1
User_Name = input('ComicsPriceGuide.com Username: ')
User_Pass = input('ComicsPriceGuide.com Password: ')
Google_Workbook = input('Google Workbook Name: ')
Google_Sheet = input('Google Worksheet Name: ')
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
driver = webdriver.Chrome()
#driver.maximize_window()
def LoginComicsPriceGuide(User_Name, User_Pass):
driver.get("https://comicspriceguide.com/login")
# Login Page Elements
input_login_username = driver.find_element_by_xpath('//input[@id="user_username"]')
input_login_password = driver.find_element_by_xpath('//input[@id="user_password"]')
button_login_submit = driver.find_element_by_id("btnLogin")
# Fill out login page
input_login_username.send_keys(User_Name)
input_login_password.send_keys(User_Pass)
driver.execute_script("arguments[0].click();",button_login_submit)
# Waiting between 5 and 20 seconds to look like a user
time.sleep(random.uniform(5, 20))
return()
def SearchComic(Title, Issue):
print(fullName + " - Searching...")
# Navigating to search page
if(driver.current_url != "https://comicspriceguide.com/Search"):
driver.get("https://comicspriceguide.com/Search")
# Wait for the search page to load.
driver.implicitly_wait(15)
# Search Page HTML elements
input_search_title = driver.find_element_by_id("search")
input_search_issue = driver.find_element_by_id("issueNu")
button_search_submit = driver.find_element_by_id("btnSearch")
# Fill out the search fields
input_search_title.send_keys(Title)
input_search_issue.send_keys(Issue)
#sleep to prevent overloading the site...
time.sleep(random.uniform(2, 15))
driver.execute_script("arguments[0].click();",button_search_submit)
# Wait for results to show up
time.sleep(random.uniform(5, 30))
# Capture resulting page source
source_code = driver.page_source
# Instantiate BS4 using the source code.
soup = bs4.BeautifulSoup(source_code,'html.parser')
# Initial similarity. This similarity is between the given title and the hyperlink comic.
similarity = 0
# Link of the comic.
comic_link = ''
percentage = 0
#Determine the best match for the comic that was just serached for
for candidate in soup.find_all('a', attrs={'class':'grid_issue'}):
# Replace the superscript "#" in the comic name
a = str(candidate.text).replace("<sup>#</sup>","#").upper()
# Check for similarity between the hyperlink comic and my comic title. If more,
percentage = similar(a,fullName)
if percentage > similarity:
similarity = similar(a,fullName)
final_link = 'https://comicspriceguide.com' + str(candidate["href"])
comic_link = final_link
if percentage > 0 :
print(" Found a match, confidence: " + str(int(percentage*100)) + "% - " + comic_link)
else:
percentage = None
print(str(thisComic['Title']) + " #" + str(thisComic['Issue']) + " - " + str(thisComic['Book Link']))
comic_link = thisComic['Book Link']
array = [comic_link, percentage]
return(array)
def generate_HTMLPage(sortedsheet):
for index, thisComic in sortedsheet.iterrows():
title = str(thisComic['Title']).strip().upper()
notes = str(thisComic['Notes']).strip()
issue = int(str(thisComic['Issue']).strip())
value = int(str(thisComic['Value']).strip())
image = str(thisComic['Cover Image']).strip().upper()
grade = str(thisComic['Grade']).strip()
cgc = "No" if thisComic['CGC Graded'] == None else thisComic['CGC Graded']
Key = "No" if thisComic['CGC Graded'] == None else thisComic['CGC Graded']
variant = '' if str(thisComic['Variant']).strip() == 'nan' else str(thisComic['Variant']).strip()
url = '' if str(thisComic['Book Link']).strip() == 'nan' else str(thisComic['Book Link']).strip()
if cgc.upper() =='NO':
cgcdiv = ''
else:
cgcdiv = "<div class='cgc'>CGC</div>"
if key.upper() =='NO':
keydiv = ''
else:
keydiv = "<div class='key'>KEY</div>"
htmlBody = htmlBody + "<div class='hvrbox'><img src='" + str(image) + "' alt='Cover' class='hvrbox-layer_bottom'><div class='hvrbox-layer_top'><div class='hvrbox-text'>"
htmlBody = htmlBody + "<a href='" + str(url) + "'>" + str(title) + " #" + str(issue) + str(variant) +"<br><br>Grade: " + str(grade) + "<br><br>Value: " + locale.currency(value, grouping=True) + "<br><br>" + str(notes) + "</a></div>" + str(cgcdiv) + str(keydiv) +"</div></div>"
with open("comics.html",'w') as f:
f.write("""<style type'"text/css">
body {background-color: 282828;}
a {color: whitesmoke;text-decoration: none;}
.cgc {background-color: rgb(148, 7, 35);z-index: inherit 5;font-family: Arial, Helvetica, sans-serif;position: absolute;bottom: 0;}
.hvrbox,
.hvrbox * {box-sizing: border-box; padding: 5px;}
.hvrbox {position: relative;display: inline-block;overflow: hidden;width: 250px;height: 400px;}
.hvrbox img {width: 250px;height: 400px;}
.hvrbox .hvrbox-layer_bottom {display: block;}
.hvrbox .hvrbox-layer_top {
opacity: 0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 250px;
height: 400px;
background: rgba(0, 0, 0, 0.6);
color: #fff;
padding: 15px;
-moz-transition: all 0.4s ease-in-out 0s;
-webkit-transition: all 0.4s ease-in-out 0s;
-ms-transition: all 0.4s ease-in-out 0s;
transition: all 0.4s ease-in-out 0s;
}
.hvrbox:hover .hvrbox-layer_top,
.hvrbox.active .hvrbox-layer_top {opacity: 1;}
.hvrbox .hvrbox-text {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
font-size: 18px;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
-moz-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.hvrbox .hvrbox-text_mobile {
font-size: 15px;
border-top: 1px solid rgb(179, 179, 179); /* for old browsers */
border-top: 1px solid rgba(179, 179, 179, 0.7);
margin-top: 5px;
padding-top: 2px;
display: none;
}
.hvrbox.active .hvrbox-text_mobile {display: block;}
</style>
""")
f.write(htmlBody)
def ReadGoogleSheet(Google_Workbook, Google_Sheet):
# =============================================================================
# Read Google sheet into pandas Dataframe - Requires Service Account in Google API
# file stored in ~/.config/gspread/service_account.json
# =============================================================================
gc = gspread.service_account()
sh = gc.open(Google_Workbook)
worksheet = sh.worksheet(Google_Sheet)
Starting_DF = pd.DataFrame(worksheet.get_all_records())
sortedsheet = Starting_DF.sort_values(by=['Title','Volume','Issue'])
return Starting_DF, sortedsheet, worksheet
def BackupGoogleSheet(Sheetname):
# =============================================================================
# Make a backup of the current sheet in the event it all goes to shit
# =============================================================================
starting_rows = Starting_DF.shape[0]
starting_cols = Starting_DF.shape[1]
backup = sh.add_worksheet(title="Backup " + rundate, rows=starting_rows, cols=starting_cols)
backup.update([sortedsheet.columns.values.tolist()] + sortedsheet.values.tolist())
SheetData = ReadGoogleSheet(Google_Workbook, Google_Sheet)
StartingDF = SheetData[0]
sortedsheet = SheetData[1]
worksheet = SheetData[2]
generate_HTMLPage(sortedsheet)
BackupGoogleSheet(StartingDF) # Backup current sheet
LoginComicsPriceGuide(User_Name, User_Pass)
for index, thisComic in sortedsheet.iterrows():
try:
# =============================================================================
# Fetch required data fields
# =============================================================================
title = str(thisComic['Title']).strip().upper()
issue = int(str(thisComic['Issue']).strip())
grade = str(thisComic['Grade']).strip()
cgc = "No" if thisComic['CGC Graded'] == None else thisComic['CGC Graded']
variant = '' if str(thisComic['Variant']).strip() == 'nan' else str(thisComic['Variant']).strip()
url = '' if str(thisComic['Book Link']).strip() == 'nan' else str(thisComic['Book Link']).strip()
price_paid = float(0.00)
# Prepare the Price Paid field as an float so we can do math stuff
print(type(thisComic['Price Paid']))
if type(thisComic['Price Paid']) == str:
price_paid = float(str(thisComic['Price Paid']).strip().replace('$','') if float(str(thisComic['Price Paid']).strip().replace('$','')) != None else "0")
elif type(thisComic['Price Paid']) == float:
price_paid = thisComic['Price Paid']
else:
print('WARNING: Price Paid: ' + type(price_paid))
if thisComic['Price Paid'] == 0:
price_paid = float(0.01)
sortedsheet.at[index,'Price Paid'] = price_paid
fullName = title + " #" + str(issue) + variant
confidence = ''
print('Gathering : ' + fullName)
if url == '' :
print('No direct URL - Calling search')
search_results_Array = SearchComic(title, issue)
url = search_results_Array[0]
confidence = search_results_Array[1]
# =============================================================================
# A match has been determined - get the details
# =============================================================================
if url != '':
driver.get(url)
else:
raise ValueError(NO_SEARCH_RESULTS_FOUND,"Looks like the search gave no result. Try searching the title and issue manually to confirm the issue.",thisComic['Title'],thisComic['Issue'])
# Wait 5 seconds for page to load and get its source code
time.sleep(random.uniform(60, 240))
source_code = driver.page_source
# New BS4 Instance with the comic's page's source code
soup = bs4.BeautifulSoup(source_code,'html.parser')
# Finding out all details
publisher = soup.find('a',attrs={'id':'hypPub'}).text
volume = soup.find('span',attrs={'id':'lblVolume'}).text
notes = soup.find('span',attrs = {'id':'spQComment'}).text
keyIssue = "Yes" if "Key Issue" in soup.text else "No"
image = soup.find('img',attrs={'id':'imgCoverMn'})['src']
if image[0:4] != 'http':
# In some cases the URL is relative
image = 'https://comicspriceguide.com/' + image
basic_info = []
for s in soup.find_all('div',attrs={"class":"m-0 f-12"}):
basic_info.append(s.parent.find('span',attrs={"class":"f-11"}).text.replace(" ", " "))
published = basic_info[0] if basic_info[0] != " ADD" else "Unknown"
comic_age = basic_info[1] if basic_info[1] != " ADD" else "Unknown"
cover_price = basic_info[2] if basic_info[2] != " ADD" else "Unknown"
# =============================================================================
# Get prices into a dataframe to find our grade
# Known Defect: Comics graded as 10 fail due to the DF having '10.'not '10.0'
# =============================================================================
if len(grade) < 3:
grade = grade + ".0"
priceTable = soup.find(name='table',attrs={"id":"pricetable"})
# Load the priceTable into a dataframe
pricesdf = pd.read_html(priceTable.prettify())[0]
# Truncate Condition column values to allow matching
pricesdf['Condition'] = pricesdf['Condition'].str[:3]
pricesdf = pricesdf.rename(columns={'Graded Value *': 'Graded Value'})
thisbooksgrade = pricesdf.loc[pricesdf['Condition'] == grade]
RawValue = float(thisbooksgrade['Raw Value'].iloc[0].replace('$',''))
GradedValue = float(thisbooksgrade['Graded Value'].iloc[0].replace('$',''))
value = RawValue if cgc.upper() == 'NO' else GradedValue
characters_info = soup.find('div',attrs={'id':'dvCharacterList'}).text if soup.find('div',attrs={'id':'dvCharacterList'}) != None else "No Info Found"
story = soup.find('div',attrs={'id':'dvStoryList'}).text.replace("Stories may contain spoilers","")
url_link = driver.current_url
# =============================================================================
# Determine Book Price change from last scan using the "Value" field
# =============================================================================
# =============================================================================
# if 'Value' in thisComic:
# print(' Value Field Exists')
# LastScanValue = float(LastScanValue.replace('$',''))
# CurrentScanValue = float(value.replace('$',''))
# priceshift = round((CurrentScanValue - LastScanValue),2)
# print(' PriceShift: ' + str(priceshift))
# sortedsheet.at[index,'PriceShift'] = priceshift
# else:
# print(' Value Field does not exist, populating with current value')
# sortedsheet.at[index,'Value'] = RawValue
# =============================================================================
# =============================================================================
# update the DF
# =============================================================================
sortedsheet.at[index,'Publisher'] = publisher
sortedsheet.at[index,'Volume'] = volume
sortedsheet.at[index,'Published'] = published
sortedsheet.at[index,'KeyIssue'] = keyIssue
sortedsheet.at[index,'Cover Price'] = cover_price
sortedsheet.at[index,'Comic Age'] = comic_age
sortedsheet.at[index,'Notes'] = notes
if confidence !='':
sortedsheet.at[index,'Confidence'] = confidence
else:
sortedsheet.at[index,'Confidence'] = None
sortedsheet.at[index,'Book Link'] = url_link
sortedsheet.at[index,'Graded'] = GradedValue
sortedsheet.at[index,'Ungraded'] = RawValue
sortedsheet.at[index,'Cover Image'] = image
sortedsheet.at[index, rundate] = value
except ValueError as ve:
if(ve.args[0] == NO_SEARCH_RESULTS_FOUND):
print(" Unable to find Match for " + str(ve.args[2]) + " #" + str(ve.args[3]))
#driver.get("https://comicspriceguide.com/Search")
except Exception as e:
print("Error while working on " + title + ' ' + str(e))
#driver.get("https://comicspriceguide.com/Search")
continue
# =============================================================================
# Commit results back to Google Sheet
# =============================================================================
# with pd.ExcelWriter(Excel_Workbook_Name, mode='w') as writer:
# sortedsheet.to_excel(writer, sheet_name=Excel_Sheet_Name)
sortedsheet.fillna('', inplace=True)
worksheet.update([sortedsheet.columns.values.tolist()] + sortedsheet.values.tolist())
# =============================================================================
generate_HTMLPage(sortedsheet)
print("Work is complete.")