Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # send_sms and send_email are also available from http://pastebin.com/u/ChrisProsser
- import os, sys, urllib, datetime as dt, time, send_sms, send_mail, traceback
- # globals
- # example here checking for availability of Google Nexus 4, change globals in <>
- products = ['Nexus 4 - 16GB', 'Nexus 4 - 8GB']
- # if an entry in the check strings is found in the html then it is found to be out of stock
- check_strings = ['We are out of stock',
- 'This device is not for sale at this time',
- 'main_page_js_compiled_main_page_js__en_gb.js']
- urls = ['https://play.google.com/store/devices/details/Nexus_4_16_GB?id=nexus_4_16gb&hl=en_GB',
- 'https://play.google.com/store/devices/details/Nexus_4_8_GB?id=nexus_4_8gb&hl=en_GB']
- program = os.path.split(sys.argv[0])[1]
- # email account to send from (gmail for this email module) with base 64 encoded pwd
- email_recipient = '<email_recipient@whoever>'
- # use details from send_sms account
- sms_account, sms_sender = '<account_name>', '<sender>'
- mobile_nos = '447504750054' #string with no spaces, comma separated
- log_file_path = 'prod_available_log.csv'
- def log(param, progress, exit=False):
- log_file = open(log_file_path, 'a')
- info = ', '.join((program, dt.datetime.now().strftime("%Y-%m-%d"),
- dt.datetime.now().strftime("%H:%M:%S"), param, progress, '\n'))
- log_file.write(info)
- if exit:
- sys.exit(progress)
- log_file.close()
- def check(product_name, url):
- try:
- html = urllib.urlopen(url).read()
- except:
- if logging_on:
- err = 'Error opening URL'
- log(product_name, err, True)
- in_stock = True
- for entry in check_strings:
- if entry in html or not html:
- in_stock = False
- if not in_stock:
- log(product_name, 'Out of stock')
- else:
- try:
- msg = product_name + ' may be available now, check on ' + url
- send_sms.main(msg, mobile_nos, sms_account, sms_sender)
- except:
- exc_type, exc_value, exc_traceback = sys.exc_info()
- lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
- err = 'Error sending email: ' + ''.join('!! ' + line for line in lines)
- log(product_name, err, True)
- else:
- log(product_name, 'In stock! SMS sent.')
- try:
- f = open('prod_available.html', 'wb')
- f.write(html)
- except:
- pass
- # break loop once product is available
- return True
- return False
- def send_update():
- subject = 'Daily ' + program + ' update'
- try:
- f = open(log_file_path, 'rb')
- data = [line for line in f]
- lines = len(data)
- body = str(lines) + ' lines of data in log file so far, here are the last 10 lines:<br/>'
- body += '<br/>'.join([line for line in data[-10:]])
- f.close()
- except:
- exc_type, exc_value, exc_traceback = sys.exc_info()
- lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
- body = 'Error getting email body... ' + ''.join('!! ' + line for line in lines)
- try:
- send_mail.mail(email_sender, enc_pwd, email_recipient, subject, body, None)
- except:
- exc_type, exc_value, exc_traceback = sys.exc_info()
- lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
- err = 'Error sending update email!: ' + ''.join('!! ' + line for line in lines)
- log(product_name, err, True)
- def main():
- # set to pause for 2 mins after each run...
- finished, delay, loops = False, 120, 0
- update_interval = 86400 / delay
- while not finished:
- loops += 1
- for i in range(len(urls)):
- finished = check(products[i], urls[i])
- if loops == 1:
- send_update()
- elif loops >= update_interval:
- loops = 0
- time.sleep(delay)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement