Reworked addFeed method and have that working correctly now. Started adding updateStatus method.

This commit is contained in:
2020-03-07 19:39:09 -05:00
parent 0e0222d645
commit d4e2319045

48
main.py
View File

@@ -1,6 +1,8 @@
import feedparser as fp import feedparser as fp
from flask import Flask, jsonify from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from urllib.parse import unquote_plus
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
@@ -72,51 +74,49 @@ def updateFeeds(userID):
query = shows(podcastID=show.podcastID, userID=userID, link=url) query = shows(podcastID=show.podcastID, userID=userID, link=url)
db.session.add(query) db.session.add(query)
db.session.commit() db.session.commit()
updatedShows.append(show.title) new_show = {}
new_show['title'] = show.title
updatedShows.append(new_show)
else: else:
continue continue
count = count + 1 count = count + 1
# Clear out the array for the next podcast # Clear out the array for the next podcast
showCheck = [] showCheck = []
return jsonify(updatedShows) return jsonify(newShows=updatedShows)
# @app.route('/addFeed/<int:userID>/<string:newURL>', methods=['PUT']) @app.route('/addFeed/<int:userID>/<path:newURL>', methods=['PUT'])
@app.route('/addFeed/<int:userID>/', methods=['PUT'])
def addFeed(userID,newURL): def addFeed(userID,newURL):
count = 0 count = 0
parsed = fp.parse(newURL) parsed = fp.parse(newURL)
title = parsed.feed.title title = str(parsed.feed.title)
artwork = 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.
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 the show doesn't already exist for the logged in user it gets added
if exists is None or exists[0] != newURL: if not podcasts.query.filter_by(URL=newURL, userID=userID).first():
query = db.sql.text("""insert into Podcasts (userID, title, URL, artworkURL) values(%i, \"%s\", \"%s\", \"%s\") """ % (userID, title, newURL, artwork)) query = podcasts(userID=userID, title=title, URL=newURL, artworkURL=artwork)
db.con.execute(query) db.session.add(query)
print("%s added" % title) 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
query = db.sql.text("""select podcastID from Podcasts where userID = %i and URL = \"%s\"""" % (userID, newURL)) result = podcasts.query.filter_by(URL=newURL, userID=userID).first()
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 # 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 = db.sql.text("""insert into Shows (podcastID, userID, link) values (%i, %i, \"%s\")""" % (podcastID, userID, itemURL)) query = shows(podcastID=result.podcastID, userID=userID, link=itemURL)
db.con.execute(query) db.session.add(query)
count = count + 1 count = count + 1
return ("%s added" % title) db.session.commit()
return jsonify(message='Podcast added')
else: else:
return ("%s already exists" % title) return jsonify(message='Podcast already exists')
@app.route('/listShows/<int:userID>/', methods=['GET']) @app.route('/listPodcasts/<int:userID>/', methods=['GET'])
def listShows(userID): def listPodcasts(userID):
output = [] output = []
y = 0 y = 0
# grabs podcast urls from logged in user # 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 # if the username doesn't already exist it will be added
else: else:
return jsonify(message='Username already exists') return jsonify(message='Username already exists')
@app.route('/updateStatus/<int:userID>/<int:podcastID>/<int:status>')
def updateStatus(userID, podcastID, status):
return ''
if __name__ == "__main__": if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True) app.run(host='0.0.0.0', debug=True)