def _checkStringLength(self, string: str, max_length: int): '''文字列の型確認及び文字の長さ確認。 \n だめだったら例外を投げ、問題なければ何も起きない ''' if type(string) is not str or type(max_length) is not int: raise TwitterAPIInputError('invaild argment type') if len(string) > max_length: raise TwitterAPIInputError('Too many characters. Excess:{}'.format( len(string) - max_length))
def setMediaId(self, id:str): '''アップロード済みのmedia idをひとつづつ指定する''' p = re.compile('\d+') if p.fullmatch(id) is None: raise TwitterAPIInputError('Only numbers can be specified in the argument.') if self.__media_count >= 4: raise TwitterAPIInputError('You can specify up to 4') if self.__media_ids is None: self.__media_ids = [] self.__media_ids.append(id) self.__media_count += 1
def setCount(self, num: int): '''取得するツイートの個数。 \n 指定できる範囲は 1 ~ 100 ''' MAX_COUNT = 100 MIN_COUNT = 1 if num > MAX_COUNT: raise TwitterAPIInputError( 'Input value is too large. max: {}'.format(MAX_COUNT)) if num < MIN_COUNT: raise TwitterAPIInputError( 'Input value is too small. max: {}'.format(MIN_COUNT)) self.__count = str(num)
def setUntil(self, year: int, month: int, day: int): '''指定した年月日以前のツイートを取得する。遡れない場合もあるらしい ''' current_date = datetime.date.today() if year < 1 or year > current_date.year: raise TwitterAPIInputError('invaild input year.') if month < 1 or month > 12 or (year == current_date.year and month > current_date.month): raise TwitterAPIInputError('invaild input month.') if (day < 1 or day > calendar.monthrange(year, month) or (year == current_date.year and month == current_date.month and day > current_date.day)): raise TwitterAPIInputError('invaild input day') self.__until = '{:0=4}-{:0=2}-{:0=2}'.format(year, month, day)
def _checkImageFile(self, file_path: str) -> str: '''指定したファイルパスが画像かどうかを判定し、画像であれば画像の種類を返す。 \n 画像でなければ例外を投げる。 ''' if file_path is None: raise TwitterAPIInputError('argment is None.') if not os.path.isfile(file_path): raise TwitterAPIInputError( f'The specified file does not exist. path="{file_path}"') image_type = imghdr.what(file_path) if image_type is None: raise TwitterAPIInputError( f'Please specify the image file. "{file_path}" is not image.') return image_type
def _checkInputCorrectness(self): if self.__user_id is None and self.__screen_name is None: raise TwitterAPIInputError('rquired parameter is not input.') super()._setQueryParam('user_id', self.__user_id) super()._setQueryParam('screen_name', self.__screen_name) super()._setQueryParam('include_entities', self.__include_entities)
def _checkInputCorrectness(self): if self.__search_query is None: raise TwitterAPIInputError('rquired parameter is not input.') super()._setQueryParam('q', self.__search_query) super()._setQueryParam('count', self.__count) super()._setQueryParam('until', self.__until) super()._setQueryParam('result_type', self.__result_type)
def setTweet(self, tweet:str): '''投稿するtweet文 ''' MAX_LENGTH = 140 if len(tweet) > MAX_LENGTH: raise TwitterAPIInputError('Too many characters. Excess:{}'.format(len(tweet) - MAX_LENGTH)) self.__tweet = tweet
def setSearchQuery(self, query: str): '''ツイートの検索文字列 ''' MAX_LENGTH = 500 if len(query) > MAX_LENGTH: raise TwitterAPIInputError('Too many characters. Excess:{}'.format( len(tweet) - MAX_LENGTH)) self.__search_query = query
def _checkInputCorrectness(self): if self.__tweet is None: raise TwitterAPIInputError('rquired parameter is not input.') super()._setQueryParam('status', self.__tweet) super()._setQueryParam('in_reply_to_status_id', self.__reply_target_id) super()._setQueryParam('possibly_sensitive', self.__is_sensitive) if self.__media_ids is not None: super()._setQueryParam('media_ids', ','.join(self.__media_ids))
def setReplyTargetId(self, id:str): '''リプする場合の対象tweet idをセット ''' if id is None: return p = re.compile('\d+') if p.fullmatch(id) is None: raise TwitterAPIInputError('Only numbers can be specified in the argument.') self.__reply_target_id = id
def setResultType(self, result_type: str): '''以下のいずれかを指定 \n mixed ... 混合 \n recent ... 最近のツイートのみ取得 \n popular ... 人気のあるツイートのみ取得 ''' types = ['mixed', 'recent', 'popular'] if result_type not in types: raise TwitterAPIInputError('invaild input param') self.__result_type = result_type
def _checkInputCorrectness(self): if self.__image_file_path is None: raise TwitterAPIInputError('rquired parameter is not input.') image_type = super()._checkImageFile(self.__image_file_path) # media/uploadとは違いエンコード済みの画像はツイッター側で認識されなかったため、生データを送信 # super()._setEncodeMediaPath('image', self.__image_file_path, 'image/' + image_type) super()._setMediaPath('image', self.__image_file_path, 'image/' + image_type) super()._setPostParam('include_entities', self.__include_entities) super()._setPostParam('skip_status', self.__skip_status)
def _checkInputCorrectness(self): if self.__param_set_count == 0: raise TwitterAPIInputError('Set at least one parameter.') super()._setQueryParam('name', self.__name) super()._setQueryParam('url', self.__url) super()._setQueryParam('location', self.__location) super()._setQueryParam('description', self.__description) super()._setQueryParam('profile_link_color', self.__profile_link_color) super()._setQueryParam('include_entities', self.__include_entities) super()._setQueryParam('skip_status', self.__skip_status)
def setProfileLinkColor(self, color: str): '''プロフィールのリンクの色を調整できる。 \n 3桁または6桁の16新数で指定(文字列) ''' super()._checkStringLength(color, 6) color = color.upper() p = re.compile('([A-F0-9]{3}){1,2}') if p.fullmatch(color): raise TwitterAPIInputError( 'Only 3 or 6 digit hexadecimal string can be specified') self.__profile_link_color = color self.__param_set_count += 1
def _checkInputCorrectness(self): if self.__timeline_type == TwitterTimelineType.USER: if self.__user_id is None and self.__screen_name is None: raise TwitterAPIInputError( 'user_id or screen_name must be set') super()._setQueryParam('user_id', self.__user_id) super()._setQueryParam('screen_name', self.__screen_name) super()._setQueryParam('include_rts', self.__include_rts) if (self.__timeline_type == TwitterTimelineType.USER or self.__timeline_type == TwitterTimelineType.HOME): super()._setQueryParam('exclude_replies', self.__exclude_replies) super()._setQueryParam('count', self.__count) super()._setQueryParam('since_id', self.__since_id) super()._setQueryParam('max_id', self.__max_id) super()._setQueryParam('trim_user', self.__trim_user) super()._setQueryParam('contributor_details', self.__contributor_details) super()._setQueryParam('include_entities', self.__include_entities)
def _checkInputCorrectness(self): if self.__oauth_callback is None: raise TwitterAPIInputError('rquired parameter is not input.') super()._setAuthHeaderExtentionParam('oauth_callback', self.__oauth_callback) super()._setQueryParam('x_auth_access_type', self.__x_auth_access_type)
def _checkInputCorrectness(self): if self.__destroy_id == '': raise TwitterAPIInputError('rquired parameter is not input.') super()._setQueryParam('trim_user', self.__is_trim_user)
def _checkInputCorrectness(self): if self.__media_file_path is None: raise TwitterAPIInputError('rquired parameter is not input.') image_type = super()._checkImageFile(self.__media_file_path) super()._setEncodeMediaPath('media_data', self.__media_file_path, 'image/' + image_type)