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

[archiveorg] Fix extraction #23827

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions youtube_dl/extractor/archiveorg.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ def _real_extract(self, url):
video_id = self._match_id(url)
webpage = self._download_webpage(
'http://archive.org/embed/' + video_id, video_id)
input_element_with_playlist = self._search_regex(
r'(<\s*input.*\s*class\s*=\s*[\'"].*\s*js-play8-playlist\s*.*[\'"]\s*.*>)',
webpage, 'jwplayer playlist')
jwplayer_playlist = self._parse_json(self._search_regex(
r"(?s)Play\('[^']+'\s*,\s*(\[.+\])\s*,\s*{.*?}\)",
webpage, 'jwplayer playlist'), video_id)
r'.*\s+value\s*=\s*[\'"](.+)[\'"][\s/]',
input_element_with_playlist, 'playlist data'), video_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract_attributes.

Copy link
Author

@TinyToweringTree TinyToweringTree Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer. I changed the code to use extract_attributes() in commit e910f49.

It was still bugging me that I couldn't use get_element_by_class() in the line before as it only returns the content of the element and not the element (as the name of the function indicates). So I changed get_element_by_class() (and it's associated functions) in commit b98d1c0 to accept an optional include_tag parameter. By default it's set to False. So no existing code will be affected by it.

This should prove useful in the future and make those functions more intuitive to use.

I also added tests to test_utils.py. While doing so I noticed that get_element_by_class() didn't work for class names starting with a hyphen. I fixed that, too.

info = self._parse_jwplayer_data(
{'playlist': jwplayer_playlist}, video_id, base_url=url)

Expand All @@ -52,7 +55,7 @@ def get_optional(metadata, field):
metadata = self._download_json(
'http://archive.org/details/' + video_id, video_id, query={
'output': 'json',
})['metadata']
}).get('metadata', {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point of this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to make the extraction more resilient against website changes. With metadata being optional for downloading videos, it's probably better not to crash if the metadata element changes its name.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not enough. If you reconsider this optional then you must also:

  1. Handle non dict metadata scenario.
  2. Handle download failure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Thanks. Commit 1326a5a fixes this by setting fatal=False when calling _download_json() and by checking the return value.

info.update({
'title': get_optional(metadata, 'title') or info.get('title'),
'description': clean_html(get_optional(metadata, 'description')),
Expand Down