Last active
October 9, 2018 08:33
-
-
Save tai271828/4e1a525581a681cea19a69eb884ce4f7 to your computer and use it in GitHub Desktop.
A campsaver crawler to watch the price of the commodity you want to have
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
#!/usr/bin/env python3 | |
# | |
# Copyright 2017, Taihsiang Ho <[email protected]> | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright notice, | |
# this list of conditions and the following disclaimer. | |
# | |
# 2. Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# | |
# 3. Neither the name of the copyright holder nor the names of its contributors | |
# may be used to endorse or promote products derived from this software without | |
# specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
# | |
# | |
# Mail service should only be run from a system with postfix installed. | |
# e.g. sudo apt-get install postfix on Ubuntu Xenial | |
# | |
import logging | |
import requests | |
from bs4 import BeautifulSoup | |
# For mail notification service | |
import os | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
CAMPSAVER_URL = 'https://www.campsaver.com/' | |
ITEM_SOTO_OD_1RX = 'soto-od-1rx-windmaster-stove-with-4-flex-pot-support.html' | |
THRESHOLD_PRICE = 55.0 | |
THRESHOLD_PERCENTAGE = 0.75 | |
SENT_FROM = '[email protected]' | |
SENT_TO = '[email protected]' | |
SMTPSERVER = os.environ.get('SMTP_SERVER', 'localhost') | |
ERROR_MSG = """ | |
ERROR! | |
The email for this job was supposed to be located in {} but that file | |
wasn't generated for some reason. This almost never happens, so | |
look for something farther up in the job related to this subject for | |
clues as to what might have happened | |
""" | |
format_str = "[ %(funcName)s() ] %(message)s" | |
logging.basicConfig(level=logging.INFO, format=format_str) | |
def mailit(subject, content_text, sent_from, sent_to): | |
msg = MIMEMultipart() | |
body = MIMEText(content_text) | |
msg.attach(body) | |
msg['Subject'] = subject | |
msg['From'] = sent_from | |
msg['To'] = sent_to | |
s = smtplib.SMTP(SMTPSERVER) | |
s.send_message(msg) | |
s.quit() | |
def is_threshold_price(source, target): | |
logging.debug("source is %f" % source) | |
logging.debug("target is %f" % target) | |
if source < target or source == target: | |
return True | |
else: | |
return False | |
def is_threshold_percentage(original, sale, target): | |
sale_percentage = float(sale) / original | |
logging.debug(sale_percentage) | |
logging.debug(target) | |
return is_threshold_price(sale_percentage, target) | |
def is_threshold(commodity): | |
price_original = commodity['price_original'] | |
price_sale = commodity['price_sale'] | |
flag_price = is_threshold_price(price_sale, THRESHOLD_PRICE) | |
logging.debug("flag_price is %r" % flag_price) | |
flag_percentage = is_threshold_percentage(price_original, price_sale, THRESHOLD_PERCENTAGE) | |
logging.debug("flag_percentage is %r" % flag_percentage) | |
return flag_price or flag_percentage | |
def get_web_page(url, item_entry): | |
resp = requests.get(url + item_entry) | |
if resp.status_code != 200: | |
print('Invalid url:', resp.url) | |
return None | |
else: | |
return resp.text | |
def get_commodity_info(dom): | |
soup = BeautifulSoup(dom, 'html5lib') | |
commodity = dict() | |
commodity['name'] = soup.title.text.strip() | |
prices_dict = soup.find(id='fancy-options').span.attrs | |
try: | |
commodity['price_original'] = float(prices_dict['data-variant-price']) | |
except ValueError: | |
logging.warning('data-variant-price is not found. Try content.') | |
commodity['price_original'] = float(prices_dict['content']) | |
commodity['price_sale'] = float(prices_dict['data-variant-sale']) | |
# commodity['current_price'] = soup.find(id='fancy-options').span.contents[1].contents[0].split('$')[1] | |
commodity['notification'] = is_threshold(commodity) | |
return commodity | |
if __name__ == '__main__': | |
page = get_web_page(CAMPSAVER_URL, ITEM_SOTO_OD_1RX) | |
if page: | |
commodity = get_commodity_info(page) | |
# show summary | |
logging.debug(commodity['name']) | |
for k, v in commodity.items(): | |
if k != 'name': | |
logging.debug("%s %s" % (k, v)) | |
# nofity the team if the price hits the targets | |
if commodity['notification']: | |
logging.info('Hit the target price. Mail notification.') | |
subject = commodity['name'] + ' hits the target price' | |
content_text = """ | |
Your commodity meets your expectation {} or {} of the original price. | |
Click the link to go to the commodity page: | |
""" | |
content_text += CAMPSAVER_URL + ITEM_SOTO_OD_1RX | |
content_text.format(THRESHOLD_PRICE, THRESHOLD_PERCENTAGE) | |
mailit(subject, content_text, SENT_FROM, SENT_TO) | |
else: | |
logging.info('Does not hit the target price. Do nothing.') | |
else: | |
# if there is no page, nofity maintainers | |
logging.warning('No page. Mail notification.') | |
mailit("No Campsaver watching page", CAMPSAVER_URL + ITEM_SOTO_OD_1RX, SENT_FROM, SENT_TO) | |
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
html5 | |
requests | |
bs4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment