Adding firebonk api

This commit is contained in:
Patrick Müller 2022-07-04 20:01:51 +02:00
parent 318e9b1bb6
commit de70e3b474
Signed by: Paddy
GPG Key ID: 37ABC11275CAABCE
3 changed files with 57 additions and 3 deletions

View File

@ -25,6 +25,8 @@ class Crawler:
article_urls.append(
'https://www.ka-news.de/region/karlsruhe/falschparker-werden-in-karlsruhe-ab-juli-frueher-abgeschleppt-art-2813321')
article_urls.append(
'https://www.ka-news.de/region/karlsruhe/musikalische-klaenge-in-der-karlsruher-innenstadt-karlsruhe-klingt-music-to-go-findet-zum-10-mal-statt-art-2813273')
articles = []

View File

@ -0,0 +1,45 @@
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)

13
main.py
View File

@ -1,5 +1,7 @@
import sql
import os
from crawler import Crawler
from firebonk_api import FirebonkApi
def __check_and_insert_in_database__(conn, article) -> bool:
@ -29,12 +31,17 @@ if __name__ == '__main__':
crawl = Crawler('https://www.ka-news.de')
conn = sql.get_connection()
user = os.environ.get('FIREBONK_USER')
pw = os.environ.get('FIREBONK_PASSWORD')
api = FirebonkApi('https://www.firebonk.de/api/v1', user, pw)
articles = crawl.check_for_new_yaa_articles()
for article in articles:
if not __check_and_insert_in_database__(conn, article):
print('New!')
else:
print('Old!')
# Post is new
api.create_post(article.title, article.summary, article.url, article.image_url)
conn.close()