-
Notifications
You must be signed in to change notification settings - Fork 2
/
discourse.py
68 lines (54 loc) · 2.54 KB
/
discourse.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
import requests
class DiscourseClient(object):
def __init__(self, host, api_username, api_key):
self.host = host
self.api_username = api_username
self.api_key = api_key
self.params = {'api_key': api_key, "api_username": api_username}
self.headers = {"Accept": "application/json; charset=utf-8"}
def user(self, username):
return self._get("/users/{0}.json".format(username))
def new_post(self, title=None, content="Default Message", id=None):
post = {}
if title is not None:
post.update({"title": title})
if id is not None:
post.update({"topic_id": id})
post.update({"raw": content})
post.update({"category": 56})
response = requests.post(
'{}/posts?api_key={}&api_username={}'.format(self.host, self.api_key, self.api_username),
data=post, headers={"Content-Type": "application/x-www-form-urlencoded;"})
def send_pm(self, username=None, title=None, content="Default Message", id=None):
post = {}
if title is not None:
post.update({"title": title})
if id is not None:
post.update({"topic_id": id})
post.update({"raw": content})
if username is not None:
post.update({"target_usernames": username})
post.update({"archetype": "private_message"})
response = requests.post('{}/posts?api_key={}&api_username={}'.format(self.host, self.api_key, self.api_username),
data=post, headers={"Content-Type": "application/x-www-form-urlencoded;"})
return response
def get_likes(self, id):
return self._get("/post_action_users?id={}&post_action_type_id=2".format(id))
def get_pms(self, username):
return self._get("/topics/private-messages/{0}.json".format(username))
def read_pm(self, pm_id=None):
if pm_id is None:
raise Exception("Must include pm_id.")
pm_data = self.get_topic(pm_id)
cleaned_posts = []
for post in pm_data['post_stream']['posts']:
del post['avatar_template']
cleaned_posts.append(post)
return cleaned_posts
def get_topic(self, topic_id=None):
if topic_id is None:
raise Exception("Must include topic_id.")
return self._get("/t/{0}.json".format(topic_id))
def _get(self, path):
response = requests.get('{}{}?api_key={}&api_username={}'.format(self.host, path, self.api_key, self.api_username), headers=self.headers)
return response.json()