mirror of
https://github.com/bspeice/elektricity
synced 2025-08-02 05:25:42 -04:00
Now working with Pocket Casts!
This commit is contained in:
@ -8,13 +8,18 @@ import requests
|
||||
from feedgen.feed import FeedGenerator
|
||||
|
||||
from podcast import BasePodcast
|
||||
from datetime import datetime
|
||||
from pytz import UTC
|
||||
|
||||
|
||||
class BassdriveParser(HTMLParser):
|
||||
links = []
|
||||
record_link_text = False
|
||||
link_url = ''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.links = []
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
href = ''
|
||||
for attr, val in attrs:
|
||||
@ -27,20 +32,21 @@ class BassdriveParser(HTMLParser):
|
||||
|
||||
def handle_data(self, data):
|
||||
if self.record_link_text:
|
||||
print(self.link_url)
|
||||
self.links.append((data, self.link_url))
|
||||
self.record_link_text = False
|
||||
|
||||
def get_links(self):
|
||||
# Reverse to sort in descending date order
|
||||
self.links.reverse()
|
||||
return self.links
|
||||
|
||||
def clear_links(self):
|
||||
self.links = []
|
||||
|
||||
|
||||
class BassdriveFeed(BasePodcast):
|
||||
def __init__(self, *args, **kwargs):
|
||||
print(kwargs)
|
||||
self.url = kwargs['url']
|
||||
self.logo = kwargs['logo']
|
||||
# Get the title and DJ while handling trailing slash
|
||||
url_pretty = unquote(self.url)
|
||||
elems = filter(lambda x: x, url_pretty.split('/'))
|
||||
@ -56,13 +62,14 @@ class BassdriveFeed(BasePodcast):
|
||||
|
||||
# And turn them into something usable
|
||||
fg = FeedGenerator()
|
||||
fg.load_extension('podcast')
|
||||
#fg.load_extension('podcast')
|
||||
fg.id(self.url)
|
||||
fg.title(self.title)
|
||||
fg.description(self.title)
|
||||
fg.author({'name': self.dj})
|
||||
fg.language('en')
|
||||
fg.link({'href': self.url, 'rel': 'alternate'})
|
||||
fg.logo(self.logo)
|
||||
|
||||
for link in links:
|
||||
fe = fg.add_entry()
|
||||
@ -71,4 +78,14 @@ class BassdriveFeed(BasePodcast):
|
||||
fe.description(link[0])
|
||||
fe.enclosure(self.url + link[1], 0, 'audio/mpeg')
|
||||
|
||||
# Bassdrive always uses date strings of
|
||||
# [yyyy.mm.dd] with 0 padding, so that
|
||||
# makes our lives easy
|
||||
date_start = link[0].find('[')
|
||||
date_str = link[0][date_start:date_start+12]
|
||||
published = datetime.strptime(date_str, '[%Y.%m.%d]')
|
||||
fe.pubdate(UTC.localize(published))
|
||||
fe.guid((link[0]))
|
||||
|
||||
parser.clear_links()
|
||||
return fg
|
||||
|
Reference in New Issue
Block a user