Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

youtube.py 3.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Original code from https://github.com/sphinx-contrib/youtube with a few modifications.
  5. #
  6. from __future__ import division
  7. import re
  8. from docutils import nodes
  9. from docutils.parsers.rst import directives, Directive
  10. CONTROL_HEIGHT = 30
  11. def get_size(d, key):
  12. if key not in d:
  13. return None
  14. m = re.match("(\d+)(|%|px)$", d[key])
  15. if not m:
  16. raise ValueError("invalid size %r" % d[key])
  17. return int(m.group(1)), m.group(2) or "px"
  18. def css(d):
  19. return "; ".join(sorted("%s: %s" % kv for kv in d.items()))
  20. class youtube(nodes.General, nodes.Element): pass
  21. def visit_youtube_node(self, node):
  22. aspect = node["aspect"]
  23. width = node["width"]
  24. height = node["height"]
  25. if aspect is None:
  26. aspect = 16, 9
  27. div_style = {}
  28. if (height is None) and (width is not None) and (width[1] == "%"):
  29. div_style = {
  30. "padding-top": "%dpx" % CONTROL_HEIGHT,
  31. "padding-bottom": "%f%%" % (width[0] * aspect[1] / aspect[0]),
  32. "width": "%d%s" % width,
  33. "position": "relative",
  34. }
  35. style = {
  36. "position": "absolute",
  37. "top": "0",
  38. "left": "0",
  39. "width": "100%",
  40. "height": "100%",
  41. "border": "0",
  42. }
  43. attrs = {
  44. "src": "https://www.youtube.com/embed/%s?autoplay=1" % node["id"],
  45. "style": css(style),
  46. }
  47. else:
  48. if width is None:
  49. if height is None:
  50. width = 560, "px"
  51. else:
  52. width = height[0] * aspect[0] / aspect[1], "px"
  53. if height is None:
  54. height = width[0] * aspect[1] / aspect[0], "px"
  55. style = {
  56. "width": "%d%s" % width,
  57. "height": "%d%s" % (height[0] + CONTROL_HEIGHT, height[1]),
  58. "border": "0",
  59. }
  60. attrs = {
  61. "src": "https://www.youtube.com/embed/%s?autoplay=1" % node["id"],
  62. "style": css(style),
  63. }
  64. attrs["allowfullscreen"] = "true"
  65. div_attrs = {
  66. "CLASS": "youtube_wrapper",
  67. "style": css(div_style),
  68. }
  69. self.body.append(self.starttag(node, "div", **div_attrs))
  70. self.body.append(self.starttag(node, "iframe", **attrs))
  71. self.body.append("</iframe></div>")
  72. def depart_youtube_node(self, node):
  73. pass
  74. def visit_youtube_node_latex(self,node):
  75. self.body.append(r'\begin{quote}\begin{center}\fbox{\url{https://youtu.be/%s}}\end{center}\end{quote}'%node['id'])
  76. class YouTube(Directive):
  77. has_content = True
  78. required_arguments = 1
  79. optional_arguments = 0
  80. final_argument_whitespace = False
  81. option_spec = {
  82. "width": directives.unchanged,
  83. "height": directives.unchanged,
  84. "aspect": directives.unchanged,
  85. }
  86. def run(self):
  87. if "aspect" in self.options:
  88. aspect = self.options.get("aspect")
  89. m = re.match("(\d+):(\d+)", aspect)
  90. if m is None:
  91. raise ValueError("invalid aspect ratio %r" % aspect)
  92. aspect = tuple(int(x) for x in m.groups())
  93. else:
  94. aspect = None
  95. width = get_size(self.options, "width")
  96. height = get_size(self.options, "height")
  97. return [youtube(id=self.arguments[0], aspect=aspect, width=width, height=height)]
  98. def unsupported_visit_youtube(self, node):
  99. self.builder.warn('youtube: unsupported output format (node skipped)')
  100. raise nodes.SkipNode
  101. _NODE_VISITORS = {
  102. 'html': (visit_youtube_node, depart_youtube_node),
  103. 'latex': (visit_youtube_node_latex, depart_youtube_node),
  104. 'man': (unsupported_visit_youtube, None),
  105. 'texinfo': (unsupported_visit_youtube, None),
  106. 'text': (unsupported_visit_youtube, None)
  107. }
  108. def setup(app):
  109. app.add_node(youtube, **_NODE_VISITORS)
  110. app.add_directive("youtube", YouTube)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...