Esempio n. 1
0
def display_favorite_question(favorite_id):
    """Return favorite related quesion, answers and other data."""
    if check_favorites():
        answers = ''
        with open(FAVORITES, "r") as f:
            for el in json.load(f)['items']:
                if el["question_id"] == favorite_id:
                    question = """
                    <h3>{title}</h3>
                    <br>
                    {body}
                    <br><br>
                    """.format(title=el["title"], body=el["body_markdown"])
                    resp_count = 1
                    for answer in el["answers"]:
                        answers = answers + """
                        <u>Accepted: {accepted}</u>
                        <br>
                        <a href="{link}">Link</a>
                        <br>
                        Response number: {number}
                        <br>
                        {response}
                        <br><br>
                        """.format(
                            accepted=answer["is_accepted"], 
                            link=answer["share_link"], 
                            number=str(resp_count), 
                            response=answer["body"])
                        resp_count += 1
                    logger.debug("Return favorite related quesion, answers and other data. Favorite Id: {}".format(favorite_id))
                    return (question, resp_count, answers)
            logger.debug("[!] Unable to find any favorite related quesion, answers and other data.")
            return False    
Esempio n. 2
0
def request_favorites(oauth_manager):
    stored_access_token = False
    if not check_access_token():
        if not check_user_credentials():
            print("[!] Unable to request for favorites.")
            logger.debug("[!] Unable to send request favorites.")
            return False
            # prompt_user_credentials(bypass_check=True)

        access_token = request_access_token()
        with open(USERDATA, 'a') as f:
            f.write("\n{0}".format(access_token))
            print()
            print("Access token saved to file.")
            logger.debug("Access token saved to file.")
        
        stored_access_token = access_token

    if not stored_access_token:
        logger.debug("Access token exist, retrieving favorites from file.")
        with open(USERDATA, 'r') as f:
            user_data = f.readlines()
            stored_access_token = user_data[2]

    with open(FAVORITES, 'w') as f:
            json.dump(oauth_manager.fetch_from_api(stored_access_token).json(), f, indent=4)
            print()
            print("Favorites are locally stored!")
            logger.debug("Favorites are locally stored.")
            return True
def cache_favorites(interface, OAuth2Session, Firefox, browser_opts, queue):
    logger.debug("Creating oAuth Session")
    oauth_manager = oAuthManager(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI,
                                 OAuth2Session, SCOPE)
    oauth_manager.create_session()
    if not check_access_token():
        browser = Firefox(options=browser_opts)

        driver = DriverManager(CLIENT_ID, CLIENT_SECRET, browser)

        user_data = get_user_credentials()

        resp = request_access_token(driver, oauth_manager, user_data)
        if not resp:
            print("Unable to get the access token.")
            return

    if request_favorites(oauth_manager):
        return get_questions()
    else:
        return False
Esempio n. 4
0
def get_questions():
    if check_favorites():
        response_list = []
        with open(FAVORITES, 'r') as f:
            for el in json.load(f)['items']:
                for x in el:
                    # print("""
                    # Title:      {title}
                    # Text:       {text}
                    # Answered:   {is_answered}
                    # Views:      {views}
                    # Answers:    {answers}
                    # Score:      {score}
                    # Link:       {link}
                    # """.format(
                    #     title=el['title'], 
                    #     text=el['body_markdown'], 
                    #     is_answered=el['is_answered'], 
                    #     views=el['view_count'],
                    #     answers=el['answer_count'],
                    #     score=el['score'],
                    #     link=el['link'],))
                    response_dict = {
                        "question_id": el['question_id'],
                        "tags": el['tags'],
                        "title": el['title'],
                        # "text": el['body_markdown'],
                        "answered": el['is_answered'],
                        # "views": el['view_count'],
                        # "answers": el['answer_count'],
                        "score": el['score'],
                        # "link": el['link'],
                    }
                response_list.append(response_dict)
        logger.debug("Questions were collected.")
        return response_list                    
    return False
Esempio n. 5
0
def check_user_credentials():
    if os.path.isfile(USERDATA):
        with open(USERDATA, 'r') as f:
            arr_len = f.readlines()
            if len(arr_len) > 1 and len(arr_len) < 4:
                logger.debug("User credentials are stored.")
                return True
            logger.debug("[!] User credentials are not stored, though file with user data exists.")
            return False
    logger.debug("[!] User credentials are not stored and neither does the user_data file exist.")
    return False
Esempio n. 6
0
def check_access_token():
    if os.path.isfile(USERDATA):
        with open(USERDATA, 'r') as f:
            arr_len = f.readlines()
            if len(arr_len) == 3:
                logger.debug("Access token is stored.")
                return True
            logger.debug("Access token is not stored.")
            return False
    logger.debug("[!] Access token is not stored and neither does the user_data file exist.")
    return False
Esempio n. 7
0
 def extract_state_token(self, current_url):
     attempts = 3
     while not attempts == 0:
         try:
             access_token, state = current_url.split("#")[1].split("&")
             access_token = access_token.split("=")[1]
             state = state.split("=")[1]
             logger.debug("Extracted access_token: {}".format(access_token))
             logger.debug("Extracted state: {}".format(state))
             return (access_token, state)
             # attempts = 0
         except:
             attempts -= 1
             time.sleep(2)
             logger.debug("[!] Unable to extract access_token and state.")
Esempio n. 8
0
def request_access_token(driver, oauth_manager, user_data):

    oauth_manager.get_authorization_state_url()

    driver._go_to(oauth_manager.authorization_url)

    with open(USERDATA, 'r') as f:
        user_data = f.readlines()

        print()
        print("Entering user data...")

        user_list_dict = [{'value': user_data[0], "xpath":'//*[@id="email"]'}, {'value': user_data[1], "xpath":'//*[@id="password"]'}]
        driver._type(*user_list_dict)

        driver._click('//*[@id="submit-button"]')
        
        print()
        print("Searching for access token...")

        url = driver._get_current_url()
        print(url)
        driver._quit()

        print()
        print("Extracting access token...")
        logger.debug("Extracting access token.")

        try:
            access_token, state = oauth_manager.extract_state_token(url)
            with open(USERDATA, 'a') as f:
                f.write("\n{0}".format(access_token))
                print()
                print("Access token saved to file.")
                logger.debug("Access token saved to file.")
                return True
        except TypeError:
            print("Unable to extract token! (It could be related to incorrect credentials)")
            logger.debug("[!] Unable to extract token! (It could be related to incorrect credentials or captcha)")
            # sys.exit(1)
            return False
Esempio n. 9
0
 def _go_to(self, authorization_url):
     logger.debug("Driver going to: {}".format(authorization_url))
     self.driver.get(authorization_url)
Esempio n. 10
0
 def _click(self, login_button_xpath):
     logger.debug("Clicked on login button.")
     submit_button = self.driver.find_elements_by_xpath(
         login_button_xpath)[0]
     submit_button.click()
Esempio n. 11
0
def get_user_credentials():
    with open(USERDATA, 'r') as f:
        user_data = f.readlines()
        logger.debug("Retrieving user credentials.")
        return [data.strip("\n") for data in user_data]
Esempio n. 12
0
def check_favorites():
    if os.path.isfile(FAVORITES) and not os.stat(FAVORITES).st_size == 0:
        logger.debug("Favorites are locally stored.")
        return True
    logger.debug("[!] Favorites are not locally stored.")
    return False
Esempio n. 13
0
 def _get_current_url(self):
     logger.debug("Current URL: {}".format(self.driver.current_url))
     time.sleep(2)
     return self.driver.current_url
Esempio n. 14
0
 def _quit(self):
     logger.debug("Driver process ended.")
     self.driver.quit()
Esempio n. 15
0
 def create_session(self):
     logger.debug("Created oAuth Session.")
     self.oauth = self.oauth_session(self.client_id,
                                     redirect_uri=self.redirect_uri,
                                     scope=self.scope)
Esempio n. 16
0
 def get_authorization_state_url(self):
     vars_ = self.oauth.authorization_url(
         'https://stackoverflow.com/oauth/dialog')
     logger.debug("Getting authorization url: {}".format(vars_[0]))
     self.authorization_url, self.state = vars_
Esempio n. 17
0
 def _type(self, *args):
     logger.debug("Typing user email and password.")
     for each_input in args:
         # self.driver.type(each_input['value'], xpath=each_input['xpath'])
         field = self.driver.find_element_by_xpath(each_input['xpath'])
         field.send_keys(each_input['value'])
Esempio n. 18
0
 def fetch_from_api(self, access_token):
     filter = "!)5bKRrkYVUeqn(O3dIVXK4gH800i"
     url = BASE_API + "me/favorites?pagesize=100&order=desc&sort=added&site=stackoverflow&filter={0}&access_token={1}&key={2}".format(
         filter, access_token, self.client_secret)
     logger.debug("Fetching from API: {}".format(url))
     return self.oauth.get(url)