Created
December 18, 2014 10:51
-
-
Save pankaj28843/30a0100d660029bae00d to your computer and use it in GitHub Desktop.
price alert flipkart
This file contains hidden or 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/python2.7 | |
import re | |
import requests | |
from envelopes import Envelope, SMTP | |
from lxml import etree | |
from splinter import Browser | |
REGEX_FIND_NUMBER = re.compile(r'\d*\.\d+|\d+') | |
REGEX_FIND_COMMA = re.compile(r'\s*,\s*') | |
AWS_SES_SMTP_SERVER = SMTP( | |
host='email-smtp.us-east-1.amazonaws.com', | |
port=587, | |
login='', | |
password='', | |
tls=True, | |
) | |
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36" | |
PHANTOM_JS_SERVICE_ARGS = [ | |
'--proxy=127.0.0.1:8123', | |
'--proxy-type=http', | |
] | |
def get_price_from_text(text): | |
# strip the text | |
text = text.strip() | |
# remove comma | |
text = REGEX_FIND_COMMA.sub('', text) | |
# find decimal string | |
decimals = REGEX_FIND_NUMBER.findall(text) | |
# price string should be first one | |
price_string = decimals[0] | |
# convert to float and return | |
return float(price_string) | |
def get_text_for_etree_node(node): | |
# get text for current node | |
text = node.text or '' | |
# get tail for current node | |
tail = node.tail or '' | |
# get text for all children | |
children_text = ''.join(map(get_text_for_etree_node, node)) | |
return text + children_text + tail | |
def get_price_from_amazon(url): | |
req = requests.get(url) | |
root = etree.HTML(req.text) | |
target_element = root.find(".//span[@id='priceblock_ourprice']") | |
target_element_text = get_text_for_etree_node(target_element) | |
price = get_price_from_text(target_element_text) | |
return price | |
def get_price_from_flipkart(browser, url): | |
JAVASCRIPT_CODE_EXTRACT_PRICES = """ | |
function getElementsByClassName(class_name) { | |
var elements = document.getElementsByClassName("selling-price"); | |
var results = new Array(); | |
for (var i = 0; i < elements.length; i++) { | |
results.push(elements[i]); | |
} | |
return results; | |
} | |
function getElementsByXpath(xpath) { | |
var results = document.evaluate( | |
xpath, | |
document, | |
null, | |
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, | |
null | |
); | |
var elements = [] | |
for (var J = 0; J < results.snapshotLength; ++J) { | |
elements.push(results.snapshotItem(J)); | |
} | |
return elements; | |
} | |
// SELLING_PRICES = getElementsByClassName("selling-price").map(function(x){return x.textContent}); | |
SELLING_PRICES = getElementsByXpath('//*[@data-omnifield="eVar48"]').map(function(x) { | |
return x.textContent | |
}); | |
""" | |
browser.visit(url) | |
browser.execute_script(JAVASCRIPT_CODE_EXTRACT_PRICES) | |
SELLING_PRICES = browser.evaluate_script("""SELLING_PRICES""") | |
return min(map(get_price_from_text, SELLING_PRICES)) | |
def send_email_for_price_alert(website_name, url, price, item_name): | |
envelope = Envelope( | |
from_addr=(u'[email protected]', u'Price Alert'), | |
to_addr=(u'[email protected]', u'Your name'), | |
subject=u'Price Alert: {} - INR {}/- - {}'.format(website_name, price, item_name), | |
text_body=u""" | |
Item - {item_name}. | |
Price - INR {price}/- | |
URL - {url} | |
- | |
Sent using Price Alert Python Script | |
""".format(item_name=item_name, price=price, url=url) | |
) | |
AWS_SES_SMTP_SERVER.send(envelope) | |
FLIPKART_WATCH_LIST = [ | |
[ | |
"http://www.flipkart.com/lenovo-thinkpad-e431-notebook-3rd-gen-ci7-4gb-500gb-win8-2gb-graph-touch/p/itmdxsvwgjfww7ds", | |
"Lenovo Thinkpad E431 Notebook (3rd Gen Ci7/ 4GB/ 500GB/ Win8/ 2GB Graph/ Touch)(Black)", | |
50000, | |
] | |
] | |
if __name__ == "__main__": | |
browser = Browser( | |
"phantomjs", | |
user_agent=USER_AGENT, | |
service_args=PHANTOM_JS_SERVICE_ARGS, | |
) | |
for (url, item_name, min_price) in FLIPKART_WATCH_LIST: | |
price = get_price_from_flipkart(browser, url) | |
website_name = "Flipkart.com" | |
if price <= min_price: | |
send_email_for_price_alert(website_name, url, price, item_name) | |
browser.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import error. no module named 'requests' .help me out of this error