import feedparser as fp from flask import Flask, jsonify from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://pc:pc@67.149.14.121:3306/Podcast' db = SQLAlchemy(app) class users(db.Model): userID = db.Column(db.Integer, primary_key=True) username = db.Column(db.Text, unique=True) password = db.Column(db.Text) name = db.Column(db.Text) name = db.Column(db.Text) email = db.Column(db.Text) class shows(db.Model): showID = db.Column(db.Integer, primary_key=True) podcastID = db.Column(db.Integer) userID = db.Column(db.Integer) link = db.Column(db.Text) started = db.Column(db.Boolean, default=0) position = db.Column(db.Text, default=0) class podcasts(db.Model): podcastID = db.Column(db.Integer, primary_key=True) userID = db.Column(db.Integer) title = db.Column(db.Text) URL = db.Column(db.Text) artworkURL = db.Column(db.Text) @app.route('/', methods=['GET']) def index(): return 'stuff' @app.route('/login', methods=['GET']) def login(): username = 'Dan' password = 'password' try: user = users.query.filter_by(username=username).first() except: jsonify({'message': 'Login Failed'}) return jsonify(userid=user.userID) @app.route('/updateFeeds//', methods={'POST'}) def updateFeeds(userID): showCheck = [] updatedShows = [] # 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 for show in pc: parsed = fp.parse(show.URL) query = shows.query.filter_by(podcastID=show.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=show.podcastID, userID=userID, link=url) db.session.add(query) db.session.commit() updatedShows.append(show.title) else: continue count = count + 1 # Clear out the array for the next podcast showCheck = [] 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): output = [] y = 0 # grabs podcast urls from logged in user mreh = podcasts.query.filter_by(userID=userID).all() for each in mreh: result = {} result['podcastID'] = each.podcastID result['Title'] = each.title output.append(result) return jsonify(shows=output) @app.route('/register/&&&', methods=['POST']) def register(username, password, name, email): # checks to see if the username already exists if not users.query.filter_by(username=username).first(): new_user = users(username=username, password=password, name=name, email=email) db.session.add(new_user) db.session.commit() # Returns the new userID newID = users.query.filter_by(username=username).first() return jsonify(userid=newID.userID) # if the username doesn't already exist it will be added else: return jsonify(message='Username already exists') if __name__ == "__main__": app.run(host='0.0.0.0', debug=True)