diff --git a/main.py b/main.py index 06ff2b0..55ae7c9 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,17 @@ import database as db -import Feed -from flask import request, url_for +import feedparser as fp +from flask import jsonify from flask_api import FlaskAPI, status, exceptions + app = FlaskAPI(__name__) -@app.route('/') +@app.route('/', methods=['GET']) def index(): - return { - 'stuff' - } -@app.route('/login') + return 'stuff' + +@app.route('/login', methods=['GET']) def login(): username = 'Dan' password = 'password' @@ -19,12 +19,15 @@ def login(): query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username) result = db.con.execute(query).fetchone() - return result[0] + return jsonify(result[0]) -@app.route('/updateFeeds//') +@app.route('/updateFeeds//', methods={'GET'}) def updateFeeds(userID): showCheck = [] + updatedShows = [] + + # grabs podcast urls from logged in user query = db.sql.text("""select URL, podcastID, title from Podcasts where userID = %i""" % userID) shows = db.con.execute(query).fetchall() @@ -49,13 +52,90 @@ def updateFeeds(userID): query = db.sql.text("""insert into Shows (podcastID, userID, link) values (%i, %i, \"%s\")""" % ( shows[y][1], userID, url)) db.con.execute(query) - print("%s has a new show" % shows[y][2]) + updatedShows.append(shows[y][2]) else: continue count = count + 1 y = y + 1 showCheck = [] # Clear out the array for the next podcast + return jsonify(updatedShows) + + + +# @app.route('/addFeed//', methods=['PUT']) +@app.route('/addFeed//', methods=['PUT']) +def addFeed(userID,newURL): + count = 0 + + parsed = fp.parse(newURL) + + title = parsed.feed.title + artwork = 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. + query = db.sql.text("""select URL from Podcasts where URL = \"%s\" and userID = %i""" % (newURL, userID)) + exists = db.con.execute(query).fetchone() +# if the show doesn't already exist for the logged in user it gets added + if exists is None or exists[0] != newURL: + query = db.sql.text("""insert into Podcasts (userID, title, URL, artworkURL) values(%i, \"%s\", \"%s\", \"%s\") """ % (userID, title, newURL, artwork)) + db.con.execute(query) + print("%s added" % title) +# Once the podcast is added we grab the podcastID so we can import the shows + query = db.sql.text("""select podcastID from Podcasts where userID = %i and URL = \"%s\"""" % (userID, newURL)) + result = db.con.execute(query).fetchone() + podcastID = result[0] + +# 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 = db.sql.text("""insert into Shows (podcastID, userID, link) values (%i, %i, \"%s\")""" % (podcastID, userID, itemURL)) + db.con.execute(query) + count = count + 1 + return ("%s added" % title) + else: + return ("%s already exists" % title) + +@app.route('/listShows//', methods=['GET']) +def listShows(userID): + podcasts=[] + pc="" + y=0 + # grabs podcast urls from logged in user + query = db.sql.text("""select podcastID, title from Podcasts where userID = %i""" % userID) + results = db.con.execute(query).fetchall() + for each in results: + # podcasts.append(results[y][0]) + podcasts.append(results[y][1]) + + y=y+1 + # x=0 + # for each2 in podcasts: + # pc=jsonify( + # showID=podcasts[0][0], + # showname=podcasts[0][1] + # ) + # x=x+1 + # return jsonify(pc) + return jsonify(podcasts) + +@app.route('/register/,,,', methods=['GET']) +def register(username, password, name, email): +# checks to see if the username already exists + query = db.sql.text("""select username from Users where username = \"%s\"""" % username) + exists = db.con.execute(query).fetchone() +# if the username doesn't already exist it will be added + if exists is None or exists[0] != username: + query = db.sql.text( + """insert into Users (username, password, name, email) values(\"%s\", \"%s\", \"%s\", \"%s\") """ % ( + username, password, name, email)) + db.con.execute(query) +# Returns the new userID + query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username) + result = db.con.execute(query).fetchone() + return jsonify(result[0]) + else: + return "Username already exists" + if __name__ == "__main__": - app.run(host='0.0.0.0', debug=True) - + app.run(host='0.0.0.0', debug=True) \ No newline at end of file