From d4e23190452ccce92f10309cde9244267dda1495 Mon Sep 17 00:00:00 2001 From: Dan Dembinski Date: Sat, 7 Mar 2020 19:39:09 -0500 Subject: [PATCH] Reworked addFeed method and have that working correctly now. Started adding updateStatus method. --- main.py | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 4850451..f8a6397 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ import feedparser as fp from flask import Flask, jsonify from flask_sqlalchemy import SQLAlchemy +from urllib.parse import unquote_plus + app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False @@ -58,7 +60,7 @@ def updateFeeds(userID): # grabs podcast urls from logged in user pc = podcasts.query.filter_by(userID=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: parsed = fp.parse(show.URL) query = shows.query.filter_by(podcastID=show.podcastID).all() @@ -72,51 +74,49 @@ def updateFeeds(userID): query = shows(podcastID=show.podcastID, userID=userID, link=url) db.session.add(query) db.session.commit() - updatedShows.append(show.title) + new_show = {} + new_show['title'] = show.title + updatedShows.append(new_show) else: continue count = count + 1 # Clear out the array for the next podcast showCheck = [] - return jsonify(updatedShows) + return jsonify(newShows=updatedShows) -# @app.route('/addFeed//', methods=['PUT']) -@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 + 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. - 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) + if not podcasts.query.filter_by(URL=newURL, userID=userID).first(): + query = podcasts(userID=userID, title=title, URL=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 - 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] - + result = podcasts.query.filter_by(URL=newURL, userID=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 = db.sql.text("""insert into Shows (podcastID, userID, link) values (%i, %i, \"%s\")""" % (podcastID, userID, itemURL)) - db.con.execute(query) + query = shows(podcastID=result.podcastID, userID=userID, link=itemURL) + db.session.add(query) count = count + 1 - return ("%s added" % title) + db.session.commit() + return jsonify(message='Podcast added') else: - return ("%s already exists" % title) + return jsonify(message='Podcast already exists') -@app.route('/listShows//', methods=['GET']) -def listShows(userID): +@app.route('/listPodcasts//', methods=['GET']) +def listPodcasts(userID): output = [] y = 0 # grabs podcast urls from logged in user @@ -143,7 +143,9 @@ def register(username, password, name, email): # if the username doesn't already exist it will be added else: return jsonify(message='Username already exists') - +@app.route('/updateStatus///') +def updateStatus(userID, podcastID, status): + return '' if __name__ == "__main__": app.run(host='0.0.0.0', debug=True) \ No newline at end of file