コード例 #1
0
ファイル: live_test.py プロジェクト: whs/terroroftinytown
    def test_custom_services(self):
        # for python 2.6 compatbility
        if os.environ.get('NO_LIVE_SERVICE_TEST'):
            print('SKIP')
            return

        filenames = get_definition_filenames()
        for filename in filenames:
            service_name = os.path.split(filename)[-1].replace('.txt', '')
            params = MOCK_PARAMS[service_name]
            service = registry[u(service_name)](params)

            print('Brought up service', service)

            with codecs.open(filename, 'r', encoding='utf-8') as def_file:
                for shortcode, expected_result in iterate_definition_file(def_file):
                    service.current_shortcode = shortcode
                    url = params['url_template'].format(shortcode=shortcode)

                    print('Requesting', url, 'Expect:', expected_result)

                    response = service.fetch_url(url)
                    url_status, result_url, encoding = service.process_response(response)

                    print('  Got', url_status, result_url, encoding)

                    if url_status == URLStatus.ok:
                        self.assertEqual(expected_result, result_url)
                    else:
                        self.assertEqual(expected_result, url_status)
コード例 #2
0
    def test_custom_services(self):
        # for python 2.6 compatbility
        if os.environ.get('NO_LIVE_SERVICE_TEST'):
            print('SKIP')
            return

        filenames = get_definition_filenames()
        for filename in filenames:
            service_name = os.path.split(filename)[-1].replace('.txt', '')

            #             if service_name not in ('tinyurl',):
            #                 print('Skip', service_name)
            #                 continue

            params = MOCK_PARAMS[service_name]
            service = registry[u(service_name)](params)

            print('Brought up service', service)

            service.prepare()

            with codecs.open(filename, 'rb') as def_file:
                for shortcode, expected_result in iterate_definition_file(
                        def_file):
                    service.current_shortcode = shortcode
                    url = params['url_template'].format(shortcode=shortcode)

                    print('Requesting', url, 'Expect:', expected_result)

                    try:
                        response = service.fetch_url(url)
                    except MalformedResponse:
                        url_status, result_url, encoding = (
                            URLStatus.unavailable, None, None)
                    else:
                        url_status, result_url, encoding = service.process_response(
                            response)

                    if terroroftinytown.six.PY2 and \
                            isinstance(result_url, terroroftinytown.six.binary_type):
                        result_url = result_url.decode(encoding)

                    print('  Got', url_status, result_url, encoding)

                    if url_status == URLStatus.ok:
                        self.assertEqual(expected_result, result_url)
                    else:
                        self.assertEqual(expected_result, url_status)

                    time.sleep(0.7)
コード例 #3
0
    def test_custom_services(self):
        # for python 2.6 compatbility
        if os.environ.get('NO_LIVE_SERVICE_TEST'):
            print('SKIP')
            return

        filenames = get_definition_filenames()
        for filename in filenames:
            service_name = os.path.split(filename)[-1].replace('.txt', '')

#             if service_name not in ('tinyurl',):
#                 print('Skip', service_name)
#                 continue

            params = MOCK_PARAMS[service_name]
            service = registry[u(service_name)](params)

            print('Brought up service', service)

            service.prepare()

            with codecs.open(filename, 'rb') as def_file:
                for shortcode, expected_result in iterate_definition_file(def_file):
                    service.current_shortcode = shortcode
                    url = params['url_template'].format(shortcode=shortcode)

                    print('Requesting', url, 'Expect:', expected_result)
                    
                    try:
                        response = service.fetch_url(url)
                    except MalformedResponse:
                        url_status, result_url, encoding = (URLStatus.unavailable, None, None)
                    else:
                        url_status, result_url, encoding = service.process_response(response)

                    if terroroftinytown.six.PY2 and \
                            isinstance(result_url, terroroftinytown.six.binary_type):
                        result_url = result_url.decode(encoding)

                    print('  Got', url_status, result_url, encoding)

                    if url_status == URLStatus.ok:
                        self.assertEqual(expected_result, result_url)
                    else:
                        self.assertEqual(expected_result, url_status)

                    time.sleep(0.7)
コード例 #4
0
ファイル: scraper.py プロジェクト: ZeroCry/terroroftinytown
 def get_service(self):
     if self.params['name'] in registry:
         return registry[self.params['name']]
     else:
         return registry[u('_default')]
コード例 #5
0
ファイル: registry.py プロジェクト: whs/terroroftinytown
from terroroftinytown.services.base import DefaultService
from terroroftinytown.services.isgd import IsgdService
from terroroftinytown.six import u
from terroroftinytown.services.bitly import BitlyService


registry = {}
'''Mapping of unicode strings to BaseService classes.'''


registry[u('_default')] = DefaultService
registry[u('isgd')] = IsgdService
registry[u('bitly')] = BitlyService