<commit_before># -*- coding: utf-8 -*-
"""
    line.client
    ~~~~~~~~~~~

    LineClient for sending and receiving message from LINE server.

    :copyright: (c) 2014 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
import rsa
import requests
try:
    import simplejson as json
except ImportError:
    import json

from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TCompactProtocol

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

#from curve import CurveThrift
from curve import CurveThrift
from curve.ttypes import TalkException
from curve.ttypes import ToType, ContentType

class LineAPI(object):
    """This class is a wrapper of LINE API

    """
    LINE_DOMAIN = "http://gd2.line.naver.jp"

    LINE_HTTP_URL          = LINE_DOMAIN + "/api/v4/TalkService.do"
    LINE_HTTP_IN_URL       = LINE_DOMAIN + "/P4"
    LINE_CERTIFICATE_URL   = LINE_DOMAIN + "/Q"
    LINE_SESSION_LINE_URL  = LINE_DOMAIN + "/authct/v1/keys/line"
    LINE_SESSION_NAVER_URL = LINE_DOMAIN + "/authct/v1/keys/naver"

    ip          = "127.0.0.1"
    version     = "5.1.2"
    com_name    = ""
    revision    = 0
    certificate = ""

    _session = requests.session()
    _headers = {}

    def ready(self):
        """
        After login, make `client` and `client_in` instance
        to communicate with LINE server
        """
        raise Exception("Code is removed because of the request of LINE corporation")

    def updateAuthToken(self):
        """
        After login, update authToken to avoid expiration of
        authToken. This method skip the PinCode validation step.
        """
        if self.certificate:
            self.login()
            self.tokenLogin()

            return True
        else:
            self.raise_error("You need to login first. There is no valid certificate")

    def tokenLogin(self):
        self.transport = THttpClient.THttpClient(self.LINE_HTTP_URL)
        self.transport.setCustomHeaders(self._headers)

        self.protocol = TCompactProtocol.TCompactProtocol(self.transport)
        self._client  = CurveThrift.Client(self.protocol)
        
    def login(self):
        """Login to LINE server."""
        if self.provider == CurveThrift.Provider.LINE: # LINE
            j = self._get_json(self.LINE_SESSION_LINE_URL)
        else: # NAVER
            j = self._get_json(self.LINE_SESSION_NAVER_URL)

        session_key = j['session_key']
        message     = (chr(len(session_key)) + session_key +
                       chr(len(self.id)) + self.id +
                       chr(len(self.password)) + self.password).encode('utf-8')

        keyname, n, e = j['rsa_key'].split(",")
        pub_key       = rsa.PublicKey(int(n,16), int(e,16))
        crypto        = rsa.encrypt(message, pub_key).encode('hex')

        self.transport = THttpClient.THttpClient(self.LINE_HTTP_URL)
        self.transport.setCustomHeaders(self._headers)

        self.protocol = TCompactProtocol.TCompactProtocol(self.transport)
        self._client   = CurveThrift.Client(self.protocol)

        msg = self._client.loginWithIdentityCredentialForCertificate(
                self.id, self.password, keyname, crypto, True, self.ip,
                self.com_name, self.provider, self.certificate)

        self._headers['X-Line-Access'] = msg.verifier
        self._pinCode = msg.pinCode

        if msg.type == 3:
            print "Enter PinCode '%s' to your mobile phone in 2 minutes"\
                    % self._pinCode

            raise Exception("Code is removed because of the request of LINE corporation")
        else:
            self.authToken =self._headers['X-Line-Access'] = msg.authToken
<commit_msg>fix duplicate pincode check. Thanks to @anusoft<commit_after># -*- coding: utf-8 -*-
"""
    line.client
    ~~~~~~~~~~~

    LineClient for sending and receiving message from LINE server.

    :copyright: (c) 2014 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""
import rsa
import requests
try:
    import simplejson as json
except ImportError:
    import json

from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TCompactProtocol

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

#from curve import CurveThrift
from curve import CurveThrift
from curve.ttypes import TalkException
from curve.ttypes import ToType, ContentType

class LineAPI(object):
    """This class is a wrapper of LINE API

    """
    LINE_DOMAIN = "http://gd2.line.naver.jp"

    LINE_HTTP_URL          = LINE_DOMAIN + "/api/v4/TalkService.do"
    LINE_HTTP_IN_URL       = LINE_DOMAIN + "/P4"
    LINE_CERTIFICATE_URL   = LINE_DOMAIN + "/Q"
    LINE_SESSION_LINE_URL  = LINE_DOMAIN + "/authct/v1/keys/line"
    LINE_SESSION_NAVER_URL = LINE_DOMAIN + "/authct/v1/keys/naver"
    
    CERT_FILE = ".line.crt"

    ip          = "127.0.0.1"
    version     = "5.1.2"
    com_name    = ""
    revision    = 0
    certificate = ""

    _session = requests.session()
    _headers = {}

    def ready(self):
        """
        After login, make `client` and `client_in` instance
        to communicate with LINE server
        """
        raise Exception("Code is removed because of the request of LINE corporation")

    def updateAuthToken(self):
        """
        After login, update authToken to avoid expiration of
        authToken. This method skip the PinCode validation step.
        """
        if self.certificate:
            self.login()
            self.tokenLogin()

            return True
        else:
            self.raise_error("You need to login first. There is no valid certificate")

    def tokenLogin(self):
        self.transport = THttpClient.THttpClient(self.LINE_HTTP_URL)
        self.transport.setCustomHeaders(self._headers)

        self.protocol = TCompactProtocol.TCompactProtocol(self.transport)
        self._client  = CurveThrift.Client(self.protocol)
        
    def login(self):
        """Login to LINE server."""
        if self.provider == CurveThrift.Provider.LINE: # LINE
            j = self._get_json(self.LINE_SESSION_LINE_URL)
        else: # NAVER
            j = self._get_json(self.LINE_SESSION_NAVER_URL)

        session_key = j['session_key']
        message     = (chr(len(session_key)) + session_key +
                       chr(len(self.id)) + self.id +
                       chr(len(self.password)) + self.password).encode('utf-8')

        keyname, n, e = j['rsa_key'].split(",")
        pub_key       = rsa.PublicKey(int(n,16), int(e,16))
        crypto        = rsa.encrypt(message, pub_key).encode('hex')

        self.transport = THttpClient.THttpClient(self.LINE_HTTP_URL)
        self.transport.setCustomHeaders(self._headers)

        self.protocol = TCompactProtocol.TCompactProtocol(self.transport)
        self._client   = CurveThrift.Client(self.protocol)

        try:
            with open(self.CERT_FILE,'r') as f:
                self.certificate = f.read()
                f.close()
        except:
            self.certificate = ""

        msg = self._client.loginWithIdentityCredentialForCertificate(
                self.id, self.password, keyname, crypto, True, self.ip,
                self.com_name, self.provider, self.certificate)

        if msg.type == 1:
            self.certificate = msg.certificate
            self.authToken = self._headers['X-Line-Access'] = msg.authToken
        elif msg.type == 2:
            msg = "require QR code"
            self.raise_error(msg)
        elif msg.type == 3:
            self._headers['X-Line-Access'] = msg.verifier
            self._pinCode = msg.pinCode

            print "Enter PinCode '%s' to your mobile phone in 2 minutes"\
                    % self._pinCode

            j = self.get_json(self.LINE_CERTIFICATE_URL)
            self.verifier = j['result']['verifier']

            msg = self._client.loginWithVerifierForCertificate(self.verifier)
            if msg.type == 1:
                if msg.certificate is not None:
                    with open(self.CERT_FILE,'w') as f:
                        f.write(msg.certificate)
                    self.certificate = msg.certificate
                if msg.authToken is not None:
                    self.authToken = self._headers['X-Line-Access'] = msg.authToken
                    return True
            else:
                msg = "Require device confirm"
                self.raise_error(msg)

            #raise Exception("Code is removed because of the request of LINE corporation")
        else:
            self.authToken = self._headers['X-Line-Access'] = msg.authToken

{'fraction_non_alphanumeric': 0.10245244098097639, 'fraction_numerical': 0.006761402704561082, 'mean_word_length': 2.2286348501664817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5657484', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2505, 'n_words': 609}