Originally Posted by
noidea
Completely forgot about that, sorry :S
I havent encountered that yet, but I'll fix it.
edit: Ok, working on stopping the threads atm because that seems to be the biggest issue. Just fixed the urllib exception.
python Code:
# -*- coding: utf-8 -*-
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="lucas"
__date__ ="$Nov 24, 2010 11:52:39 PM$"
import os
import urllib
import threading
import time
import Queue
import subprocess
import sys
shutdown_event = threading.Event()
class NeweggCheck(threading.Thread):
def __init__(self, url, timecheck=30):
threading.Thread.__init__(self)
self.url, self.timecheck = url, timecheck
def run(self):
print 'thread started', self.url
price = 0
while not shutdown_event.is_set():
#read the page into a string list
try:
lines = urllib.urlopen(self.url).readlines()
except (KeyboardInterrupt, SysExit):
pass
except:
time.sleep(self.timecheck)
continue
# The line to search for in the website's code
# line_catch = 'onclick="javascript:Biz.Product.DetailPage.switchCrossSellItem(this,\''
line_catch = 'Biz.Product.DetailPage.switchCrossSellItem(this,\''
# Check each line for that line above
found = False
for line in lines:
if line.find(line_catch, 0, len(line)-1) != -1: # Searches for the phrase from within the line
print line[line.find("'$")+1: line.find('\',')], self.url# Gets the price of the item from the line
price_string = line[line.find("'$")+1: line.find('\',')]
price_float = float(price_string[1:])
if price != price_float:
# add the speech into the speech queue
speechThread.add_speech("'the. prices. have. updated. . . It. is. now. %s'" % price_string)
print 'Price of', self.url, 'has changed'
price = price_float
f = open('newegg.txt', 'a')
f.write(price_string+' '+self.url+'\n')
f.close()
found = True
break
if not found:
print 'POSSIBLE PRICE CHANGE, CANNOT FIND PRICE', self.url
speechThread.add_speech("espeak 'possible price. change'")
wait_time = time.time() + self.timecheck
while time.time() < wait_time and not shutdown_event.is_set():
time.sleep(0.016)
class SpeechQueue(threading.Thread):
'''
Thread to make speech un-garbled.
'''
def __init__(self,min_wait=0.016):
threading.Thread.__init__(self)
self.queue = Queue.Queue()
if min_wait < 0.016:
self.min_wait = 0.016
else:
self.min_wait = min_wait
def add_speech(self, speech):
'''
Adds the speech into the queue to be spoken later.
'''
self.queue.put(speech)
def run(self):
print('started speech thread ..')
while not shutdown_event.is_set():
if not self.queue.empty():
words = self.queue.get()
# create subprocess to speak.
subprocess.call(['espeak', "'%s'"%words])
time.sleep(self.min_wait)
if __name__ == "__main__":
urls = ['http://www.newegg.com/Product/Product.aspx?Item=N82E16822136533',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16820148357',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16812123282',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16814500115',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16820231310',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16823201033',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16826125108',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16827135204']
time_between = 20 * 60 # amount in seconds to wait before checking the site again.
thread_list = []
# start the speech thread
speechThread = SpeechQueue()
speechThread.start()
#start the price check threads
for url in urls:
thread_list.append(NeweggCheck(url, timecheck=time_between))
thread_list[len(thread_list)-1].start()
try:
while True:
time.sleep(0.016)
except (KeyboardInterrupt, SystemExit):
shutdown_event.set()
while speechThread.is_alive():
time.sleep(0.016)
print("speech thread killed.")
for th in thread_list:
while th.is_alive():
time.sleep(0.016)
print("killed all price threads.")
print("Application Terminated successfully")
sys.exit(0)
Threads are all killed gracefully and the application exits nice!
E: More thread-safe version:
python Code:
# -*- coding: utf-8 -*-
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="lucas"
__date__ ="$Nov 24, 2010 11:52:39 PM$"
import os
import urllib
import threading
import time
import Queue
import subprocess
import sys
shutdown_event = threading.Event()
class NeweggCheck(threading.Thread):
def __init__(self, url, timecheck=30):
threading.Thread.__init__(self)
self.url, self.timecheck = url, timecheck
def sleep(self, sleep_time):
'''
Sleeps the thread, but also checks for the shutdown signal. sleep_time is in
seconds.
'''
wait_time = time.time() + (sleep_time * 1000)
while time.time() < wait_time and not shutdown_event.is_set():
time.sleep(0.016)
def run(self):
print 'thread started', self.url
price = 0
while not shutdown_event.is_set():
#read the page into a string list
try:
lines = urllib.urlopen(self.url).readlines()
except (KeyboardInterrupt, SysExit):
pass
except:
self.sleep(self.timecheck)
continue
# The line to search for in the website's code
# line_catch = 'onclick="javascript:Biz.Product.DetailPage.switchCrossSellItem(this,\''
line_catch = 'Biz.Product.DetailPage.switchCrossSellItem(this,\''
# Check each line for that line above
found = False
for line in lines:
if line.find(line_catch, 0, len(line)-1) != -1: # Searches for the phrase from within the line
print line[line.find("'$")+1: line.find('\',')], self.url# Gets the price of the item from the line
price_string = line[line.find("'$")+1: line.find('\',')]
price_float = float(price_string[1:])
if price != price_float:
# add the speech into the speech queue
speechThread.add_speech("'the. prices. have. updated. . . It. is. now. %s'" % price_string)
print 'Price of', self.url, 'has changed'
price = price_float
f = open('newegg.txt', 'a')
f.write(price_string+' '+self.url+'\n')
f.close()
found = True
break
if not found:
print 'POSSIBLE PRICE CHANGE, CANNOT FIND PRICE', self.url
speechThread.add_speech("espeak 'possible price. change'")
self.sleep(self.timecheck)
class SpeechQueue(threading.Thread):
'''
Thread to make speech un-garbled.
'''
def __init__(self,min_wait=0.016):
threading.Thread.__init__(self)
self.queue = Queue.Queue()
if min_wait < 0.016:
self.min_wait = 0.016
else:
self.min_wait = min_wait
def add_speech(self, speech):
'''
Adds the speech into the queue to be spoken later.
'''
self.queue.put(speech)
def run(self):
print('started speech thread ..')
while not shutdown_event.is_set():
if not self.queue.empty():
words = self.queue.get()
# create subprocess to speak.
subprocess.call(['espeak', "'%s'"%words])
time.sleep(self.min_wait)
if __name__ == "__main__":
urls = ['http://www.newegg.com/Product/Product.aspx?Item=N82E16822136533',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16820148357',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16812123282',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16814500115',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16820231310',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16823201033',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16826125108',
'http://www.newegg.com/Product/Product.aspx?Item=N82E16827135204']
time_between = 20 * 60 # amount in seconds to wait before checking the site again.
thread_list = []
# start the speech thread
speechThread = SpeechQueue()
speechThread.start()
#start the price check threads
for url in urls:
thread_list.append(NeweggCheck(url, timecheck=time_between))
thread_list[len(thread_list)-1].start()
try:
while True:
time.sleep(0.016)
except (KeyboardInterrupt, SystemExit):
shutdown_event.set()
while speechThread.is_alive():
time.sleep(0.016)
print("speech thread killed.")
for th in thread_list:
while th.is_alive():
time.sleep(0.016)
print("killed all price threads.")
print("Application Terminated successfully")
sys.exit(0)
E2: Fixed a small math issue..