Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishnu8299 authored Jul 24, 2024
0 parents commit 4560f3d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
81 changes: 81 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from flask import Flask, request, render_template, send_file
import requests
import feedparser
import json
import re

app = Flask(__name__)

def sanitize_filename(name):
return re.sub(r'[^a-zA-Z0-9_\-\.]', '_', name)

def verify_rss_url(rss_url):
try:
response = requests.get(rss_url)
if response.status_code != 200:
return False, f"The URL did not return a successful response. Status code: {response.status_code}"

content_type = response.headers.get('Content-Type', '')
if 'xml' not in content_type.lower():
return False, f"The URL does not seem to point to an RSS feed (Content-Type: {content_type})"

feed = feedparser.parse(response.content)
if feed.bozo:
return False, "Failed to parse RSS feed. The URL may not be an RSS feed."

feed_info = {
'title': feed.feed.title,
'link': feed.feed.link,
'description': feed.feed.description,
'entries': [],
'total_entries': len(feed.entries)
}

for entry in feed.entries:
entry_info = {
'title': entry.title,
'link': entry.link,
'published': entry.published,
'summary': entry.summary
}
# Check for common places where images might be stored in RSS entries
if 'media_content' in entry and entry.media_content:
entry_info['image'] = entry.media_content[0]['url']
elif 'media_thumbnail' in entry and entry.media_thumbnail:
entry_info['image'] = entry.media_thumbnail[0]['url']
elif 'image' in entry:
entry_info['image'] = entry.image
elif 'enclosures' in entry and entry.enclosures:
entry_info['image'] = entry.enclosures[0]['href']

feed_info['entries'].append(entry_info)

# Use the feed title as the file name
file_name = sanitize_filename(feed.feed.title) + '.json'

# Save feed_info to a JSON file
with open(file_name, 'w') as json_file:
json.dump(feed_info, json_file, indent=4)

return True, file_name
except requests.RequestException as e:
return False, f"An error occurred while trying to fetch the URL: {e}"

@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
rss_url = request.form['rss_url']
is_valid, result = verify_rss_url(rss_url)
if is_valid:
return render_template('result.html', feed_info=result) # Pass feed_info to the template
else:
return render_template('index.html', error=result)

return render_template('index.html')

@app.route('/download/<filename>', methods=['GET'])
def download_file(filename):
return send_file(filename, as_attachment=True)

if __name__ == '__main__':
app.run(debug=True)
17 changes: 17 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>RSS Feed Verifier</title>
</head>
<body>
<h1>RSS Feed Verifier</h1>
<form method="post">
<label for="rss_url">Enter RSS feed URL:</label>
<input type="text" id="rss_url" name="rss_url" required>
<button type="submit">Verify</button>
</form>
{% if error %}
<p style="color: red;">{{ error }}</p>
{% endif %}
</body>
</html>
30 changes: 30 additions & 0 deletions templates/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<title>RSS Feed Information</title>
</head>
<body>
<h1>RSS Feed Information</h1>
<h2><strong>Feed Title:</strong> {{ feed_info['title'] }}</h2>
<p><strong>Feed Link:</strong> <a href="{{ feed_info['link'] }}">{{ feed_info['link'] }}</a></p>
<p><strong>Feed Description:</strong> {{ feed_info['description'] }}</p>
<p><strong>Total Entries:</strong> {{ feed_info['total_entries'] }}</p>

<h3>Entries:</h3>
<ul>
{% for entry in feed_info['entries'] %}
<li>
<strong>Title:</strong> <a href="{{ entry['link'] }}">{{ entry['title'] }}</a><br>
<strong>Published:</strong> {{ entry['published'] }}<br>
<strong>Summary:</strong> {{ entry['summary'] }}<br>
{% if 'image' in entry %}
<img src="{{ entry['image'] }}" alt="Entry Image"><br>
{% endif %}
</li>
{% endfor %}
</ul>

<a href="{{ url_for('download_file', filename=file_name) }}">Download JSON file</a><br>
<a href="{{ url_for('index') }}">Back</a>
</body>
</html>

0 comments on commit 4560f3d

Please sign in to comment.