Exemple #1
0
def in_ua_black_list(request):
    """检查手机ua是否在黑名单里
    """
    conf = game_config.sys_config.get('ua_black_list',{})
    flag = False
    ua = uamobile.detect(request.META)
    if request.agent_device['device'].is_smartphone:
        if request.agent_device['device'].is_ios:
            return True
        return False
    #docomo
    elif ua.is_docomo():
        if ua.model in conf.get('docomo',[]):
            flag = True
    #au
    elif ua.is_ezweb():
        if ua.model in conf.get('au',[]):
            flag = True
    #softbank
    elif ua.is_softbank():
        if ua.model in conf.get('softbank',[]):
            flag = True
    #willcom机型全部屏蔽
    elif ua.is_willcom():
        flag = True
    else:
        userAgent = str(request.META.get("HTTP_USER_AGENT")).lower()
        match = re.match("(.*)"+"mozilla"+"(.*)", userAgent)
        if not match:
            match = re.match("(.*)"+"willcom"+"(.*)", userAgent)
        if match:
            flag = True
    return flag
    def process_response(self, request, response):
        # encoding変換済みならスキップ
        if getattr(response, 'encoded', False):
            return response

        agent = uamobile.detect(request.META)

        if not agent.is_nonmobile():
            encoding = 'UTF-8'
            if response['content-type'].startswith('text'):
                c = unicode(response.content, 'utf8')
                if agent.is_docomo():
                    response.content = c.encode('cp932', 'replace')
                    encoding = 'Shift_JIS'
                elif agent.is_ezweb():
                    c = utils.RE_UNI_EMOJI_KDDI.sub(
                        lambda m: utils.kddi_uni2xuni(m.group()), c)
                    response.content = c.encode('cp932', 'replace')
                    encoding = 'Shift_JIS'
                elif agent.is_softbank():
                    c = utils.RE_UNI_EMOJI_DOCOMO.sub(
                        lambda m: utils.uni_docomo2softbank(m.group()), c)
                    response.content = c.encode('utf8', 'replace')
            response[
                'content-type'] = 'application/xhtml+xml; charset=%s' % encoding

        return response
Exemple #3
0
def in_ua_black_list(request):
    """检查手机ua是否在黑名单里
    """
    conf = game_config.sys_config.get('ua_black_list', {})
    flag = False
    ua = uamobile.detect(request.META)
    if request.agent_device['device'].is_smartphone:
        if request.agent_device['device'].is_ios:
            return True
        return False
    #docomo
    elif ua.is_docomo():
        if ua.model in conf.get('docomo', []):
            flag = True
    #au
    elif ua.is_ezweb():
        if ua.model in conf.get('au', []):
            flag = True
    #softbank
    elif ua.is_softbank():
        if ua.model in conf.get('softbank', []):
            flag = True
    #willcom机型全部屏蔽
    elif ua.is_willcom():
        flag = True
    else:
        userAgent = str(request.META.get("HTTP_USER_AGENT")).lower()
        match = re.match("(.*)" + "mozilla" + "(.*)", userAgent)
        if not match:
            match = re.match("(.*)" + "willcom" + "(.*)", userAgent)
        if match:
            flag = True
    return flag
 def func(ip, useragent):
     device = detect({'HTTP_USER_AGENT': useragent, 'REMOTE_ADDR': ip})
     assert device.is_docomo() is docomo
     assert device.is_ezweb() is kddi
     assert device.is_softbank() is softbank
     assert device.is_crawler()
     assert device.is_bogus()
 def func(ip, useragent):
     device = detect({'HTTP_USER_AGENT': useragent,
                      'REMOTE_ADDR'    : ip})
     assert device.is_docomo() is docomo
     assert device.is_ezweb() is kddi
     assert device.is_softbank() is softbank
     assert device.is_crawler()
     assert device.is_bogus()
Exemple #6
0
 def process_request(self, request):
     try:
         request.device = detect(request.META)
         if request.device.is_nonmobile():
             request.is_mobile = False
         else:
             request.is_mobile = True
     except NoMatchingError, e:
         request.is_mobile = False
def test_extra_ip():
    ctxt1 = Context(extra_crawler_ips=['192.168.0.0/24'])
    ua = detect({'HTTP_USER_AGENT': 'Mozilla/3.0(WILLCOM;KYOCERA/WX310K/2;1.2.7.17.000000/0.1/C100) Opera 7.0',
                 'REMOTE_ADDR'    : '192.168.0.1',
                 },
                context=ctxt1)
    assert ua.is_willcom()
    assert ua.is_crawler() is True
    assert ua.is_bogus() is True

    ctxt2 = Context(extra_crawler_ips=[])
    ua = detect({'HTTP_USER_AGENT': 'Mozilla/3.0(WILLCOM;KYOCERA/WX310K/2;1.2.7.17.000000/0.1/C100) Opera 7.0',
                 'REMOTE_ADDR'    : '192.168.0.1',
                 },
                context=ctxt2)
    assert ua.is_willcom()
    assert ua.is_crawler() is False
    assert ua.is_bogus() is True
Exemple #8
0
 def process_request(self, request):
   try:
     request.device = detect(request.META)
     if request.device.is_nonmobile():
       request.is_mobile = False
     else:
       request.is_mobile = True
   except NoMatchingError, e:
     request.is_mobile = False
Exemple #9
0
 def detect(self, environ):
     encoding = None
     # try:
     import uamobile
     agent = uamobile.detect(environ)
     if agent.is_nonmobile() or agent.is_softbank():
         encoding = 'utf-8'
     else:
         encoding = 'cp932'
     return encoding
Exemple #10
0
 def detect(self, environ):
     encoding = None
     # try:
     import uamobile
     agent = uamobile.detect(environ)
     if agent.is_nonmobile() or agent.is_softbank():
         encoding = 'utf-8'
     else:
         encoding = 'cp932'
     return encoding
Exemple #11
0
def to_device_name(META):
    device = detect(META)
    if device.is_nonmobile():
        ua = META.get('HTTP_USER_AGENT', '')
        if _IOS_RE.search(ua):
            return DeviceType.SP_iPhone
        elif _ANDROID_RE.search(ua):
            return DeviceType.SP_Android
        else:
            return DeviceType.Unknown
    else:
        return DeviceType.FP
Exemple #12
0
def to_device_name(META):
    device = detect(META)
    if device.is_nonmobile():
        ua = META.get('HTTP_USER_AGENT', '')
        if _IOS_RE.search(ua):
            return DeviceType.SP_iPhone
        elif _ANDROID_RE.search(ua):
            return DeviceType.SP_Android
        else:
            return DeviceType.Unknown
    else:
        return DeviceType.FP
    def process_request(self, request):
        request.agent = uamobile.detect(request.META)
        if request.agent.is_nonmobile() or request.agent.is_docomo():
            return

        def _convert_dict(d, agent):
            for k, v in d.iteritems():
                if agent.is_softbank():
                    d[k] = utils.RE_UNI_EMOJI_SOFTBANK.sub(lambda m:utils.uni_softbank2docomo(m.group()), v)
                elif agent.is_ezweb():
                    d[k] = utils.RE_UNI_EMOJI_KDDI.sub(lambda m:utils.uni_kddi2docomo(m.group()), v)

        request.GET._mutable = True
        request.POST._mutable = True
        _convert_dict(request.GET, request.agent)
        _convert_dict(request.POST, request.agent)
        request.GET._mutable = False
        request.POST._mutable = False
Exemple #14
0
    def process_request(self, request):
        request.agent = uamobile.detect(request.META)
        if request.agent.is_nonmobile() or request.agent.is_docomo():
            return

        def _convert_dict(d, agent):
            for k, v in d.iteritems():
                if agent.is_softbank():
                    d[k] = utils.RE_UNI_EMOJI_SOFTBANK.sub(
                        lambda m: utils.uni_softbank2docomo(m.group()), v)
                elif agent.is_ezweb():
                    d[k] = utils.RE_UNI_EMOJI_KDDI.sub(
                        lambda m: utils.uni_kddi2docomo(m.group()), v)

        request.GET._mutable = True
        request.POST._mutable = True
        _convert_dict(request.GET, request.agent)
        _convert_dict(request.POST, request.agent)
        request.GET._mutable = False
        request.POST._mutable = False
Exemple #15
0
    def _featurephone_check(self, request, context):
        """
        uamobileのdetectを使い、featurephone識別をする
        """
        ua_agent = detect(request.META, context)
        self.detect = ua_agent

        if ua_agent.is_docomo():
            self.is_featurephone = True
            self.is_docomo = True
            self.name = 'docomo'
        elif ua_agent.is_ezweb():
            self.is_featurephone = True
            self.is_au = True
            self.is_ezweb = True
            self.name = 'au'
        elif ua_agent.is_softbank():
            self.is_featurephone = True
            self.is_softbank = True
            self.name = 'softbank'
    def process_response(self, request, response):
        # encoding変換済みならスキップ
        if getattr(response, 'encoded', False):
            return response

        agent = uamobile.detect(request.META)

        if not agent.is_nonmobile():
            encoding = 'UTF-8'
            if response['content-type'].startswith('text'):
                c = unicode(response.content,'utf8')
                if agent.is_docomo():
                    response.content = c.encode('cp932','replace')
                    encoding = 'Shift_JIS'
                elif agent.is_ezweb():
                    c = utils.RE_UNI_EMOJI_KDDI.sub(lambda m:utils.kddi_uni2xuni(m.group()), c)
                    response.content = c.encode('cp932','replace')
                    encoding = 'Shift_JIS'
                elif agent.is_softbank():
                    c = utils.RE_UNI_EMOJI_DOCOMO.sub(lambda m:utils.uni_docomo2softbank(m.group()), c)
                    response.content = c.encode('utf8','replace')
            response['content-type'] = 'application/xhtml+xml; charset=%s' % encoding
        
        return response
Exemple #17
0
 def process_request(self, request):
     request.device = detect(request.META, self.context)
     _thread_locals.request = request
     _thread_locals.device  = request.device
 def func(ip, useragent):
     device = detect({'HTTP_USER_AGENT': useragent,
                      'REMOTE_ADDR'    : ip})
     assert device.is_docomo()
     assert device.is_bogus()
     assert device.is_crawler() is False
Exemple #19
0
 def check_device(self):
     return detect(self.handler.request.environ)
Exemple #20
0
 def get_agent(self, request):
     return getattr(request, 'agent', uamobile.detect(request.META))
Exemple #21
0
 def get_agent(self, request):
     return getattr(request, 'agent', uamobile.detect(request.META))
Exemple #22
0
 def process_request(self, request):
     request.device = detect(request.META, self.context)
     _thread_locals.request = request
     _thread_locals.device = request.device
Exemple #23
0
 def func(version, ua):
     device = detect({'HTTP_USER_AGENT': ua})
     assert device.flash_version == version, device.flash_version
     assert device.supports_flash() == (version is not None)