def __init__(self, keyword, language=''):
     self.client = twingly_search.Client(user_agent="MyCompany/1.0")
     self.q = self.client.query()
     self.q.search_query = "%s sort-order:asc sort:published " % keyword
     self.q.language = language
     self.q.start_time = datetime.datetime.utcnow() - datetime.timedelta(
         weeks=10)
     self.keyword = keyword
    def test_execute_query_with_invalid_api_key(self):
        temp_key = os.environ.get(TWINGLY_SEARCH_KEY)
        os.environ[TWINGLY_SEARCH_KEY] = 'wrong'
        c = twingly_search.Client()

        with Betamax(c._session).use_cassette('search_without_valid_api_key'):
            with self.assertRaises(twingly_search.TwinglySearchException):
                q = 'something'
                c.execute_query(q)

        os.environ[TWINGLY_SEARCH_KEY] = temp_key
    def test_search_for_spotify_on_sv_blogs(self):
        c = twingly_search.Client()

        with Betamax(
                c._session).use_cassette('search_for_spotify_on_sv_blogs'):
            q = 'spotify page-size:10 lang:sv'
            result = c.execute_query(q)
            self.assertIsNotNone(result)
            self.assertEqual(result.incomplete_result, False)
            self.assertEqual(result.number_of_matches_returned, 10)
            self.assertEqual(len(result.posts), 10)
    def test_search_for_spotify_on_sv_blogs_using_query_object(self):
        c = twingly_search.Client()

        with Betamax(c._session).use_cassette(
                'search_for_spotify_on_sv_blogs_using_query'):
            q = c.query()
            q.search_query = 'spotify page-size:20'
            q.language = 'en'
            q.start_time = dateutil.parser.parse("2017-03-09 18:03:43")
            result = c.execute_query(q)
            self.assertIsNotNone(result)
            self.assertEqual(result.incomplete_result, False)
            self.assertEqual(result.number_of_matches_returned, 1)
            self.assertEqual(len(result.posts), 1)
 def setUp(self):
     self._client = twingly_search.Client()
 def test_with_no_api_key_at_all(self):
     temp_key = os.environ.get(TWINGLY_SEARCH_KEY)
     del os.environ[TWINGLY_SEARCH_KEY]
     with self.assertRaises(twingly_search.TwinglySearchException):
         twingly_search.Client()
     os.environ[TWINGLY_SEARCH_KEY] = temp_key
 def test_with_optional_user_agent_given(self):
     c = twingly_search.Client(user_agent="Test User-Agent")
     self.assertEqual(c._user_agent, "Test User-Agent")
 def test_without_api_key_as_parameter(self):
     c = twingly_search.Client()
     self.assertEqual(c.api_key, os.environ.get(TWINGLY_SEARCH_KEY))
 def test_query(self):
     q = twingly_search.Client().query()
     self.assertIsInstance(q, twingly_search.Query)
 def test_new(self):
     c = twingly_search.Client("test-key")
     self.assertEqual(
         c._user_agent,
         'Twingly Search Python Client/%s' % (twingly_search.__version__))
Пример #11
0
 def __init__(self):
     self.client = twingly_search.Client()
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import twingly_search

client = twingly_search.Client()
q = '"hello world" tspan:24h'
results = client.execute_query(q)

for post in results.posts:
    print(post.url)