123 lines
4.1 KiB
Python
123 lines
4.1 KiB
Python
from flask import Flask, render_template, request, jsonify
|
|
import xml.etree.ElementTree as ET
|
|
import logging
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.ERROR)
|
|
|
|
# Parse the XML file and extract data
|
|
def parse_xml(file_path):
|
|
try:
|
|
tree = ET.parse(file_path)
|
|
root = tree.getroot()
|
|
|
|
items = []
|
|
run_time_element = root.find("run_time")
|
|
|
|
if run_time_element is not None:
|
|
run_time = run_time_element.text
|
|
else:
|
|
run_time = 'N/A'
|
|
|
|
for item in root.findall('.//item'):
|
|
title_element = item.attrib.get('title')
|
|
title = title_element if title_element is not None else 'N/A'
|
|
|
|
link_element = item.attrib.get('link')
|
|
link = link_element if link_element is not None else '#'
|
|
|
|
description_element = item.attrib.get('description')
|
|
description = description_element if description_element is not None else 'N/A'
|
|
|
|
source_element = item.attrib.get('source')
|
|
source = source_element if source_element is not None else 'N/A'
|
|
|
|
guid_element = item.attrib.get('guid')
|
|
guid = guid_element if guid_element is not None else 'N/A'
|
|
|
|
pub_date_element = item.attrib.get('pubDate')
|
|
pub_date = pub_date_element if pub_date_element is not None else 'N/A'
|
|
|
|
georss_point_element = item.attrib.get('{http://www.georss.org/georss}point')
|
|
georss_point = georss_point_element if georss_point_element is not None else 'N/A'
|
|
|
|
items.append({
|
|
'title': title,
|
|
'link': link,
|
|
'description': description,
|
|
'source': source,
|
|
'guid': guid,
|
|
'pub_date': pub_date,
|
|
'georss_point': georss_point
|
|
})
|
|
|
|
return items, run_time # Return the extracted items and run time value
|
|
|
|
except FileNotFoundError:
|
|
app.logger.error('XML file not found.')
|
|
|
|
return [], ''
|
|
|
|
def write_to_xml(file_path, items):
|
|
root = ET.Element("root")
|
|
|
|
for item in items:
|
|
new_element = ET.SubElement(root, "item")
|
|
new_element.set("title", item['title'])
|
|
new_element.set("link", item['link'])
|
|
|
|
tree = ET.ElementTree(root)
|
|
tree.write(file_path, encoding="utf-8", xml_declaration=True)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
items, run_time = parse_xml('./indeed_output.xml') # Retrieve the items and run time
|
|
return render_template('index.html', items=items, run_time=run_time) # Pass the run time to the template
|
|
|
|
@app.route('/search')
|
|
def search():
|
|
query = request.args.get('query')
|
|
items, run_time = parse_xml('./indeed_output.xml') # Retrieve the items and run time
|
|
|
|
# Filter the items based on the search query
|
|
filtered_items = []
|
|
for item in items:
|
|
if query.lower() in item['title'].lower() or query.lower() in item['description'].lower():
|
|
filtered_items.append(item)
|
|
|
|
return render_template('index.html', items=filtered_items, run_time=run_time) # Pass the run time to the template
|
|
|
|
@app.route('/report', methods=['POST'])
|
|
def report():
|
|
title = request.form.get('title')
|
|
link = request.form.get('link')
|
|
|
|
reported_items = list(parse_xml('./reported.xml'))
|
|
reported_items[0].append({'title': title, 'link': link}) # Append the item to the first element of the tuple
|
|
write_to_xml('./reported.xml', reported_items[0]) # Pass the first element of the tuple to write_to_xml
|
|
|
|
response = {
|
|
'message': 'Item reported successfully! Our team will review this listing to see if it is in line with our Statement of Faith. Appropriate filtering will be applied.'
|
|
}
|
|
return jsonify(response)
|
|
|
|
|
|
# Schedule the xmldownloader.py to run every hour
|
|
def run_xmldownloader():
|
|
subprocess.run(['python', 'xmldownloader.py'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# Schedule the xmldownloader.py to run every hour
|
|
schedule.every().hour.do(run_xmldownloader)
|
|
|
|
# Run the Flask app
|
|
app.run()
|
|
|
|
# Start the scheduled job in a separate thread
|
|
while True:
|
|
schedule.run_pending()
|
|
time.sleep(1)
|