Merge remote-tracking branch 'yan12125/IE_camdemy'
[youtube-dl] / youtube_dl / extractor / camdemy.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (compat_urllib_parse, compat_urlparse)
8 from ..utils import parse_iso8601
9
10
11 class CamdemyIE(InfoExtractor):
12     _VALID_URL = r'http://www.camdemy.com/media/(?P<id>\d+)'
13     _TESTS = [{
14         # single file
15         'url': 'http://www.camdemy.com/media/5181/',
16         'md5': '5a5562b6a98b37873119102e052e311b',
17         'info_dict': {
18             'id': '5181',
19             'ext': 'mp4',
20             'title': 'Ch1-1 Introduction, Signals (02-23-2012)',
21             'thumbnail': 're:^https?://.*\.jpg$',
22             'description': '',
23             'creator': 'ss11spring',
24             'upload_date': '20130114',
25             'timestamp': 1358154556,
26         }
27     }, {
28         # With non-empty description
29         'url': 'http://www.camdemy.com/media/13885',
30         'md5': '4576a3bb2581f86c61044822adbd1249',
31         'info_dict': {
32             'id': '13885',
33             'ext': 'mp4',
34             'title': 'EverCam + Camdemy QuickStart',
35             'thumbnail': 're:^https?://.*\.jpg$',
36             'description': 'md5:050b62f71ed62928f8a35f1a41e186c9',
37             'creator': 'evercam',
38             'upload_date': '20140620',
39             'timestamp': 1403271569,
40         }
41     }, {
42         # External source
43         'url': 'http://www.camdemy.com/media/14842',
44         'md5': '50e1c3c3aa233d3d7b7daa2fa10b1cf7',
45         'info_dict': {
46             'id': '2vsYQzNIsJo',
47             'ext': 'mp4',
48             'upload_date': '20130211',
49             'uploader': 'Hun Kim',
50             'description': 'Excel 2013 Tutorial for Beginners - How to add Password Protection',
51             'uploader_id': 'hunkimtutorials',
52             'title': 'Excel 2013 Tutorial - How to add Password Protection',
53         }
54     }]
55
56     def _real_extract(self, url):
57         video_id = self._match_id(url)
58
59         page = self._download_webpage(url, video_id)
60
61         srcFrom = self._html_search_regex(
62             r"<div class='srcFrom'>Source: <a title='([^']+)'", page,
63             'external source', default=None)
64
65         if srcFrom:
66             return self.url_result(srcFrom)
67
68         oembed_obj = self._download_json(
69             'http://www.camdemy.com/oembed/?format=json&url=' + url, video_id)
70
71         thumb_url = oembed_obj['thumbnail_url']
72         video_folder = compat_urlparse.urljoin(thumb_url, 'video/')
73         fileListXML = self._download_xml(
74             compat_urlparse.urljoin(video_folder, 'fileList.xml'),
75             video_id, 'Filelist XML')
76         fileName = fileListXML.find('./video/item/fileName').text
77
78         creation_time = self._html_search_regex(
79             r"<div class='title'>Posted :</div>[\r\n ]*<div class='value'>([^<>]+)<",
80             page, 'creation time', flags=re.MULTILINE) + '+08:00'
81         creation_timestamp = parse_iso8601(creation_time, delimiter=' ')
82
83         view_count_str = self._html_search_regex(
84             r"<div class='title'>Views :</div>[\r\n ]*<div class='value'>([^<>]+)<",
85             page, 'view count', flags=re.MULTILINE)
86         views = int(view_count_str.replace(',', ''))
87
88         return {
89             'id': video_id,
90             'url': compat_urlparse.urljoin(video_folder, fileName),
91             'title': oembed_obj['title'],
92             'thumbnail': thumb_url,
93             'description': self._html_search_meta('description', page),
94             'creator': oembed_obj['author_name'],
95             'duration': oembed_obj['duration'],
96             'timestamp': creation_timestamp,
97             'view_count': views,
98         }
99
100
101 class CamdemyFolderIE(InfoExtractor):
102     _VALID_URL = r'http://www.camdemy.com/folder/(?P<id>\d+)'
103     _TESTS = [{
104         # links with trailing slash
105         'url': 'http://www.camdemy.com/folder/450',
106         'info_dict': {
107             'id': '450',
108             'title': '信號與系統 2012 & 2011 (Signals and Systems)',
109         },
110         'playlist_mincount': 145
111     }, {
112         # links without trailing slash
113         # and multi-page
114         'url': 'http://www.camdemy.com/folder/853',
115         'info_dict': {
116             'id': '853',
117             'title': '科學計算 - 使用 Matlab'
118         },
119         'playlist_mincount': 20
120     }, {
121         # with displayMode parameter. For testing the codes to add parameters
122         'url': 'http://www.camdemy.com/folder/853/?displayMode=defaultOrderByOrg',
123         'info_dict': {
124             'id': '853',
125             'title': '科學計算 - 使用 Matlab'
126         },
127         'playlist_mincount': 20
128     }]
129
130     def _real_extract(self, url):
131         folder_id = self._match_id(url)
132
133         # Add displayMode=list so that all links are displayed in a single page
134         parsed_url = list(compat_urlparse.urlparse(url))
135         query = dict(compat_urlparse.parse_qsl(parsed_url[4]))
136         query.update({'displayMode': 'list'})
137         parsed_url[4] = compat_urllib_parse.urlencode(query)
138         final_url = compat_urlparse.urlunparse(parsed_url)
139
140         page = self._download_webpage(final_url, folder_id)
141         matches = re.findall(r"href='(/media/\d+/?)'", page)
142
143         entries = [self.url_result('http://www.camdemy.com' + media_path)
144                    for media_path in matches]
145
146         folder_title = self._html_search_meta('keywords', page)
147
148         return self.playlist_result(entries, folder_id, folder_title)