Ejemplo n.º 1
0
    def test_standard_calls(self, mock_sleep, mock_get):

        mock_get_response = mock.Mock()
        mock_get_response.content = self.html
        mock_get.return_value = mock_get_response

        results = poogle.google_search('test query')
        self.assertEqual(len(results), 10)

        results = poogle.google_search('test query', 20)
        self.assertEqual(len(results), 20)

        mock_sleep.assert_not_called()
Ejemplo n.º 2
0
    def test_standard_calls(self, mock_sleep, mock_get):

        mock_get_response = mock.Mock()
        mock_get_response.content = self.html
        mock_get.return_value = mock_get_response

        results = poogle.google_search('test query')
        self.assertEqual(len(results), 10)

        results = poogle.google_search('test query', 20)
        self.assertEqual(len(results), 20)

        mock_sleep.assert_not_called()
Ejemplo n.º 3
0
        def _lucky(args, response):
            """
            @type   args:       argparse.Namespace
            @type   response:   firefly.containers.Response
            """
            try:
                query = ' '.join(args.query)
                results = google_search(query, 1)
            except PoogleNoResultsError:
                message = "Sorry, I couldn't find anything for {q}".format(q=style(query, bold=True))
                response.add_message(message)
                return

            response.add_message(results[0].url.as_string())
Ejemplo n.º 4
0
        def _search(args, response):
            """
            @type   args:       argparse.Namespace
            @type   response:   firefly.containers.Response
            """
            try:
                query = ' '.join(args.query)
                results = google_search(query, args.results)
            except PoogleNoResultsError:
                message = "Sorry, I couldn't find anything for {q}".format(q=style(query, bold=True))
                response.add_message(message)
                return

            formatted_results = []
            for result in results:
                formatted_results.append(
                    self.template.format(title=style(result.title, bold=True), url=result.url.as_string())
                )

            response.add_message(
                self.separator.join(formatted_results)
            )
Ejemplo n.º 5
0
def cli(ctx, query, results, plain):
    """
    Execute a Google search query and display the results
    """
    assert isinstance(ctx, Context)

    # Execute our search query
    click.echo('Executing search query for {q}\n'.format(q=click.style(query, 'blue', bold=True)))
    results = google_search(query, results)

    # Split our query parts for highlighting
    query_parts = query.split()

    for result in results:
        # Formatted results
        if not plain:
            title = click.style(result.title, bold=True)
            for part in query_parts:
                ctx.log.info('Parsing highlighting for query parameter %s', part)
                matches = re.findall(re.escape(part), title, re.IGNORECASE)
                if not matches:
                    continue

                matches = set(matches)
                for match in matches:
                    highlighted = click.style(match, 'blue', bold=True)
                    title = title.replace(match, highlighted + click.style('', bold=True, reset=False))

            click.secho(title)
            click.secho('=' * 30)
            click.secho(result.url.as_string())
        else:
            click.echo(result.title)
            click.echo('=' * 30)
            click.echo(result.url.as_string())

        click.echo()