Posted By

ahawker on 01/13/10


Tagged

python mechanize


Versions (?)

eCost Bot


 / Published in: Python
 

eCost Bot. Attempts to win bids on eCost.com "outrageous offers" sales.

  1. import time, sys, re
  2. from threading import Thread, Event
  3. from mechanize import Browser, LinkNotFoundError
  4.  
  5. base_url = 'http://www.ecost.com/'
  6. countdown_url = '%sCountdown.aspx?size=9999999' % (base_url)
  7. checkout_url = '%sCheckout/eCost/Checkout.aspx' % (base_url)
  8. cart_url = '%sShoppingCart.aspx?action=view' % (base_url)
  9.  
  10. class BotManager:
  11. def __init__(self):
  12. self.event = Event()
  13. self.bots = []
  14.  
  15. def add_bots(self, bots):
  16. for b in bots:
  17. self.bots.append(b)
  18.  
  19. def start(self, time_offset=5):
  20. for b in self.bots:
  21. b.go(self.event)
  22. time.sleep(time_offset)
  23. for b in self.bots:
  24. b.join()
  25.  
  26.  
  27. class Bot(Thread):
  28. def __init__(self, usr, pwd, pid, sale_cost, notification_email=None, \
  29. purchase_item=None, max_iterations=sys.maxint, timeout=30):
  30.  
  31. self.login = usr
  32. self.password = pwd
  33. self.product_id = pid
  34. self.sale_cost = sale_cost
  35. self.notification_email = notification_email
  36. self.purchase_item = purchase_item
  37. self.max_iterations = max_iterations
  38. self.timeout = timeout
  39.  
  40. self.browser = Browser()
  41. Thread.__init__(self)
  42. print "Initializing eCost Bot with login name: %s." % (self.login)
  43.  
  44. def go(self, event):
  45. self.event = event
  46. self.start()
  47.  
  48. def run(self):
  49. product_found = self._product_search(self.product_id, self.max_iterations, self.timeout)
  50.  
  51. if not product_found:
  52. return
  53.  
  54. add_to_cart = self.browser.follow_link(url_regex=product_found.group(1))
  55. print "Product has been found and added to your shopping cart."
  56.  
  57. try:
  58. view_cart = self.browser.follow_link(url=cart_url)
  59. except LinkNotFoundError:
  60. pass
  61.  
  62. print "Navigating to checkout page..."
  63. checkout = self.browser.open(checkout_url)
  64.  
  65. print "Attempting to login as: %s." % (self.login)
  66. self._login()
  67.  
  68. if self.notification_email:
  69. self._send_notification_email(self.notification_email)
  70.  
  71. if not self.purchase_item:
  72. print "You have elected not to automatically purchase the item, \
  73. you must manually log into your eCost account to complete the transaction."
  74. return
  75.  
  76. print "should complete purchase steps here..."
  77. print "end"
  78.  
  79. def _send_notification_email(self, notification_email):
  80. import smtplib
  81. from email.mime.text import MIMEText
  82. body = "This is a message from your eCostAutobuyer that an item has \
  83. been added to the shopping cart of your account."
  84. msg = MIMEText(body)
  85. msg['Subject'] = "Notification: An item has been added to your eCost account"
  86. msg['From'] = 'email_to_send_notification_from'
  87. msg['To'] = notification_email
  88.  
  89. s = smtplib.SMTP('smtp.gmail.com:587')
  90. s.starttls()
  91. s.login('email_to_send_notification_from', 'password')
  92. s.sendmail('email_to_send_notification_from', [notification_email], msg.as_string())
  93. s.quit()
  94. print "A notification has been sent to %s" % (notification_email)
  95.  
  96.  
  97. def _product_search(self, product_id, max_iterations, timeout):
  98. print "Starting our product search for: %s" % (product_id)
  99. cart_regex = re.compile('<a.*%s.*\s+.*(\d{7,})\_.*.jpg' % (product_id))
  100.  
  101. self.browser.open(countdown_url)
  102.  
  103. iteration = 0
  104.  
  105. while not self.event.isSet():
  106. source = self.browser.response().read()
  107. found_item = cart_regex.search(str(source))
  108.  
  109. if iteration > max_iterations:
  110. break
  111.  
  112. if not found_item:
  113. time.sleep(timeout)
  114. iteration += 1
  115. self.browser.reload()
  116. continue
  117.  
  118. self.event.set()
  119. return found_item
  120. return None
  121.  
  122.  
  123. def _login(self):
  124. self.browser.select_form(name='aspnetForm')
  125. self.browser['ctl00$uxMainPlaceHolder$uxEmail'] = self.login
  126. self.browser['ctl00$uxMainPlaceHolder$uxPassword'] = self.password
  127. self.browser.submit()
  128. print "%s successfully logged in." % (self.login)
  129.  
  130.  
  131.  
  132. def main():
  133. bm = BotManager()
  134. b1 = Bot('username', 'password' 'pid', 'sale_cost', 'notification email address')
  135. b2 = Bot('username', 'password', 'pid', 'sale_cost', 'notification email address')
  136. bm.add_bots([b1,b2])
  137. bm.start()
  138.  
  139.  
  140. main()

Report this snippet  

You need to login to post a comment.