-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
781979f
commit 1a6a4aa
Showing
2 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
This is my Stage 2 file. i have used file systems to build this app. | ||
# overview of filesystem used in this project. | ||
i have files such as - | ||
|
||
users.txt - this file has python dictionary which contains username as key and base64 encoded string as value which is the password(we were provided with base64 strings for password so we did'nt encode it at back end . | ||
but you can encode this in front end using javascript and send it to the backend. | ||
|
||
category.txt - this file is a python dictionary with key and category name and value is an integer which represents the number of acts this particular category has. | ||
|
||
acts.txt - this file is a python dictionary with key as actID and value as to which category it belongs to. | ||
|
||
and we have categories folder in which we create a folder for each category and place all the acts for that category in its folder. | ||
example we have categories abc and efg and act(1) belongs to abc similarly act(2) belongs to efg. then categories folder looks like | ||
categories/abc/1 and categories/efg2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
from flask import Flask, render_template , request ,jsonify | ||
import hashlib | ||
import json | ||
import os | ||
import shutil | ||
import datetime | ||
import base64 | ||
import binascii | ||
import re | ||
from werkzeug import secure_filename | ||
from flask import request | ||
|
||
app= Flask(__name__) | ||
|
||
@app.route('/') | ||
|
||
@app.route("/api/v1/users",methods=['POST']) | ||
def about(): | ||
r={} | ||
ip=request.get_json() | ||
if request.method != 'POST': | ||
return jsonify(r),405 | ||
name=ip['username'] | ||
pas=ip['password'] | ||
|
||
f = open("users.txt",'r') | ||
user = json.load(f) | ||
if name in user.keys(): | ||
return jsonify(r),400 | ||
|
||
if len(pas) != 40: | ||
return jsonify(r),400 | ||
try: | ||
sha_int = int(pas, 16) | ||
except ValueError: | ||
return jsonify(r),400 | ||
|
||
user[name] = pas | ||
f = open("users.txt",'w') | ||
json.dump(user,f) | ||
f.close() | ||
|
||
return jsonify(r),201 | ||
|
||
|
||
|
||
@app.route("/api/v1/users/<username>",methods=['DELETE']) | ||
def remove(username): | ||
r={} | ||
if request.method != 'DELETE': | ||
return jsonify(r),405 | ||
|
||
f = open("users.txt",'r') | ||
user=json.load(f) | ||
f.close() | ||
if username in user.keys(): | ||
del user[username] | ||
f=open("users.txt",'w') | ||
json.dump(user,f) | ||
f.close() | ||
return jsonify(r),200 | ||
else: | ||
return jsonify(r),400 | ||
|
||
@app.route("/api/v1/categories",methods=['GET']) | ||
def list_cat(): | ||
r={} | ||
if request.method =='GET': | ||
f=open("category.txt",'r') | ||
r=json.load(f) | ||
if bool(r)==False: | ||
return jsonify(r),204 | ||
return jsonify(r),200 | ||
else: | ||
return jsonify(r),405 | ||
|
||
@app.route("/api/v1/categories",methods=['POST']) | ||
def add_cat(): | ||
r={} | ||
if request.method == 'POST': | ||
f=open("category.txt",'r') | ||
a=json.load(f) | ||
f.close() | ||
ip=request.get_json() | ||
if ip[0] in a.keys(): | ||
return jsonify(r),400 | ||
else: | ||
cn=ip[0] | ||
os.mkdir("categories/"+cn) | ||
fp=open("categories/"+cn+"/act.txt",'w') | ||
d=[] | ||
json.dump(d,fp) | ||
fp.close() | ||
n=open("category.txt",'r') | ||
temp=json.load(n) | ||
temp[cn]=0 | ||
n.close() | ||
n=open("category.txt",'w') | ||
json.dump(temp,n) | ||
n.close() | ||
return jsonify(r),201 | ||
else: | ||
return jsonify(r),405 | ||
|
||
@app.route("/api/v1/categories/<categoryName>",methods=['DELETE']) | ||
def rem_cat(categoryName): | ||
r={} | ||
if request.method != 'DELETE': | ||
return jsonify(r),405 | ||
fp=open("category.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
if categoryName not in temp.keys(): | ||
return jsonify(r),400 | ||
|
||
del temp[categoryName] | ||
|
||
fp=open("category.txt",'w') | ||
json.dump(temp,fp) | ||
fp.close() | ||
shutil.rmtree('categories/'+categoryName) | ||
return jsonify(r),200 | ||
|
||
|
||
@app.route("/api/v1/acts",methods=['POST']) | ||
def upload_act(): | ||
r={} | ||
if request.method !='POST': | ||
return jsonify(r),405 | ||
fp=open("acts.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
ip=request.get_json() | ||
if str(ip['actId']) in temp.keys(): | ||
return jsonify(r),400 | ||
|
||
fp=open("users.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
if ip['username'] not in temp.keys(): | ||
return jsonify(r),400 | ||
|
||
try: | ||
datetime.datetime.strptime(ip['timestamp'],'%d-%m-%Y:%S-%M-%H') | ||
except: | ||
return jsonify(r),400 | ||
|
||
if not re.match("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$",ip['imgB64']): | ||
return jsonify(r),400 | ||
|
||
if "upvote" in ip.keys(): | ||
return jsonify(r),400 | ||
|
||
fp=open("category.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
if ip['categoryName'] not in temp.keys(): | ||
return jsonify(r),400 | ||
|
||
fp=open("acts.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
actId=ip['actId'] | ||
temp[actId]=ip['categoryName']; | ||
fp=open("acts.txt",'w') | ||
json.dump(temp,fp) | ||
fp.close() | ||
|
||
fp=open("categories/"+ip['categoryName']+"/act.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
d={} | ||
d["actId"]=ip['actId'] | ||
d["username"]=ip['username'] | ||
d["timestamp"]=ip['timestamp'] | ||
d["caption"]=ip['caption'] | ||
d["upvotes"]=0 | ||
d["imgB64"]=ip['imgB64'] | ||
temp.append(d) | ||
fp=open("categories/"+ip['categoryName']+"/act.txt",'w') | ||
json.dump(temp,fp) | ||
fp.close() | ||
|
||
fp=open("category.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
temp[ip['categoryName']]=temp[ip['categoryName']]+1 | ||
fp=open("category.txt",'w') | ||
json.dump(temp,fp) | ||
fp.close() | ||
|
||
return jsonify(r),200 | ||
|
||
@app.route("/api/v1/categories/<categoryName>/acts",methods=['GET']) | ||
def list_acts_lt100(categoryName): | ||
r=[] | ||
if request.method !='GET': | ||
return jsonify(r),405 | ||
fp=open("category.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
if categoryName not in temp.keys(): | ||
return jsonify(r),204 | ||
|
||
print(request.args.get('start')) | ||
if request.args.get('start') is not None: | ||
st= int(request.args.get('start')) | ||
ed= int(request.args.get('end')) | ||
fp=open("categories/"+categoryName+"/act.txt",'r') | ||
s=json.load(fp) | ||
fp.close() | ||
if len(s)<ed or st<0: | ||
return jsonify(r),413 | ||
elif len(s) == 0: | ||
return jsonify(r),204 | ||
else: | ||
s.sort(key=lambda x:x['timestamp'], reverse = True) | ||
t1=[] | ||
for i in range(st-1,ed): | ||
t1.append(s[i]) | ||
print("first") | ||
return jsonify(t1),200 | ||
elif request.args.get('start') is None: | ||
fp=open("categories/"+categoryName+"/act.txt",'r') | ||
temp=json.load(fp) | ||
if len(temp) >= 100 : | ||
return jsonify(r),413 | ||
elif len(temp) == 0: | ||
return jsonify(r),204 | ||
else: | ||
r=temp | ||
print("hello") | ||
return jsonify(r),200 | ||
|
||
@app.route("/api/v1/categories/<categoryName>/acts/size",methods=['GET']) | ||
def list_no_of_acts(categoryName): | ||
r=[] | ||
if request.method !='GET': | ||
return json.dumps(r),405 | ||
fp=open("category.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
if categoryName not in temp.keys(): | ||
return json.dumps(r),204 | ||
|
||
|
||
|
||
fp=open("categories/"+categoryName+"/act.txt",'r') | ||
temp=json.load(fp) | ||
r.append(len(temp)) | ||
return jsonify(r),200 | ||
|
||
|
||
@app.route("/api/v1/acts/upvote",methods=['POST']) | ||
def upvote(): | ||
r={} | ||
|
||
if request.method!='POST': | ||
return jsonify(r),405 | ||
|
||
fp=open("acts.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
ip=request.get_json() | ||
|
||
x=ip[0] | ||
z=str(x) | ||
y=temp[z] | ||
|
||
if str(x) not in temp.keys(): | ||
return jsonify(r),400 | ||
|
||
fp=open("categories/"+y+"/act.txt",'r') | ||
t1=json.load(fp) | ||
fp.close() | ||
for i in range(len(t1)): | ||
ab=t1[i] | ||
if ab['actId']==x: | ||
ab['upvotes']+=1 | ||
|
||
fp=open("categories/"+y+"/act.txt",'w') | ||
json.dump(t1,fp) | ||
fp.close() | ||
return jsonify(r),200 | ||
|
||
@app.route("/api/v1/acts/<actId>",methods=['DELETE']) | ||
def del_act(actId): | ||
r={} | ||
if request.method != 'DELETE': | ||
return jsonify(r),405 | ||
|
||
fp=open("acts.txt",'r') | ||
temp=json.load(fp) | ||
fp.close() | ||
|
||
if actId not in temp.keys(): | ||
return jsonify(r),400 | ||
|
||
catn=temp[actId] | ||
fp=open("categories/"+catn+"/act.txt",'r') | ||
t1=json.load(fp) | ||
|
||
fp.close() | ||
|
||
for i in range(len(t1)): | ||
ab=t1[i] | ||
if ab['actId'] == int(actId): | ||
t1.remove(t1[i]) | ||
fp=open("categories/"+catn+"/act.txt",'w') | ||
json.dump(t1,fp) | ||
fp.close() | ||
fp=open("acts.txt",'r') | ||
q=json.load(fp) | ||
fp.close() | ||
del q[actId] | ||
fp=open("acts.txt",'w') | ||
json.dump(q,fp) | ||
fp.close() | ||
fp=open("category.txt",'r') | ||
w=json.load(fp) | ||
fp.close() | ||
temp=w[catn] | ||
w[catn]=temp-1 | ||
fp=open("category.txt",'w') | ||
json.dump(w,fp) | ||
fp.close() | ||
return jsonify(r),200 | ||
|
||
|
||
if __name__=='__main__': | ||
app.run(port=88,debug=True) |