Skip to content

Commit

Permalink
Grab RSS media:content as Entry thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
passiomatic committed Jul 18, 2023
1 parent 74e12ee commit 70085d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion coldsweat/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def _parse_feed(self, data):

timestamp = t.get_timestamp(default=self.instant)
content_type, content = t.get_content(('text/plain', ''))
thumbnail_url = t.get_thumbnail_url()

entry = {
'feed_id': self.feed.id,
Expand All @@ -250,6 +251,7 @@ def _parse_feed(self, data):
'author': t.get_author() or feed_author,
'content': content,
'content_type': content_type,
'thumbnail_url': thumbnail_url,
'published_on': timestamp
}
new_entries.append(entry)
Expand All @@ -270,7 +272,7 @@ def _parse_feed(self, data):
# MySQL doesn't support conlict targets, see:
# https://stackoverflow.com/questions/74691515/python-peewee-using-excluded-to-resolve-conflict-resolution
count += (Entry.insert_many(batch).on_conflict(
# Pass down these new values only for certain values
# Pass down these new values only for certain fields
preserve=[Entry.title, Entry.author, Entry.content, Entry.content_type])
.as_rowcount()
.execute())
Expand Down
17 changes: 16 additions & 1 deletion coldsweat/translators.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def get_title(self):
Feed.MAX_TITLE_LENGTH)
return ''

IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif']

class EntryTranslator(object):

Expand Down Expand Up @@ -87,7 +88,21 @@ def get_content(self, default):
# app.logger.debug(u'no entry content found, using default')
return default

# Nullable fields
def get_thumbnail_url(self):
#See https://www.rssboard.org/media-rss
if 'media_content' in self.entry_dict:
media_content = self.entry_dict['media_content'][0]
try:
image_type = media_content['type']
except KeyError:
image_type = None
if image_type in IMAGE_TYPES:
try:
return media_content['url']
except KeyError:
pass
# @@TODO: Try to get thumbnail[0] instead
return ''

def get_link(self):
# Special case for FeedBurner entries
Expand Down

0 comments on commit 70085d1

Please sign in to comment.