import json from datetime import datetime from flask import Blueprint, request, jsonify with open("data.json", "r", encoding="utf8") as read_data: 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) @api_channels.route("/channels/") def get_channel(channel): """ 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. """ if "vote" in request.args: vote = str(request.args["vote"]) if channel in channels: # Adds/substracts 1 from the channel. if vote == "upvote": channels[channel] += 1 elif vote == "downvote": channels[channel] -= 1 else: return "Vote word not recognised." # Write to database file. with open("data.json", "w", encoding="utf8") as write_data: json.dump(channels, write_data, indent=4) # 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}") 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 else: return "Channel not found on the list." @api_channels.route("/channels//image.svg") def img_channel(channel): """ Returns the YouTube score in a svg image. """ if channel in channels: return f""" {channels[channel]} """ else: return "Channel not found on the list"