mirror of
https://github.com/JoseDeFreitas/awesome-youtubers.git
synced 2025-01-03 03:20:48 -05:00
Set data queries
This commit is contained in:
parent
1e24e86d02
commit
f3b1cd3333
@ -1,8 +1,7 @@
|
|||||||
import threading
|
import threading
|
||||||
from flask import (
|
from flask import (
|
||||||
Flask, jsonify,
|
Flask, make_response,
|
||||||
make_response, request,
|
request, render_template
|
||||||
render_template
|
|
||||||
)
|
)
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_limiter import Limiter
|
from flask_limiter import Limiter
|
||||||
@ -23,8 +22,8 @@ limiter = Limiter(
|
|||||||
|
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
|
|
||||||
# Database model
|
|
||||||
|
|
||||||
|
# Database model
|
||||||
|
|
||||||
class Channel(db.Model):
|
class Channel(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
@ -32,6 +31,12 @@ class Channel(db.Model):
|
|||||||
vote = db.Column(db.Integer, default=0, nullable=False)
|
vote = db.Column(db.Integer, default=0, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
|
channels = Channel.query.order_by(Channel.name).all()
|
||||||
|
channels_names = [cname.name for cname in channels]
|
||||||
|
|
||||||
|
|
||||||
|
# Routes
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
""" Main route of the website. """
|
""" Main route of the website. """
|
||||||
@ -42,10 +47,7 @@ def index():
|
|||||||
def list_channels():
|
def list_channels():
|
||||||
""" Lists all channels in the database. """
|
""" Lists all channels in the database. """
|
||||||
|
|
||||||
return render_template(
|
return render_template("all.html", channels=channels)
|
||||||
"all.html",
|
|
||||||
channels=Channel.query.order_by(Channel.name).all()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/channels/<channel>")
|
@app.route("/channels/<channel>")
|
||||||
@ -60,22 +62,24 @@ def get_channel(channel):
|
|||||||
if "vote" in request.args:
|
if "vote" in request.args:
|
||||||
vote = str(request.args["vote"])
|
vote = str(request.args["vote"])
|
||||||
|
|
||||||
if channel in channels:
|
if channel in channels_names:
|
||||||
|
cvote = Channel.query.filter_by(name=channel).first()
|
||||||
# Adds/substracts 1 from the channel.
|
# Adds/substracts 1 from the channel.
|
||||||
if vote == "upvote":
|
if vote == "upvote":
|
||||||
channels[channel] += 1
|
cvote.vote += 1
|
||||||
elif vote == "downvote":
|
elif vote == "downvote":
|
||||||
channels[channel] -= 1
|
cvote.vote -= 1
|
||||||
else:
|
else:
|
||||||
return "Vote word not recognised."
|
return "Vote word not recognised."
|
||||||
|
|
||||||
# Write to database file.
|
# Write to database file.
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
return f"You {vote}d successfully the channel {channel}."
|
return f"You {vote}d successfully the channel {channel}."
|
||||||
else:
|
else:
|
||||||
return "Channel not found on the list."
|
return "Channel not found on the list."
|
||||||
else:
|
else:
|
||||||
if channel in Channel.query.order_by(Channel.name).all():
|
if channel in channels_names:
|
||||||
return "Channel: " + channel
|
return "Channel: " + channel
|
||||||
else:
|
else:
|
||||||
return "Channel not found on the list."
|
return "Channel not found on the list."
|
||||||
@ -85,7 +89,8 @@ def get_channel(channel):
|
|||||||
def img_channel(channel):
|
def img_channel(channel):
|
||||||
""" Returns the YouTube score in a svg image. """
|
""" Returns the YouTube score in a svg image. """
|
||||||
|
|
||||||
if channel in Channel.query.order_by(Channel.name).all():
|
if channel in channels_names:
|
||||||
|
cvote = Channel.query.filter_by(name=channel).first()
|
||||||
svg_image = f"""
|
svg_image = f"""
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
|
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
|
||||||
width="52px" height="22px" viewBox="0 0 52 22" fill="none">
|
width="52px" height="22px" viewBox="0 0 52 22" fill="none">
|
||||||
@ -98,7 +103,7 @@ def img_channel(channel):
|
|||||||
<rect x="0.5" y="0.5" height="99%" width="51" fill="none"/>
|
<rect x="0.5" y="0.5" height="99%" width="51" fill="none"/>
|
||||||
<g>
|
<g>
|
||||||
<text x="5" y="15" fill="#00b4f0" class="text">
|
<text x="5" y="15" fill="#00b4f0" class="text">
|
||||||
{channels[channel]}
|
{cvote.vote}
|
||||||
</text>
|
</text>
|
||||||
</g>
|
</g>
|
||||||
</svg>
|
</svg>
|
||||||
@ -108,7 +113,7 @@ def img_channel(channel):
|
|||||||
response.headers.set('Content-Type', 'image/svg+xml')
|
response.headers.set('Content-Type', 'image/svg+xml')
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
return "Channel not found on the list"
|
return "Channel not found on the list."
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -32,4 +32,4 @@ video quality, coverage of the topics it explains, among others), but, of course
|
|||||||
so I think being able to receive opinions from all users is a good way to filter and have truly
|
so I think being able to receive opinions from all users is a good way to filter and have truly
|
||||||
awesome content based on the community's opinion.
|
awesome content based on the community's opinion.
|
||||||
|
|
||||||
<img src="https://awesome-youtubers.herokuapp.com//channels/3Blue1Brown/image.svg">
|
<img src="http://127.0.0.1:5000/channels/Don_Jones/image.svg">
|
@ -1,4 +1,4 @@
|
|||||||
{% extends base.html %}
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<table>
|
<table>
|
||||||
@ -6,9 +6,11 @@
|
|||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Vote</th>
|
<th>Vote</th>
|
||||||
</tr>
|
</tr>
|
||||||
|
{% for channel in channels %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ channels.name }}</td>
|
<td>{{ channel.name }}</td>
|
||||||
<td>{{ channels.vote }}</td>
|
<td>{{ channel.vote }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
{% endblock %}
|
{% endblock %}
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user