Files
podcast/main.py

259 lines
8.9 KiB
Python

import feedparser as fp
from flask import Flask, jsonify, request
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)
#############
# ALTER TABLE shows AUTO_INCREMENT = 1;
# ALTER TABLE podcasts AUTO_INCREMENT = 1;
# ALTER TABLE users AUTO_INCREMENT = 1;
#############
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)
email = db.Column(db.Text)
# token = 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, default=None)
@app.route('/', methods=['GET'])
def index():
stuff = """<b> /login </b<br>Method: 'GET' JSON: 'username', 'password'<br>
<b>/updateFeeds</b><br>Method: 'PUT', JSON:: 'podcastID' <i>note leave podcastID blank to update all feeds</i><br><br>
<b>/addFeed</b>to Come</b><br><br>
<b>/listPoddcasts/<userID><b><br><br>
"""
return 'stuff'
@app.route('/login/', methods=['GET'])
def login():
username = 'Dan'
password = 'password'
data = request.get_json()
user = users.query.filter_by(username=data['username']).first()
if data['password'] != user.password:
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 = []
updatedShows = []
if data['podcastID'] is None or data['podcastID'] == '':
# grabs podcast urls from logged in user
pc = podcasts.query.filter_by(userID=data['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(userID=data['userID'], 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=data['userID'], link=url)
db.session.add(query)
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 = []
db.session.commit()
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/', methods=['POST'])
def addFeed():
count = 0
data = request.get_json()
parsed = fp.parse(data['newURL'])
title = str(parsed.feed.title)
if not parsed.feed.title:
artwork = str(parsed.feed.image)
else:
artwork = ''
# 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)
count = count + 1
db.session.commit()
return jsonify(title=title, added=1)
else:
return jsonify(title=title, added=0)
# @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)
# count = count + 1
# db.session.commit()
# return jsonify(message='Podcast added')
# else:
# return jsonify(message='Podcast already exists')
@app.route('/listPodcasts/<int:userID>/', methods=['GET'])
def listPodcasts(userID):
output = []
y = 0
# grabs podcast urls from logged in user
pc = podcasts.query.filter_by(userID=userID).all()
for each in pc:
result = {}
result['podcastID'] = each.podcastID
result['Title'] = each.title
output.append(result)
return jsonify(shows=output)
@app.route('/register/', methods=['POST'])
def register():
data = request.get_json()
# checks to see if the username already exists
if not users.query.filter_by(username=data['username']).first():
new_user = users(username=data['username'], password=data['password'], name=data['name'], email=data['email'])
db.session.add(new_user)
db.session.commit()
# Returns the new userID
newID = users.query.filter_by(username=data['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')
@app.route('/updateStatus/<int:userID>/<int:podcastID>/<int:status>')
def updateStatus(userID, podcastID, status):
return ''
@app.route('/updateTime/<int:userID>/<int:podcastID>/<int:time>')
def updateTime(userID, podcastID,time):
return ''
@app.route('/deleteUser/<int:userID>/', methods=['DELETE'])
def deleteUser(userID):
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/')
def removeFeed():
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__":
app.run(host='0.0.0.0', debug=True)