Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct parsing of media values #441

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 26 additions & 10 deletions feedparser/namespaces/mediarss.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ def _end_media_keywords(self):
if term.strip():
self._add_tag(term.strip(), None, None)

def _start_media_title(self, attrs_d):
self._start_title(attrs_d)

def _end_media_title(self):
title_depth = self.title_depth
self._end_title()
self.title_depth = title_depth

def _start_media_group(self, attrs_d):
# don't do anything, but don't break the enclosed tags either
pass
Expand Down Expand Up @@ -85,10 +77,34 @@ def _end_media_credit(self):
context["media_credit"][-1]["content"] = credit

def _start_media_description(self, attrs_d):
self._start_description(attrs_d)
context = self._get_context()
context.setdefault("media_description", [])
attrs = attrs_d.copy()
if "type" in attrs:
attrs["type"] = self.map_content_type(attrs["type"])
context["media_description"].append(attrs)
self.push("media_desc", 1)

def _end_media_description(self):
self._end_description()
description_ = self.pop("media_desc")
if description_ is not None and description_.strip():
context = self._get_context()
context["media_description"][-1]["content"] = description_

def _start_media_title(self, attrs_d):
context = self._get_context()
context.setdefault("media_title", [])
attrs = attrs_d.copy()
if "type" in attrs:
attrs["type"] = self.map_content_type(attrs["type"])
context["media_title"].append(attrs)
self.push("title", 1)

def _end_media_title(self):
title_ = self.pop("title")
if title_ is not None and title_.strip():
context = self._get_context()
context["media_title"][-1]["content"] = title_

def _start_media_restriction(self, attrs_d):
context = self._get_context()
Expand Down
2 changes: 1 addition & 1 deletion tests/wellformed/media-rss/item_media_title_type_plain.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Description: media:title @type="plain"
Expect: not bozo and entries[0]['title_detail']['type'] == 'text/plain'
Expect: not bozo and entries[0]['media_title'][0]['type'] == 'text/plain'
-->
<rss xmlns:media="http://search.yahoo.com/mrss/">
<channel>
Expand Down
16 changes: 16 additions & 0 deletions tests/wellformed/media-rss/media_content_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Description: media_content title and description at entry bottom
Expect: entries[0]['summary'] == 'Test description' and entries[0]['media_content'][0]['url'] == 'https://example.com/img.jpg' and entries[0]['media_title'][0]['content'] == 'Media title' and entries[0]['media_title'][0]['type'] == 'text/plain' and entries[0]['media_description'][0]['content'] == 'Media description' and entries[0]['media_description'][0]['type'] == 'text/plain' and entries[0]['media_credit'][0]['content'] == 'Media credit'
-->
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:media="http://search.yahoo.com/mrss/">
<entry>
<media:content width="644" height="322"
url="https://example.com/img.jpg">
<media:title type="plain">Media title</media:title>
<media:description type="plain">Media description</media:description>
<media:credit scheme="urn:ebu">Media credit</media:credit>
</media:content>
<title>Example Atom</title>
<description>Test description</description>
</entry>
</feed>
16 changes: 16 additions & 0 deletions tests/wellformed/media-rss/media_content_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Description: media_content title and description at entry top
Expect: entries[0]['summary'] == 'Test description' and entries[0]['media_content'][0]['url'] == 'https://example.com/img.jpg' and entries[0]['media_title'][0]['content'] == 'Media title' and entries[0]['media_title'][0]['type'] == 'text/plain' and entries[0]['media_description'][0]['content'] == 'Media description' and entries[0]['media_description'][0]['type'] == 'text/plain' and entries[0]['media_credit'][0]['content'] == 'Media credit'
-->
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:media="http://search.yahoo.com/mrss/">
<entry>
<title>Example Atom</title>
<description>Test description</description>
<media:content width="644" height="322"
url="https://example.com/img.jpg">
<media:title type="plain">Media title</media:title>
<media:description type="plain">Media description</media:description>
<media:credit scheme="urn:ebu">Media credit</media:credit>
</media:content>
</entry>
</feed>
22 changes: 22 additions & 0 deletions tests/wellformed/media-rss/media_content_3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
Description: media_content with multiple media tags
Expect: entries[0]['summary'] == 'Test description' and entries[0]['media_content'][0]['url'] == 'https://example.com/img.jpg' and entries[0]['media_title'][0]['content'] == 'Media title' and entries[0]['media_title'][0]['type'] == 'text/plain' and entries[0]['media_description'][0]['content'] == 'Media description' and entries[0]['media_description'][0]['type'] == 'text/plain' and entries[0]['media_credit'][0]['content'] == 'Media credit' and entries[0]['media_content'][1]['url'] == 'https://example.com/img2.jpg' and entries[0]['media_title'][1]['content'] == 'Media title 2' and entries[0]['media_title'][1]['type'] == 'text/plain' and entries[0]['media_description'][1]['content'] == 'Media description 2' and entries[0]['media_description'][0]['type'] == 'text/plain' and entries[0]['media_credit'][1]['content'] == 'Media credit 2'
-->
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:media="http://search.yahoo.com/mrss/">
<entry>
<title>Example Atom</title>
<description>Test description</description>
<media:content width="644" height="322"
url="https://example.com/img.jpg">
<media:title type="plain">Media title</media:title>
<media:description type="plain">Media description</media:description>
<media:credit scheme="urn:ebu">Media credit</media:credit>
</media:content>
<media:content width="644" height="322"
url="https://example.com/img2.jpg">
<media:title type="plain">Media title 2</media:title>
<media:description type="plain">Media description 2</media:description>
<media:credit scheme="urn:ebu">Media credit 2</media:credit>
</media:content>
</entry>
</feed>
2 changes: 1 addition & 1 deletion tests/wellformed/media-rss/media_group.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
Description: media_group
Expect: entries[0]['description'] == 'Test Description' and entries[0]['media_credit'][0]['role'] == 'creator' and entries[0]['media_credit'][0]['content'] == 'Creator' and entries[0]['media_credit'][1]['role'] == 'writer' and entries[0]['media_credit'][1]['content'] == 'Writer' and entries[0]['media_restriction']['relationship'] == 'allow' and entries[0]['media_restriction']['content'] == ['au', 'us'] and entries[0]['media_license']['type'] == 'plain' and entries[0]['media_license']['href'] == 'http://example.com/' and entries[0]['media_license']['content'] == 'License' and entries[0]['media_rating']['content'] == 'adult'
Expect: entries[0]['media_description'][0]['content'] == 'Test Description' and entries[0]['media_credit'][0]['role'] == 'creator' and entries[0]['media_credit'][0]['content'] == 'Creator' and entries[0]['media_credit'][1]['role'] == 'writer' and entries[0]['media_credit'][1]['content'] == 'Writer' and entries[0]['media_restriction']['relationship'] == 'allow' and entries[0]['media_restriction']['content'] == ['au', 'us'] and entries[0]['media_license']['type'] == 'plain' and entries[0]['media_license']['href'] == 'http://example.com/' and entries[0]['media_license']['content'] == 'License' and entries[0]['media_rating']['content'] == 'adult'
-->
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:media="http://search.yahoo.com/mrss/">
<entry>
Expand Down