Skip to content

Commit

Permalink
Parse the entry attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
bbolli committed Oct 26, 2020
1 parent dfd355c commit 22f51e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions feedparser/parsers/json.py
Expand Up @@ -104,6 +104,9 @@ def parse_entry(self, e):
if 'author' in e:
self.parse_author(e['author'], entry)

if 'attachments' in e:
entry['enclosures'] = [self.parse_attachment(a) for a in e['attachments']]

return entry

def parse_author(self, parent, dest):
Expand All @@ -115,3 +118,11 @@ def parse_author(self, parent, dest):
detail['email'] = parent['url'][7:]
else:
detail['href'] = parent['url']

def parse_attachment(self, attachment):
enc = FeedParserDict()
enc['href'] = attachment['url']
enc['type'] = attachment['mime_type']
if 'size_in_bytes' in attachment:
enc['length'] = attachment['size_in_bytes']
return enc
7 changes: 7 additions & 0 deletions tests/json/example.json
Expand Up @@ -12,6 +12,13 @@
"title": "post 2",
"content_text": "This is a second item.",
"url": "https://example.org/second-item",
"attachments": [
{
"url": "https://example.org/second-item-show.mp4",
"mime_type": "video/mp4",
"size_in_bytes": 12000000
}
],
"tags": ["test", "JSON"]
},
{
Expand Down

0 comments on commit 22f51e8

Please sign in to comment.