def login_google(browser, auth_text, username, password): """login using Google""" LOGGER.info('"%s": Login method Google', username) auth_text1 = auth_text.split('\t<a href="') auth_text2 = auth_text1[1].split('" class="sa') time.sleep(1) browser.go_to(auth_text2[0]) LOGGER.info('"%s": Typing in username', username) browser.type(username, into='Email') LOGGER.info('"%s": pressing next button', username) browser.click(css_selector="#next") time.sleep(2) LOGGER.info('"%s": Typing in password', username) browser.type(password, css_selector="input") LOGGER.info('"%s": pressing sign in button', username) browser.click(css_selector="#submit") time.sleep(3) # Some why it wont click and login immediately. This seems to work time.sleep(1) browser.go_to(auth_text2[0]) time.sleep(1) browser.go_to(auth_text2[0]) time.sleep(1) browser.click( css_selector="#sa_add2 > div:nth-child(4) > a.sa_link.gogo > div") time.sleep(3) return browser
def set_credentials(self, credentials): """Set the credentials""" LOGGER.info('"%s": setting credentials', credentials['username']) self.login_method = credentials['login_method'] self.username = credentials['username'] self.password = credentials['password'] self.login()
def message(self, message): """Send message to conference""" LOGGER.info('"%s": CONF "%s": start send message', self.api_wrapper.client.username, self.conference_id) browser = self.api_wrapper.client.get_browser() try: browser.go_to( 'https://rivalregions.com/#slide/conference/{}'.format( self.conference_id)) browser.refresh() time.sleep(2) character_count = 0 tmp_messages = [] for sentence in message.split('\n'): sentence_character_count = 0 tmp_sentence = [] for word in sentence.split(' '): sentence_character_count += len(word) + 1 if sentence_character_count >= 899: message = '{}\n{}'.format('\n'.join(tmp_messages), ' '.join(tmp_sentence)) LOGGER.info('"%s": CONF "%s": next message length: %s', self.api_wrapper.client.username, self.conference_id, len(message)) browser.type(message, id='message') browser.click(id='chat_send') sentence_character_count = 0 tmp_sentence = [] character_count = 0 tmp_messages = [] tmp_sentence.append(word) sentence = ' '.join(tmp_sentence) character_count += len(sentence) + 1 if character_count >= 900: message = '\n'.join(tmp_messages) LOGGER.info('"%s": CONF "%s": next message length: %s', self.api_wrapper.client.username, self.conference_id, len(message)) browser.type(message, id='message') browser.click(id='chat_send') character_count = 0 tmp_messages = [] tmp_messages.append(sentence) if tmp_messages: message = '\n'.join(tmp_messages) LOGGER.info('"%s": CONF "%s": next message length: %s', self.api_wrapper.client.username, self.conference_id, len(message)) browser.type(message, id='message') browser.click(id='chat_send') LOGGER.info('"%s": CONF "%s": finished sending message', self.api_wrapper.client.username, self.conference_id) finally: browser.close_current_tab()
def login_vk(browser, auth_text, username, password): """login using VK""" LOGGER.info('Login method VK') auth_text1 = auth_text.split("(\'.vkvk\').attr(\'url\', \'") auth_text2 = auth_text1[1].split('&response') browser.go_to(auth_text2[0]) browser.type(username, into='email') browser.type(password, xpath="/html/body/div/div/div/div[2]/form/div/div/input[7]") browser.click('Log in') return browser
def message(self, message): """send personal message""" LOGGER.info('"%s": PM: user id %s', self.api_wrapper.client.username, self.profile_id) browser = self.api_wrapper.client.get_browser() try: browser.go_to('https://rivalregions.com/#messages/{}'.format( self.profile_id)) self.api_wrapper.client.send_chat(browser, message) LOGGER.info('"%s:" PM: user id %s, finished sending message', self.api_wrapper.client.username, self.profile_id) finally: browser.close_current_tab()
def remove_cookie(cls, username): """Remove cookie from storage""" LOGGER.info('"%s": Removing cookie', username) cookies = None try: with open('{}/cookies.json'.format(DATA_DIR), 'r') as cookies_file: cookies = json.load(cookies_file) except FileNotFoundError: cookies = {} cookies.pop(username, None) with open('{}/cookies.json'.format(DATA_DIR), 'w+') as cookies_file: json.dump(cookies, cookies_file) LOGGER.info('"%s": Removed cookie', username)
def message(self, message): """send message to language chat""" LOGGER.info('"%s": CHAT: language %s', self.api_wrapper.client.username, self.language) browser = self.api_wrapper.client.get_browser() try: browser.go_to( 'https://rivalregions.com/#slide/chat/lang_{}'.format( self.language)) self.api_wrapper.client.send_chat(browser, message) LOGGER.info('"%s": CHAT: language %s, finished sending message', self.api_wrapper.client.username, self.language) finally: browser.close_current_tab()
def login_facebook(browser, auth_text, username, password): """login using Facebook""" LOGGER.info('Login method Facebook') auth_text1 = \ auth_text.split('">\r\n\t\t\t\t<div class="sa_sn imp float_left" ') auth_text2 = auth_text1[0].split('200px;"><a class="sa_link" href="') url = auth_text2[1] browser.go_to(url) browser.type(username, into='Email') browser.type(password, into='Password') browser.click('Log In') time.sleep(5) browser.click(css_selector='.sa_sn.imp.float_left') return browser
def post(self, path, data=None): """Send post request to Rival Regions""" if path[0] == '/': path = path[1:] if not data: data = {} data['c'] = self.var_c LOGGER.info('"%s": POST: "%s"', self.username, path) if self.session: response = self.session.post( "https://rivalregions.com/{}".format(path), data=data) self.check_response(response) else: raise NoLogginException() return response.text
def get(self, path, add_var_c=False): """Send get request to Rival Regions""" if path[0] == '/': path = path[1:] params = {} if add_var_c: params['c'] = self.var_c LOGGER.info('"%s": GET: "%s" var_c: %s', self.username, path, add_var_c) if self.session: response = self.session.get( url='https://rivalregions.com/{}'.format(path), params=params) self.check_response(response) else: raise NoLogginException() return response.text
def notification(self, message, sound): """Send notification to conference""" LOGGER.info('"%s": CONF: %s notification', self.api_wrapper.client.username, self.conference_id) data = { 'sound': 1 if sound else 0, 'text': message, 'c': self.api_wrapper.client.var_c, } if self.api_wrapper.client.session: response = self.api_wrapper.client.session.post( "https://rivalregions.com/rival/konffcm/{}/".format( self.conference_id), data=data) self.api_wrapper.client.check_response(response) else: raise authentication_handler.NoLogginException() LOGGER.info('"%s": CONF: id %s send notification ', self.api_wrapper.client.username, self.conference_id) return response.text
def change_title(self, title): """Change title of conference""" LOGGER.info('"%s": CONF: %s change title: %s', self.api_wrapper.client.username, self.conference_id, title) data = { 't': title, 'c': self.api_wrapper.client.var_c, } if self.api_wrapper.client.session: response = self.api_wrapper.client.session.post( "https://rivalregions.com/rival/changename/{}/".format( self.conference_id), data=data) self.api_wrapper.client.check_response(response) else: raise authentication_handler.NoLogginException() LOGGER.info('"%s": CONF: id %s changed title', self.api_wrapper.client.username, self.conference_id) return response.text
def get_cookies(cls, username): """Read cookies for username""" LOGGER.info('"%s": Reading cookie', username) cookies = [] try: with open('{}/cookies.json'.format(DATA_DIR), 'r') as cookies_file: cookies_data = json.load(cookies_file) for cookie_username, user_cookies in cookies_data.items(): if cookie_username == username: LOGGER.info('"%s": Found cookies', username) for cookie_name, cookie in user_cookies.items(): expires = datetime.fromtimestamp( int(cookie['expiry'])) if datetime.now() >= expires: LOGGER.info('"%s": Cookie is expired', username) return None cookies.append( cls.create_cookie( cookie_name, cookie['expiry'], cookie['value'], )) return cookies except FileNotFoundError: pass return cookies
def write_cookies(cls, username, passed_cookies): """Write cookie to file""" LOGGER.info('"%s": Saving cookie', username) cookies = None try: with open('{}/cookies.json'.format(DATA_DIR), 'r') as cookies_file: cookies = json.load(cookies_file) if not cookies: raise FileNotFoundError except FileNotFoundError: cookies = {username: {}} if username not in cookies: cookies[username] = {} for cookie in passed_cookies: cookies[username][cookie['name']] = { 'expiry': cookie['expires'], 'value': cookie['value'], } with open('{}/cookies.json'.format(DATA_DIR), 'w+') as cookies_file: json.dump(cookies, cookies_file) LOGGER.info('"%s": Saved cookie', username)
def login(self): """Login user if needed""" LOGGER.info('"%s": start login, method: "%s"', self.username, self.login_method) cookies = CookieHandler.get_cookies(self.username) if not cookies: cookies = [] LOGGER.info('"%s": no cookie, new login, method "%s"', self.username, self.login_method) login_method_dict = { 'g': login_methods.login_google, 'google': login_methods.login_google, 'v': login_methods.login_vk, 'vk': login_methods.login_vk, 'f': login_methods.login_facebook, 'facebook': login_methods.login_facebook, } auth_text = requests.get("https://rivalregions.com").text browser = Browser(showWindow=self.show_window) if self.login_method in login_method_dict: browser = login_method_dict[self.login_method](browser, auth_text, self.username, self.password) else: LOGGER.info('"%s": Invalid login method "%s"', self.username, self.login_method) sys.exit() LOGGER.info('"%s": Get PHPSESSID', self.username) browser_cookie = browser.get_cookie('PHPSESSID') if browser_cookie: expiry = browser_cookie.get('expiry', None) value = browser_cookie.get('value', None) LOGGER.info('"%s": "value": %s, "expiry": %s', self.username, value, expiry) cookie = CookieHandler.create_cookie('PHPSESSID', expiry, value) cookies.append(cookie) else: raise NoCookieException() cookie_names = ['rr_f'] for cookie_name in cookie_names: browser_cookie = browser.get_cookie(cookie_name) if browser_cookie: LOGGER.info('"%s": Get %s', self.username, cookie_name) expiry = browser_cookie.get('expiry', None) value = browser_cookie.get('value', None) cookies.append( CookieHandler.create_cookie(cookie_name, expiry, value)) LOGGER.info('"%s": "value": %s, "expiry": %s', self.username, value, expiry) else: raise NoCookieException() CookieHandler.write_cookies(self.username, cookies) LOGGER.debug('"%s": closing login tab', self.username) browser.close_current_tab() else: LOGGER.info('"%s": Cookies found', self.username) self.session = cfscrape.CloudflareScraper() for cookie in cookies: self.session.cookies.set(**cookie) LOGGER.debug('"%s": set the var_c', self.username) response = self.session.get('https://rivalregions.com/#overview') lines = response.text.split("\n") for line in lines: if re.match("(.*)var c_html(.*)", line): var_c = line.split("'")[-2] LOGGER.debug('"%s": got var_c: %s', self.username, var_c) self.var_c = line.split("'")[-2]
def __init__(self, *args, **kwargs): Exception.__init__(self, *args, **kwargs) LOGGER.warning('RRClientException')
def __init__(self, *args, **kwargs): Exception.__init__(self, *args, **kwargs) LOGGER.warning('Session has expired')
def __init__(self, *args, **kwargs): Exception.__init__(self, *args, **kwargs) LOGGER.warning('Not logged in')
def __init__(self, *args, **kwargs): Exception.__init__(self, *args, **kwargs) LOGGER.warning('No cookie found')
def __init__(self, show_window=False): self.show_window = show_window LOGGER.info('Initialize authentication handler, show window: "%s"', self.show_window)