-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
244 lines (220 loc) · 8.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from flask import Flask, render_template, request, session, redirect, url_for, jsonify, send_file
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import praw
import time
import pdfkit
app = Flask(__name__)
app.app_context().push()
app.secret_key = 'supersecretkey'
# Configure database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
db = SQLAlchemy(app)
# Define User model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(50), unique=True, nullable=False)
password = db.Column(db.String(50), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<User %r>' % self.name
# Define Post model
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
user = db.relationship('User', backref=db.backref('posts', lazy=True))
upvotes = db.Column(db.Integer)
comment_count = db.Column(db.Integer)
video_link = db.Column(db.String(200))
image_link = db.Column(db.String(200))
def __repr__(self):
return '<Post %r>' % self.title
# Define Home Page
@app.route('/')
def home():
return render_template('home.html')
# Define Register Page
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
password = request.form['password']
password2 = request.form['password2']
if password == password2:
user = User(name=name, email=email, password=password)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
else:
return render_template('register.html')
return render_template('register.html')
# Define Login Page
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email, password=password).first()
if user:
session['user_id'] = user.id
return redirect(url_for('dashboard'))
else:
return render_template('login.html', message='Invalid email or password')
return render_template('login.html')
# Define Dashboard
@app.route('/dashboard')
def dashboard():
if 'user_id' in session:
user = User.query.filter_by(id=session['user_id']).first()
# Connect to Reddit API
reddit = praw.Reddit(client_id='JMqEtE1wdrD39jhY_ZwTQA',
client_secret='dDqqLlBcblEFzj9EylLgckbPIHdz6w',
user_agent='technicalqualifierwasveryeasy')
subreddit = reddit.subreddit('popular')
# Crawl new posts and add them to database
for post in subreddit.new(limit=100):
# Check if post already exists in database
if not db.session.query(Post).filter(Post.title == post.title).first():
# Create new post and add to database
new_post = Post(title=post.title, content=post.selftext, upvotes=post.score, comment_count=post.num_comments, video_link=None, image_link=None, user=user)
# Check if the post is a video post
if post.is_video:
new_post.video_link = post.media['reddit_video']['fallback_url']
# Check if the post is an image post
elif post.url.endswith(('.jpg', '.jpeg', '.png', '.gif')):
new_post.image_link = post.url
db.session.add(new_post)
db.session.commit()
# Get all posts from database and display on dashboard
posts = Post.query.all()
if request.headers.get('accept') == 'application/json':
# If the request accepts JSON, return the posts as JSON
json_posts = []
for post in posts:
json_post = {
'title': post.title,
'content': post.content,
'upvotes': post.upvotes,
'comment_count': post.comment_count,
'video_link': post.video_link,
'image_link': post.image_link
}
json_posts.append(json_post)
return jsonify(posts=json_posts)
else:
# Otherwise, render the dashboard template
return render_template('dashboard.html', user=user, posts=posts)
else:
return redirect(url_for('login'))
@app.route('/download-pdf')
def download_pdf():
if 'user_id' in session:
user = User.query.filter_by(id=session['user_id']).first()
posts = Post.query.all()
html_content = render_template('dashboard.html', user=user, posts=posts)
options = {
'enable-local-file-access': None,
'quiet': '',
'disable-smart-shrinking': '',
'no-stop-slow-scripts': ''
}
pdfkit.from_string(html_content, 'output.pdf', options=options)
return send_file('output.pdf', as_attachment=True)
else:
session.clear()
return redirect('/')
# Define Api Login
@app.route('/api/login', methods=['GET', 'POST'])
def api_login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email, password=password).first()
if user:
session['user_id'] = user.id
print(jsonify({'message': 'Login Successful, you are redirected in 5 seconds'}))
time.sleep(5)
return redirect('/api/allposts')
else:
return jsonify({'message': 'Your credentials are incorrect. Please try again'})
# Define Api Register
@app.route('/api/register', methods=['GET', 'POST'])
def api_register():
if request.method == 'POST':
email = request.form['email']
name = request.form['name']
password = request.form['password']
user = User(name=name, email=email, password=password)
if user:
session['user_id'] = user.id
print(jsonify({'message': 'Login Successful, you are redirected in 5 seconds'}))
time.sleep(5)
return redirect('/api/allposts')
else:
return jsonify({'message': 'Your credentials are incorrect. Please try again'})
# Define all post api
@app.route('/api/allposts/')
def api_all_posts():
if 'user_id' in session:
posts = Post.query.all()
posts_list = []
for post in posts:
post_dict = {}
for key, value in post.__dict__.items():
if key != '_sa_instance_state':
post_dict[key] = value
posts_list.append(post_dict)
return jsonify(posts=posts_list)
else:
return jsonify({'message': 'Try again after logging in'})
# Define selected post api
# Example usage: /api/selectedposts?name=title&value=brandefense
@app.route('/api/selectedposts/')
def api_selected_posts():
if 'user_id' in session:
name = request.args.get('name')
value = request.args.get('value')
posts = Post.query.filter(getattr(Post, name).ilike(f'%{value}%')).all()
posts_list = []
for post in posts:
post_dict = {}
for key, value in post.__dict__.items():
if key != '_sa_instance_state':
post_dict[key] = value
posts_list.append(post_dict)
return jsonify(posts=posts_list)
else:
return jsonify({'message': 'Try again after logging in'})
# Define upvotes filter
# Example usage: /api/upvotes?value=100
@app.route('/api/upvotes')
def api_upvotes():
if 'user_id' in session:
value = request.args.get('value')
if value:
posts = Post.query.filter(Post.upvotes > int(value)).all()
else:
posts = Post.query.all()
posts_list = []
for post in posts:
post_dict = {}
for key, value in post.__dict__.items():
if key != '_sa_instance_state':
post_dict[key] = value
posts_list.append(post_dict)
return jsonify(posts=posts_list)
else:
return jsonify({'message': 'Try again after logging in'})
# Define Logout
@app.route('/logout')
def logout():
session.pop('user_id', None)
return redirect(url_for('home'))
if __name__ == '__main__':
db.create_all()
app.run(debug=True)