<s> """<STR_LIT>""" <EOL> import rsa <EOL> import requests <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from thrift . transport import TTransport <EOL> from thrift . transport import TSocket <EOL> from thrift . transport import THttpClient <EOL> from thrift . protocol import TCompactProtocol <EOL> import sys <EOL> reload ( sys ) <EOL> sys . setdefaultencoding ( "<STR_LIT:utf-8>" ) <EOL> from curve import CurveThrift <EOL> from curve . ttypes import TalkException <EOL> from curve . ttypes import ToType , ContentType <EOL> class LineAPI ( object ) : <EOL> """<STR_LIT>""" <EOL> LINE_DOMAIN = "<STR_LIT>" <EOL> LINE_HTTP_URL = LINE_DOMAIN + "<STR_LIT>" <EOL> LINE_HTTP_IN_URL = LINE_DOMAIN + "<STR_LIT>" <EOL> LINE_CERTIFICATE_URL = LINE_DOMAIN + "<STR_LIT>" <EOL> LINE_SESSION_LINE_URL = LINE_DOMAIN + "<STR_LIT>" <EOL> LINE_SESSION_NAVER_URL = LINE_DOMAIN + "<STR_LIT>" <EOL> CERT_FILE = "<STR_LIT>" <EOL> ip = "<STR_LIT:127.0.0.1>" <EOL> version = "<STR_LIT>" <EOL> com_name = "<STR_LIT>" <EOL> revision = <NUM_LIT:0> <EOL> certificate = "<STR_LIT>" <EOL> _headers = { } <EOL> def __init__ ( self ) : <EOL> object . __init__ ( self ) <EOL> self . _session = requests . session ( ) <EOL> def ready ( self ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def updateAuthToken ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . certificate : <EOL> self . login ( ) <EOL> self . tokenLogin ( ) <EOL> return True <EOL> else : <EOL> self . raise_error ( "<STR_LIT>" ) <EOL> def tokenLogin ( self ) : <EOL> self . transport = THttpClient . THttpClient ( self . LINE_HTTP_URL ) <EOL> self . transport . setCustomHeaders ( self . _headers ) <EOL> self . protocol = TCompactProtocol . TCompactProtocol ( self . transport ) <EOL> self . _client = CurveThrift . Client ( self . protocol ) <EOL> def login ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . provider == CurveThrift . Provider . LINE : <EOL> j = self . _get_json ( self . LINE_SESSION_LINE_URL ) <EOL> else : <EOL> j = self . _get_json ( self . LINE_SESSION_NAVER_URL ) <EOL> session_key = j [ '<STR_LIT>' ] <EOL> message = ( chr ( len ( session_key ) ) + session_key + <EOL> chr ( len ( self . id ) ) + self . id + <EOL> chr ( len ( self . password ) ) + self . password ) . encode ( '<STR_LIT:utf-8>' ) <EOL> keyname , n , e = j [ '<STR_LIT>' ] . split ( "<STR_LIT:U+002C>" ) <EOL> pub_key = rsa . PublicKey ( int ( n , <NUM_LIT:16> ) , int ( e , <NUM_LIT:16> ) ) <EOL> crypto = rsa . encrypt ( message , pub_key ) . encode ( '<STR_LIT>' ) <EOL> self . transport = THttpClient . THttpClient ( self . LINE_HTTP_URL ) <EOL> self . transport . setCustomHeaders ( self . _headers ) <EOL> self . protocol = TCompactProtocol . TCompactProtocol ( self . transport ) <EOL> self . _client = CurveThrift . Client ( self . protocol ) <EOL> try : <EOL> with open ( self . CERT_FILE , '<STR_LIT:r>' ) as f : <EOL> self . certificate = f . read ( ) <EOL> f . close ( ) <EOL> except : <EOL> self . certificate = "<STR_LIT>" <EOL> msg = self . _client . loginWithIdentityCredentialForCertificate ( <EOL> self . id , self . password , keyname , crypto , True , self . ip , <EOL> self . com_name , self . provider , self . certificate ) <EOL> if msg . type == <NUM_LIT:1> : <EOL> self . certificate = msg . certificate <EOL> self . authToken = self . _headers [ '<STR_LIT>' ] = msg . authToken <EOL> elif msg . type == <NUM_LIT:2> : <EOL> msg = "<STR_LIT>" <EOL> self . raise_error ( msg ) <EOL> elif msg . type == <NUM_LIT:3> : <EOL> self . _headers [ '<STR_LIT>' ] = msg . verifier <EOL> self . _pinCode = msg . pinCode <EOL> print "<STR_LIT>" % self . _pinCode <EOL> j = self . get_json ( self . LINE_CERTIFICATE_URL ) <EOL> self . verifier = j [ '<STR_LIT:result>' ] [ '<STR_LIT>' ] <EOL> msg = self . _client . loginWithVerifierForCertificate ( self . verifier ) <EOL> if msg . type == <NUM_LIT:1> : <EOL> if msg . certificate is not None : <EOL> with open ( self . CERT_FILE , '<STR_LIT:w>' ) as f : <EOL> f . write ( msg . certificate ) <EOL> self . certificate = msg . certificate <EOL> if msg . authToken is not None : <EOL> self . authToken = self . _headers [ '<STR_LIT>' ] = msg . authToken <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> self . raise_error ( msg ) <EOL> else : <EOL> self . authToken = self . _headers [ '<STR_LIT>' ] = msg . authToken <EOL> return True <EOL> def get_json ( self , url ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( self . _session . get ( url , headers = self . _headers ) . text ) <EOL> def _getProfile ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getProfile ( ) <EOL> def _getAllContactIds ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getAllContactIds ( ) <EOL> def _getBlockedContactIds ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getBlockedContactIds ( ) <EOL> def _getContacts ( self , ids ) : <EOL> """<STR_LIT>""" <EOL> if type ( ids ) != list : <EOL> msg = "<STR_LIT>" <EOL> self . raise_error ( msg ) <EOL> return self . _client . getContacts ( ids ) <EOL> def _findAndAddContactsByMid ( self , mid , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findAndAddContactsByMid ( seq , mid ) <EOL> def _findContactByUserid ( self , userid ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findContactByUserid ( userid ) <EOL> def _findAndAddContactsByUserid ( self , userid , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findAndAddContactsByUserid ( seq , userid ) <EOL> def _findContactsByPhone ( self , phones ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findContactsByPhone ( phones ) <EOL> def _findAndAddContactsByPhone ( self , phones , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findAndAddContactsByPhone ( seq , phones ) <EOL> def _findContactsByEmail ( self , emails ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findContactsByEmail ( emails ) <EOL> def _findAndAddContactsByEmail ( self , emails , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . findAndAddContactsByEmail ( seq , emails ) <EOL> def _createRoom ( self , ids , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . createRoom ( seq , ids ) <EOL> def _getRoom ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getRoom ( id ) <EOL> def _inviteIntoRoom ( self , roomId , contactIds = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . inviteIntoRoom ( <NUM_LIT:0> , roomId , contactIds ) <EOL> def _leaveRoom ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . leaveRoom ( <NUM_LIT:0> , id ) <EOL> def _createGroup ( self , name , ids , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . createGroup ( seq , name , ids ) <EOL> def _getGroups ( self , ids ) : <EOL> """<STR_LIT>""" <EOL> if type ( ids ) != list : <EOL> msg = "<STR_LIT>" <EOL> self . raise_error ( msg ) <EOL> return self . _client . getGroups ( ids ) <EOL> def _getGroupIdsJoined ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getGroupIdsJoined ( ) <EOL> def _getGroupIdsInvited ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getGroupIdsInvited ( ) <EOL> def _acceptGroupInvitation ( self , groupId , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . acceptGroupInvitation ( seq , groupId ) <EOL> def _kickoutFromGroup ( self , groupId , contactIds = [ ] , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . kickoutFromGroup ( seq , groupId , contactIds ) <EOL> def _cancelGroupInvitation ( self , groupId , contactIds = [ ] , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . cancelGroupInvitation ( seq , groupId , contactIds ) <EOL> def _inviteIntoGroup ( self , groupId , contactIds = [ ] , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . inviteIntoGroup ( seq , groupId , contactIds ) <EOL> def _leaveGroup ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . leaveGroup ( <NUM_LIT:0> , id ) <EOL> def _getRecentMessages ( self , id , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . getRecentMessages ( id , count ) <EOL> def _sendMessage ( self , message , seq = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . sendMessage ( seq , message ) <EOL> def _getLastOpRevision ( self ) : <EOL> return self . _client . getLastOpRevision ( ) <EOL> def _fetchOperations ( self , revision , count = <NUM_LIT:50> ) : <EOL> return self . _client . fetchOperations ( revision , count ) <EOL> def _getMessageBoxCompactWrapUp ( self , id ) : <EOL> try : <EOL> return self . _client . getMessageBoxCompactWrapUp ( id ) <EOL> except : <EOL> return None <EOL> def _getMessageBoxCompactWrapUpList ( self , start = <NUM_LIT:1> , count = <NUM_LIT:50> ) : <EOL> try : <EOL> return self . _client . getMessageBoxCompactWrapUpList ( start , count ) <EOL> except Exception as e : <EOL> msg = e <EOL> self . raise_error ( msg ) <EOL> def raise_error ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> raise Exception ( "<STR_LIT>" % msg ) <EOL> def _get_json ( self , url ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( self . _session . get ( url , headers = self . _headers ) . text ) <EOL> def post_content ( self , url , data = None , files = None ) : <EOL> return self . _session . post ( url , headers = self . _headers , data = data , files = files ) </s>
