younow.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import itertools
  2. from .common import InfoExtractor
  3. from ..compat import compat_str
  4. from ..utils import (
  5. ExtractorError,
  6. format_field,
  7. int_or_none,
  8. try_get,
  9. )
  10. CDN_API_BASE = 'https://cdn.younow.com/php/api'
  11. MOMENT_URL_FORMAT = '%s/moment/fetch/id=%%s' % CDN_API_BASE
  12. class YouNowLiveIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?younow\.com/(?P<id>[^/?#&]+)'
  14. _TEST = {
  15. 'url': 'https://www.younow.com/AmandaPadeezy',
  16. 'info_dict': {
  17. 'id': 'AmandaPadeezy',
  18. 'ext': 'mp4',
  19. 'is_live': True,
  20. 'title': 'March 26, 2017',
  21. 'thumbnail': r're:^https?://.*\.jpg$',
  22. 'tags': ['girls'],
  23. 'categories': ['girls'],
  24. 'uploader': 'AmandaPadeezy',
  25. 'uploader_id': '6716501',
  26. 'uploader_url': 'https://www.younow.com/AmandaPadeezy',
  27. 'creator': 'AmandaPadeezy',
  28. },
  29. 'skip': True,
  30. }
  31. @classmethod
  32. def suitable(cls, url):
  33. return (False
  34. if YouNowChannelIE.suitable(url) or YouNowMomentIE.suitable(url)
  35. else super(YouNowLiveIE, cls).suitable(url))
  36. def _real_extract(self, url):
  37. username = self._match_id(url)
  38. data = self._download_json(
  39. 'https://api.younow.com/php/api/broadcast/info/curId=0/user=%s'
  40. % username, username)
  41. if data.get('errorCode') != 0:
  42. raise ExtractorError(data['errorMsg'], expected=True)
  43. uploader = try_get(
  44. data, lambda x: x['user']['profileUrlString'],
  45. compat_str) or username
  46. return {
  47. 'id': uploader,
  48. 'is_live': True,
  49. 'title': uploader,
  50. 'thumbnail': data.get('awsUrl'),
  51. 'tags': data.get('tags'),
  52. 'categories': data.get('tags'),
  53. 'uploader': uploader,
  54. 'uploader_id': data.get('userId'),
  55. 'uploader_url': 'https://www.younow.com/%s' % username,
  56. 'creator': uploader,
  57. 'view_count': int_or_none(data.get('viewers')),
  58. 'like_count': int_or_none(data.get('likes')),
  59. 'formats': [{
  60. 'url': '%s/broadcast/videoPath/hls=1/broadcastId=%s/channelId=%s'
  61. % (CDN_API_BASE, data['broadcastId'], data['userId']),
  62. 'ext': 'mp4',
  63. 'protocol': 'm3u8',
  64. }],
  65. }
  66. def _extract_moment(item, fatal=True):
  67. moment_id = item.get('momentId')
  68. if not moment_id:
  69. if not fatal:
  70. return
  71. raise ExtractorError('Unable to extract moment id')
  72. moment_id = compat_str(moment_id)
  73. title = item.get('text')
  74. if not title:
  75. title = 'YouNow %s' % (
  76. item.get('momentType') or item.get('titleType') or 'moment')
  77. uploader = try_get(item, lambda x: x['owner']['name'], compat_str)
  78. uploader_id = try_get(item, lambda x: x['owner']['userId'])
  79. uploader_url = format_field(uploader, None, 'https://www.younow.com/%s')
  80. entry = {
  81. 'extractor_key': 'YouNowMoment',
  82. 'id': moment_id,
  83. 'title': title,
  84. 'view_count': int_or_none(item.get('views')),
  85. 'like_count': int_or_none(item.get('likes')),
  86. 'timestamp': int_or_none(item.get('created')),
  87. 'creator': uploader,
  88. 'uploader': uploader,
  89. 'uploader_id': uploader_id,
  90. 'uploader_url': uploader_url,
  91. 'formats': [{
  92. 'url': 'https://hls.younow.com/momentsplaylists/live/%s/%s.m3u8'
  93. % (moment_id, moment_id),
  94. 'ext': 'mp4',
  95. 'protocol': 'm3u8_native',
  96. }],
  97. }
  98. return entry
  99. class YouNowChannelIE(InfoExtractor):
  100. _VALID_URL = r'https?://(?:www\.)?younow\.com/(?P<id>[^/]+)/channel'
  101. _TEST = {
  102. 'url': 'https://www.younow.com/its_Kateee_/channel',
  103. 'info_dict': {
  104. 'id': '14629760',
  105. 'title': 'its_Kateee_ moments'
  106. },
  107. 'playlist_mincount': 8,
  108. }
  109. def _entries(self, username, channel_id):
  110. created_before = 0
  111. for page_num in itertools.count(1):
  112. if created_before is None:
  113. break
  114. info = self._download_json(
  115. '%s/moment/profile/channelId=%s/createdBefore=%d/records=20'
  116. % (CDN_API_BASE, channel_id, created_before), username,
  117. note='Downloading moments page %d' % page_num)
  118. items = info.get('items')
  119. if not items or not isinstance(items, list):
  120. break
  121. for item in items:
  122. if not isinstance(item, dict):
  123. continue
  124. item_type = item.get('type')
  125. if item_type == 'moment':
  126. entry = _extract_moment(item, fatal=False)
  127. if entry:
  128. yield entry
  129. elif item_type == 'collection':
  130. moments = item.get('momentsIds')
  131. if isinstance(moments, list):
  132. for moment_id in moments:
  133. m = self._download_json(
  134. MOMENT_URL_FORMAT % moment_id, username,
  135. note='Downloading %s moment JSON' % moment_id,
  136. fatal=False)
  137. if m and isinstance(m, dict) and m.get('item'):
  138. entry = _extract_moment(m['item'])
  139. if entry:
  140. yield entry
  141. created_before = int_or_none(item.get('created'))
  142. def _real_extract(self, url):
  143. username = self._match_id(url)
  144. channel_id = compat_str(self._download_json(
  145. 'https://api.younow.com/php/api/broadcast/info/curId=0/user=%s'
  146. % username, username, note='Downloading user information')['userId'])
  147. return self.playlist_result(
  148. self._entries(username, channel_id), channel_id,
  149. '%s moments' % username)
  150. class YouNowMomentIE(InfoExtractor):
  151. _VALID_URL = r'https?://(?:www\.)?younow\.com/[^/]+/(?P<id>[^/?#&]+)'
  152. _TEST = {
  153. 'url': 'https://www.younow.com/GABO.../20712117/36319236/3b316doc/m',
  154. 'md5': 'a30c70eadb9fb39a1aa3c8c0d22a0807',
  155. 'info_dict': {
  156. 'id': '20712117',
  157. 'ext': 'mp4',
  158. 'title': 'YouNow capture',
  159. 'view_count': int,
  160. 'like_count': int,
  161. 'timestamp': 1490432040,
  162. 'upload_date': '20170325',
  163. 'uploader': 'GABO...',
  164. 'uploader_id': 35917228,
  165. },
  166. }
  167. @classmethod
  168. def suitable(cls, url):
  169. return (False
  170. if YouNowChannelIE.suitable(url)
  171. else super(YouNowMomentIE, cls).suitable(url))
  172. def _real_extract(self, url):
  173. video_id = self._match_id(url)
  174. item = self._download_json(MOMENT_URL_FORMAT % video_id, video_id)
  175. return _extract_moment(item['item'])