handle feeds with enclosures (featured media / podcasts) #35
@ -85,6 +85,11 @@ def create_frontmatter(entry):
|
|||||||
for t in entry.tags:
|
for t in entry.tags:
|
||||||
tags.append(t['term'])
|
tags.append(t['term'])
|
||||||
|
|
||||||
|
if "featured_image" in entry:
|
||||||
|
featured_image = entry.featured_image
|
||||||
|
else:
|
||||||
|
featured_image = ''
|
||||||
|
|
||||||
card_type = "network"
|
card_type = "network"
|
||||||
if entry.feed_name == "pen.lumbung.space":
|
if entry.feed_name == "pen.lumbung.space":
|
||||||
card_type = "pen"
|
card_type = "pen"
|
||||||
@ -110,7 +115,8 @@ def create_frontmatter(entry):
|
|||||||
'original_link': entry.link,
|
'original_link': entry.link,
|
||||||
'feed_name': entry['feed_name'],
|
'feed_name': entry['feed_name'],
|
||||||
'tags': str(tags),
|
'tags': str(tags),
|
||||||
'card_type': card_type
|
'card_type': card_type,
|
||||||
|
'featured_image': featured_image
|
||||||
}
|
}
|
||||||
|
|
||||||
return frontmatter
|
return frontmatter
|
||||||
@ -136,11 +142,33 @@ def sanitize_yaml (frontmatter):
|
|||||||
|
|
||||||
return frontmatter
|
return frontmatter
|
||||||
|
|
||||||
|
def parse_enclosures(post_dir, entry):
|
||||||
|
"""
|
||||||
|
Parses feed enclosures which are featured media
|
||||||
|
Can be featured image but also podcast entries
|
||||||
|
https://pythonhosted.org/feedparser/reference-entry-enclosures.html
|
||||||
|
"""
|
||||||
|
#TODO parse more than images
|
||||||
|
#TODO handle the fact it could be multiple items
|
||||||
|
|
||||||
|
for e in entry.enclosures:
|
||||||
|
if "type" in e:
|
||||||
|
print("found enclosed media", e.type)
|
||||||
|
if "image/" in e.type:
|
||||||
|
featured_image = grab_media(post_dir, e.href)
|
||||||
|
entry["featured_image"] = featured_image
|
||||||
|
else:
|
||||||
|
print("FIXME:ignoring enclosed", e.type)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
def create_post(post_dir, entry):
|
def create_post(post_dir, entry):
|
||||||
"""
|
"""
|
||||||
write hugo post based on RSS entry
|
write hugo post based on RSS entry
|
||||||
"""
|
"""
|
||||||
|
if "enclosures" in entry:
|
||||||
|
entry = parse_enclosures(post_dir, entry)
|
||||||
|
|
||||||
frontmatter = create_frontmatter(entry)
|
frontmatter = create_frontmatter(entry)
|
||||||
|
|
||||||
if not os.path.exists(post_dir):
|
if not os.path.exists(post_dir):
|
||||||
@ -169,18 +197,25 @@ def grab_media(post_directory, url, prefered_name=None):
|
|||||||
"""
|
"""
|
||||||
media_item = urlparse(url).path.split('/')[-1]
|
media_item = urlparse(url).path.split('/')[-1]
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'User-Agent': 'https://git.autonomic.zone/ruangrupa/lumbunglib',
|
||||||
|
'From': 'info@lumbung.space' # This is another valid field
|
||||||
|
}
|
||||||
if prefered_name:
|
if prefered_name:
|
||||||
media_item = prefered_name
|
media_item = prefered_name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not os.path.exists(os.path.join(post_directory, media_item)):
|
if not os.path.exists(os.path.join(post_directory, media_item)):
|
||||||
#TODO: stream is true is a conditional so we could check the headers for things, mimetype etc
|
#TODO: stream is true is a conditional so we could check the headers for things, mimetype etc
|
||||||
response = requests.get(url, stream=True)
|
response = requests.get(url, headers=headers, stream=True)
|
||||||
if response.ok:
|
if response.ok:
|
||||||
with open(os.path.join(post_directory, media_item), 'wb') as media_file:
|
with open(os.path.join(post_directory, media_item), 'wb') as media_file:
|
||||||
shutil.copyfileobj(response.raw, media_file)
|
shutil.copyfileobj(response.raw, media_file)
|
||||||
print('Downloaded media item', media_item)
|
print('Downloaded media item', media_item)
|
||||||
return media_item
|
return media_item
|
||||||
|
else:
|
||||||
|
print("Download failed", response.status_code)
|
||||||
|
return url
|
||||||
return media_item
|
return media_item
|
||||||
elif os.path.exists(os.path.join(post_directory, media_item)):
|
elif os.path.exists(os.path.join(post_directory, media_item)):
|
||||||
return media_item
|
return media_item
|
||||||
@ -235,6 +270,7 @@ def grab_feed(feed_url):
|
|||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if "status" in data:
|
||||||
print(data.status, feed_url)
|
print(data.status, feed_url)
|
||||||
if data.status == 200:
|
if data.status == 200:
|
||||||
# 304 means the feed has not been modified since we last checked
|
# 304 means the feed has not been modified since we last checked
|
||||||
|
@ -8,6 +8,7 @@ original_link: "{{ frontmatter.original_link }}"
|
|||||||
feed_name: "{{ frontmatter.feed_name}}"
|
feed_name: "{{ frontmatter.feed_name}}"
|
||||||
categories: ["{{ frontmatter.card_type }}", "{{ frontmatter.feed_name}}"]
|
categories: ["{{ frontmatter.card_type }}", "{{ frontmatter.feed_name}}"]
|
||||||
tags: {{ frontmatter.tags }}
|
tags: {{ frontmatter.tags }}
|
||||||
|
{% if frontmatter.featured_image %}featured_image: "{{frontmatter.featured_image}}"{% endif %}
|
||||||
---
|
---
|
||||||
|
|
||||||
{{ content }}
|
{{ content }}
|
||||||
|
Loading…
Reference in New Issue
Block a user