def get_post_likes(self, shortcode: str = None, count: int = 50, convert: bool = True, preload: bool = False): """Get likes of a Post in the form of usernames or `Profile` objects. Arguments: shortcode: the shortcode of the post count: maximum limit of likes you want to get convert: convert fetched usernames to `Profile` objects if True preload: converts all items yielded from the generator to `Profile` instances and returns a list if True Returns: list: if preload=True, which contains `Profile` instances generator: if preload=False, which yields `Profile` instances """ assert shortcode, "Empty arguments" self._logger.info("Fetching likes of :{0}".format(shortcode)) post = self.get_post(shortcode) likes = post.fetch_likes(count) if next(likes) is False: self._logger.error("No likes found.") return [] if not convert: return likes if preload: return instance_worker(self._session, Profile, likes) else: return instance_generator(self._session, Profile, likes)
def get_explore_posts(self, count: int = 50, only: str = None, timestamp_limit: dict = None, preload: bool = False): """Get posts in the 'discover' feed section, in the form of `Post` objects. Arguments: count: maximum limit of posts you want to get only: only this type of posts will be downloaded [image, video, sidecar] timestamp_limit: only get posts created between these timestamps, {"before": <before timestamp>, "after", <after_timestamp>} preload: converts all items yielded from the generator to `Post` instances and returns a list if True Returns: list: if preload=True, which contains `Post` instances generator: if preload=False, which yields `Post` instances """ self._logger.info("Fetching explore posts...") explore = Explore(self._session) posts = explore.fetch_posts(count, only, timestamp_limit) if next(posts) is False: self._logger.error("No explore feed posts found.") return [] if preload: return instance_worker(self._session, Post, posts) else: return instance_generator(self._session, Post, posts)
def get_hashtag_posts(self, tag: str, count: int = 50, only: str = None, timestamp_limit: dict = None, preload: bool = False): """Get posts with the hashtag name in the form of `Post` objects. Arguments: tag: hashtag name count: maximum limit of posts you want to get only: only this type of posts will be downloaded [image, video, sidecar] timestamp_limit: only get posts created between these timestamps, {"before": <before timestamp>, "after", <after_timestamp>} preload: converts all items yielded from the generator to `Post` instances and returns a list if True Returns: list: if preload=True, which contains `Post` instances generator: if preload=False, which yields `Post` instances """ assert tag, "Empty arguments" self._logger.info("Fetching hashtag posts of #{0}...".format(tag)) hashtag = Hashtag(self._session, tag) posts = hashtag.fetch_posts(count, only, timestamp_limit) if next(posts) is False: self._logger.error("No hashtag posts found for #{0}.".format(tag)) return [] if preload: return instance_worker(self._session, Post, posts) else: return instance_generator(self._session, Post, posts)
def get_user_followings(self, name: str, count: int = 50, convert: bool = True, preload: bool = False): """Get a user's followings in the form of `Profile` objects or just plain usernames. Arguments: name: the user's username count: maximum limit of followings you want to get convert: convert fetched usernames to `Profile` objects if True preload: converts all items yielded from the generator to `Profile` instances and returns a list if True Returns: list: if preload=True, which contains `Profile` instances generator: if preload=False, which yields `Profile` instances """ assert name, "Empty arguments" self._logger.info("Fetching @{0}'s followings...".format(name)) user = self.get_profile(name) usernames = user.fetch_followings(count) if next(usernames) is False: self._logger.error( "No following users found for @{0}.".format(name)) return [] if not convert: return usernames if preload: return instance_worker(self._session, Profile, usernames) else: return instance_generator(self._session, Profile, usernames)
def get_user_tagged_posts(self, name: str, count: int = 50, only: str = None, timestamp_limit: dict = None, preload: bool = False): """Get posts that tagged the user in the form of `Post` objects. Arguments: name: the user's username count: maximum limit of posts you want to get only: only this type of posts will be downloaded [image, video, sidecar] timestamp_limit: only get posts created between these timestamps, {"before": <before timestamp>, "after", <after_timestamp>} preload: converts all items yielded from the generator to `Post` instances and returns a list if True Returns: list: if preload=True, which contains `Post` instances generator: if preload=False, which yields `Post` instances """ assert name, "Empty arguments" self._logger.info("Fetching @{0}'s tagged posts...".format(name)) user = self.get_profile(name) posts = user.fetch_tagged_posts(count, only, timestamp_limit) if next(posts) is False: self._logger.error("No tagged posts found for @{0}.".format(name)) return [] if preload: return instance_worker(self._session, Post, posts) else: return instance_generator(self._session, Post, posts)
def _get_objects_from_file(self, obj, prefix_char: str, file: IOBase, preload: bool = False): if not isinstance(file, IOBase): raise ValueError("'file' argument must be an opend file") try: lines = file.readlines() lines = [ line.strip()[1:] for line in lines if len(line) > 1 and line.strip()[0] == prefix_char ] # filter out invalid lines and strip them if not lines: self._logger.error("No data can be retrieved from file.") return [] if preload: return instance_worker(self._session, obj, lines) else: return instance_generator(self._session, obj, lines) finally: file.close()