class CompanyPrice: def __init__(self): self.sinaApi = 'http://hq.sinajs.cn/list=%s' self.db = DbConn() def getPriceInfo(self, code): r = random.uniform(0, 3) time.sleep(r) url = self.sinaApi % (code) page = Crawler.getPage(url, "gbk") tmp = page.split('"') return tmp[1].split(',') def getCompanyList(self): sql = "select code,type from company;" sqlIn = """insert into price(companyid,price,createtime) values('%s','%s','%s')""" self.db.execute(sql) res = self.db.getRes() for i in res: if 'sh' == i[1]: post = 'sh' + i[0] else: post = 'sz' + i[0] priceInfo = self.getPriceInfo(post) if len(priceInfo) < 31: print i[0] + " failed!" continue currentPrice = priceInfo[3] currentTime = priceInfo[30] cmd = sqlIn % (i[0],currentPrice,currentTime) self.db.execute(cmd) print i[0], currentPrice, currentTime
class StockList: def __init__(self, url): self.url = url self.db = DbConn() def close(self): self.db.close() def getIndexPage(self): return Crawler.getPage(self.url, "gbk") def getResult(self, res): arr = [] for item in res: tmp = item.text l = len(tmp) name = tmp[:l - 8].replace(' ', '') code = tmp[l - 7:l - 1].replace(' ', '') arr.append((name, code)) return arr def getCompany(self): page = self.getIndexPage() sql = """insert into company(code,name,type) values('%s','%s','%s')""" soup = BeautifulSoup(page) liangshi = soup.findAll('div', attrs={'class': 'result'}) hushi = liangshi[0].find('ul').findAll('li') shenshi = liangshi[1].find('ul').findAll('li') chuangye = liangshi[2].find('ul').findAll('li') arr = self.getResult(hushi) for i in arr: cmd = sql % (i[1], i[0], 'sh') self.db.execute(cmd) arr = self.getResult(shenshi) for i in arr: cmd = sql % (i[1], i[0], 'sz') self.db.execute(cmd) arr = self.getResult(chuangye) for i in arr: cmd = sql % (i[1], i[0], 'cz') self.db.execute(cmd)
def __init__(self, url): self.url = url self.db = DbConn()
self.q_lock = threading.Lock() # lock for q sync self.db_conn = db # connection to database self.is_armed = False # is system armed? self.was_alert = False # comes lukes code on run everything return self.is_active_incident = False # is there an active alert self.is_max_alert = False # is it already escalated / high alert self.is_panic = False # was this a panic button alert self.record_incident = False # should i record incident? self.response_received = False self.sound_pid = -1 if __name__ == '__main__': # global shared resources message_q = deque() db_connection = DbConn(Constant.host, Constant.uname, Constant.password, Constant.db_name) shared_resources = Driver(message_q, db_connection) pi_client = Client(Constant.host_ip, Constant.port, shared_resources) pi_system_thread = ClientThread(pi_client, shared_resources) pi_system_thread.start() message_handler_thread = MessageHandlerThread(shared_resources) logic_handler_thread = LogicHandlerThread(shared_resources) print("threads created in Driver") message_handler_thread.start() print("started message handler thread") # comment out if you do not have sensors logic_handler_thread.start() print("started logic handler thread")
def __init__(self): self.sinaApi = 'http://hq.sinajs.cn/list=%s' self.db = DbConn()
from flask import Flask, render_template from DbConn import DbConn app = Flask(__name__) dbConn = DbConn() @app.route('/') def index(): # dbConn.generate_all_hotels_for_prices() return render_template('index.html') @app.route('/get_all_hotels_prices/', methods=['GET', 'POST']) def get_all_hotels_prices(): return dbConn.get_all_hotels_prices() @app.route('/get_all_hotels/', methods=['GET', 'POST']) def get_all_hotels(): return dbConn.get_all_hotels() @app.route('/get_near_bus/<lat>/<lon>/', methods=['GET', 'POST']) def get_near_bus(lat, lon): return dbConn.get_near_bus(lat, lon) @app.route('/get_near_bus2/<lat>/<lon>/', methods=['GET', 'POST']) def get_near_bus2(lat, lon): return dbConn.get_near_bus2(lat, lon) @app.route('/get_near_road/<lat>/<lon>/', methods=['GET', 'POST']) def get_near_road(lat, lon):