Created
December 28, 2023 04:59
-
-
Save mosajjal/95edc3c532918d1a27ae81184a85d8d7 to your computer and use it in GitHub Desktop.
Converts the incoming webhook from falconfeeds to an RSS feed that you can ingest. please consider T&C of Falconfeeds before making the RSS public
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
from bottle import Bottle, request, response, static_file | |
import json | |
import xml.etree.ElementTree as ET | |
from datetime import datetime | |
import pytz | |
import os | |
app = Bottle() | |
rss_file = 'rss_feed.xml' | |
# Route to serve the rss_feed.xml file | |
@app.route('/rss_feed.xml', method='GET') | |
def serve_rss_feed(): | |
return static_file(rss_file, root='.', mimetype='application/rss+xml') | |
# Function to convert JSON data to RSS item | |
def json_to_rss_item(json_data): | |
title = json_data['data']['title'] | |
content = json_data['data']['content'] | |
# Prettify JSON data | |
prettified_json = json.dumps(json_data['data'], indent=4) | |
# Append prettified JSON to content | |
full_content = f"{content}\n\nPrettified JSON:\n{prettified_json}" | |
link = json_data['data']['publishedURL'] | |
pub_date_timestamp = json_data['data']['publishedTimestampInMilliseconds'] / 1000 | |
pub_date = datetime.fromtimestamp(pub_date_timestamp, tz=pytz.utc).strftime('%a, %d %b %Y %H:%M:%S %Z') | |
item = ET.Element('item') | |
ET.SubElement(item, 'title').text = title | |
ET.SubElement(item, 'description').text = full_content | |
ET.SubElement(item, 'link').text = link | |
ET.SubElement(item, 'pubDate').text = pub_date | |
return item | |
# Route to handle POST requests. This address is what you'll set up in falconfeeds as your webhook receiver | |
@app.route('/falconfeedshook', method='POST') | |
def update_rss(): | |
json_data = request.json | |
# Load existing RSS feed or create new one | |
if os.path.exists(rss_file): | |
tree = ET.parse(rss_file) | |
rss = tree.getroot() | |
channel = rss.find('channel') | |
else: | |
rss = ET.Element('rss', version='2.0') | |
channel = ET.SubElement(rss, 'channel') | |
# Convert JSON to RSS item and add to the feed | |
item = json_to_rss_item(json_data) | |
channel.append(item) | |
# Save updated RSS feed to file | |
tree = ET.ElementTree(rss) | |
tree.write(rss_file) | |
response.content_type = 'application/json' | |
return {"message": "RSS feed updated successfully"} | |
if __name__ == '__main__': | |
app.run(host='127.0.0.1', port=10008, debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment