pyvideo.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import int_or_none
  4. class PyvideoIE(InfoExtractor):
  5. _VALID_URL = r'https?://(?:www\.)?pyvideo\.org/(?P<category>[^/]+)/(?P<id>[^/?#&.]+)'
  6. _TESTS = [{
  7. 'url': 'http://pyvideo.org/pycon-us-2013/become-a-logging-expert-in-30-minutes.html',
  8. 'info_dict': {
  9. 'id': 'become-a-logging-expert-in-30-minutes',
  10. },
  11. 'playlist_count': 2,
  12. }, {
  13. 'url': 'http://pyvideo.org/pygotham-2012/gloriajw-spotifywitherikbernhardsson182m4v.html',
  14. 'md5': '5fe1c7e0a8aa5570330784c847ff6d12',
  15. 'info_dict': {
  16. 'id': '2542',
  17. 'ext': 'm4v',
  18. 'title': 'Gloriajw-SpotifyWithErikBernhardsson182.m4v',
  19. },
  20. }]
  21. def _real_extract(self, url):
  22. mobj = self._match_valid_url(url)
  23. category = mobj.group('category')
  24. video_id = mobj.group('id')
  25. entries = []
  26. data = self._download_json(
  27. f'https://raw.githubusercontent.com/pyvideo/data/master/{category}/videos/{video_id}.json',
  28. video_id, fatal=False)
  29. if data:
  30. for video in data['videos']:
  31. video_url = video.get('url')
  32. if video_url:
  33. if video.get('type') == 'youtube':
  34. entries.append(self.url_result(video_url, 'Youtube'))
  35. else:
  36. entries.append({
  37. 'id': str(data.get('id') or video_id),
  38. 'url': video_url,
  39. 'title': data['title'],
  40. 'description': data.get('description') or data.get('summary'),
  41. 'thumbnail': data.get('thumbnail_url'),
  42. 'duration': int_or_none(data.get('duration')),
  43. })
  44. else:
  45. webpage = self._download_webpage(url, video_id)
  46. title = self._og_search_title(webpage)
  47. media_urls = self._search_regex(
  48. r'(?s)Media URL:(.+?)</li>', webpage, 'media urls')
  49. for m in re.finditer(
  50. r'<a[^>]+href=(["\'])(?P<url>http.+?)\1', media_urls):
  51. media_url = m.group('url')
  52. if re.match(r'https?://www\.youtube\.com/watch\?v=.*', media_url):
  53. entries.append(self.url_result(media_url, 'Youtube'))
  54. else:
  55. entries.append({
  56. 'id': video_id,
  57. 'url': media_url,
  58. 'title': title,
  59. })
  60. return self.playlist_result(entries, video_id)