Example #1
0
    def __init__(self,
                 api_key='',
                 api_secret='',
                 access_token='',
                 access_secret=''):
        # if argments is not set, the constructor reads config and set default parameter.
        if api_key == '' or api_secret == '' or access_token == '' or access_secret == '':
            if not os.path.exists(self.__class__.__CONFIG_PATH):
                raise IOError(self.__class__.__CONFIG_PATH)

            cfg = configparser.ConfigParser()
            cfg.read(self.__class__.__CONFIG_PATH, 'UTF-8')

            if api_key == '':
                api_key = cfg.get('default', 'TWITTER_API_CONSUMER_KEY')
                if api_key is None:
                    raise TwitterAPIClientError('api key not specified.')
            if api_secret == '':
                api_secret = cfg.get('default',
                                     'TWITTER_API_CONSUMER_KEY_SECRET')
                if api_secret is None:
                    raise TwitterAPIClientError('api secret not specified.')

            if self._useDefaultAccessToken():
                if access_token == '':
                    access_token = cfg.get('default',
                                           'TWITTER_API_ACCESS_TOKEN')
                    if access_token is None:
                        raise TwitterAPIClientError(
                            'access token not specified.')
                if access_secret == '':
                    access_secret = cfg.get('default',
                                            'TWITTER_API_ACCESS_TOKEN_SECRET')
                    if access_secret is None:
                        raise TwitterAPIClientError(
                            'access secret not specified.')

        self.__API_KEY = api_key
        self.__API_SECRET = api_secret
        self.__ACCESS_TOKEN = access_token
        self.__ACCESS_SECRET = access_secret
        self.__REQUEST_METHOD = self._requestMethod().upper()

        self.__request_params = None
        self.__media_params = None
        self.__auth_extentions = None
        self.__endpoint = None
        self.__is_media_upload = False
        self.__not_use_access_token = False

        self.__initializeParams()
Example #2
0
    def exec(self, inp:TwitterApiMediaUploadInput) -> TwitterApiMediaUploadOutput:
        if not isinstance(inp, TwitterApiMediaUploadInput):
            raise TwitterAPIClientError('invaild argment')

        super()._setEndPoint('https://upload.twitter.com/1.1/media/upload.json')
        results = super().exec(inp)
        return TwitterApiMediaUploadOutput(results)
Example #3
0
    def __buildMultipartParam(self, delimiter, key, value) -> bytes:
        '''multipartパラメータの1つの送信パラメータを作成
        '''
        # get value
        data = None
        if type(value) is str:
            data = value.encode()

        if type(value) is dict:
            file_path = value.get('file_path')
            if file_path:
                with open(file_path, 'rb') as media_file:
                    data = media_file.read()
                if value.get('is_encode') == True:
                    data = base64.b64encode(data)

        if data is None:
            raise TwitterAPIClientError('incorrect input parameter type.')

        # build data
        param = []
        param.append(delimiter)
        param.append(
            f'Content-Disposition: form-data; name="{key}"; '.encode())
        if type(value) is dict:
            param.append(f'Content-Type: {value.get("mime_type")}'.encode())
            if value.get('is_encode') == True:
                param.append('Content-Transfer-Encoding: base64'.encode())
        param.append(b'')
        param.append(data)

        return b"\r\n".join(param)
 def exec(self, inp:TwitterApiOauthRequestTokenInput) -> TwitterApiOauthRequestTokenOutput:
     if not isinstance(inp, TwitterApiOauthRequestTokenInput):
         raise TwitterAPIClientError('invaild argment.')
     # set endpoint
     super()._setEndPoint('https://api.twitter.com/oauth/request_token')
     results = super().exec(inp)
     return TwitterApiOauthRequestTokenOutput(results)
Example #5
0
 def exec(self, inp:TwitterApiStatusesUpdateInput) -> TwitterApiStatusesUpdateOutput:
     if not isinstance(inp, TwitterApiStatusesUpdateInput):
         raise TwitterAPIClientError('invaild argment.')
     super()._addPath('statuses/update')
     super()._addExtension('json')
     results = super().exec(inp)
     return TwitterApiStatusesUpdateOutput(results)
Example #6
0
 def exec(self,
          inp: TwitterApiSearchTweetsInput) -> TwitterApiSearchTweetsOutput:
     if not isinstance(inp, TwitterApiSearchTweetsInput):
         raise TwitterAPIClientError('invaild argment')
     super()._addPath('search/tweets')
     super()._addExtension('json')
     results = super().exec(inp)
     return TwitterApiSearchTweetsOutput(results)
Example #7
0
 def setMaxId(self, id: str):
     '''これに指定したID以下(より古い)のIDの検索結果を取得
     '''
     p = re.compile('\d+')
     if p.fullmatch(id) is None:
         raise TwitterAPIClientError(
             'Only numbers can be specified in the argument.')
     self.__max_id = id
Example #8
0
 def setSinceId(self, id: str):
     '''これに指定したIDよりも大きい(より新しい)IDの検索結果を取得
     '''
     p = re.compile('\d+')
     if p.fullmatch(id) is None:
         raise TwitterAPIClientError(
             'Only numbers can be specified in the argument.')
     self.__since_id = id
Example #9
0
 def setDestroyId(self, id: str):
     '''削除する対象のツイートidを指定
     \n エンドポイントの確定もあるので一度だけの呼び出しとする
     '''
     p = re.compile('\d+')
     if p.fullmatch(id) is None:
         raise TwitterAPIClientError(
             'Only numbers can be specified in the argument.')
     self.__destroy_id = id
Example #10
0
 def exec(
     self, inp: TwitterApiAccountUpdateProfileImageInput
 ) -> TwitterApiAccountUpdateProfileImageOutput:
     if not isinstance(inp, TwitterApiAccountUpdateProfileImageInput):
         raise TwitterAPIClientError('invaild argment.')
     super()._addPath('account/update_profile_image')
     super()._addExtension('json')
     results = super().exec(inp)
     return TwitterApiAccountUpdateProfileImageOutput(results)
Example #11
0
 def exec(self, inp: TwitterApiUsersShowInput) -> TwitterApiUsersShowOutput:
     if not isinstance(inp, TwitterApiUsersShowInput):
         raise TwitterAPIClientError('invaild argment.')
     # set endpoint
     super()._addPath('users/show')
     super()._addExtension('json')
     # execute
     results = super().exec(inp)
     return TwitterApiUsersShowOutput(results)
Example #12
0
    def __setInputParams(self, inp):
        '''TwitterApiBaseInpuの継承クラスのインスタンスを指定することで、値をセットする
        '''
        if not isinstance(inp, TwitterApiBaseInput):
            raise TwitterAPIClientError('invaild argment.')

        inp._checkInputCorrectness()
        self.__request_params = inp._getQueryParams()
        self.__media_params = inp._getPostParams()
        self.__auth_extentions = inp._getAuthHeaderExtentionParams()
Example #13
0
 def exec(
     self, inp: TwitterApiStatusesDestoryInput
 ) -> TwitterApiStatusesDestoryOutput:
     if not isinstance(inp, TwitterApiStatusesDestoryInput):
         raise TwitterAPIClientError('invaild argment.')
     # make endpoint
     super()._addPath('statuses/destroy')
     super()._addPath(inp.getDestroyId())
     super()._addExtension('json')
     # execute
     results = super().exec(inp)
     return TwitterApiStatusesDestoryOutput(results)
Example #14
0
    def exec(
        self, inp: TwitterApiStatusesTimelineInput
    ) -> TwitterApiStatusesTimelineOutput:
        if not isinstance(inp, TwitterApiStatusesTimelineInput):
            raise TwitterAPIClientError('invaild argment.')

        endpoint = 'statuses/home_timeline'
        if inp.getTimelineType() == TwitterTimelineType.USER:
            endpoint = 'statuses/user_timeline'
        if inp.getTimelineType() == TwitterTimelineType.MENTIONS:
            endpoint = 'statuses/mentions_timeline'

        super()._addPath(endpoint)
        super()._addExtension('json')
        results = super().exec(inp)
        return TwitterApiStatusesTimelineOutput(results)
Example #15
0
 def setUserId(self, id: str):
     p = re.compile('\d+')
     if p.fullmatch(id) is None:
         raise TwitterAPIClientError(
             'Only numbers can be specified in the argument.')
     self.__user_id = id