Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. import webbrowser
  2. import os
  3. import time
  4. import random
  5. import math
  6.  
  7. class Website:
  8. # Website class acts as a sort of a config, so that maybe
  9. # updating the version of the tubeplayer on a different system
  10. # would be easier. Basically, I don't see how Website class
  11. # would change in the future but it's certain that the Tubeplayer
  12. # class will.
  13. def __init__(self, data, automate=True):
  14. self.data = data
  15. if automate:
  16. self.construct()
  17. self.open()
  18. def construct(self):
  19. file = open("tmphtml.html","w").write(self.data)
  20.  
  21. def open(self):
  22. webbrowser.get().open("file://"+os.path.abspath("tmphtml.html"))
  23.  
  24.  
  25. def tubetime(tubetime):
  26. blanks = time.mktime((2000, 1, 1, 0, 0, 0, 0, 1, -1))
  27. if len(tubetime.split(":")) == 1:
  28. conv = time.strptime("2000:"+tubetime, "%Y:%S")
  29. elif len(tubetime.split(":")) == 2:
  30. conv = time.strptime("2000:"+tubetime, "%Y:%M:%S")
  31. elif len(tubetime.split(":")) == 3:
  32. conv = time.strptime("2000:"+tubetime, "%Y:%H:%M:%S")
  33. return time.mktime(conv)-blanks
  34.  
  35. print("tubetime() examples:")
  36. print(tubetime("36"))
  37. print(tubetime("2:36"))
  38. print(tubetime("11:2:36"))
  39.  
  40. # key: video id, value: [volume, playback rate, seek to seconds]
  41. # possible problems: if you have two videos A ?????-????? (where ? is any character) and ?????Q????? they are going to break somehow because javascript functions can't have - in their names so I've replaced them with Q. this is highly unlikely to ever happen.
  42. class Tubeplayer:
  43. def __init__(self, videos={"ClDmutiXnBo": [38,1,0],"67QtXZsA2ME": [20,1,0], "BIRJMESl4U8": [60,1,0], "Ag6y6jz7bQQ": [100,1,0]}):
  44. a = videos
  45.  
  46. website = """
  47. <!DOCTYPE html>
  48. <html>
  49. <body>
  50. <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->"""
  51. for video_id in a.keys():
  52. website += """
  53. <div id="player{video_id}"></div>""".format(video_id=video_id.replace("-","Q"))
  54.  
  55. website +="""
  56. <script>
  57. // 2. This code loads the IFrame Player API code asynchronously.
  58. var tag = document.createElement('script');
  59.  
  60. tag.src = "https://www.youtube.com/iframe_api";
  61. var firstScriptTag = document.getElementsByTagName('script')[0];
  62. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);"""
  63.  
  64. for video_id in a.keys():
  65. website += """
  66. var player{video_id};""".format(video_id=video_id.replace("-","Q"))
  67.  
  68. website +="""
  69. function onYouTubeIframeAPIReady() {"""
  70.  
  71. for video_id in a.keys():
  72. actual_id = video_id
  73. if video_id.startswith("_"):
  74. actual_id = video_id[3:]
  75. # playerVars { 'controls': 0: no controls, 1: default controls, 2: better performance but requires user to start playback, except not because it's being handled by onPlayerReady call }
  76. website += """
  77. player{video_id} = new YT.Player('player{video_id}', {{
  78. height: '200',
  79. width: '200',
  80. playerVars: {{ 'autoplay': 1, 'controls': 2 }},
  81. videoId: '{actual_id}',
  82. events: {{ 'onReady': onPlayerReady{video_id}, }}
  83. }});""".format(video_id=video_id.replace("-","Q"), actual_id=actual_id)
  84.  
  85. website +="""
  86. }"""
  87.  
  88. for video_id, config in a.items():
  89. website += """
  90. // 4. The API will call this function when the video player is ready.
  91. function onPlayerReady{video_id}(event) {{
  92. event.target.playVideo();
  93. event.target.setVolume({config[0]});
  94. event.target.setPlaybackRate({config[1]});
  95. event.target.seekTo({config[2]}, true);
  96. event.target.setLoop(true);
  97. }}""".format(video_id=video_id.replace("-","Q"), config=config)
  98.  
  99. website +="""
  100. function stopVideo() {
  101. player.stopVideo();
  102. }
  103. </script>
  104. </body>
  105. </html>"""
  106.  
  107. Website(website)
  108.  
  109. if __name__ == "__main__":
  110. veganchill = {"Deg7wrqa2fE": [67,1,0], "BwvzdxI45x8": [100,1,0]}
  111. jungle = {"ClDmutiXnBo": [38,1,0],"67QtXZsA2ME": [20,1,0], "BIRJMESl4U8": [60,1,0], "Ag6y6jz7bQQ": [100,1,0]}
  112. cri_du_chat = {"0K78eRSqDkk": [100,0.5,0],"_0_0K78eRSqDkk":[100,0.75,0],"_1_0K78eRSqDkk":[100,1,0],"La1D4cNQ5kQ": [45,1,0]}
  113.  
  114. # max 9, use alternative_set for more
  115. def randomizem(video,n,vol=[100,0],seek=[tubetime("1:10"),16],alternative_set="_"):
  116. if type(vol) == type(1): # random between given and double the given, but not over 100.
  117. vol = [min(vol,100),min(100-min(vol,100),vol)]
  118. rval = {}
  119. for x in range(n):
  120. rval["_%d%s"%(x,alternative_set)+video] = [vol[0]+random.randint(0,vol[1]), random.choice([0.5,0.75,1,1.5]), seek[0]+random.random()*seek[1]]
  121. return rval
  122. # max 9, use alternative_set for more
  123. def quicksucc(video,n,vol=[100,0],speed=1,seek=[0,0.015],alternative_set="_"):
  124. rval = {}
  125. for x in range(n):
  126. rval["_%d%s"%(x,alternative_set)+video] = [vol[0]+x*vol[1], speed, seek[0]+seek[1]*x]
  127. return rval
  128.  
  129. # custom funk: Sin Controlled Time Difference
  130. bells = {}
  131. num_steps = 7
  132. stepnum = 0
  133. for R in range(num_steps+1): # includes both ends
  134. step = math.sin(0.5*math.pi*(R/num_steps))
  135. bells["_%d_"%(stepnum)+"Q5dU6serXkg"] = [35, 1.5, 60+0.15*step]
  136. stepnum += 1
  137. # premade funks
  138. rain = randomizem("jX6kn9_U8qk",3,8,alternative_set="R")
  139. whale = quicksucc("savCAd6RyPI",5,vol=[25,1],speed=1, seek=[tubetime("15:0"),0.115])
  140. Tubeplayer({**bells,**rain,**whale})
  141. print("Now playing:")
  142. for K,V in {**bells,**rain,**whale}.items():
  143. print("%s = %s"%(K,V))
  144.  
  145. # More songs below:
  146. machine_harmonic_but_very_chill="""
  147. bells = variate("Q5dU6serXkg",5,12)
  148. rain = variate("jX6kn9_U8qk",3,16)
  149. smooth = variate("IU13sdrLQ-M",4,40)
  150. whale = variate("savCAd6RyPI",4,40,[10,3])
  151. Tubeplayer({**bells,**rain,**smooth,**whale})"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement