[toypics] Fix extraction
[youtube-dl] / youtube_dl / extractor / toypics.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 import re
6
7
8 class ToypicsIE(InfoExtractor):
9     IE_DESC = 'Toypics user profile'
10     _VALID_URL = r'https?://videos\.toypics\.net/view/(?P<id>[0-9]+)/.*'
11     _TEST = {
12         'url': 'http://videos.toypics.net/view/514/chancebulged,-2-1/',
13         'md5': '16e806ad6d6f58079d210fe30985e08b',
14         'info_dict': {
15             'id': '514',
16             'ext': 'mp4',
17             'title': 'Chance-Bulge\'d, 2',
18             'age_limit': 18,
19             'uploader': 'kidsune',
20         }
21     }
22
23     def _real_extract(self, url):
24         mobj = re.match(self._VALID_URL, url)
25         video_id = mobj.group('id')
26         page = self._download_webpage(url, video_id)
27         formats = self._parse_html5_media_entries(url, page, video_id)[0]['formats']
28         title = self._html_search_regex([
29             r'<h1[^>]+class=["\']view-video-title[^>]+>([^<]+)</h',
30             r'<title>([^<]+) - Toypics</title>',
31         ], page, 'title')
32         username = self._html_search_regex(
33             r'More videos from <strong>([^<]+)</strong>', page, 'username')
34         return {
35             'id': video_id,
36             'formats': formats,
37             'title': title,
38             'uploader': username,
39             'age_limit': 18,
40         }
41
42
43 class ToypicsUserIE(InfoExtractor):
44     IE_DESC = 'Toypics user profile'
45     _VALID_URL = r'https?://videos\.toypics\.net/(?P<username>[^/?]+)(?:$|[?#])'
46     _TEST = {
47         'url': 'http://videos.toypics.net/Mikey',
48         'info_dict': {
49             'id': 'Mikey',
50         },
51         'playlist_mincount': 19,
52     }
53
54     def _real_extract(self, url):
55         mobj = re.match(self._VALID_URL, url)
56         username = mobj.group('username')
57
58         profile_page = self._download_webpage(
59             url, username, note='Retrieving profile page')
60
61         video_count = int(self._search_regex(
62             r'public/">Public Videos \(([0-9]+)\)</a></li>', profile_page,
63             'video count'))
64
65         PAGE_SIZE = 8
66         urls = []
67         page_count = (video_count + PAGE_SIZE + 1) // PAGE_SIZE
68         for n in range(1, page_count + 1):
69             lpage_url = url + '/public/%d' % n
70             lpage = self._download_webpage(
71                 lpage_url, username,
72                 note='Downloading page %d/%d' % (n, page_count))
73             urls.extend(
74                 re.findall(
75                     r'<div[^>]+class=["\']preview[^>]+>\s*<a[^>]+href="(https?://videos.toypics.net/view/[^"]+)"',
76                     lpage))
77
78         return {
79             '_type': 'playlist',
80             'id': username,
81             'entries': [{
82                 '_type': 'url',
83                 'url': eurl,
84                 'ie_key': 'Toypics',
85             } for eurl in urls]
86         }