def getcommand(btngrp): global scode global task btn = btngrp.checkedButton() c = btn.toolTip() btn.setChecked(False) userid = user.getuser() t = c.split(" ") scode = t[1] dealui.detail.setText(c) if t[0] == "buy": show() task = 1 elif t[0] == "sell": conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute( ''' select * from investment where user_id=? and stock_code=? ''', (userid, scode)) r = cmd.fetchall() conn.close() if len(r) != 0: show() task = 2 else: dealui.notify.setText("no stocks to sell") task = 0 hid()
def delaccount(): userid = user.getuser() user.userlogout() conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute(''' delete from users where user_id=? ''', (userid, )) cmd.execute( ''' delete from investment where user_id=? ''', (userid, )) cmd.execute( ''' delete from user_status where userid=? ''', (userid, )) cmd.execute( ''' delete from user_phone where user_id=? ''', (userid, )) cmd.execute( ''' delete from gen_account where user_id=? ''', (userid, )) cmd.execute( ''' delete from pro_account where user_id=? ''', (userid, )) conn.close() os.remove("userimg/" + str(userid) + ".jpg") user.changeuserbar(homeui) homeui.status.setText("account deleted successfully") exitb()
def sell(): global scode userid = user.getuser() try: num = int(dealui.amount.text()) except: num = 0 dealui.notify.setText("enter valid value.") return False conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute( ''' select * from users where user_id=? ''', (userid, )) userfund = cmd.fetchone()[6] cmd.execute( ''' select * from stock_live where stock_code=? ''', (scode, )) price = cmd.fetchone()[1] cmd.execute( ''' select * from investment where user_id=? and stock_code=? ''', (userid, scode)) r = cmd.fetchall() if len(r) == 0: return False else: count = int(r[0][2]) if num > count: dealui.notify.setText("not enough stock to sell") return False elif num < count: cmd.execute( ''' update investment set count=? where user_id=? and stock_code=? ''', (count - num, userid, scode)) elif num == count: cmd.execute( ''' delete from investment where user_id=? and stock_code=? ''', (userid, scode)) userfund = userfund + num * price cmd.execute( ''' update users set fund=? where user_id=? ''', (userfund, userid)) conn.close() hid() dealui.notify.setText("successfully sold stocks") loadtable() return True
def onFollow(event): """ 使用者【關注】事件 """ print("使用者關注", event, flush=True) myuser = user.getuser(event.source.user_id) myuser.state = 1 message = response.generate_response_from_directories('init') linebot_api.reply_message(event.reply_token, message)
def showprofile(hui): global homeui homeui = hui homeui.hide() conn = sqlite3.connect("database.db") userloggedid = user.getuser() with conn: cmd = conn.cursor() cmd.execute( ''' select * from users where user_id=? ''', (userloggedid, )) r = cmd.fetchone() cmd.execute( ''' select * from user_phone where user_id=? ''', (userloggedid, )) p = cmd.fetchall() conn.close() profileui.delbutton.setText("delete account") profileui.fname.setText(r[1]) profileui.lname.setText(r[2]) profileui.email.setText(r[3]) profileui.userid.setText(r[0]) profileui.acctype.setText(r[5]) if r[5] == "general": profileui.price.setText(user.perstockprice + " per stock") elif r[5] == "professional": profileui.price.setText(user.peryearprice + " per year") phonep = p[0][1] phones = p[1][1] profileui.phonep.setText(phonep) profileui.phones.setText(phones) profileui.confirmdel.hide() profileui.delbutton.show() profileui.confirmdelbutton.hide() profileui.confirmdel.setAlignment(QtCore.Qt.AlignCenter) imgname = "userimg/" + str(userloggedid) + ".jpg" profileui.imgbox.setStyleSheet("background:url(" + imgname + ");") profileui.canceldel.hide() global profilebool if profilebool: profileui.confirmdelbutton.clicked.connect(delaccount) profileui.canceldel.clicked.connect(canceldel) profileui.delbutton.clicked.connect(deluser) profileui.logoutbutton.clicked.connect(logb) profileui.investment.clicked.connect(invest) profileui.homebutton.clicked.connect(exitb) profilebool = False profileui.show()
def loaduserstock(): u = user.getuser() conn = sqlite3.connect("database.db") inui.emailmsg.setText("") with conn: cmd = conn.cursor() cmd.execute( ''' select * from investment where user_id=? order by count desc ''', (u, )) r = cmd.fetchall() conn.close() tbl = inui.datatable tbl.setRowCount(len(r) + 1) tbl.setColumnCount(4) tbl.setItem(0, 0, QtWidgets.QTableWidgetItem("Stock Code")) tbl.setItem(0, 1, QtWidgets.QTableWidgetItem("Count")) tbl.setItem(0, 2, QtWidgets.QTableWidgetItem("Current Price")) tbl.setItem(0, 3, QtWidgets.QTableWidgetItem("Currency")) tbl.item(0, 0).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(0, 1).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(0, 2).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(0, 3).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(0, 0).setBackground(QtGui.QColor(181, 115, 81)) tbl.item(0, 1).setBackground(QtGui.QColor(181, 115, 81)) tbl.item(0, 2).setBackground(QtGui.QColor(181, 115, 81)) tbl.item(0, 3).setBackground(QtGui.QColor(181, 115, 81)) tbl.item(0, 0).setForeground(QtGui.QColor(255, 255, 255)) tbl.item(0, 1).setForeground(QtGui.QColor(255, 255, 255)) tbl.item(0, 2).setForeground(QtGui.QColor(255, 255, 255)) tbl.item(0, 3).setForeground(QtGui.QColor(255, 255, 255)) for i in range(1, len(r) + 1): d = r[i - 1] tbl.setItem(i, 0, QtWidgets.QTableWidgetItem(d[1])) tbl.setItem(i, 1, QtWidgets.QTableWidgetItem(str(d[2]))) tbl.setItem( i, 2, QtWidgets.QTableWidgetItem(str(stockdb.search_stock_price(d[1])))) tbl.setItem( i, 3, QtWidgets.QTableWidgetItem(str(stockdb.search_stock_curr(d[1])))) tbl.item(i, 0).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(i, 1).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(i, 2).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(i, 3).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(i, 0).setBackground(QtGui.QColor(171, 181, 164)) tbl.item(i, 1).setBackground(QtGui.QColor(149, 181, 157)) tbl.item(i, 2).setBackground(QtGui.QColor(149, 181, 157)) tbl.item(i, 3).setBackground(QtGui.QColor(149, 181, 157))
def mailstockdata(): try: u = user.getuseremail() msg = EmailMessage() msg['Subject'] = 'stock investment details' msg['From'] = user.clientemail msg['To'] = user.getuseremail() userid = user.getuser() conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute( ''' select * from investment where user_id=? ''', (userid, )) r = cmd.fetchall() conn.close() m = "your stock investment details are :\n\n\n" m = m + str("user id : " + userid + "\n\n") if len(r) == 0: m = "no stocks owned" else: for data in r: m = m + "stock code : " + str(data[1]) + "\n" cdetail = stockdb.search_company(data[1]) cname = cdetail[1] m = m + "company : " + cname + "\n" m = m + "no. of stock : " + str(data[2]) + "\n\n" msg.set_content(m) with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(user.clientemail, user.clientpassword) smtp.send_message(msg) inui.emailmsg.setText("details sent to your registered email") except: pass
def onPostback(event): """ 使用者【PostBack】事件 """ print("使用者【PostBack】事件", event, flush=True) myuser = user.getuser(event.source.user_id)
def onMessage(event): """ 使用者【文字訊息】事件 """ # # print(event, flush=True) # 拿使用者資料、傳入response的參數 myuser = user.getuser(event.source.user_id) msg_message = '' msg_attachment_path = '' # 判斷附件 attachmentExt = '' # 附件的附檔明 if isinstance(event.message, TextMessage): msg_message = str(event.message.text) print('文字訊息:', msg_message, flush=True) elif isinstance(event.message, ImageMessage): attachmentExt = 'jpg' print('圖片訊息', flush=True) elif isinstance(event.message, VideoMessage): attachmentExt = 'mp4' print('影片訊息', flush=True) elif isinstance(event.message, AudioMessage): attachmentExt = 'm4a' print('聲音訊息', flush=True) # 下載附件 # 拿訊息 if attachmentExt: message_content = linebot_api.get_message_content(event.message.id) # 下載 (暫暫存) with tempfile.NamedTemporaryFile(dir=fileutil.dir_temp, prefix=attachmentExt+'-', delete=False) as tf: for chunk in message_content.iter_content(): tf.write(chunk) tempfile_path = tf.name # 加上副檔名 msg_attachment_path = tempfile_path + '.' + attachmentExt os.rename(tempfile_path, msg_attachment_path) # 製作回覆 if msg_message == 'reset': myuser.state = 0 message = TextSendMessage(text='已重設你的狀態state') elif msg_message == 'checkstate': message = TextSendMessage(text='你的狀態碼:'+str(myuser.state)+' \nUID:'+str(myuser.uid)) elif msg_message == 'version': message = TextSendMessage(text='LineBot-ElderlyPicGenerator '+str(version)) # elif msg_message == 'tmpeep110': #when demo, remove this # # upload rich menu ppic, before this, register ruch menu first # with open( os.path.join(fileutil.dir_resp, 'richmenu_state110', 'v2.jpg') , 'rb') as f: # linebot_api.set_rich_menu_image('richmenu-87d4b4dbe02db04127c03ca06f5b9ba7', 'image/jpeg', f) # message = TextSendMessage(text='ok') # elif msg_message == 'tmpeep112': #when demo, remove this # # upload rich menu ppic, before this, register ruch menu first # with open( os.path.join(fileutil.dir_resp, 'richmenu_state112', 'v2.jpg') , 'rb') as f: # linebot_api.set_rich_menu_image('richmenu-063fc5e646c13b50d27811df86d7c647', 'image/jpeg', f) message = TextSendMessage(text='ok') else: message = response.determine_response(myuser, msg_message, msg_attachment_path, attachmentExt) # 發送回覆 linebot_api.reply_message(event.reply_token, message) # 掛上 Rich Menu attach_richmenu_id = response.determine_attach_rich_menus(myuser) if attach_richmenu_id: linebot_api.link_rich_menu_to_user(event.source.user_id, attach_richmenu_id) print('掛上Rich Menu', attach_richmenu_id, flush=True) else: linebot_api.unlink_rich_menu_from_user(event.source.user_id) return;
def buy(): global scode userid = user.getuser() try: num = int(dealui.amount.text()) except: num = 0 dealui.notify.setText("enter value.") return False conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute( ''' select * from users where user_id=? ''', (userid, )) userfund = cmd.fetchone()[6] cmd.execute( ''' select * from stock_live where stock_code=? ''', (scode, )) price = cmd.fetchone()[1] if (userfund < (price * num)): dealui.notify.setText("not enough fund") hid() return False else: userfund = userfund - price * num with conn: cmd = conn.cursor() cmd.execute( ''' select * from investment where user_id=? and stock_code=? ''', (userid, scode)) r = cmd.fetchall() if len(r) == 0: cmd.execute( ''' insert into investment(user_id,stock_code,count) values(?,?,?) ''', (userid, scode, num)) else: count = int(r[0][2]) cmd.execute( ''' update investment set count=? where user_id=? and stock_code=? ''', (count + num, userid, scode)) cmd.execute( ''' update users set fund=? where user_id=? ''', (userfund, userid)) conn.close() hid() dealui.notify.setText("successfully bought stocks") loadtable() return True
def loadtable(): tbl = dealui.stocktable userid = user.getuser() conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute(''' select * from stock_live order by stock_code ''') d = cmd.fetchall() conn.close() n = len(d) tbl.setRowCount(n + 1) tbl.setColumnCount(7) tbl.setItem(0, 0, QtWidgets.QTableWidgetItem("stock code")) tbl.setItem(0, 1, QtWidgets.QTableWidgetItem("current price")) tbl.setItem(0, 2, QtWidgets.QTableWidgetItem("abs change")) tbl.setItem(0, 3, QtWidgets.QTableWidgetItem("per change")) tbl.setItem(0, 4, QtWidgets.QTableWidgetItem("buy")) tbl.setItem(0, 5, QtWidgets.QTableWidgetItem("sell")) tbl.setItem(0, 6, QtWidgets.QTableWidgetItem("own")) for i in range(0, 7): tbl.item(0, i).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(0, i).setBackground(QtGui.QColor(181, 115, 81)) tbl.item(0, i).setForeground(QtGui.QColor(255, 255, 255)) btn_grp = QButtonGroup() btn_grp.setExclusive(True) for i in range(0, n): for j in range(0, 4): tbl.setItem(i + 1, j, QtWidgets.QTableWidgetItem(str(d[i][j]))) tbl.item(i + 1, j).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(i + 1, j).setBackground(QtGui.QColor(149, 181, 157)) tbl.item(i + 1, 0).setBackground(QtGui.QColor(171, 181, 164)) conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute( ''' select * from deal where stock_code=? ''', (d[i][0], )) option = cmd.fetchall() conn.close() buyoption = option[0][1] selloption = option[0][2] btn = QPushButton() btn.setText('buy') btn.setStyleSheet( "background-color: rgb(255, 87, 98);font-size:20px;font-variant:small-caps;" ) btn.setToolTip("buy " + d[i][0]) btn.setCheckable(True) btn.setChecked(False) btn.clicked.connect(lambda: getcommand(btn_grp)) if buyoption == 1: tbl.setCellWidget(i + 1, 4, btn) else: tbl.setCellWidget(i + 1, 4, btn) btn.setDisabled(True) btn_grp.addButton(btn) btn = QPushButton() btn.setText('sell') btn.setStyleSheet( "background-color:rgb(90, 181, 70);font-size:20px;font-variant:small-caps;" ) btn.setToolTip("sell " + d[i][0]) btn.setCheckable(True) btn.setChecked(False) btn.clicked.connect(lambda: getcommand(btn_grp)) if selloption == 1: tbl.setCellWidget(i + 1, 5, btn) else: tbl.setCellWidget(i + 1, 5, btn) btn.setDisabled(True) btn_grp.addButton(btn) conn = sqlite3.connect("database.db") with conn: cmd = conn.cursor() cmd.execute( ''' select count from investment where user_id=? and stock_code=? ''', (userid, d[i][0])) c = cmd.fetchall() conn.close() if len(c) == 0: scount = 0 else: scount = c[0][0] tbl.setItem(i + 1, 6, QtWidgets.QTableWidgetItem(str(scount))) tbl.item(i + 1, 6).setTextAlignment(QtCore.Qt.AlignCenter) tbl.item(i + 1, 6).setBackground(QtGui.QColor(253, 174, 95))