Started reworking the login scrip to actually have logic. Can now update individual shows. Added json input to some methods. Finished user delete method. Finished remove feed method.
This commit is contained in:
140
main.py
140
main.py
@@ -1,5 +1,5 @@
|
|||||||
import feedparser as fp
|
import feedparser as fp
|
||||||
from flask import Flask, jsonify
|
from flask import Flask, jsonify, request
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -34,34 +34,43 @@ class podcasts(db.Model):
|
|||||||
URL = db.Column(db.Text)
|
URL = db.Column(db.Text)
|
||||||
artworkURL = db.Column(db.Text)
|
artworkURL = db.Column(db.Text)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
|
|
||||||
return 'stuff'
|
return 'stuff'
|
||||||
|
|
||||||
@app.route('/login', methods=['GET'])
|
|
||||||
|
@app.route('/login/', methods=['GET'])
|
||||||
def login():
|
def login():
|
||||||
|
|
||||||
username = 'Dan'
|
username = 'Dan'
|
||||||
password = 'password'
|
password = 'password'
|
||||||
|
|
||||||
try:
|
data = request.get_json()
|
||||||
user = users.query.filter_by(username=username).first()
|
|
||||||
except:
|
|
||||||
jsonify({'message': 'Login Failed'})
|
|
||||||
return jsonify(userid=user.userID)
|
|
||||||
|
|
||||||
|
user = users.query.filter_by(username=data['username']).first()
|
||||||
|
|
||||||
@app.route('/updateFeeds/<int:userID>/', methods={'POST'})
|
if data['password'] != user.password:
|
||||||
def updateFeeds(userID):
|
return jsonify(message='Login Failed')
|
||||||
|
else:
|
||||||
|
return jsonify(message='User logged in', userID=user.userID)
|
||||||
|
|
||||||
|
@app.route('/updateFeeds/', methods={'PUT'})
|
||||||
|
def updateFeeds():
|
||||||
|
|
||||||
|
data = request.get_json()
|
||||||
showCheck = []
|
showCheck = []
|
||||||
updatedShows = []
|
updatedShows = []
|
||||||
|
|
||||||
|
if data['podcastID'] is None or data['podcastID'] == '':
|
||||||
# grabs podcast urls from logged in user
|
# grabs podcast urls from logged in user
|
||||||
pc = podcasts.query.filter_by(userID=userID).all()
|
pc = podcasts.query.filter_by(userID=data['userID']).all()
|
||||||
|
|
||||||
# loops through the returned urls and parses each rss feed
|
# loops through the returned urls and parses each rss feed
|
||||||
for show in pc:
|
for show in pc:
|
||||||
parsed = fp.parse(show.URL)
|
parsed = fp.parse(show.URL)
|
||||||
query = shows.query.filter_by(podcastID=show.podcastID).all()
|
query = shows.query.filter_by(userID=data['userID'], podcastID=show.podcastID).all()
|
||||||
for x in query:
|
for x in query:
|
||||||
showCheck.append(x.link)
|
showCheck.append(x.link)
|
||||||
count = 0
|
count = 0
|
||||||
@@ -69,9 +78,8 @@ def updateFeeds(userID):
|
|||||||
url = parsed.entries[count].enclosures[0].get('href')
|
url = parsed.entries[count].enclosures[0].get('href')
|
||||||
# after parsing out the show URL we check if it already exists by comparing it against the showCheck array containing all show URLs
|
# after parsing out the show URL we check if it already exists by comparing it against the showCheck array containing all show URLs
|
||||||
if url not in showCheck:
|
if url not in showCheck:
|
||||||
query = shows(podcastID=show.podcastID, userID=userID, link=url)
|
query = shows(podcastID=show.podcastID, userID=data['userID'], link=url)
|
||||||
db.session.add(query)
|
db.session.add(query)
|
||||||
db.session.commit()
|
|
||||||
new_show = {}
|
new_show = {}
|
||||||
new_show['title'] = show.title
|
new_show['title'] = show.title
|
||||||
updatedShows.append(new_show)
|
updatedShows.append(new_show)
|
||||||
@@ -80,31 +88,84 @@ def updateFeeds(userID):
|
|||||||
count = count + 1
|
count = count + 1
|
||||||
# Clear out the array for the next podcast
|
# Clear out the array for the next podcast
|
||||||
showCheck = []
|
showCheck = []
|
||||||
|
db.session.commit()
|
||||||
return jsonify(newShows=updatedShows)
|
return jsonify(newShows=updatedShows)
|
||||||
|
else:
|
||||||
|
pc = podcasts.query.filter_by(userID=data['userID'], podcastID=data['podcastID']).first()
|
||||||
|
parsed = fp.parse(pc.URL)
|
||||||
|
|
||||||
|
query = shows.query.filter_by(userID=data['userID'], podcastID=data['podcastID']).all()
|
||||||
|
for x in query:
|
||||||
|
showCheck.append(x.link)
|
||||||
|
count = 0
|
||||||
|
for items in parsed['entries']:
|
||||||
|
url = parsed.entries[count].enclosures[0].get('href')
|
||||||
|
# after parsing out the show URL we check if it already exists by comparing it against the showCheck array containing all show URLs
|
||||||
|
if url not in showCheck:
|
||||||
|
query = shows(podcastID=pc.podcastID, userID=data['userID'], link=url)
|
||||||
|
db.session.add(query)
|
||||||
|
db.session.commit()
|
||||||
|
new_show = {}
|
||||||
|
new_show['title'] = pc.title
|
||||||
|
updatedShows.append(new_show)
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
return jsonify(singleShow=updatedShows)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/addFeed/<int:userID>/<path:newURL>', methods=['PUT'])
|
@app.route('/addFeed', methods=['POST'])
|
||||||
def addFeed(userID,newURL):
|
def addFeed():
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
data = request.get_json()
|
||||||
parsed = fp.parse(newURL)
|
parsed = fp.parse(data['newURL'])
|
||||||
|
|
||||||
title = str(parsed.feed.title)
|
title = str(parsed.feed.title)
|
||||||
artwork = str(parsed.feed.image)
|
artwork = str(parsed.feed.image)
|
||||||
|
|
||||||
# checks to see if the podcast URL already exists for logged in user and skips it if it has already been imported.
|
# checks to see if the podcast URL already exists for logged in user and skips it if it has already been imported.
|
||||||
# if the show doesn't already exist for the logged in user it gets added
|
# if the show doesn't already exist for the logged in user it gets added
|
||||||
if not podcasts.query.filter_by(URL=newURL, userID=userID).first():
|
if not podcasts.query.filter_by(URL=data['newURL'], userID=data['userID']).first():
|
||||||
query = podcasts(userID=userID, title=title, URL=newURL, artworkURL=artwork)
|
query = podcasts(userID=data['userID'], title=title, URL=data['newURL'], artworkURL=artwork)
|
||||||
db.session.add(query)
|
db.session.add(query)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
# Once the podcast is added we grab the podcastID so we can import the shows
|
# Once the podcast is added we grab the podcastID so we can import the shows
|
||||||
result = podcasts.query.filter_by(URL=newURL, userID=userID).first()
|
result = podcasts.query.filter_by(URL=data['newURL'], userID=data['userID']).first()
|
||||||
# Import the new shows. No need to check if the show URLS already exist since it's new
|
# Import the new shows. No need to check if the show URLS already exist since it's new
|
||||||
for items in parsed['entries']:
|
for items in parsed['entries']:
|
||||||
itemURL = parsed.entries[count].enclosures[0].get('href')
|
itemURL = parsed.entries[count].enclosures[0].get('href')
|
||||||
query = shows(podcastID=result.podcastID, userID=userID, link=itemURL)
|
query = shows(podcastID=result.podcastID, userID=data['userID'], link=itemURL)
|
||||||
|
db.session.add(query)
|
||||||
|
count = count + 1
|
||||||
|
db.session.commit()
|
||||||
|
return jsonify(message='Podcast added')
|
||||||
|
else:
|
||||||
|
return jsonify(message='Podcast already exists')
|
||||||
|
|
||||||
|
@app.route('/importFeed/', methods=['POST'])
|
||||||
|
def importFeed():
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
data = request.get_json()
|
||||||
|
parsed = fp.parse(data['newURL'])
|
||||||
|
|
||||||
|
title = str(parsed.feed.title)
|
||||||
|
artwork = str(parsed.feed.image)
|
||||||
|
|
||||||
|
# checks to see if the podcast URL already exists for logged in user and skips it if it has already been imported.
|
||||||
|
# if the show doesn't already exist for the logged in user it gets added
|
||||||
|
if not podcasts.query.filter_by(URL=data['newURL'], userID=data['userID']).first():
|
||||||
|
query = podcasts(userID=data['userID'], title=title, URL=data['newURL'], artworkURL=artwork)
|
||||||
|
db.session.add(query)
|
||||||
|
db.session.commit()
|
||||||
|
# Once the podcast is added we grab the podcastID so we can import the shows
|
||||||
|
result = podcasts.query.filter_by(URL=data['newURL'], userID=data['userID']).first()
|
||||||
|
# Import the new shows. No need to check if the show URLS already exist since it's new
|
||||||
|
for items in parsed['entries']:
|
||||||
|
itemURL = parsed.entries[count].enclosures[0].get('href')
|
||||||
|
query = shows(podcastID=result.podcastID, userID=data['userID'], link=itemURL)
|
||||||
db.session.add(query)
|
db.session.add(query)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@@ -115,6 +176,7 @@ def addFeed(userID,newURL):
|
|||||||
|
|
||||||
@app.route('/listPodcasts/<int:userID>/', methods=['GET'])
|
@app.route('/listPodcasts/<int:userID>/', methods=['GET'])
|
||||||
def listPodcasts(userID):
|
def listPodcasts(userID):
|
||||||
|
|
||||||
output = []
|
output = []
|
||||||
y = 0
|
y = 0
|
||||||
# grabs podcast urls from logged in user
|
# grabs podcast urls from logged in user
|
||||||
@@ -129,15 +191,18 @@ def listPodcasts(userID):
|
|||||||
return jsonify(shows=output)
|
return jsonify(shows=output)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/register/<string:username>&<string:password>&<string:name>&<string:email>', methods=['POST'])
|
@app.route('/register/', methods=['POST'])
|
||||||
def register(username, password, name, email):
|
def register():
|
||||||
|
|
||||||
|
data = request.get_json()
|
||||||
|
|
||||||
# checks to see if the username already exists
|
# checks to see if the username already exists
|
||||||
if not users.query.filter_by(username=username).first():
|
if not users.query.filter_by(username=data['username']).first():
|
||||||
new_user = users(username=username, password=password, name=name, email=email)
|
new_user = users(username=data['username'], password=data['password'], name=data['name'], email=data['email'])
|
||||||
db.session.add(new_user)
|
db.session.add(new_user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
# Returns the new userID
|
# Returns the new userID
|
||||||
newID = users.query.filter_by(username=username).first()
|
newID = users.query.filter_by(username=data['username']).first()
|
||||||
return jsonify(userid=newID.userID)
|
return jsonify(userid=newID.userID)
|
||||||
# if the username doesn't already exist it will be added
|
# if the username doesn't already exist it will be added
|
||||||
else:
|
else:
|
||||||
@@ -154,14 +219,27 @@ def updateTime(userID, podcastID,time):
|
|||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
@app.route('/deleteUser/<int:userID>')
|
@app.route('/deleteUser/<int:userID>/', methods=['DELETE'])
|
||||||
def deleteUser(userID):
|
def deleteUser(userID):
|
||||||
return''
|
|
||||||
|
users.query.filter_by(userID=userID).delete()
|
||||||
|
podcasts.query.filter_by(userID=userID).delete()
|
||||||
|
shows.query.filter_by(userID=userID).delete()
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return jsonify(message="User deleted")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/removeFeed/<int:userID>/<int:podcastID>')
|
@app.route('/removeFeed/')
|
||||||
def removeFeed(userID, podcastID):
|
def removeFeed():
|
||||||
return ''
|
|
||||||
|
data = request.get_json()
|
||||||
|
|
||||||
|
podcasts.query.filter_by(userID=data['userID'], podcastID=data['podcastID']).delete()
|
||||||
|
shows.query.filter_by(userID=data['userID'], podcastID=data['podcastID']).delete()
|
||||||
|
|
||||||
|
return jsonify(message='Show delete')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host='0.0.0.0', debug=True)
|
app.run(host='0.0.0.0', debug=True)
|
||||||
Reference in New Issue
Block a user