Few improvements
[youtube-dl] / youtube_dl / extractor / sexykarma.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 import re
6 import datetime
7
8 class SexyKarmaIE(InfoExtractor):
9     _VALID_URL = r'https?://(?:www\.)?sexykarma\.com/gonewild/video/.+\-(?P<id>[a-zA-Z0-9\-]+)(.html)'
10     _TESTS = [{
11         'url': 'http://www.sexykarma.com/gonewild/video/taking-a-quick-pee-yHI70cOyIHt.html',
12         'md5': 'b9798e7d1ef1765116a8f516c8091dbd',
13         'info_dict': {
14             'id': 'yHI70cOyIHt',
15             'ext': 'mp4',
16             'title': 'Taking a quick pee.',
17             'uploader_id': 'wildginger7',
18             'thumbnail': 're:^https?://.*\.jpg$',
19             'duration': int,
20             'view_count': int,
21             'upload_date': '20141007',
22         }
23     }, {
24         'url': 'http://www.sexykarma.com/gonewild/video/pot-pixie-tribute-8Id6EZPbuHf.html',
25         'md5': 'dd216c68d29b49b12842b9babe762a5d',
26         'info_dict': {
27             'id': '8Id6EZPbuHf',
28             'ext': 'mp4',
29             'title': 'pot_pixie tribute',
30             'uploader_id': 'banffite',
31             'thumbnail': 're:^https?://.*\.jpg$',
32             'duration': int,
33             'view_count': int,
34             'upload_date': '20141013',
35         }
36     }]
37
38     def _real_extract(self, url):
39         video_id = self._match_id(url)
40
41         webpage = self._download_webpage(url, video_id)
42               
43         title = self._html_search_regex(r'<h2 class="he2"><span>(.*?)</span>', webpage, 'title')
44         uploader_id = self._html_search_regex(r'class="aupa">\n*(.*?)</a>', webpage, 'uploader')
45         url = self._html_search_regex(r'<p><a href="(.*?)" ?\n*target="_blank"><font color', webpage, 'url')
46         thumbnail = self._html_search_regex(r'<div id="player" style="z-index:1;"> <span id="edge"></span> <span id="container"><img[\n ]*src="(.+?)"', webpage, 'thumbnail')
47         
48         str_duration = self._html_search_regex(r'<tr>[\n\s]*<td>Time: </td>[\n\s]*<td align="right"><span>(.+)\n*', webpage, 'duration')
49         duration = self._to_seconds(str_duration)
50
51         str_views = self._html_search_regex(r'<tr>[\n\s]*<td>Views: </td>[\n\s]*<td align="right"><span>(.+)</span>', webpage, 'view_count')
52         view_count = int(str_views)
53         # print view_count
54
55         date = self._html_search_regex(r'class="aup">Added: <strong>(.*?)</strong>', webpage, 'date')
56         d = datetime.datetime.strptime(date, '%B %d, %Y')
57         upload_date = d.strftime('%Y%m%d')
58
59         return {
60             'id': video_id,
61             'title': title,
62             'uploader_id': uploader_id,
63             'url': url,
64             'thumbnail': thumbnail,
65             'duration': duration,
66             'view_count': view_count,
67             'upload_date': upload_date,
68         }
69
70     def _to_seconds(self, timestr):
71         seconds= 0
72         for part in timestr.split(':'):
73             seconds= seconds*60 + int(part)
74         return seconds