Created
April 15, 2019 03:20
-
-
Save moselhy/546b8c1ef2eeaca66e086e4fc78741b9 to your computer and use it in GitHub Desktop.
Given a list of ISBN's (through stdin, line-separated), get the highest-offering school and buyback price for each one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Mohamed Moselhy | |
# April 14 2019 | |
import requests | |
from bs4 import BeautifulSoup | |
import urllib3 | |
from progress.bar import Bar | |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
# | |
# class PriceBar(progress.bar.ChargingBar): | |
# def __init__(self, isbn_list, schools): | |
# super().__init__() | |
# self.isbnList = isbn_list | |
# self.schools = schools | |
# | |
# message = 'Fetching prices:' | |
# suffix = 'ISBN: %(isbn) (%(isbn_idx)/%(isbn_max)). School: %(school) (%(school_idx)/%(school_max))' | |
# # suffix = '%(isbn)' | |
# | |
# # def isbn_idx(self): | |
# # return int((self.index/len(self.schools))) % len(self.isbnList) | |
# # | |
# # def isbn_max(self): | |
# # return len(self.isbnList) | |
# # | |
# # def isbn_p(self): | |
# # return self.isbn_idx()/len(self.isbnList) * 100 | |
# # | |
# # @property | |
# # def isbn(self): | |
# # return self.isbnList[self.isbn_idx()] | |
# # | |
# # @property | |
# # def school_idx(self): | |
# # return (self.index/len(self.isbnList)) % len(self.schools) | |
# # | |
# # @property | |
# # def school_p(self): | |
# # return self.school_idx()/len(self.schools) * 100 | |
# # | |
# # @property | |
# # def school_max(self): | |
# # return len(self.schools) | |
# # | |
# # @property | |
# # def school(self): | |
# # return self.schools[self.school_idx()] | |
def get_schools(): | |
schools = {} | |
url = r'http://sellmytextbooks.org/map.cfm?area=ON' | |
html_content = requests.get(url, verify=False) | |
soup = BeautifulSoup(html_content.text, features='html.parser') | |
link_tags = soup.find_all('a') | |
for l in link_tags: | |
school_id = l.get('href').split('=')[-1] | |
if school_id.isdigit(): | |
schools[int(school_id)] = l.text | |
return schools | |
def get_price(isbn, school_id): | |
payload = { | |
'isbn': isbn, | |
'Submit': 'Check Price >>' | |
} | |
with requests.session() as s: | |
url = 'http://sellmytextbooks.org/members/{0}/index.cfm?index=QUOTE'.format(str(school_id)) | |
req = requests.Request('POST', url, data=payload) | |
prepped = req.prepare() | |
response = s.send(prepped) | |
soup = BeautifulSoup(response.text, features='html.parser') | |
strongs = soup.find_all('strong') | |
strong_text = [s.text for s in strongs if '$' in s.text] | |
if len(strong_text) == 0: | |
price = 0 | |
else: | |
price = strong_text[0].split('$')[-1].strip() | |
return float(price) | |
def get_all_prices(isbns): | |
prices = {} | |
schools = get_schools() | |
barMessage = "Fetching prices..." | |
# bar = PriceBar(isbnList, schools) | |
bar = Bar(message=barMessage, max=len(isbnList) * len(schools)) | |
for isbn in isbnList: | |
prices[isbn] = (0, "None") | |
for school in schools: | |
bar.next() | |
price = get_price(isbn, school) | |
if price > prices[isbn][0]: | |
prices[isbn] = (price, schools[school]) | |
bar.finish() | |
return prices | |
if __name__ == "__main__": | |
isbnList = [] | |
isbn = input() | |
while isbn.isdigit(): | |
isbnList.append(isbn) | |
isbn = input() | |
prices = get_all_prices(isbnList) | |
for isbn in prices: | |
print("ISBN: {0}, Price: ${1}, Buyer: {2}".format(isbn, prices[isbn][0], prices[isbn][1])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment