2021-02-28 12:01:11 -05:00
|
|
|
import json
|
2021-02-28 20:51:29 -05:00
|
|
|
from datetime import datetime
|
2021-03-01 12:42:37 -05:00
|
|
|
from flask import Blueprint, request, jsonify, make_response
|
2021-02-28 12:01:11 -05:00
|
|
|
|
2021-02-28 15:25:12 -05:00
|
|
|
with open("data.json", "r", encoding="utf8") as read_data:
|
2021-02-28 12:01:11 -05:00
|
|
|
channels = json.load(read_data)
|
|
|
|
|
|
|
|
api_channels = Blueprint('api_channels', __name__)
|
|
|
|
|
|
|
|
|
|
|
|
@api_channels.route("/channels/all")
|
|
|
|
def list_channels():
|
|
|
|
""" Lists all channels in the database. """
|
|
|
|
|
|
|
|
return jsonify(channels)
|
|
|
|
|
|
|
|
|
2021-02-28 17:52:08 -05:00
|
|
|
@api_channels.route("/channels/<channel>")
|
|
|
|
def get_channel(channel):
|
2021-02-28 20:37:04 -05:00
|
|
|
"""
|
|
|
|
If no query specified, prints the name of the
|
|
|
|
YouTube channel typed. When a query with the
|
|
|
|
name "vote" is given, adds or substracts 1
|
|
|
|
from the specified YouTube score.
|
|
|
|
"""
|
|
|
|
|
2021-02-28 17:52:08 -05:00
|
|
|
if "vote" in request.args:
|
2021-02-28 15:25:12 -05:00
|
|
|
vote = str(request.args["vote"])
|
2021-02-28 16:18:51 -05:00
|
|
|
|
2021-02-28 17:52:08 -05:00
|
|
|
if channel in channels:
|
2021-02-28 20:51:29 -05:00
|
|
|
# Adds/substracts 1 from the channel.
|
2021-02-28 17:52:08 -05:00
|
|
|
if vote == "upvote":
|
|
|
|
channels[channel] += 1
|
|
|
|
elif vote == "downvote":
|
|
|
|
channels[channel] -= 1
|
|
|
|
else:
|
|
|
|
return "Vote word not recognised."
|
2021-02-28 12:01:11 -05:00
|
|
|
|
2021-02-28 20:51:29 -05:00
|
|
|
# Write to database file.
|
2021-02-28 17:52:08 -05:00
|
|
|
with open("data.json", "w", encoding="utf8") as write_data:
|
|
|
|
json.dump(channels, write_data, indent=4)
|
|
|
|
|
2021-02-28 20:51:29 -05:00
|
|
|
# Write to log file.
|
|
|
|
with open("log.txt", "a") as append:
|
|
|
|
today = datetime.today().strftime('%Y-%m-%d-%H:%M')
|
|
|
|
append.write(f"\n{vote.title()}d {channel} on {today}")
|
|
|
|
|
2021-02-28 17:52:08 -05:00
|
|
|
return f"You {vote}d successfully the channel {channel}."
|
|
|
|
else:
|
|
|
|
return "Channel not found on the list."
|
|
|
|
else:
|
|
|
|
if channel in channels:
|
|
|
|
return "Channel: " + channel
|
2021-02-28 15:25:12 -05:00
|
|
|
else:
|
2021-02-28 17:52:08 -05:00
|
|
|
return "Channel not found on the list."
|
|
|
|
|
2021-02-28 15:25:12 -05:00
|
|
|
|
2021-02-28 17:52:08 -05:00
|
|
|
@api_channels.route("/channels/<channel>/image.svg")
|
|
|
|
def img_channel(channel):
|
2021-02-28 20:37:04 -05:00
|
|
|
""" Returns the YouTube score in a svg image. """
|
2021-03-01 09:53:43 -05:00
|
|
|
|
2021-03-01 12:42:37 -05:00
|
|
|
svg_image = f"""
|
2021-02-28 17:52:08 -05:00
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
|
|
|
|
width="52px" height="22px" viewBox="0 0 52 22" fill="none">
|
|
|
|
<style>
|
|
|
|
.text {{
|
|
|
|
font-family: "Segoe UI", Ubuntu, Sans-Serif;
|
|
|
|
font-weight: bold;
|
|
|
|
}}
|
2021-03-01 09:53:43 -05:00
|
|
|
</style>
|
|
|
|
<rect x="0.5" y="0.5" height="99%" width="51" fill="none"/>
|
2021-02-28 17:52:08 -05:00
|
|
|
<g>
|
|
|
|
<text x="5" y="17" fill="#00b4f0" class="text">
|
|
|
|
{channels[channel]}
|
|
|
|
</text>
|
|
|
|
</g>
|
|
|
|
</svg>
|
|
|
|
"""
|
2021-03-01 12:42:37 -05:00
|
|
|
|
|
|
|
if channel in channels:
|
|
|
|
response = make_response(svg_image)
|
|
|
|
response.headers.set('Content-Type', 'image/svg+xml')
|
|
|
|
return response
|
2021-02-28 12:01:11 -05:00
|
|
|
else:
|
2021-02-28 17:52:08 -05:00
|
|
|
return "Channel not found on the list"
|