Skip to content

Community

Implements the Community class.

Community(lemmy, community)

A class for Communities.

To obtain an instance of this class for c/test run:

community = lemmy.get_community("test")

Initialize a Community instance.

Parameters:

Source code in pylemmy/models/community.py
23
24
25
26
27
28
29
30
31
32
33
34
def __init__(self, lemmy: "pylemmy.Lemmy", community: api.community.CommunityView):
    """Initialize a Community instance.

    :param lemmy: A Lemmy instance.
    :param community: A [CommunityView](
    https://join-lemmy.org/api/interfaces/CommunityView.html).
    """
    self.lemmy = lemmy
    self.blocked = community.blocked
    self.safe = community.community
    self.counts = community.counts
    self.subscribed = community.subscribed

stream: CommunityStream property

Returns a stream of content.

This stream is to be used to monitor posts or comments.

Example:

community = lemmy.get_community("test")
for post in community.stream.get_posts():
    process_post(post)

create_post(name, **kwargs)

Create a new post in this Community.

Parameters:

  • name (str) –

    Name of the post.

  • kwargs –

    See optional arguments in CreatePost.

Returns:

  • Post –

    The created Post.

Source code in pylemmy/models/community.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def create_post(self, name: str, **kwargs) -> Post:
    """Create a new post in this Community.

    :param name: Name of the post.
    :param kwargs: See optional arguments in [CreatePost](
    https://join-lemmy.org/api/interfaces/CreatePost.html).
    :return: The created Post.
    """
    self.lemmy.get_token()
    payload = api.post.CreatePost(name=name, community_id=self.safe.id, **kwargs)
    result = self.lemmy.post_request(LemmyAPI.Post, params=payload)
    parsed_result = api.post.PostResponse(**result)

    return Post(self.lemmy, parsed_result.post_view, community=self)

get_comments(**kwargs)

Gets a list of Comments from this community.

Parameters:

Source code in pylemmy/models/community.py
63
64
65
66
67
68
69
70
71
72
73
def get_comments(self, **kwargs) -> List[Comment]:
    """Gets a list of Comments from this community.

    :param kwargs: See optional arguments in [GetComments](
    https://join-lemmy.org/api/interfaces/GetComments.html).
    """
    payload = api.comment.GetComments(community_id=self.safe.id, **kwargs)
    result = self.lemmy.get_request(LemmyAPI.GetComments, params=payload)
    parsed_result = api.comment.GetCommentsResponse(**result)

    return [Comment(self.lemmy, comment) for comment in parsed_result.comments]

get_posts(**kwargs)

Gets a list of Posts from this community.

Parameters:

  • kwargs –

    See optional arguments in GetPosts.

Source code in pylemmy/models/community.py
51
52
53
54
55
56
57
58
59
60
61
def get_posts(self, **kwargs) -> List[Post]:
    """Gets a list of Posts from this community.

    :param kwargs: See optional arguments in [GetPosts](
    https://join-lemmy.org/api/interfaces/GetPosts.html).
    """
    payload = api.post.GetPosts(community_id=self.safe.id, **kwargs)
    result = self.lemmy.get_request(LemmyAPI.GetPosts, params=payload)
    parsed_result = api.post.GetPostsResponse(**result)

    return [Post(self.lemmy, post, community=self) for post in parsed_result.posts]

list_comment_reports(**kwargs)

List comment reports in this community.

Parameters:

Source code in pylemmy/models/community.py
83
84
85
86
87
88
89
def list_comment_reports(self, **kwargs) -> List[api.comment.CommentReportView]:
    """List comment reports in this community.

    :param kwargs: See optional arguments in [ListCommentReports](
    https://join-lemmy.org/api/interfaces/ListCommentReports.html).
    """
    return self.lemmy.list_comment_reports(community_id=self.safe.id, **kwargs)

list_post_reports(**kwargs)

List post reports in this community.

Parameters:

Source code in pylemmy/models/community.py
75
76
77
78
79
80
81
def list_post_reports(self, **kwargs) -> List[api.post.PostReportView]:
    """List post reports in this community.

    :param kwargs: See optional arguments in [ListPostReports](
    https://join-lemmy.org/api/interfaces/ListPostReports.html).
    """
    return self.lemmy.list_post_reports(community_id=self.safe.id, **kwargs)

CommunityStream(community)

Helper class to stream content from a specific community.

This class shouldn't be directly initialized, but rather accessed through [Community][pylemmy.community.Community].

Example:

community = lemmy.get_community("test")
for post in community.stream.get_posts():
    process_post(post)

Initialize a CommunityStream.

Parameters:

  • community (Community) –

    A Community to run the stream on.

Source code in pylemmy/models/community.py
119
120
121
122
123
124
def __init__(self, community: Community):
    """Initialize a CommunityStream.

    :param community: A Community to run the stream on.
    """
    self.community = community

get_comments(**kwargs)

Get a stream of Comments in the Community.

Parameters:

Source code in pylemmy/models/community.py
138
139
140
141
142
143
144
145
146
147
148
def get_comments(self, **kwargs):
    """Get a stream of Comments in the Community.

    :param kwargs: See the optional arguments in
    [stream_generator][pylemmy.utils.stream_generator].
    """
    return stream_generator(
        self.community.get_comments,
        lambda x: str(x.comment_view.comment.ap_id),
        **kwargs,
    )

get_posts(**kwargs)

Get a stream of Posts in the Community.

Parameters:

Source code in pylemmy/models/community.py
126
127
128
129
130
131
132
133
134
135
136
def get_posts(self, **kwargs):
    """Get a stream of Posts in the Community.

    :param kwargs: See the optional arguments in
    [stream_generator][pylemmy.utils.stream_generator].
    """
    return stream_generator(
        self.community.get_posts,
        lambda x: str(x.post_view.post.ap_id),
        **kwargs,
    )

MultiCommunityStream(communities)

Helper class to stream content from multiple communities.

This class shouldn't be directly initialized, but rather accessed through Lemmy.

Example: A simple example where we want to print the content of each post.

def process_content(elem: Union[Post, Comment]):
        print(elem.post_view.post.name)

multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
multi_stream.content_apply(process_content)

Initializer for MultiCommunityStream.

Parameters:

  • communities (List[Community]) –

    A list of communities to monitor.

Source code in pylemmy/models/community.py
167
168
169
170
171
172
def __init__(self, communities: List[Community]):
    """Initializer for MultiCommunityStream.

    :param communities: A list of communities to monitor.
    """
    self.communities = communities

comments_apply(callback, **kwargs)

Apply a callback function to a stream of Comments in the Communities.

Example: A simple example where we want to print the content of each comment.

def process_content(elem: Union[Post, Comment]):
    print(elem.comment_view.comment.content)

multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
multi_stream.content_apply(process_content)

Parameters:

  • callback (Callable[[Comment], Any]) –

    Function that will be called for each Comment.

  • kwargs –

    See the optional arguments in stream_generator.

Source code in pylemmy/models/community.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def comments_apply(self, callback: Callable[[Comment], Any], **kwargs):
    """Apply a callback function to a stream of Comments in the Communities.

    Example:
    A simple example where we want to print the content of each comment.

        def process_content(elem: Union[Post, Comment]):
            print(elem.comment_view.comment.content)

        multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
        multi_stream.content_apply(process_content)

    :param callback: Function that will be called for each Comment.
    :param kwargs: See the optional arguments in
    [stream_generator][pylemmy.utils.stream_generator].
    """
    results_fns = [c.get_comments for c in self.communities]
    unique_keys_fns = [lambda x: str(x.comment_view.comment.ap_id)] * len(
        self.communities
    )
    stream_apply(results_fns, unique_keys_fns, callback, **kwargs)

content_apply(callback, **kwargs)

Apply a callback function to a stream of Comments and Posts.

Example: A simple example where we want to print the content of each post/comment.

def process_content(elem: Union[Post, Comment]):
    if isinstance(Post, elem):
        print(elem.post_view.post.name)
    elif isinstance(Comment, elem):
        print(elem.comment_view.comment.content)

multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
multi_stream.content_apply(process_content)

Parameters:

  • callback (Callable[[Union[Comment, Post]], Any]) –

    Function that will be called for each Comment/Post.

  • kwargs –

    See the optional arguments in stream_generator.

Source code in pylemmy/models/community.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def content_apply(self, callback: Callable[[Union[Comment, Post]], Any], **kwargs):
    """Apply a callback function to a stream of Comments and Posts.

    Example:
    A simple example where we want to print the content of each post/comment.

        def process_content(elem: Union[Post, Comment]):
            if isinstance(Post, elem):
                print(elem.post_view.post.name)
            elif isinstance(Comment, elem):
                print(elem.comment_view.comment.content)

        multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
        multi_stream.content_apply(process_content)


    :param callback: Function that will be called for each Comment/Post.
    :param kwargs: See the optional arguments in
    [stream_generator][pylemmy.utils.stream_generator].
    """
    posts_fns: List[Callable[[KwArg(Any)], Iterable[Union[Post, Comment]]]] = [
        c.get_posts for c in self.communities
    ]
    comments_fns: List[Callable[[KwArg(Any)], Iterable[Union[Post, Comment]]]] = [
        c.get_comments for c in self.communities
    ]

    posts_unique_keys_fns = [lambda x: "post_" + str(x.post_view.post.ap_id)] * len(
        self.communities
    )
    comments_unique_keys_fns = [
        lambda x: "comment_" + str(x.comment_view.comment.ap_id)
    ] * len(self.communities)
    stream_apply(
        posts_fns + comments_fns,
        posts_unique_keys_fns + comments_unique_keys_fns,
        callback,
        **kwargs,
    )

posts_apply(callback, **kwargs)

Apply a callback function to a stream of Posts in the Communities.

Example: A simple example where we want to print the content of each post.

def process_content(elem: Union[Post, Comment]):
    print(elem.post_view.post.name)

multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
multi_stream.content_apply(process_content)

Parameters:

  • callback (Callable[[Post], Any]) –

    Function that will be called for each Post.

  • kwargs –

    See the optional arguments in stream_generator.

Source code in pylemmy/models/community.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def posts_apply(self, callback: Callable[[Post], Any], **kwargs):
    """Apply a callback function to a stream of Posts in the Communities.

    Example:
    A simple example where we want to print the content of each post.

        def process_content(elem: Union[Post, Comment]):
            print(elem.post_view.post.name)

        multi_stream = lemmy.multi_communities_stream(["community1", "community2"])
        multi_stream.content_apply(process_content)

    :param callback: Function that will be called for each Post.
    :param kwargs: See the optional arguments in
    [stream_generator][pylemmy.utils.stream_generator].
    """
    results_fns = [c.get_posts for c in self.communities]
    unique_keys_fns = [lambda x: str(x.post_view.post.ap_id)] * len(
        self.communities
    )
    stream_apply(results_fns, unique_keys_fns, callback, **kwargs)