Skip to content

Snscrape twitter

SnscrapeTwitterReader #

Bases: BaseReader

SnscrapeTwitter reader. Reads data from a twitter profile.

Parameters:

Name Type Description Default
username str

Twitter Username.

required
num_tweets int

Number of tweets to fetch.

required
Source code in llama-index-integrations/readers/llama-index-readers-snscrape-twitter/llama_index/readers/snscrape_twitter/base.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class SnscrapeTwitterReader(BaseReader):
    """SnscrapeTwitter reader. Reads data from a twitter profile.

    Args:
        username (str): Twitter Username.
        num_tweets (int): Number of tweets to fetch.
    """

    def __init__(self) -> None:
        """Initialize SnscrapeTwitter reader."""

    def load_data(self, username: str, num_tweets: int) -> List[Document]:
        """Load data from a twitter profile.

        Args:
            username (str): Twitter Username.
            num_tweets (int): Number of tweets to fetch.


        Returns:
            List[Document]: List of documents.
        """
        import snscrape.modules.twitter as sntwitter

        attributes_container = []
        for i, tweet in enumerate(
            sntwitter.TwitterSearchScraper(f"from:{username}").get_items()
        ):
            if i > num_tweets:
                break
            attributes_container.append(tweet.rawContent)
        return [Document(text=attributes_container, extra_info={"username": username})]

load_data #

load_data(username: str, num_tweets: int) -> List[Document]

Load data from a twitter profile.

Parameters:

Name Type Description Default
username str

Twitter Username.

required
num_tweets int

Number of tweets to fetch.

required

Returns:

Type Description
List[Document]

List[Document]: List of documents.

Source code in llama-index-integrations/readers/llama-index-readers-snscrape-twitter/llama_index/readers/snscrape_twitter/base.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def load_data(self, username: str, num_tweets: int) -> List[Document]:
    """Load data from a twitter profile.

    Args:
        username (str): Twitter Username.
        num_tweets (int): Number of tweets to fetch.


    Returns:
        List[Document]: List of documents.
    """
    import snscrape.modules.twitter as sntwitter

    attributes_container = []
    for i, tweet in enumerate(
        sntwitter.TwitterSearchScraper(f"from:{username}").get_items()
    ):
        if i > num_tweets:
            break
        attributes_container.append(tweet.rawContent)
    return [Document(text=attributes_container, extra_info={"username": username})]