You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
10 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. int_or_none,
  8. parse_resolution,
  9. try_get,
  10. unified_timestamp,
  11. url_or_none,
  12. urljoin,
  13. )
  14. class PeerTubeIE(InfoExtractor):
  15. _INSTANCES_RE = r'''(?:
  16. # Taken from https://instances.joinpeertube.org/instances
  17. tube\.openalgeria\.org|
  18. peertube\.pointsecu\.fr|
  19. peertube\.nogafa\.org|
  20. peertube\.pl|
  21. megatube\.lilomoino\.fr|
  22. peertube\.tamanoir\.foucry\.net|
  23. peertube\.inapurna\.org|
  24. peertube\.netzspielplatz\.de|
  25. video\.deadsuperhero\.com|
  26. peertube\.devosi\.org|
  27. peertube\.1312\.media|
  28. tube\.worldofhauru\.xyz|
  29. tube\.bootlicker\.party|
  30. skeptikon\.fr|
  31. peertube\.geekshell\.fr|
  32. tube\.opportunis\.me|
  33. peertube\.peshane\.net|
  34. video\.blueline\.mg|
  35. tube\.homecomputing\.fr|
  36. videos\.cloudfrancois\.fr|
  37. peertube\.viviers-fibre\.net|
  38. tube\.ouahpiti\.info|
  39. video\.tedomum\.net|
  40. video\.g3l\.org|
  41. fontube\.fr|
  42. peertube\.gaialabs\.ch|
  43. peertube\.extremely\.online|
  44. peertube\.public-infrastructure\.eu|
  45. tube\.kher\.nl|
  46. peertube\.qtg\.fr|
  47. tube\.22decembre\.eu|
  48. facegirl\.me|
  49. video\.migennes\.net|
  50. janny\.moe|
  51. tube\.p2p\.legal|
  52. video\.atlanti\.se|
  53. troll\.tv|
  54. peertube\.geekael\.fr|
  55. vid\.leotindall\.com|
  56. video\.anormallostpod\.ovh|
  57. p-tube\.h3z\.jp|
  58. tube\.darfweb\.eu|
  59. videos\.iut-orsay\.fr|
  60. peertube\.solidev\.net|
  61. videos\.symphonie-of-code\.fr|
  62. testtube\.ortg\.de|
  63. videos\.cemea\.org|
  64. peertube\.gwendalavir\.eu|
  65. video\.passageenseine\.fr|
  66. videos\.festivalparminous\.org|
  67. peertube\.touhoppai\.moe|
  68. peertube\.duckdns\.org|
  69. sikke\.fi|
  70. peertube\.mastodon\.host|
  71. firedragonvideos\.com|
  72. vidz\.dou\.bet|
  73. peertube\.koehn\.com|
  74. peer\.hostux\.social|
  75. share\.tube|
  76. peertube\.walkingmountains\.fr|
  77. medias\.libox\.fr|
  78. peertube\.moe|
  79. peertube\.xyz|
  80. jp\.peertube\.network|
  81. videos\.benpro\.fr|
  82. tube\.otter\.sh|
  83. peertube\.angristan\.xyz|
  84. peertube\.parleur\.net|
  85. peer\.ecutsa\.fr|
  86. peertube\.heraut\.eu|
  87. peertube\.tifox\.fr|
  88. peertube\.maly\.io|
  89. vod\.mochi\.academy|
  90. exode\.me|
  91. coste\.video|
  92. tube\.aquilenet\.fr|
  93. peertube\.gegeweb\.eu|
  94. framatube\.org|
  95. thinkerview\.video|
  96. tube\.conferences-gesticulees\.net|
  97. peertube\.datagueule\.tv|
  98. video\.lqdn\.fr|
  99. meilleurtube\.delire\.party|
  100. tube\.mochi\.academy|
  101. peertube\.dav\.li|
  102. media\.zat\.im|
  103. pytu\.be|
  104. peertube\.valvin\.fr|
  105. peertube\.nsa\.ovh|
  106. video\.colibris-outilslibres\.org|
  107. video\.hispagatos\.org|
  108. tube\.svnet\.fr|
  109. peertube\.video|
  110. videos\.lecygnenoir\.info|
  111. peertube3\.cpy\.re|
  112. peertube2\.cpy\.re|
  113. videos\.tcit\.fr|
  114. peertube\.cpy\.re
  115. )'''
  116. _UUID_RE = r'[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}'
  117. _VALID_URL = r'''(?x)
  118. (?:
  119. peertube:(?P<host>[^:]+):|
  120. https?://(?P<host_2>%s)/(?:videos/(?:watch|embed)|api/v\d/videos)/
  121. )
  122. (?P<id>%s)
  123. ''' % (_INSTANCES_RE, _UUID_RE)
  124. _TESTS = [{
  125. 'url': 'https://peertube.moe/videos/watch/2790feb0-8120-4e63-9af3-c943c69f5e6c',
  126. 'md5': '80f24ff364cc9d333529506a263e7feb',
  127. 'info_dict': {
  128. 'id': '2790feb0-8120-4e63-9af3-c943c69f5e6c',
  129. 'ext': 'mp4',
  130. 'title': 'wow',
  131. 'description': 'wow such video, so gif',
  132. 'thumbnail': r're:https?://.*\.(?:jpg|png)',
  133. 'timestamp': 1519297480,
  134. 'upload_date': '20180222',
  135. 'uploader': 'Luclu7',
  136. 'uploader_id': '7fc42640-efdb-4505-a45d-a15b1a5496f1',
  137. 'uploder_url': 'https://peertube.nsa.ovh/accounts/luclu7',
  138. 'license': 'Unknown',
  139. 'duration': 3,
  140. 'view_count': int,
  141. 'like_count': int,
  142. 'dislike_count': int,
  143. 'tags': list,
  144. 'categories': list,
  145. }
  146. }, {
  147. 'url': 'https://peertube.tamanoir.foucry.net/videos/watch/0b04f13d-1e18-4f1d-814e-4979aa7c9c44',
  148. 'only_matching': True,
  149. }, {
  150. # nsfw
  151. 'url': 'https://tube.22decembre.eu/videos/watch/9bb88cd3-9959-46d9-9ab9-33d2bb704c39',
  152. 'only_matching': True,
  153. }, {
  154. 'url': 'https://tube.22decembre.eu/videos/embed/fed67262-6edb-4d1c-833b-daa9085c71d7',
  155. 'only_matching': True,
  156. }, {
  157. 'url': 'https://tube.openalgeria.org/api/v1/videos/c1875674-97d0-4c94-a058-3f7e64c962e8',
  158. 'only_matching': True,
  159. }, {
  160. 'url': 'peertube:video.blender.org:b37a5b9f-e6b5-415c-b700-04a5cd6ec205',
  161. 'only_matching': True,
  162. }]
  163. @staticmethod
  164. def _extract_peertube_url(webpage, source_url):
  165. mobj = re.match(
  166. r'https?://(?P<host>[^/]+)/videos/watch/(?P<id>%s)'
  167. % PeerTubeIE._UUID_RE, source_url)
  168. if mobj and any(p in webpage for p in (
  169. '<title>PeerTube<',
  170. 'There will be other non JS-based clients to access PeerTube',
  171. '>We are sorry but it seems that PeerTube is not compatible with your web browser.<')):
  172. return 'peertube:%s:%s' % mobj.group('host', 'id')
  173. @staticmethod
  174. def _extract_urls(webpage, source_url):
  175. entries = re.findall(
  176. r'''(?x)<iframe[^>]+\bsrc=["\'](?P<url>(?:https?:)?//%s/videos/embed/%s)'''
  177. % (PeerTubeIE._INSTANCES_RE, PeerTubeIE._UUID_RE), webpage)
  178. if not entries:
  179. peertube_url = PeerTubeIE._extract_peertube_url(webpage, source_url)
  180. if peertube_url:
  181. entries = [peertube_url]
  182. return entries
  183. def _real_extract(self, url):
  184. mobj = re.match(self._VALID_URL, url)
  185. host = mobj.group('host') or mobj.group('host_2')
  186. video_id = mobj.group('id')
  187. video = self._download_json(
  188. 'https://%s/api/v1/videos/%s' % (host, video_id), video_id)
  189. title = video['name']
  190. formats = []
  191. for file_ in video['files']:
  192. if not isinstance(file_, dict):
  193. continue
  194. file_url = url_or_none(file_.get('fileUrl'))
  195. if not file_url:
  196. continue
  197. file_size = int_or_none(file_.get('size'))
  198. format_id = try_get(
  199. file_, lambda x: x['resolution']['label'], compat_str)
  200. f = parse_resolution(format_id)
  201. f.update({
  202. 'url': file_url,
  203. 'format_id': format_id,
  204. 'filesize': file_size,
  205. })
  206. formats.append(f)
  207. self._sort_formats(formats)
  208. def account_data(field):
  209. return try_get(video, lambda x: x['account'][field], compat_str)
  210. category = try_get(video, lambda x: x['category']['label'], compat_str)
  211. categories = [category] if category else None
  212. nsfw = video.get('nsfw')
  213. if nsfw is bool:
  214. age_limit = 18 if nsfw else 0
  215. else:
  216. age_limit = None
  217. return {
  218. 'id': video_id,
  219. 'title': title,
  220. 'description': video.get('description'),
  221. 'thumbnail': urljoin(url, video.get('thumbnailPath')),
  222. 'timestamp': unified_timestamp(video.get('publishedAt')),
  223. 'uploader': account_data('displayName'),
  224. 'uploader_id': account_data('uuid'),
  225. 'uploder_url': account_data('url'),
  226. 'license': try_get(
  227. video, lambda x: x['licence']['label'], compat_str),
  228. 'duration': int_or_none(video.get('duration')),
  229. 'view_count': int_or_none(video.get('views')),
  230. 'like_count': int_or_none(video.get('likes')),
  231. 'dislike_count': int_or_none(video.get('dislikes')),
  232. 'age_limit': age_limit,
  233. 'tags': try_get(video, lambda x: x['tags'], list),
  234. 'categories': categories,
  235. 'formats': formats,
  236. }