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

[iTunes] Add new extractor (Closes #2097) #9590

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions youtube_dl/extractor/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
from .iprima import IPrimaIE
from .iqiyi import IqiyiIE
from .ir90tv import Ir90TvIE
from .itunes import iTunesIE
from .ivi import (
IviIE,
IviCompilationIE
Expand Down
59 changes: 59 additions & 0 deletions youtube_dl/extractor/itunes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# coding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor

from ..utils import (
extract_attributes,
int_or_none,
unescapeHTML,
unified_strdate,
)


class iTunesIE(InfoExtractor):
_VALID_URL = r'https?://itunes\.apple\.com/[a-z]{2}/[a-z0-9-]+/(?P<display_id>[a-z0-9-]+)?/(?:id)?(?P<id>[0-9]+)'
Copy link

Choose a reason for hiding this comment

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

It should be the following as the (valid) URL variations of https://itunes.apple.com/us/itunes-u/uc-davis-symphony-orchestra/id403834767 don't match otherwise

_VALID_URL = r'https?://itunes\.apple\.com/[a-z]{2}?/?[a-z0-9-]+/?(?P<display_id>[a-z0-9-]+)?/(?:id)?(?P<id>[0-9]+)'

_TEST = {
'url': 'https://itunes.apple.com/us/itunes-u/uc-davis-symphony-orchestra/id403834767',
'info_dict': {
'id': '403834767',
'title': 'UC Davis Symphony Orchestra & University Chorus',
},
'playlist_count': 31,
}

def _real_extract(self, url):
mobj = re.match(self._VALID_URL, url)
playlist_id, display_id = mobj.group('id', 'display_id')
if not display_id:
display_id = playlist_id

webpage = self._download_webpage(url, display_id)

video_infos = re.findall(r'var\s+__desc_popup_d_\d+\s*=\s*({[^><]+});', webpage)
html_entries = re.findall(r'<tr\s+[^>]*role="row"[^>]+>', webpage)
Copy link
Collaborator

Choose a reason for hiding this comment

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

get_element_by_attribute() may be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will get the content between <tr> and </tr>, but not the content inside the tags, like <tr preview-duration="485000">


entries = []
for idx, html_entry in enumerate(html_entries):
video_info = self._parse_json(video_infos[idx], display_id)
entry = extract_attributes(html_entry)
entries.append({
'id': entry['adam-id'],
'title': entry['preview-title'],
'description': video_info.get('description'),
'url': entry.get('audio-preview-url', entry.get('video-preview-url')),
'duration': int_or_none(entry.get('duration')),
'release_date': unified_strdate(video_info.get('release_date')),
'track': unescapeHTML(entry.get('preview-title')),
'track_number': int_or_none(entry.get('row-number')),
'track_id': entry.get('adam-id'),
'artist': unescapeHTML(entry.get('preview-artist')),
'album': unescapeHTML(entry.get('preview-album')),
})

title = self._html_search_regex(r'<h1>(.+)</h1>',
webpage, 'title')

return self.playlist_result(entries, playlist_id, title)