Add log function

This commit is contained in:
Jose De Freitas 2021-02-28 20:51:29 -05:00
parent 7c98a15263
commit 82fff5b2d2
4 changed files with 12 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import json
from datetime import datetime
from flask import Blueprint, request, jsonify
with open("data.json", "r", encoding="utf8") as read_data:
@ -27,6 +28,7 @@ def get_channel(channel):
vote = str(request.args["vote"])
if channel in channels:
# Adds/substracts 1 from the channel.
if vote == "upvote":
channels[channel] += 1
elif vote == "downvote":
@ -34,9 +36,15 @@ def get_channel(channel):
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."

View File

@ -1,10 +1,10 @@
{
"Tech With Tim": 0,
"Tech With Tim": 2,
"Derek Banas": 0,
"Don Jones": 0,
"Corey Schafer": 0,
"Brian Will": 0,
"LearningLad": 0,
"LearningLad": -1,
"David Bombal": 0,
"Ben Eater": 0,
"The Coding Train": 0,

1
voter/log.txt Normal file
View File

@ -0,0 +1 @@
-

View File

@ -16,7 +16,7 @@ app.register_blueprint(api_channels)
@app.route("/")
def main():
return "Working"
return "Awesome YouTubers voting system website."
if __name__ == "__main__":