ka-news-yaa-crawler/firebonk_api.py

46 lines
1014 B
Python
Raw Normal View History

2022-07-04 18:01:51 +00:00
import logging
import requests
class FirebonkApi:
def __init__(self, api_url, user, password):
self.url = api_url
self.token = self.__auth_request__(user, password)
def __auth_request__(self, user, password) -> str:
body = {
'username': user,
'password': password
}
res = requests.post(self.url + '/auth/login', json=body)
return res.json()['auth_token']
def create_post(self, title, summary, url, image_url):
headers = {'Authorization': 'Bearer {}'.format(self.token)}
body = {
'data': {
'message': '# ' + title + '\n\n' + summary + '\n\n Link zum Artikel: ' + url,
'content': {
'metadata': {
'visibility': 1,
'archived': False,
'pinned': False,
'locked_comments': False
},
'topics': [
{
'name': 'ka-news'
}
]
}
}
}
res = requests.post(self.url + '/post/container/51', json=body, headers=headers)
if res.status_code != 200:
logging.critical('Cannot create post! Message: %s', res.content)