refactored into python3 script for discourse api
This commit is contained in:
parent
0fecad10ad
commit
d204f74d82
7 changed files with 148 additions and 105 deletions
112
invite.py
Executable file
112
invite.py
Executable file
|
@ -0,0 +1,112 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import configparser
|
||||
import datetime
|
||||
import logging
|
||||
import optparse
|
||||
import requests
|
||||
|
||||
class JsonObject(object):
|
||||
'''An object constructed from a JSON response'''
|
||||
|
||||
def __init__(self, json):
|
||||
self.update_attributes(json)
|
||||
|
||||
def __str__(self):
|
||||
return '{} with ID: {}'.format(self.__class__.__name__, self.id)
|
||||
|
||||
def __repr__(self):
|
||||
vars_string = str(self.__dict__)
|
||||
|
||||
replace = {': ': '=', '{': '', '}': ''}
|
||||
for key in replace:
|
||||
vars_string = vars_string.replace(key, replace[key])
|
||||
|
||||
return '{}({})'.format(self.__class__.__name__, vars_string)
|
||||
|
||||
def update_attributes(self, json):
|
||||
self.__dict__.update(json)
|
||||
|
||||
class Topic(JsonObject):
|
||||
|
||||
def __init__(self, client, **kwargs):
|
||||
self.client = client
|
||||
super().__init__(**kwargs)
|
||||
|
||||
class Client(object):
|
||||
|
||||
def __init__(self, host, api_username='', api_key=''):
|
||||
self.host = host
|
||||
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update({
|
||||
'Api-Username': api_username,
|
||||
'Api-Key': api_key,
|
||||
})
|
||||
|
||||
# Class Methods
|
||||
def _request(self, method, path, params=None, data=None, json=None):
|
||||
response = self.session.request(
|
||||
method=method.upper(),
|
||||
url=requests.compat.urljoin(self.host, path),
|
||||
params=params,
|
||||
data=data,
|
||||
json=json,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()
|
||||
|
||||
def create_topic(self, title, raw, category=None):
|
||||
response = self._request('POST', 'posts.json', json={
|
||||
'title': title,
|
||||
'raw': raw,
|
||||
'category': category,
|
||||
})
|
||||
|
||||
return Topic(client=self, json=response)
|
||||
|
||||
def main():
|
||||
op = optparse.OptionParser()
|
||||
op.add_option("-c", "--config", default="invite.conf")
|
||||
op.add_option("-v", "--verbose", action="store_true")
|
||||
options, args = op.parse_args()
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
configfile = options.config
|
||||
if not config.read(configfile):
|
||||
op.error(f"Configuration file '{configfile}' not found or not readable.")
|
||||
|
||||
if options.verbose:
|
||||
logging.basicConfig()
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
client = Client(
|
||||
host = config['discourse']['uri'],
|
||||
api_username = config['discourse']['api_username'],
|
||||
api_key = config['discourse']['api_key']
|
||||
)
|
||||
|
||||
now = datetime.date.today()
|
||||
date = now + datetime.timedelta(3 - now.weekday())
|
||||
date_str = date.strftime('%d.%m.%Y')
|
||||
|
||||
subject = f'Einladung zum Plenum am {date_str}'
|
||||
msg_body = '''Hallo zusammen,
|
||||
|
||||
am Donnerstag findet wieder unser monatliches Plenum statt.
|
||||
Wir treffen uns um 20 Uhr im virtuellen Raum unter:
|
||||
|
||||
https://talk.kabi.tk/krautspace
|
||||
|
||||
Antwortet einfach hier auf diesen Post, wenn ihr etwas thematisieren möchtet.
|
||||
Wir freuen uns auf zahlreiche Teilnehmer:innnen.
|
||||
|
||||
Mit den besten Grüßen,
|
||||
|
||||
das Einladeskript'''
|
||||
client.create_topic(title=subject, raw=msg_body, category=7)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue