def login(self): """Login LINE.""" self.driver.get(f'chrome-extension://{UID}/index.html') sleep(0.5) # browser may confirm leaving of the current page try: self.driver.switch_to.alert.accept() except NoAlertPresentException: pass sleep(0.5) # get email and password fields email_field = self.driver.find_element_by_id('line_login_email') pass_field = self.driver.find_element_by_id('line_login_pwd') # clear fields email_field.clear() pass_field.clear() # send keys email_field.send_keys(EMAIL) pass_field.send_keys(PASSWORD) sleep(0.5) self.driver.find_element_by_id('login_btn').click() sleep(1.5) while True: try: verify_code = (self.driver.find_element_by_css_selector( '#login_content div.mdCMN01Code').text) except NoSuchElementException: logger.debug('no verify code needed.') break if verify_code: logger.debug(f'input verify code: ({verify_code})') else: logger.info('Loggged in successfully!') break sleep(1)
def __init__(self, command_executor=None, reuse_session_id=None): """Add LINE chrome extension.""" info = None if isfile(SESSION_CACHE_PATH): with open(SESSION_CACHE_PATH) as f: info = json.load(f) chrome_options = Options() chrome_options.add_extension(EXTENSTION_PATH) if command_executor: self.driver = RemotePatch( command_executor=command_executor, desired_capabilities=chrome_options.to_capabilities(), reuse_session_id=reuse_session_id, ) else: if info: self.driver = RemotePatch( desired_capabilities=chrome_options.to_capabilities(), **info) logger.debug(f'resue: {info}') else: self.driver = RemotePatch( command_executor=COMMAND_EXECUTOR, desired_capabilities=chrome_options.to_capabilities(), ) try: # test if session still alive logger.info(f'current url: {self.driver.current_url}') except WebDriverException as e: if 'No active session with ID' in e.args[0]: logger.warn('session seems dead, create a new one...') os.remove(SESSION_CACHE_PATH) self.__init__()
def handle(self): """Handle incoming data.""" self.data = self.rfile.readline().strip() logger.info("{} wrote:".format(self.client_address[0])) logger.info(self.data) os.system(self.data.decode('utf-8')) self.wfile.write(b'ok')
def select_and_send(friend, message, random_offset=None, good_morning=False): """Select a friend and send message.""" b = get_browser() sleep(3) for i in range(10): try: if random_offset: random_secs = random.choice(range(random_offset)) logger.info(f'randomly offset: {random_secs} second(s)') sleep(random_secs) b.select_friend(friend) sleep(1) if good_morning: from goodmorning.image import generate img_path = generate(message, '/tmp/font.otf') b.send_img(uri=f'file://{img_path}') else: b.send_msg(message) sleep(0.5) break except Exception: logger.warn(f'Unable to select {friend} (retry left: {10 - i})') sleep(1) b.pickle_driver()
import socketserver import os from ajilog import logger from ihateline.browser import Browser class TCPHandler(socketserver.StreamRequestHandler): """TCP server.""" def handle(self): """Handle incoming data.""" self.data = self.rfile.readline().strip() logger.info("{} wrote:".format(self.client_address[0])) logger.info(self.data) os.system(self.data.decode('utf-8')) self.wfile.write(b'ok') if __name__ == "__main__": b = Browser() b.login() sleep(10) b.pickle_driver() # b.cache_session_info() HOST, PORT = '', 9999 with socketserver.TCPServer((HOST, PORT), TCPHandler) as server: logger.info('server started') server.serve_forever()