Updated requirements text. Got podcast import working. Got show import working. Added "add feed" working. Added exit option. Added some existing podcast/show check logic.
This commit is contained in:
102
main.py
102
main.py
@@ -1,26 +1,35 @@
|
|||||||
import opml
|
import opml
|
||||||
# import sqlalchemy as db
|
# import sqlalchemy as db
|
||||||
import database as db
|
import database as db
|
||||||
|
import feedparser as fp
|
||||||
|
|
||||||
userID = None
|
userID = None
|
||||||
|
|
||||||
def podcastImport (userID):
|
def podcastImport (userID):
|
||||||
print(userID)
|
|
||||||
podcasts = db.sql.Table('Users', db.metadata, autoload=True, autoload_with=db.engine)
|
|
||||||
|
|
||||||
outline = opml.parse("podcast_republic_podcasts.opml")
|
outline = opml.parse("podcast_republic_podcasts.opml")
|
||||||
|
#get the number of shows from the OPML file
|
||||||
x = len(outline)
|
x = len(outline)
|
||||||
y=0
|
y=0
|
||||||
|
|
||||||
|
#loops through each podcast and parses out relevant information
|
||||||
while y < x:
|
while y < x:
|
||||||
title = outline[y].title
|
title = outline[y].title
|
||||||
url = outline[y].xmlUrl
|
url = outline[y].xmlUrl
|
||||||
artwork = outline[y].pr_artwork
|
artwork = outline[y].pr_artwork
|
||||||
desc = outline[y].pr_desc
|
desc = outline[y].pr_desc
|
||||||
query = """insert into podcasts""""(userID, title, URL, desc, artworkURL values (""" + userID + """,""" + title + """,""" + url + """,""" + desc + """,""" + artwork + """)"""
|
|
||||||
|
|
||||||
# print(str(query))
|
#checks to see if the podcast URL already exists for logged in user and skips it if it has already been imported.
|
||||||
db.con.execute(query)
|
query = db.sql.text("""select URL from Podcasts where URL = \"%s\" and userID = %i""" % (url, 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] != url:
|
||||||
|
query = db.sql.text("""insert into Podcasts (userID, title, URL, artworkURL) values(%i, \"%s\", \"%s\", \"%s\") """ % (userID, title, url, artwork))
|
||||||
|
db.con.execute(query)
|
||||||
|
print("%s added" % title)
|
||||||
|
else:
|
||||||
|
print("%s already exists" % title)
|
||||||
y=y+1
|
y=y+1
|
||||||
|
|
||||||
|
|
||||||
@@ -29,9 +38,7 @@ def login():
|
|||||||
password = 'password'
|
password = 'password'
|
||||||
|
|
||||||
|
|
||||||
user = db.sql.Table('Users', db.metadata, autoload=True, autoload_with=db.engine)
|
query = db.sql.text(""" select userID from Users where username = \'%s\'""" % username)
|
||||||
|
|
||||||
query = db.sql.text(""" select userID from Users where username = \"""" + username+"""\"""")
|
|
||||||
result = db.con.execute(query).fetchone()
|
result = db.con.execute(query).fetchone()
|
||||||
|
|
||||||
print(result[0])
|
print(result[0])
|
||||||
@@ -39,10 +46,71 @@ def login():
|
|||||||
return result[0]
|
return result[0]
|
||||||
|
|
||||||
|
|
||||||
while True:
|
def updateFeeds(userID):
|
||||||
print("user " + str(userID))
|
showCheck = []
|
||||||
|
#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()
|
||||||
|
|
||||||
print("1. Login \n 2. Upload")
|
#loops through the returned urls and parses each rss feed
|
||||||
|
y=0
|
||||||
|
for x in shows:
|
||||||
|
#Here we're grabbing all the show urls for each podcast and moving them into a separate array.
|
||||||
|
query = db.sql.text("""select link from Shows where userID = %i and podcastID = %i""" % (userID, shows[y][1]))
|
||||||
|
showGrab = db.con.execute(query).fetchall()
|
||||||
|
z=0
|
||||||
|
for each in showGrab:
|
||||||
|
showCheck.append(showGrab[z][0])
|
||||||
|
z=z+1
|
||||||
|
|
||||||
|
parsed = fp.parse(shows[y][0])
|
||||||
|
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 = db.sql.text("""insert into Shows (podcastID, userID, link) values (%i, %i, \"%s\")""" % (shows[y][1], userID, url))
|
||||||
|
db.con.execute(query)
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
count = count + 1
|
||||||
|
print("%s has been updated" % shows[y][2])
|
||||||
|
y=y+1
|
||||||
|
showCheck = [] #Clear out the array for the next podcast
|
||||||
|
|
||||||
|
def addFeed(userID):
|
||||||
|
newURL = input("Enter Feed URL: ")
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
print("%s already exists" % title)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("1. Login\n2. Upload\n3. Update feeds\n4. Add Feed\n9. Exit\n")
|
||||||
val = input(">>>")
|
val = input(">>>")
|
||||||
if val == "1":
|
if val == "1":
|
||||||
userID = login()
|
userID = login()
|
||||||
@@ -51,3 +119,15 @@ while True:
|
|||||||
print("Please login first")
|
print("Please login first")
|
||||||
else:
|
else:
|
||||||
podcastImport(userID)
|
podcastImport(userID)
|
||||||
|
elif val == "3":
|
||||||
|
if userID is None or userID == '':
|
||||||
|
print("Please login first")
|
||||||
|
else:
|
||||||
|
updateFeeds(userID)
|
||||||
|
elif val == "4":
|
||||||
|
if userID is None or userID == '':
|
||||||
|
print("Please login first")
|
||||||
|
else:
|
||||||
|
addFeed(userID)
|
||||||
|
elif val == "9":
|
||||||
|
exit()
|
||||||
File diff suppressed because one or more lines are too long
@@ -1,3 +1,5 @@
|
|||||||
OPML
|
feedparser==5.2.1
|
||||||
sqlalchemy
|
lxml==4.5.0
|
||||||
pymysql
|
opml==0.5
|
||||||
|
PyMySQL==0.9.3
|
||||||
|
SQLAlchemy==1.3.13
|
||||||
|
|||||||
Reference in New Issue
Block a user