Esempio n. 1
0
    def generate_response(self) -> Tuple[Any, int]:
        mobile = 'Android' in self.user_agent or 'iPhone' in self.user_agent

        content_filter = Filter(self.session['fernet_keys'],
                                mobile=mobile,
                                config=self.config)
        full_query = gen_query(self.query, self.request_params, self.config,
                               content_filter.near)
        get_body = g.user_request.send(query=full_query)

        # Produce cleanable html soup from response
        html_soup = bsoup(content_filter.reskin(get_body.text), 'html.parser')
        html_soup.insert(
            0,
            bsoup(TOR_BANNER, 'html.parser')
            if g.user_request.tor_valid else bsoup('', 'html.parser'))

        if self.feeling_lucky:
            return get_first_link(html_soup), 1
        else:
            formatted_results = content_filter.clean(html_soup)

            # Append user config to all search links, if available
            param_str = ''.join(
                '&{}={}'.format(k, v)
                for k, v in self.request_params.to_dict(flat=True).items()
                if self.config.is_safe_key(k))
            for link in formatted_results.find_all('a', href=True):
                if 'search?' not in link['href'] or link['href'].index(
                        'search?') > 1:
                    continue
                link['href'] += param_str

            return formatted_results, content_filter.elements
Esempio n. 2
0
def search():
    request_params = request.args if request.method == 'GET' else request.form
    q = request_params.get('q')

    if q is None or len(q) == 0:
        return redirect('/')
    else:
        # Attempt to decrypt if this is an internal link
        try:
            q = Fernet(app.secret_key).decrypt(q.encode()).decode()
        except InvalidToken:
            pass

    user_agent = request.headers.get('User-Agent')
    mobile = 'Android' in user_agent or 'iPhone' in user_agent

    content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key)
    full_query = gen_query(q,
                           request_params,
                           content_filter.near,
                           language=g.user_config.lang)
    get_body = g.user_request.send(query=full_query)

    results = content_filter.reskin(get_body)
    formatted_results = content_filter.clean(
        BeautifulSoup(results, 'html.parser'))

    return render_template('display.html',
                           query=urlparse.unquote(q),
                           response=formatted_results)
Esempio n. 3
0
    def generate_response(self) -> str:
        """Generates a response for the user's query

        Returns:
            str: A string response to the search query, in the form of a URL
                 or string representation of HTML content.

        """
        mobile = 'Android' in self.user_agent or 'iPhone' in self.user_agent

        content_filter = Filter(self.session_key,
                                mobile=mobile,
                                config=self.config)
        full_query = gen_query(self.query, self.request_params, self.config)
        self.full_query = full_query

        # force mobile search when view image is true and
        # the request is not already made by a mobile
        view_image = ('tbm=isch' in full_query and self.config.view_image
                      and not g.user_request.mobile)

        get_body = g.user_request.send(query=full_query,
                                       force_mobile=view_image)

        # Produce cleanable html soup from response
        html_soup = bsoup(get_body.text, 'html.parser')

        # Replace current soup if view_image is active
        if view_image:
            html_soup = content_filter.view_image(html_soup)

        # Indicate whether or not a Tor connection is active
        if g.user_request.tor_valid:
            html_soup.insert(0, bsoup(TOR_BANNER, 'html.parser'))

        if self.feeling_lucky:
            return get_first_link(html_soup)
        else:
            formatted_results = content_filter.clean(html_soup)

            # Append user config to all search links, if available
            param_str = ''.join(
                '&{}={}'.format(k, v)
                for k, v in self.request_params.to_dict(flat=True).items()
                if self.config.is_safe_key(k))
            for link in formatted_results.find_all('a', href=True):
                link['rel'] = "nofollow noopener noreferrer"
                if 'search?' not in link['href'] or link['href'].index(
                        'search?') > 1:
                    continue
                link['href'] += param_str

            return str(formatted_results)
Esempio n. 4
0
def search():
    request_params = request.args if request.method == 'GET' else request.form
    q = request_params.get('q')

    if q is None or len(q) == 0:
        return redirect('/')
    else:
        # Attempt to decrypt if this is an internal link
        try:
            q = Fernet(app.secret_key).decrypt(q.encode()).decode()
        except InvalidToken:
            pass

    feeling_lucky = q.startswith('! ')

    if feeling_lucky:  # Well do you, punk?
        q = q[2:]

    user_agent = request.headers.get('User-Agent')
    mobile = 'Android' in user_agent or 'iPhone' in user_agent

    content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key)
    full_query = gen_query(q, request_params, g.user_config,
                           content_filter.near)
    get_body = g.user_request.send(query=full_query)
    dirty_soup = BeautifulSoup(content_filter.reskin(get_body), 'html.parser')

    if feeling_lucky:
        return redirect(
            get_first_link(dirty_soup),
            303)  # Using 303 so the browser performs a GET request for the URL
    else:
        formatted_results = content_filter.clean(dirty_soup)

    # Set search type to be used in the header template to allow for repeated searches
    # in the same category
    search_type = request_params.get('tbm') if 'tbm' in request_params else ''

    return render_template(
        'display.html',
        query=urlparse.unquote(q),
        search_type=search_type,
        dark_mode=g.user_config.dark,
        response=formatted_results,
        search_header=render_template('header.html',
                                      dark_mode=g.user_config.dark,
                                      q=urlparse.unquote(q),
                                      search_type=search_type,
                                      mobile=g.user_request.mobile)
        if 'isch' not in search_type else '')
Esempio n. 5
0
    def generate_response(self) -> Tuple[Any, int]:
        """Generates a response for the user's query

        Returns:
            Tuple[Any, int]: A tuple in the format (response, # of elements)
                             For example, in the case of a "feeling lucky"
                             search, the response is a result URL, with no
                             encrypted elements to account for. Otherwise, the
                             response is a BeautifulSoup response body, with
                             N encrypted elements to track before key regen.

        """
        mobile = 'Android' in self.user_agent or 'iPhone' in self.user_agent

        content_filter = Filter(self.session['fernet_keys'],
                                mobile=mobile,
                                config=self.config)
        full_query = gen_query(self.query, self.request_params, self.config,
                               content_filter.near)
        get_body = g.user_request.send(query=full_query)

        # Produce cleanable html soup from response
        html_soup = bsoup(content_filter.reskin(get_body.text), 'html.parser')
        html_soup.insert(
            0,
            bsoup(TOR_BANNER, 'html.parser')
            if g.user_request.tor_valid else bsoup('', 'html.parser'))

        if self.feeling_lucky:
            return get_first_link(html_soup), 0
        else:
            formatted_results = content_filter.clean(html_soup)

            # Append user config to all search links, if available
            param_str = ''.join(
                '&{}={}'.format(k, v)
                for k, v in self.request_params.to_dict(flat=True).items()
                if self.config.is_safe_key(k))
            for link in formatted_results.find_all('a', href=True):
                if 'search?' not in link['href'] or link['href'].index(
                        'search?') > 1:
                    continue
                link['href'] += param_str

            return formatted_results, content_filter.elements
Esempio n. 6
0
    def generate_response(self) -> Tuple[Any, int]:
        mobile = 'Android' in self.user_agent or 'iPhone' in self.user_agent

        content_filter = Filter(self.session['fernet_keys'],
                                mobile=mobile,
                                config=self.config)
        full_query = gen_query(self.query, self.request_params, self.config,
                               content_filter.near)
        get_body = g.user_request.send(query=full_query).text

        # Produce cleanable html soup from response
        html_soup = BeautifulSoup(content_filter.reskin(get_body),
                                  'html.parser')

        if self.feeling_lucky:
            return get_first_link(html_soup), 1
        else:
            formatted_results = content_filter.clean(html_soup)
            return formatted_results, content_filter.elements
Esempio n. 7
0
def search():
    q = request.args.get('q')
    if q is None or len(q) <= 0:
        return render_template('error.html')

    user_agent = request.headers.get('User-Agent')
    mobile = 'Android' in user_agent or 'iPhone' in user_agent

    content_filter = Filter(mobile, user_config)
    full_query = gen_query(q, request.args, content_filter.near)
    get_body = g.user_request.send(query=full_query)

    shoogle_results = content_filter.reskin(get_body)
    formatted_results = content_filter.clean(
        BeautifulSoup(shoogle_results, 'html.parser'))

    return render_template('display.html',
                           query=urlparse.unquote(q),
                           response=formatted_results)
Esempio n. 8
0
def search():
    request_params = request.args if request.method == 'GET' else request.form
    q = request_params.get('q')

    if q is None or len(q) == 0:
        return redirect('/')
    else:
        # Attempt to decrypt if this is an internal link
        try:
            q = Fernet(app.secret_key).decrypt(q.encode()).decode()
        except InvalidToken:
            pass

    feeling_lucky = q.startswith('! ')

    if feeling_lucky:  # Well do you, punk?
        q = q[2:]

    user_agent = request.headers.get('User-Agent')
    mobile = 'Android' in user_agent or 'iPhone' in user_agent

    content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key)
    full_query = gen_query(q,
                           request_params,
                           content_filter.near,
                           language=g.user_config.lang)
    get_body = g.user_request.send(query=full_query)
    dirty_soup = BeautifulSoup(content_filter.reskin(get_body), 'html.parser')

    if feeling_lucky:
        return redirect(
            get_first_link(dirty_soup),
            303)  # Using 303 so the browser performs a GET request for the URL
    else:
        formatted_results = content_filter.clean(dirty_soup)

    return render_template('display.html',
                           query=urlparse.unquote(q),
                           response=formatted_results)