-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlemmyposter.py
156 lines (116 loc) · 5.51 KB
/
lemmyposter.py
1
2
3
4
5
6
7
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
### Lemmy Poster
## Uses Lemmy HTTP requests via API: https://join-lemmy.org/api/classes/LemmyHttp.html
import requests
## Obtain auth token
## Returns token
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#login
def login(server, username, pw):
userDetails = {
"username_or_email": username,
"password": pw
}
try:
request = requests.post(f"{server}/api/v3/user/login", json = userDetails)
return request.json()["jwt"]
except Exception as e:
print(f"Error logging in with {username}: {e}")
exit()
## Get Community ID from Community Name (NOT display name)
## Returns community id
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#getCommunity
def getCommunityID(authToken, server, communityName):
try:
request = requests.get(f"{server}/api/v3/community?auth={authToken}&name={communityName}")
communityId = request.json()["community_view"]["community"]["id"]
return communityId
except Exception as e:
print(f"Error when fetching community ID for '{communityName}': {e}")
exit()
## Create Post
## Returns post id
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#createPost
def setPost(authToken, server, community, postName, postURL = None, postBody = None, nsfw = False):
postContent = {
"auth": authToken,
"community_id": getCommunityID(authToken, server, community),
"name": postName,
"language_id" : 37, # I think that's English going by the json received from other posts
"nsfw" : nsfw
}
if postURL:
postContent["url"] = postURL
if postBody:
postContent["body"] = postBody
try:
request = requests.post(f"{server}/api/v3/post", json = postContent)
return request.json()["post_view"]["post"]["id"]
except Exception as e:
print(f"Error encountered while posting: '{postContent}': {e}")
return False
## Create a comment for a given Post
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#createComment
def setComment(authToken, server, content, postId, parentID = None):
commentContent = {
"auth" : authToken,
"content" : content,
"post_id" : postId
}
if parentID:
commentContent["parent_id"] = parentID
request = requests.post(f"{server}/api/v3/comment", json = commentContent)
if not request.ok:
print(f"Error encountered while commenting: {content}\r\n\r\nResponse: {request.text}")
return request.json()["comment_view"]["comment"]["id"]
## Distinguish a comment
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#distinguishComment
def distinguishComment(authToken, server, commentID):
distinguishPayload = {
"auth" : authToken,
"comment_id" : commentID,
"distinguished" : True
}
request = requests.post(f"{server}/api/v3/comment/distinguish", json = distinguishPayload)
if not request.ok:
print(f"Error encountered while distinguishing comment: {request.text}")
return request.json()["comment_view"]["comment"]["distinguished"]
## Get posts from a Community by Community Name (NOT display name)
## Returns list of posts
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#getPosts
def getPosts(authToken, server, communityName):
try:
posts = []
page = 1
while ((request := requests.get(f"{server}/api/v3/post/list?auth={authToken}&community_name={communityName}&page={page}")) and len(request.json()["posts"]) > 0):
for postDetails in request.json()["posts"]:
posts.append(postDetails)
page += 1
return posts
except Exception as e:
print(f"Error when fetching posts from '{communityName}' on '{server}': {e}")
exit()
## Get comments for a specific post
## Returns list of comments
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#getComments
def getComments(authToken, server, postID):
try:
request = requests.get(f"{server}/api/v3/comment/list?auth={authToken}&post_id={postID}")
posts = request.json()
return posts
except Exception as e:
print(f"Error when fetching comments from '{communityName}' on '{server}': {e}")
exit()
## Get the "credit" comment for a specific post (if there is one)
## Returns a single comment
## Reference: https://join-lemmy.org/api/classes/LemmyHttp.html#getComments
def getCreditComment(authToken, server, postID):
try:
request = requests.get(f"{server}/api/v3/comment/list?auth={authToken}&post_id={postID}&sort=Old")
posts = request.json()
#Our credit comments are distinguished so we can use this to check
if(len(posts["comments"]) > 0 and posts["comments"][0]["comment"]["distinguished"] and "Originally posted by" in posts["comments"][0]["comment"]["content"]):
return posts["comments"][0]["comment"]["content"]
else:
return "" # We must return an empty string here to avoid breaking iteration later - "None" for example will generate an error
except Exception as e:
print(f"Error when fetching credit comment from '{communityName}' on '{server}': {e}")
exit()