def send_messge_and_save(self, id=None, text=None, bot_name=None): if text and id and bot_name: if id not in self.log: self.log.append(id) filewriter.save_log_file(self.name, self.log) log.logger.info('New hotdeal has just been registerd. (%s)' % (id)) telegrambot.send_message(text, bot_name)
def end_report(self): duration = int((datetime.now() - self.starttime).total_seconds() / 60) log.logger.info( '[durations %d min] Instagram process has completed. FOLLOWER_CNT (%d),FOLLOWING_CNT (%d),FOLLOW_CNT (%d), LIKE_CNT (%d), REPLY_CNT (%d), FOLLOW_ACCEPT_CNT (%d), FOLLOWING_CANCEL_CNT (%d), FAIL_CNT (%d)' % (duration, self.FOLLOWER_CNT, self.FOLLOWING_CNT, self.FOLLOW_CNT, self.LIKE_CNT, self.REPLY_CNT, self.FOLLOW_ACCEPT_CNT, self.FOLLOWING_CANCEL_CNT, self.FAIL_CNT)) # 당분간 텔레그램으로 결과알림을 받자 telegrambot.send_message( '[durations %d min] Instagram process has completed. FOLLOWER_CNT (%d),FOLLOWING_CNT (%d),FOLLOW_CNT (%d), LIKE_CNT (%d), REPLY_CNT (%d), FOLLOW_ACCEPT_CNT (%d), FOLLOWING_CANCEL_CNT (%d), FAIL_CNT (%d)' % (duration, self.FOLLOWER_CNT, self.FOLLOWING_CNT, self.FOLLOW_CNT, self.LIKE_CNT, self.REPLY_CNT, self.FOLLOW_ACCEPT_CNT, self.FOLLOWING_CANCEL_CNT, self.FAIL_CNT), 'dev') self.FOLLOW_CNT = 0 self.LIKE_CNT = 0 self.REPLY_CNT = 0 self.FAIL_CNT = 0 self.REPLY = [] self.destroy() exit()
def save(self, id, text): if text and id: self.count = self.count + 1 telegrambot.send_message(text) if id not in self.log: self.log.append(id) filewriter.save_log_file(self.name, self.log)
def check_in(driver, userid): try: alert_container = WebDriverWait(driver, 3).until( EC.visibility_of_element_located( (By.XPATH, "//div[@class='sweet-alert showSweetAlert visible']")), "alert_container", ) # wait until 3 sec until alert is present # if present, execute code in this block # click "OK" button and logout alert_container.find_element( By.XPATH, "//button[@class='confirm'][normalize-space()='OK']").click() logger.debug("unable to check in because of warning") logger.info("%s unable to check in", userid) message = "Unable to check in because you are on leave" logout_app(driver, message) except (TimeoutException) as e: if e.msg == "alert_container": driver.find_element( By.XPATH, "//button[normalize-space()='Presensi Masuk']").click() alert_container = WebDriverWait(driver, 3).until( EC.visibility_of_element_located( (By.XPATH, "//div[@class='sweet-alert showSweetAlert visible']"))) # get alert message alert_message = alert_container.find_element(By.TAG_NAME, "h2").text if alert_message == "Anda Sudah Melakukan Presensi": logger.debug("already check in before") logger.info("%s already check in before", userid) message = "Already check in before" else: logger.debug("check in is succeed") logger.info("%s check in is succeed", userid) message = f'Check in success at {datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")} WIB' # click "OK" button and logout time.sleep(1) alert_container.find_element( By.XPATH, "//button[@class='confirm'][normalize-space()='OK']").click() logout_app(driver, message) else: logger.debug("TimeoutException is occurs") logger.exception(e) driver.quit() message = "TimeoutException is occurs" send_message(text=message) except (NoSuchElementException) as e: logger.debug("NoSuchElementException is occurs") logger.exception(e) driver.quit() message = "NoSuchElementException is occurs" send_message(text=message)
def filter_price(self, apt='', id=''): try: # 매매 for size, prices in self.prices.items(): avg_price = round(sum(prices) / len(prices)) notice = '' # 전일 데이터가 있다면 비교 후 메세지에 포함 if str(size) in self.prices_filter: yesterday_price = self.prices_filter[str(size)][1] minus = avg_price - yesterday_price increase = round((minus) / yesterday_price * 100, 1) updown = '' if increase > 0: updown = '증가' elif increase < 0: updown = '감소' if updown: notice += '%s[%s] %d만원 (%.1f%%) %s \n' % ( apt, size, minus, increase, updown) self.prices_filter[size] = [ min(prices), avg_price, max(prices) ] # 급매알리미 if self.prices_filter[size][1] * 0.9 > self.prices_filter[ size][0]: sale_percent = self.prices_filter[size][ 0] / self.prices_filter[size][1] * 100 uniqid = '%s%s%d' % (id, size, self.prices_filter[size][0]) if uniqid not in self.log: message = '%s[%s] %d만원 (평균대비 %d%% 저렴)' % ( apt, size, self.prices_filter[size][0], math.ceil(sale_percent)) self.log = filewriter.slice_json_by_max_len( self.log, max_len=100) self.send_messge_and_save(uniqid, message, 'yoona_azzi') # 전세 for size, prices in self.jeonses.items(): avg_price = round(sum(prices) / len(prices)) self.jeonses_filter[size] = [ min(prices), avg_price, max(prices) ] # 증감메세지 발송 if notice: telegrambot.send_message(notice, 'yoona_azzi') except Exception as e: log.logger.error(e, exc_info=True) return False
def send_email(sender, receiver, subject, body, attachment): """send an email :param sender: user email :type sender: str :param receiver: user email :type receiver: str :param subject: email subject :type subject: str :param body: email message :type body: str :param attachment: email attachment :type attachment: str """ try: # create a multipart message and set headers email = MIMEMultipart() email['From'] = sender email['To'] = receiver email['Subject'] = subject # add body to email email.attach(MIMEText(body, 'plain')) # get attachment with open(attachment, 'rb') as f: part = MIMEBase('application', 'octet-stream') part.set_payload(f.read()) # encode fie in ASCII encoders.encode_base64(part) # add header as key/value pair to attachment part filename = os.path.basename(attachment) part.add_header('Content-Disposition', f'attachment; filename={filename}') # add attachment to message and convert message to string email.attach(part) text = email.as_string() # login in to server context = ssl.create_default_context() with SMTP_SSL(host='mail.posindonesia.co.id', context=context) as server: server.login(USEREMAIL, PASSEMAIL) server.sendmail(sender, receiver, text) message = f'Sending file {filename} to {receiver} is succeed' logger.info(message) send_message(text=message) except Exception as e: message = f'Sending file {filename} to {receiver} is failed' logger.info(message) send_message(text=message)
def logout_app(driver, message): try: WebDriverWait(driver, 3).until( EC.element_to_be_clickable( (By.XPATH, "//a[@href='index.php?id_index=1']"))).click() except (ElementClickInterceptedException) as e: logger.debug("ElementClickInterceptedException is occurs") logger.exception(e) finally: logger.debug("logged out") driver.quit() send_message(text=message)
def scan_page(self, url): try: if self.selenium_extract_by_xpath(tag={ 'tag': 'table', 'attr': 'class', 'name': 'tbl_lst' }) is False: raise Exception('selenium_extract_by_xpath fail.') soup = BeautifulSoup(self.driver.page_source, 'html.parser') element = soup.find('table', class_="tbl_lst").find( 'tr', class_="_itemSection").find_all('td', recursive=False) if element: price_str = element[1].find('a').getText().strip() price = re.sub("\D", "", price_str) # 수집 성공로그 self.record_success_log() try: if self.log[url] > price: title = soup.find( 'div', class_="h_area").find('h2').getText().strip() service = element[0].find('img')['alt'] price_before = format(self.log[url], ',') price_new = format(price, ',') message = '[네이버쇼핑] 최저가가 갱신되었습니다.\n[%s]\n%s\n이전 가격: %s원\n최저 가격: %s원\n%s' % ( service, title, price_before, price_new, url) telegrambot.send_message(message, 'lowdeal') self.log[url] = price except Exception as e: self.log[url] = price #print(self.log) filewriter.save_log_file(self.name, self.log) except Exception as e: log.logger.error(e, exc_info=True)
def start(self): try: self.log = filewriter.get_log_file(self.name) today = datetime.today().strftime('%Y_%m_%d') yesterday = (datetime.today() - timedelta(1)).strftime('%Y_%m_%d') log_yesterday = self.log[yesterday] log_today = self.log[today] for key in log_today.keys(): if key in log_yesterday: del log_yesterday[key] if log_yesterday and len(log_yesterday) > 0: text = ', '.join(map(str, log_yesterday.keys())) text = '발송실패 모듈: %s\n확인해주세요.' % text if text: telegrambot.send_message(text, 'dev') except Exception as e: log.logger.error(e, exc_info=True)
def send_message(self): try: text = '[%s]' % self.name for idx, result in enumerate(self.result_list): try: if result['price_depature'] < 99999999999 or result[ 'price_return'] < 99999999999: text_each = '\n\n%s(%s):' % (result['title'], result['airport']) if result['price_depature'] < 99999999999: price_depature = 'depature price: {:0,.0f} won'.format( result['price_depature']) date_depature = 'depature date: %s' % ",".join( result['date_depature']) text_each += '\n%s\n%s' % (price_depature, date_depature) if result['price_return'] < 99999999999: price_return = 'return price: {:0,.0f} won'.format( result['price_return']) date_return = 'return date: %s' % ",".join( result['date_return']) text_each += '\n%s\n%s' % (price_return, date_return) log.logger.info(text_each) text += text_each if idx % 5 == 0: telegrambot.send_message(text) text = '' except: pass if text: telegrambot.send_message(text) filewriter.remove_log_file(self.name) except Exception as e: log.logger.error(e, exc_info=True)
def login_app(action, userid=USERID, password=PASSWORD): try: driver.get(URL) driver.find_element(By.NAME, "UserID").send_keys(userid) driver.find_element(By.NAME, "Password").send_keys(password) driver.find_element(By.XPATH, "//button[normalize-space()='Login']").click() # wait until 3 sec until alert is present # if present, execute code in this block WebDriverWait(driver, 3).until(EC.alert_is_present()) alert = driver.switch_to.alert alert.accept() driver.quit() logger.debug("userid %s and password %s is not valid", userid, password) logger.info("%s login failed", userid) message = "User ID or Password you entered is not valid" send_message(text=message) except (TimeoutException) as e: # alert not present, execute code in this block logger.debug("%s is logged in", userid) logger.info("%s is logged in", userid) if action == "check_in": logger.debug("action is 'check_in'") check_in(driver, userid) elif action == "check_out": logger.debug("action is 'check_out'") check_out(driver, userid) except (NoSuchElementException) as e: logger.debug("NoSuchElementException is occurs") logger.exception(e) driver.quit() message = "NoSuchElementException is occurs" send_message(text=message)
async def sell_all(): # 우리나라 시간은 +9시간 해야됨 sell_time = '23:59' sell_check = True percent = 1 # 09:00이 되면 전부 매도 while (1): now = datetime.now() print(now) await asyncio.sleep(10) if str(now)[11:16] != sell_time: sell_check = True if str(datetime.now())[11:16] == sell_time and sell_check == True: sell_check = False balances = upbit.get_balances() text = str( datetime.now()) + "\n===============매도 전 잔고===============\n" send_message(text, ) print(text) with open("sell_log.txt", "a", encoding='utf8') as f: f.write(text) for i in balances: print(i) with open("sell_log.txt", "a", encoding='utf8') as f: text = str(datetime.now()) + str(i) + "\n" f.write(text) if i['currency'] == "KRW": send_message(text, ) continue try: ret = upbit.sell_market_order( "KRW-{}".format(i['currency']), float(i['balance']) * percent) price = float(i['avg_buy_price']) * float(i['balance']) text = "종류 : " + i['currency'] + "\n주문가능금액/수량 : " + i[ 'balance'] + "\n주문 중 묶여있는 금액/수량 : " + i[ 'locked'] + "\n평단가 : " + i[ 'avg_buy_price'] + "\n금액 : " + str(price) t_sell_log = threading.Thread(target=send_message, args=(text, )) # 스레드 생성 t_sell_log.start() t_sell_log.join() print(text) #print(ret) print("sell", i['currency']) except Exception as e: print(i['currency'], "에러발생") print(e) text = "===============매도 후 잔고===============" send_message(text, ) print(text) await asyncio.sleep(3) with open("sell_log.txt", "a", encoding='utf8') as f: f.write("매도 후 잔고\n") for i in upbit.get_balances(): with open("sell_log.txt", "a", encoding='utf8') as f: text = str(datetime.now()) + str(i) + "\n" f.write(text) text = "종류 : " + i['currency'] + "\n주문가능금액/수량 : " + i[ 'balance'] + "\n주문 중 묶여있는 금액/수량 : " + i['locked'] t2_sell_log = threading.Thread(target=send_message, args=(text, )) # 스레드 생성 t2_sell_log.start() t2_sell_log.join() print("SELL END") break return ("SELL END")
async def do_async_loop(ticker, target, reset): #list_coin = [] #list_coin.append(ticker) update_time = '00:00' break_check = False connect_check = False money = 10000 #money = 15000 #money=0 #count = int(money/target) buy_check = {} # buy_check 저장된 값 불러오기 if reset == False: read_check = [] with open("buy_check.txt", "r", encoding='utf8') as f: lines = f.readlines() line = lines[0].split(":") for i in range(len(line)): if "True" in line[i]: read_check.append(True) if "False" in line[i]: read_check.append(False) for y in range(len(ticker)): buy_check[ticker[y]] = read_check[y] print("불러옴") else: # buy_check['티커'] = True 로 초기값 넣어주기 for i in ticker: buy_check[i] = True with open("buy_check.txt", "w", encoding='utf8') as f: text = str(buy_check) + "\n" f.write(text) text = "구매 완료 목록 확인 " + str(buy_check) send_message(text, ) print(text) async with websockets.connect(UPBIT_WEB_SOCKET_ADD, ping_interval=None) as websocket: # ss format ex : '[{"ticket":"test1243563456"},{"type":"trade","codes":["KRW-BTC", "KRW-ETH"]}]' ss = '[{"ticket":"test1243563456"},{"type":"ticker","codes":' + str( ticker).replace("'", '"') + '}]' await websocket.send(ss) while (1): try: data_rev = await websocket.recv() my_json = data_rev.decode('utf8').replace("'", '"') data = json.loads(my_json) now_ticker = data['code'] # 00초일때 연결확인용으로 프린트 if str(datetime.now())[17:19] == '00': print(data['code'], data['trade_time'], data['trade_price']) # 30분마다 연결확인 텔레그램 메시지 보내기 if str(datetime.now())[14:16] != '30': connect_check = True if str(datetime.now() )[14:16] == '30' and connect_check == True: text = "연결확인\n" + str(buy_check) checking_message = threading.Thread( target=send_log, args=(text, )) # 스레드 생성 checking_message.start() connect_check = False # 현재가가 목표가가 되면 매수 주문 넣고 매시지 보내기 if target[now_ticker] == data['trade_price'] and buy_check[ now_ticker] == True: count = float(money / int(target[now_ticker])) ret = upbit.buy_limit_order(now_ticker, target[now_ticker], count) print("주문완료\n", ret, count) with open("buy_log.txt", "a", encoding='utf8') as f: text = "\n" + str(datetime.now()) + str(ret) f.write(text) buy_check[now_ticker] = False with open("buy_check.txt", "w", encoding='utf8') as f: text = str(buy_check) + "\n" f.write(text) # ret type은 dict text = "주문 완료\n종류 : {0}\n매수가격 : {1}\n시간 : {2}\n수량 : {3}".format( str(ret['market']), float(ret['price']), str(ret['created_at']), float(ret['volume'])) #send_message(text) # 스레드로 텔레그램 메시지 전송하면 에러 남 문자열이 쪼개져서 들어가는듯 # TypeError: send_message() takes 1 positional argument but 71 were given # ars를 2개로 주면 됨 뭐야 이거 >> args에 , 찍어줘야됨 t_buy_log = threading.Thread(target=send_message, args=(text, )) # 스레드 생성 t_buy_log.start() t_update = threading.Thread(target=send_target_message, args=(data['code'], data['trade_price'], buy_check)) # 스레드 생성 t_update.start() except Exception as e: print("=========ERROR=======") print(e) send_message(str(e), ) with open("error_log.txt", "a", encoding='utf8') as f: text = str(datetime.now()) + str(e) + "\n" f.write(text) if str( e ) == "code = 1006 (connection closed abnormally [internal]), no reason": print("프로그램 종료") sys.exit("프로그램 종료") # 지정된 시간이 되면 멈추고 종목 업데이트 if str(datetime.now())[11:16] != update_time: break_check = True if str(datetime.now() )[11:16] == update_time and break_check == True: print("==============BREAK==============") print(datetime.now()) break_check = False break return "END"
def scan_page(self): try: sleep(5) # 레이어가 있다면 닫기 (에스크로, 임시) try: if self.selenium_exist_by_xpath(xpath='/html/body/div[1]/div/div/div[3]/div/div/label') is True: self.selenium_click_by_xpath(xpath='/html/body/div[1]/div/div/div[3]/div/div/label') except: pass try: if self.selenium_exist_by_xpath(xpath='/html/body/div[1]/div/div/div[3]/div/div/label/input') is True: self.selenium_click_by_xpath(xpath='/html/body/div[1]/div/div/div[3]/div/div/label/input') except: pass # 레이어가 있다면 닫기 try: if self.selenium_exist_by_xpath(tag={'tag': 'button', 'attr': 'data-dismiss', 'name': 'mySmallModalLabel'}) is True: self.selenium_click_by_xpath(tag={'tag': 'button', 'attr': 'data-dismiss', 'name': 'mySmallModalLabel'}) except: pass # 문의체크 (2020-01-10) try: ask = self.driver.find_elements_by_xpath('//*[@name="inquery"]/div/div[2]/ul/li') if ask: is_ask = False for ask_li in ask: ask_number = ask_li.find_element_by_xpath('.//p[@class="text-number"]').text ask_number = int(ask_number) if ask_number > 0: is_ask = True if is_ask == True: telegrambot.send_message('정성한끼 고객이 상담을 기다리고 있습니다.', 'jshk') except: pass # 문의까지만. self.destroy() exit() # 신규주문 페이지로 이동 if self.selenium_click_by_xpath(tag={'tag': 'a', 'attr': 'data-nclicks-code', 'name': 'orddel.new'}) is False: raise Exception('selenium_click_by_xpath fail. submit') # if self.selenium_click_by_xpath(tag={'tag': 'a', 'attr': 'data-nclicks-code', 'name': 'orddel.wait'}) is False: # raise Exception('selenium_click_by_xpath fail. submit') sleep(10) # 레이어가 있다면 닫기 try: if self.selenium_exist_by_xpath(tag={'tag': 'button', 'attr': 'data-dismiss', 'name': 'mySmallModalLabel'}) is True: self.selenium_click_by_xpath(tag={'tag': 'button', 'attr': 'data-dismiss', 'name': 'mySmallModalLabel'}) except: pass # 주문 데이터 가져오기 iframe으로 변경 self.driver.switch_to.frame(frame_reference=self.driver.find_element_by_xpath('//iframe[@id="__naverpay"]')) list = self.driver.find_element_by_xpath('//*[@id="gridbox"]/div[2]/div[2]/table').find_elements_by_xpath('.//tbody/tr') window_before = self.driver.window_handles[0] prev_order_id = None no_messages = {} for index, li in enumerate(list): try: if li: soup_order_info = BeautifulSoup(li.get_attribute('innerHTML'), 'html.parser') tds = soup_order_info.find_all('td') if tds: order_id = tds[2].getText().strip() item_name = tds[18].getText() item_kind = tds[19].getText() item_option = tds[20].getText().strip() item_amount = tds[22].getText().strip() item_id = tds[17].getText() destination = tds[44].getText() # 추가상품 발송제한 if item_kind == '추가구성상품': continue # 발송내역에 없는지 확인 if not order_id or order_id in self.log: continue # *요일조리후발송 옵션인 경우 메세지에서 제외 if '조리후발송' in item_option: no_messages[order_id] = True # 다음 주문이 같은 주문번호라면 continue try: if (list[index + 1]): soup_order_info_next = BeautifulSoup(list[index + 1].get_attribute('innerHTML'), 'html.parser') tds_next = soup_order_info_next.find_all('td') order_id_next = tds_next[2].getText().strip() if order_id == order_id_next: continue except: pass try: # 메세지제외인 주문번호인 경우 제외 if no_messages[order_id]: continue except: pass if item_option: item_name = item_name + ' (' + item_option + ')' if item_amount: item_name = item_name + ' ' + item_amount + '개' # print(item_option) # print(item_amount) # print(item_kind) # print(item_name) # print(buyer) # exit() talktalklink = li.find_element_by_xpath('.//td[10]/a') # 톡톡하기 클릭 talktalklink.click() sleep(3) # 톡톡창으로 focus window_after = self.driver.window_handles[1] self.driver.switch_to.window(window_after) # 레이어가 있다면 닫기 try: if self.selenium_exist_by_xpath(xpath='//button[contains(@class,"btn_negative")]') is True: self.selenium_click_by_xpath(xpath='//button[contains(@class,"btn_negative")]') except: pass # 메세지 생성 message = self.get_delevery_message(item_id=item_id, item_name=item_name, destination=destination) if not message: raise Exception('messageText genarating fail.') # 메시지 입력 self.driver.execute_script('document.getElementById("partner_chat_write").value = "' + message + '";') if self.selenium_input_text_by_xpath(text=' ', xpath='//*[@id="partner_chat_write"]', clear=False) is False: raise Exception('selenium_input_text_by_xpath fail. chat_write') sleep(1) # 메세지 전송 if self.selenium_click_by_xpath(xpath='//*[@id="chat_wrap"]/div/div[1]/div/div[3]/div[2]/button') is False: raise Exception('selenium_click_by_xpath fail. submit') sleep(2) message = message.replace('\\n', '\n') self.log = filewriter.slice_json_by_max_len(self.log, max_len=1000) self.send_messge_and_save(order_id, message, 'jshk') # 창 닫고 복귀 self.driver.close() self.driver.switch_to.window(window_before) self.driver.switch_to.frame(frame_reference=self.driver.find_element_by_xpath('//iframe[@id="__naverpay"]')) # 테스트로그 # print(buyer) # print(order_id) # print(item_id) # print(item_name) # print(destination) # print(tds) # for idx, td in enumerate(tds): # print(idx, td.getText()) except Exception as e: log.logger.error(e, exc_info=True) self.destroy() exit() return True except Exception as e: log.logger.error(e, exc_info=True) self.destroy() exit() return False
def login(self): try: # 로그인 여부 체크 if self.connect(site_url=self.UNFOLLOW_URL, is_proxy=False, default_driver='selenium', is_chrome=True) is False: raise Exception('site connect fail') self.get_cookie() if self.connect(site_url=self.UNFOLLOW_URL, is_proxy=False, default_driver='selenium', is_chrome=True) is False: raise Exception('site connect fail') try: if self.selenium_exist_by_xpath(xpath='//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/span/a[1]/button') is False: log.logger.info('Already loggined.') return True except: pass # 로그인 되어있지 않다면 로그인 페이지로 이동 if self.connect(site_url=self.LOGIN_URL, is_proxy=False, default_driver='selenium', is_chrome=True) is False: raise Exception('site connect fail') # 계정정보 가져오기 account_data = filewriter.get_log_file(self.name + '_account') if account_data: if self.selenium_extract_by_xpath(tag={'tag': 'input', 'attr': 'name', 'name': 'username'}) is False: raise Exception('selenium_extract_by_xpath fail.') # 아이디 입력 if self.selenium_input_text_by_xpath(text=account_data[0], tag={'tag': 'input', 'attr': 'name', 'name': 'username'}) is False: raise Exception('selenium_input_text_by_xpath fail. username') # 비번 입력 if self.selenium_input_text_by_xpath(text=account_data[1], tag={'tag': 'input', 'attr': 'name', 'name': 'password'}) is False: raise Exception('selenium_input_text_by_xpath fail. password') # 아이디 입력 if self.selenium_input_text_by_xpath(text=account_data[0], tag={'tag': 'input', 'attr': 'name', 'name': 'username'}) is False: raise Exception('selenium_input_text_by_xpath fail. username') # 비번 입력 if self.selenium_input_text_by_xpath(text=account_data[1], tag={'tag': 'input', 'attr': 'name', 'name': 'password'}) is False: raise Exception('selenium_input_text_by_xpath fail. password') # 로그인하기 선택 if self.selenium_click_by_xpath(tag={'tag': 'button', 'attr': 'type', 'name': 'submit'}) is False: raise Exception('selenium_click_by_xpath fail. submit') sleep(3) # 비정상적인 로그인 시도 처리 (내가 맞습니다) try: if self.selenium_exist_by_xpath(xpath='//*[@id="react-root"]/section/div/div/div[3]/form/div[2]/span/button') is True: self.selenium_click_by_xpath(xpath='//*[@id="react-root"]/section/div/div/div[3]/form/div[2]/span/button') except: pass # 보안코드 try: if self.selenium_exist_by_xpath(xpath='//*[@id="react-root"]/section/div/div/div[1]/div/p') is True: if self.selenium_exist_by_xpath(xpath='//*[@id="react-root"]/section/div/div/div[3]/form/span/button') is True: # 보안코드가 없다면 block 처리해서 인스타그램 프로세스를 중지 if self.security_code[0] == '': self.security_code[0] = 'blocked' filewriter.save_log_file('instagram_security_code', self.security_code) log.logger.info('Instagram has just blocked.') telegrambot.send_message('Instagram has just blocked.', 'instagram') self.destroy() exit() # 발송하기 self.selenium_click_by_xpath(xpath='//*[@id="react-root"]/section/div/div/div[3]/form/span/button') # 텔레그램 알림 telegrambot.send_message('Please check instagram security code from your email in 1 minutes.', 'instagram') log.logger.info('Please check instagram security code from your email in 1 minutes.') # 수정될 때 까지 50초 대기 sleep(50) # 새롭게 입력된 데이터를 가져옵니다. self.security_code = filewriter.get_log_file('instagram_security_code') # 보안코드 입력 if self.selenium_input_text_by_xpath(text=self.security_code[0], xpath='//*[@id="security_code"]') is False: raise Exception('selenium_input_text_by_xpath fail. security_code') # 제출 self.selenium_click_by_xpath(xpath='//*[@id="react-root"]/section/div/div/div[2]/form/span/button') log.logger.info('security_code. (%s)' % (self.security_code[0])) # 사용한 코드는 제거 self.security_code[0] = '' filewriter.save_log_file('instagram_security_code', self.security_code) sleep(5) except Exception as e: log.logger.error(e, exc_info=True) pass try: if self.selenium_exist_by_xpath(xpath='//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div/div/div/div[3]/div[1]/a') is True: log.logger.info('login fail.') raise Exception('login fail error') except: pass log.logger.info('login success!') self.set_cookie() sleep(2) return True except Exception as e: log.logger.error(e, exc_info=True) self.end_report() return False
def main(): logger = logging.getLogger('main') try: today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) yesterday1 = yesterday.strftime('%d-%m-%Y') yesterday2 = yesterday.strftime('%Y%m%d') yesterday2 = dateformat.format_1(yesterday2) filename, filepath = get_attachment( subject=f'ENGINE PBB KAB NGANJUK Tgl {yesterday1}') if filename is None and filepath is None: message1 = 'Email \"SETTLEMENT PBB KAB NGANJUK\" has not been received' logger.info(message1) message2 = f'Email *SETTLEMENT PBB KAB NGANJUK* untuk transaksi tanggal {yesterday2} belum diterima' send_message(text=message2, parse_mode='MarkdownV2') send_message(chat_id=ACHMAD, text=message2, parse_mode='MarkdownV2') else: filesize = os.path.getsize(filepath) if filesize > 5: contents, date, count, total = get_content(filepath) message = f'Data available in attachment {filename}, file is saved' logger.info(message) # export as xlsx file filename1, filepath1 = xlsx_template_1(contents) sleep(3) filename2, filepath2 = xlsx_template_2(contents) if filepath1 is not None and filepath2 is not None: date = dateformat.format_1(filename[:8]) message = f''' \n*SETTLEMENT PBB KAB NGANJUK*\ \nTanggal Trx : {date}\ \nJumlah Trx : {count}\ \nTotal BSU : Rp {total}\ ''' send_message(text=message, parse_mode='MarkdownV2') send_message(chat_id=ACHMAD, text=message, parse_mode='MarkdownV2') sender = '*****@*****.**' # receiver = '*****@*****.**' receiver = '*****@*****.**' subject = f'Trx PBB Kantor Pos Nganjuk {date}' body = ''' Dengan hormat, Berikut ini kami kirimkan file sesuai dengan subject diatas. Tks =============================================== # Yogi Trismayana 991483728 # HP. 082140513878 # Kantor Pos Nganjuk 64400 # Jl. Supriyadi No. 19 Kauman - Nganjuk 64411 =============================================== ''' send_email(sender, receiver, subject, body, filepath2) else: message = f'Unexpected error occurs. {filename1} and {filename2} is not created' logger.info(message) send_message(text=message) else: if os.path.exists(filepath): os.remove(filepath) message = f'No data available in attachment {filename}, file is removed' logger.info(message) message = f'Tidak ada *SETTLEMENT PBB KAB NGANJUK* untuk transaksi tanggal {yesterday2}' send_message(text=message, parse_mode='MarkdownV2') send_message(chat_id=ACHMAD, text=message, parse_mode='MarkdownV2') except(imaplib.IMAP4.error) as e: logger.exception(e) message = 'Unexpected error is occurs' send_message(text=message)
def scan_page(self): try: sleep(5) self.remove_layer() # -- 문의체크 -- try: ask = self.driver.find_elements_by_xpath( '//*[@name="inquery"]/div/div[2]/ul/li') if ask: is_ask = False for ask_li in ask: ask_number = ask_li.find_element_by_xpath( './/p[@class="text-number"]').text ask_number = int(ask_number) if ask_number > 0: is_ask = True if is_ask == True: telegrambot.send_message('정성한끼 고객이 상담을 기다리고 있습니다.', 'jshk') except: pass # -- 신규주문 페이지로 이동 -- if self.selenium_click_by_xpath(tag={ 'tag': 'a', 'attr': 'data-nclicks-code', 'name': 'orddel.new' }) is False: raise Exception('selenium_click_by_xpath fail. orddel.new') sleep(10) self.remove_layer() # 주문 데이터 가져오기 iframe으로 변경 self.driver.switch_to.frame( frame_reference=self.driver.find_element_by_xpath( '//iframe[@id="__naverpay"]')) list = self.driver.find_element_by_xpath( '//*[@id="__app_root__"]/div/div[2]/div[3]/div[4]/div[1]/div[2]/div[1]/div[2]/div[2]/div/div[1]/table' ).find_elements_by_xpath('.//tbody/tr') order_list = {} for i, li in enumerate(list): try: if li: soup_order_info = BeautifulSoup( li.get_attribute('innerHTML'), 'html.parser') tds = soup_order_info.find_all('td') if tds: item_option = tds[16].getText().strip() item_amount = tds[18].getText().strip() item_amount = int(item_amount) destination = tds[40].getText() if item_option not in order_list: order_list[item_option] = item_amount else: order_list[item_option] = order_list[ item_option] + item_amount # 제주도인 경우 알림 if destination in '제주특별자치도': telegrambot.send_message( '제주도 주문건을 확인해주세요.', 'jshk') except Exception as e: log.logger.error(e, exc_info=True) self.destroy() exit() # -- 발송대기 주문 페이지로 이동 -- if self.selenium_click_by_xpath( xpath= '//*[@id="__app_root__"]/div/div[2]/div[1]/div/div[2]/ul/li[4]/div/a[1]' ) is False: raise Exception('selenium_click_by_xpath fail. orddel.wait') sleep(5) # 주문 데이터 가져오기 iframe으로 변경 list = self.driver.find_element_by_xpath( '//*[@id="__app_root__"]/div/div[2]/div[3]/div[4]/div[1]/div[2]/div[1]/div[2]/div[2]/div/div[1]/table' ).find_elements_by_xpath('.//tbody/tr') for i, li in enumerate(list): try: if li: soup_order_info = BeautifulSoup( li.get_attribute('innerHTML'), 'html.parser') tds = soup_order_info.find_all('td') if tds: item_option = tds[16].getText().strip() item_amount = tds[18].getText().strip() item_amount = int(item_amount) if item_option not in order_list: order_list[item_option] = item_amount else: order_list[item_option] = order_list[ item_option] + item_amount except Exception as e: log.logger.error(e, exc_info=True) self.destroy() exit() # -- 데이터 저장 -- if dict: filewriter.save_log_file('jshk_order_data', order_list) print(order_list) return True except Exception as e: self.driver.save_screenshot('smartstore_screenshot.png') log.logger.error(e, exc_info=True) self.destroy() exit() return False
def send_target_message(ticker, price, check): text = str(ticker) + "\n현재가 : " + str(price) + "\n" + str(check) send_message(text) print("메시지 전송", text)
def get_top_price(): now=datetime.now() today = str(now)[:10] text = "오늘날짜" + today send_message(text,) print("현재 시간", now) # gcp에서 시간이 우리나라 시간 -9시간 이라서 필요없음 ''' if now.hour >= 15: today = str(now - timedelta(days=1))[:10] ''' for ticker in tickers_list: url = "https://api.upbit.com/v1/candles/days" #querystring = {"market":ticker,"to":"{0} 00:00:00".format(str(datetime.now()+timedelta(days=1))[:10]),"count":"1"} querystring = {"market":ticker,"to":"{0} 00:00:00".format(today),"count":"1"} #querystring = {"market":ticker,"to":"2021-03-17 00:00:00","count":"1"} #print(querystring) response = requests.request("GET", url, params=querystring) #print(response.text) results = response.text.split('},') #print(results) r = [] candle_time = [] open = [] high = [] low = [] close = [] price = [] volume = [] close_percent = [] high_percent = [] low_percent = [] #df_today = pyupbit.get_ohlcv(ticker, interval="day", count=1) #today_open = float(df_today['open']) save = False for result in reversed(results): result=result.replace('[','') result=result.replace('{','') result=result.replace(']','') result=result.replace('}','') #날짜 date = result[result.find('","opening_price') - (result.find('","opening_price') - result.find('candle_date_time_kst":"') - len('candle_date_time_kst":"')):result.find('","opening_price')] candle_time.append(date) #print(time) #시가 open_price = result[result.find('0000,"high_price"') - (result.find('0000,"high_price"') - result.find('"opening_price":') - len('"opening_price":')):result.find('0000,"high_price"')] open.append(open_price) #print(open_price) #고가 high_price = result[result.find('0000,"low_price"') - (result.find('0000,"low_price"') - result.find('"high_price":') - len('"high_price":')):result.find('0000,"low_price"')] high.append(high_price) #high_percent_candle = (float(high_price) - today_open) / today_open * 100 #high_percent.append(high_percent_candle) #print(high_price) #저가 low_price = result[result.find('0000,"trade_price":') - (result.find('0000,"trade_price":') - result.find('"low_price":') - len('"low_price":')):result.find('0000,"trade_price":')] low.append(low_price) #low_percent_candle = (float(low_price) - today_open) / today_open * 100 #low_percent.append(low_percent_candle) #print(low_price) #종가 close_price = result[result.find('0000,"timestamp":') - (result.find('0000,"timestamp":') - result.find(',"trade_price":') - len(',"trade_price":')):result.find('0000,"timestamp":')] close.append(close_price) #print(close_price) #close_percent_candle = (float(close_price) - today_open) / today_open * 100 #close_percent.append(close_percent_candle) #거래대금 trade_price = result[result.find(',"candle_acc_trade_volume":') - (result.find(',"candle_acc_trade_volume":') - result.find('"candle_acc_trade_price":') - len('"candle_acc_trade_price":')):result.find(',"candle_acc_trade_volume":')] price.append(trade_price) ##print(trade_price) #거래량 trade_volume = result[result.find(',"prev_closing_price"') - (result.find(',"prev_closing_price"') - result.find('"candle_acc_trade_volume":') - len('"candle_acc_trade_volume":')):result.find(',"prev_closing_price"')] volume.append(trade_volume) ##print(trade_volume) # 차이/시가 *100 #등락률 #if float(high_price) >= today_open*1.07: # save = True data = {'시가' : open, '고가' : high, '저가' : low, '종가' : close, '거래대금' : price, '거래량' : volume, } df = DataFrame(data, index=candle_time) df['range'] = (float(df['고가']) - float(df['저가'])) * 0.032 target_price = round(float(df['종가']) + float(df['range']), 1) # 목표가가 100 이상이면 정수형으로 변환 if target_price >= float(100): target_price = int(target_price) if target_price >= float(1000000) and str(target_price)[-3] != "0": target_price = int(str(target_price)[:-3] + "000") elif target_price >= float(100000) and str(target_price)[-2] != "0": target_price = int(str(target_price)[:-2] + "00") elif target_price >= float(10000) and str(target_price)[-1] != "0": target_price = int(str(target_price)[:-1] + "0") elif target_price >= float(1000) and str(target_price)[-1] != "0": target_price = int(str(target_price)[:-1] + "5") df['target'] = target_price # txt파일에 저장 #log_list = [ticker, df] #save_log(log_list) # 텔레그램으로 로그 전송 #t = threading.Thread(target=send_telgm_log, args=(log_list,)) # 스레드 생성 #t.start() ''' min_price = min(buy_price) pr = float(data['거래대금'][0]) pr = int(pr) if min_price < pr: num = buy_price.index(min_price) buy_price[num] = pr buy_ticker[num] = ticker ticker_data[num] = df print("buy_price", buy_price) ''' print("buy_ticker", buy_ticker) buy_ticker.append(ticker) ticker_data.append(df) time.sleep(0.15) #print(buy_ticker, buy_price) buy_list = [buy_ticker, ticker_data, True] ''' if now.hour >= 15: data_date = str(now - timedelta(days=2))[:10] else: ''' data_date = str(now - timedelta(days=1))[:10] res = read_log() # 오늘 날짜 -1 이 저장된 로그 날짜와 같으면 구매완료 목록 초기화 안함 if data_date == res: buy_list[2] = False # 거래대금 상위 종목 저장 및 텔레그램 메시지 전송 t2 = threading.Thread(target=save_buy_list, args=(buy_list,)) # 스레드 생성 t2.start() t2.join() print("===========종목 업데이트 완료===========\n\n\n\n\n") t_start = threading.Thread(target=send_message, args=("감시 시작",)) # 스레드 생성 t_start.start() return buy_list
def main(): logger = logging.getLogger('main') try: today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) yesterday1 = yesterday.strftime('%d-%m-%Y') yesterday2 = yesterday.strftime('%Y%m%d') yesterday2 = dateformat.format_1(yesterday2) filename, filepath = get_attachment( subject=f'ENGINE PDAM KAB. NGANJUK [D039P] Rekon Tanggal {yesterday1}') if filename is None and filepath is None: message1 = 'Email \"SETTLEMENT PDAM KAB NGANJUK\" has not been received' logger.info(message1) message2 = f'Email *SETTLEMENT PDAM KAB NGANJUK* untuk transaksi tanggal {yesterday2} belum diterima' send_message(text=message2, parse_mode='MarkdownV2') send_message(chat_id=ACHMAD, text=message2, parse_mode='MarkdownV2') else: filesize = os.path.getsize(filepath) if filesize > 5: contents, date, count, total = get_content(filepath) message = f'Data available in attachment {filename}, file is saved' logger.info(message) # export as xlsx file filename, filepath = xlsx_template_3(contents) if filepath is not None: message = f''' \n*SETTLEMENT PDAM KAB NGANJUK*\ \nTanggal Trx : {date}\ \nJumlah Trx : {count}\ \nTotal BSU : Rp {total}\ ''' send_message(text=message, parse_mode='MarkdownV2') send_message(chat_id=ACHMAD, text=message, parse_mode='MarkdownV2') else: if os.path.exists(filepath): os.remove(filepath) message = f'No data available in attachment {filename}, file is removed' logger.info(message) message = 'Tidak ada *SETTLEMENT PDAM KAB NGANJUK* untuk transaksi hari kemarin' send_message(text=message, parse_mode='MarkdownV2') send_message(chat_id=ACHMAD, text=message, parse_mode='MarkdownV2') except(imaplib.IMAP4.error) as e: logger.exception(e) message = 'Unexpected error is occurs' send_message(text=message)